Welcome to the FlowStone Workshop number 17, where we give a beginners guide to computer programming using the FlowStone graphical programming language. In this issue we are going to look at how to pro- gram balancing robots similar to the ones made famous by the Segway electric vehicles. Balancing robots or inverted pendulums, as they are also known, are a relatively recent invention exploiting the advances in Gyroscope and Accelerometer technology. These new Gyros and Accelerometers are now small and cheap enough to be used in our model robots, so let’s look at what is involved!
WHAT IS A GYROSCOPE?
A Gyroscope is a device that measures angular velocity, or the speed of rotation. In the old days these were literally made by spinning a heavy disk on a spindle that resisted rotation and measured angles of deflection, these then progressed into Piezoelectric gyroscopes, and now miniature ceramic variants. Gyroscopes react quickly to rotation but don’t know orientation.
WHAT IS AN ACCELEROMETER?
An accelerometer measures acceleration “g-force” in a given direction. Since accelerometers can sense gravity they can also give you orientation. As an example most computer tablets and phones use accelerometers to rotate the screen depending on the device orientation. Accelerometers react slowly to rotation but do know orientation.
For our balancing robot we need a gyroscope, accelerometer, microprocessor, motor control- ler, 2 x geared motors and a bat- tery. I decided to use the New FlowPaw board from DSPRobot- ics (which has just enjoyed a successful KickStarter cam- paign) as this has plug-in ‘claw’ options for a range of sensors including gyros and accelerome- ters
In order to control our balancing robot we are going to have to delve a little into some complex math! Fortunately this is well documented for balancing robots and takes the form of:
Combining the accelerometer and gyro signals to give you an angle of the robot platform, this is usually done using a Kalman filter, also known as linear quadratic estimation (LQE). Whilst this will give the best results over a wide range of angles it is very complex mathematically and CPU intensive. There is a simpler approximation that seems popular called a Complementary Filter which works well up to around 30 degrees of deflection, perfect for our balancing robot:
angle = (0.98)*(angle + gyro * dt) + (0.02)*(acc)
This basic idea is to create a high pass filter for the fast initial gyro signal and a low-pass filter for the accelerometer orientation signal. Then combine the two to give you an accurate calculation of the angle.
This angle then needs to be converted into a signal to control the two wheel motors forwards and backwards. If you try to use this signal directly to control the motors they will just oscillate, as they over- shoot, making it impossible to balance perfectly still. The solution is to use a PID (Proportional, Integral, Derivative) algorithm to react quickly to change and slowdown the motors as they approach the balance point, with no overshoot. For our first version we used FlowStone to do all of the maths and connected the FlowPaw via USB to read the Gyro and Accelerometer values. We also calculated the PID using the standard Flow- Stone PID module also written in ruby code. This worked really well as the USB data rates were fast and stable so we could read the sensors, do the math in FlowStone, and then send the motor values back to the FlowPaw board.
The motor signals were sent back using the servo inputs on the FlowPaw module in ‘Rover Mode’ (speed = 100) which switches four of the servo outs to motor PWM (Pulse Width Modulation) so you can control geared motors & wheels using a low cost motor controller. We also used FlowStone to draw graphs of the sensor outputs, complementary filter, and PID, so we could see what was going on.
So within a matter of a day we got our mini Segway balancing using FlowStone and FlowPaw and we were quite pleased with ourselves. The next step was to make it wireless using the FlowPaw Bluetooth connection and we were done! Unfortunately this is where a few problems crept in, the Bluetooth module running @ 115200 baud, whist quite fast wasn’t as fast and reliable as the USB hardwired solution. Any minor dropouts would cause our robot to stumble or wobble. For normal data like servo control for robots and display strings the Bluetooth is fine but this balancing data needed to be rock steady and consistent, something wireless connections are not. SO we decided to take this project one step further, since the math for a balancing bot is a known quantity why don’t we just program it directly into the FlowPaw Firmware for the microprocessor and just bring out the PID variables to fine tune the balancing! And this is exactly what we did; behold the new Segway module:
This means that the fast math is now working at hi-speed in the microprocessor on the FlowPaw board and you can then just control the settings via FlowStone using the Bluetooth wireless module running at a slower speed. The PID module as it’s named in this case contains nothing more than a bunch of constants to setup the PID now running in the chip on the FlowPaw. This is really cool as it means anyone can now make a balancing robot with little or no knowledge about the complex maths! Just use this new module and ? ne-tune the values to match your hardware design.
To finish it off we used the 8×8 LED Display Click/Claw to make a face on the top bar. Another application would be to use it as a mini telepresence robot where we can write messages on the 8×8 LED scrolling screen remotely from FlowStone! The thing that takes the time is getting the PID values correct for your robots dynamics, having these on sliders in FlowStone made this process a lot quicker as we could do this remotely without touching the robot.
We left it balancing for about 20 mins and it was rock solid, if you pushed the bar forward simulating a human controlling it, it would roll forwards, etc. and then recover and balance again. We even programmed it to turn by adding a constant to each wheel in opposite directions using an Xbox controller in FlowStone to drive it around.
This was in fact quite a big project in the end, just reading the I2C Gyro and Accelerometer normally would take a few days’ work, fortunately using the FlowPaw board this was already done so we could concentrate on the pure maths for the balancing robot. But in the end it proved better to put all of the maths inside the super-fast microprocessor to get the best stability and use Bluetooth to just send control signals. Now that we have done the Segway Module, you can use it too! The next step could be to make a quadcopter using the same maths but in three axes— maybe this can be the next article for Robot Magazine!