SparkFun Inventor Balancing Robot (NO GYROSCOPE)

by jebeandiah in Circuits > Arduino

744 Views, 4 Favorites, 0 Comments

SparkFun Inventor Balancing Robot (NO GYROSCOPE)

20230323_143437_AdobeExpress.gif
20230323_144002_Trim_AdobeExpress (1).gif
20230320_151447_HDR.jpg
20230320_151454_HDR.jpg
20230320_151440_HDR.jpg

I wanted to make an extension of the SparkFun kit that was a bit more dynamic and interesting.

The SparkFun Guide for Circuit 5c (Autonomous Robot) will be referenced throughout: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40/circuit-5c-autonomous-robot

Supplies

5Cparts.jpg

You will also need:


computer capable of running Arduino IDE


some sort of battery pack and batteries for 5-12 volts to power the Arduino and motors


meter stick, or something of similar length and mass


popsicle sticks, or something similar to mount the proximity sensor


(optional) bluetooth shield for control (will still hold position without)

Wiring

SIK_Circuit_5C_autonomous_robot_bb_Fritzing.jpg

You can use nearly identical wiring to that of the diagram (Circuit 5C). However, you will have to wire the motor controller's GND and VIN pins directly to the battery pack and solder wires directly to the proximity sensor, or separate it from the board somehow.

Upload Code

Download this sketch and then upload it to your Arduino (you may need to put it in a folder). I took some functions (right/leftMotor, getDistance) from Circuit 5c which the SparkFun guide explains better than me. You may need to change pin designations in the code.

Build

FB7L946LFGU7ABV.jpg

Follow the instructions for Circuit 5c for the most part. The proximity sensor will need to be mounted on a stick like shown in the picture and its associated wires will need to be connected to it (I soldered mine). In the code you will need to input some dimensions of this mount.


The meter stick (or equivalent object) should be mounted vertically, and as rigidly as possible.

PID Loop Overview

COM4_2023-03-23_15-20-19_Trim_AdobeExpress.gif
better pidnototal.PNG
pidtotal.PNG

This robot uses two PID loops to control its movement. PID loops are sort of like springs that push towards a set point.


This robot originally used just one PID loop to pull it towards a manually set angle, but this caused it to drift based on how close the set angle was to the actual balancing angle of the robot. I added a second PID loop to find this angle for the original loop to use as a setpoint. It finds this angle with a PID loop with an input of how far the robot has drifted (which is estimated by adding up the outputs given to the motors). However, it is not a normal PID loop as its outputs add to the set angle rather than being an output value. Normal PID loops converge to output 0 when there is 0 error, this one needs to converge to the setpoint angle when there is 0 error. Adding its outputs achieves this as when there is 0 error the setpoint angle can be nonzero, but also will not change.

GIF 1 Shows the power given to the motors (Orange), and the changing setpoint angle (Purple).

GIF 2-3 Shows the components of the original PID.

This is an excellent resource if you want to learn more about PID controllers and robotic control schemes in general.

PID Loop Tuning

PID loops, like springs have a few key properties that characterize how they behave. In this project each PID loop has 3 properties, a cumulative constant (ik), a proportional constant (pk), and a dampening constant (dk). (I may have switched the cumulative and dampening constant explanations) The cumulative constant addresses a spring's behavior when under a force across time (the weight of an object connected to a spring). The proportional constant addresses a spring's behavior based on its extension (Hooke's Law spring constant). The dampening constant addresses a spring's behavior based on its velocity (I think internal friction of the spring). If these constants are too far from desirable values, the robot could oscillate too much and fall over (underdamped) or be too slow to fight falling over (overdamped). The simplest way to obtain usable constants is trial and error.

NOTE: These constants may change based on the shape/mass of the robot, power of the motors/battery/driver, properties of the distance sensor, and execute speed of the program.

Start by turning all the constants (indicated by l/h p/i/d k) to zero. Next, estimate your robot's balance angle by uncommenting line 100 (//Serial.println(String(angleerror));), running the code, and then holding it on the ground in the way it feels it balances. Set manualangle to a value close to what you see in the console.

Then, start tuning the proportional constant of the lower PID loop (lpk). Increase it until the robot oscillates violently too the point of falling over, then half the value. Next, start tuning the dampening constant (ldk). Increase it until your robot starts to balance. Finally, start tuning the cumulative constant of the lower PID loop (lik). Increase it very incrementally until you are satisfied with your robot's stability.

Finally, you can start tuning the higher PID loop. Do this in the same way as above, but with the higher PID loop constants (hpk, hik, hdk). Instead of looking for oscillations and stability in angle, look for stability and oscillations in the robot's position on the ground. This is a far less responsive process than tuning the lower PID loop, so it is probably best to underestimate all of these constants.

Troubleshooting

  • The mass/length of the ruler can have a very big impact on the success of your robot, so if you aren't having luck tuning the lower PID loop, try changing the object you use and how you mount it.