The Automatic Balancing example demonstrates how to implement a self-balancing robot, which can be useful to help avoid a robot tipping over when driving. As an example, FRC team 263 demonstrated the auto-balance feature effectively during the 2018 FRC Championships.
The basic principle used in the example is based upon measurement of the navX-sensor Pitch (rotation about the X axis) and Roll (rotation about the Y axis) angles. When these angles exceed the “off balance” threshold and until these angles fall below the “on balance” threshold, the drive system is automatically driven in the opposite direction at a magnitude proportional to the Pitch or Roll angle.
Note that this is just a starting point for automatic balancing, and will likely require a reasonable amount of tuning in order to work well with your robot. The selection of the magnitude of correction to apply to the drive motors in response to pitch/roll angle changes could be replaced by a PID controller in order to provide a tuning mechanism appropriate to the robot.
FRC C++ Example
Full C++ source code on GitHub
FRC Java Example
Full Java Source code on GitHub
FRC LabView Example
The navX-sensor AutoBalance LabView example shows how to make small modifications to the LabView “FRC RoboRIO Robot Project” using the “Mecanum Robot” configuration to implement high-accuracy Automatic Balancing.
RobotMain.vi
Place the NavX main vi on the block diagram and set it up to your needs. The default sample rate is 50Hz. You may need to process faster for your situation. For the SPI, I2C and USB connections the max sample rate is 200Hz.
Teleop.vi
The Teleop.vi is modified to feed the current navX-sensor “Yaw” angle reading to the Holonomic Drive VI, which rotates the joystick X/Y coordinates by the gyro angle (and thus implements FieldCentric drive control). Additionally, if a driver joystick button is pressed, the navX-sensor “Yaw” angle is reset to zero. Finally, the navX-sensor “Pitch” (X-axis) and “Roll” (Y-axis) angles are continuously compared to a “out of balance” threshold, at which point the corresponding axis motor output value is derived from the inverse of the sin of that angle, until the time when that same angle falls below the “in balance” threshold.