Robotic autonomy is the ability of some robots to act without human intervention. Though it may have once sounded like something from science fiction, autonomous robots are becoming increasingly common in our daily lives. Many of these robots have been designed to perform menial or dangerous tasks, and can be seen in hospitals, restaurants, and war zones. With the right sensors and a bit of programming, the STEMBoT can gain a level of autonomy!
The STEMBoT shown here balances with the use of the MPU6050 Plug and Play module and the sample code found here.
Digging into the code, there are several functions that have to do with the MPU6050 and the motors. The first two functions, offsetAccel and offsetGyro, take a number of data samples to establish a baseline for more accurate measurements. Since no sensor is perfect, finding out how imperfect your sensor is allows you to compensate and get more accurate data. While this calibration isn’t strictly required, it can be helpful especially if your MPU6050 is giving off weird readings.
The next set of functions are used to control the motors. They’re all basic and portable, which means you can copy and paste them into your own code! Just make sure you use them in the right order (motors need to be woken up [motorsWake()] before use).
The main function gets the MPU6050 module and LCD ready (lines 85- 88), initializes the motors (lines 90 and 91), and uses the averaging functions to generate offset values (lines 96 and 97). The infinite loop, starting at while(1), gets the readings (including temperature!) and then applies the offset by subtracting the averaged calibration values from the new readings. The data is then printed to the LCD for convenience (lines 116 – 121).
Lines 122 – 131 is where the magic happens. The MPU6050 will naturally read the acceleration due to gravity. When the SB2 is motionless on a level surface this value will be around 9.8m/s^2 along a single axis, the one going up and down (the y axis if your module is plugged into the top port). The readings along the other axes should ideally be zero, but in practice will hover around zero. When the SB2 is thrown off balance, the acceleration due to gravity will be felt along other axes. These lines of code test the acceleration felt along the z-axis (along the length of the SB2) and if that acceleration exceeds 1 m/s^2 (in either direction, + or -), the SB2 “knows” that the center of the lever hasn’t been found, and will activate the motors to move in that direction. Because this happens in an infinite loop, the SB2 will continue moving back and forth until the sweet spot is found, where the acceleration along the z-axis is close to zero. It will then stop and put the motors to sleep. The delay (await) after these lines of code makes the SB2 move for half a second before retesting the acceleration data.
The algorithm (those magic lines of code and the logic behind them) that gives the SB2 autonomy in this example is, well, crude. It gets the job done, but it’s not very efficient. The reason behind this is to demonstrate that it doesn’t require much in the way of programming to achieve some level of autonomy. It takes only a few lines of code make the SB2 think and behave on its own! With how easy it is to give this robot intelligence, it’s only a matter of time before some mad programmer starts taking over the world with STEMBoTs!
Module used: MPU6050
Program used: Balancing_Act.py