A lot of people have asked how I got the readings from the Gyro, Accelerometer, and Compass inertial measurement unit(IMU) which is used to keep PiBBOT upright.
UPDATEDAnd updated guide has been published here, with cleaner and simpler code.
In this guide I will explain how to get readings from the IMU and convert these raw readings into usable angles. I will also show how to read some of the information in the datasheets for these devices. The theory and principals below can be applied to any digital IMU, just some minor modifications need to be made. Eg register values, sensitivity level…
When using the IMU to calculate angles, readings from both the gyro and accelerometer are needed, which are then combined. This is because using either on their own will result in inaccurate readings.
Here is why; Gyros – A gyro measures the rate of rotation, which has to be tracked over time to calculate the current angle. This tracking causes the gyro to drift. However, gyros are good at measuring quick sharp movements. Accelerometers – Accelerometers are used to sense both static (e.g. gravity) and dynamic (e.g. sudden starts/stops) acceleration. They don’t need to be tracked like a gyro and can measure the current angle at any given time. Accelerometers however are very noisy and are only useful for tracking angles over a long period of time.
I have had success with getting PiBBOT to balance.
When I saw my first two wheel balancing robot I was very fascinated. And after receiving my Raspberry Pi, I decided to build one myself.
PiBBOT (PiBalancing roBOT) is my first successful balancing robot. And it has room for improvement and extra functionality.
When building PiBBOT, I had a few roadblocks I needed to overcome;
I originally had the Anker as the power source for both Pi and the motors, however the amperage was too low.
Original H-Bridge not powerful enough for my motors
Gyro calculation off by 15 degrees
The TFT displays the angles from the accelerometer, gyro, complementary filter and power to the motors.
The buttons are to turn the motors on and off and to reset the gyro.
Incorporate the wheel encoders for better balance
Build a sturdier frame
Implement direction control
The able to self right itself after falling over
Retrieve a can of beer from the fridge
PiBBOT consists of these components;
Adafruit: TFT; 2.2″ 18-bit color TFT LCD display RF Receiver :RF M4 Receiver – 315MHz
Pololu; Motors; 9.7:1 Metal Gearmotor 25Dx48L mm with 48 CPR Encoder Wheels; Pololu Wheel 90x10mm IMU; MinIMU-9 v2 Gyro, Accelerometer, and Compass (L3GD20 and LSM303DLHC).
Anker; Battery; Anker® Astro3 10000mAh 5V / 9V / 12V 2A Dual USB Output External Battery
Tenergy; Battery; 7.2V Tenergy 3800mAh Flat NiMH High Power
I’m using a shield from Texy to attach my TFT to my Pi.
This is soooo much easier then connecting up the cables individually, which requires me to always refer to the wiring diagram… and it always takes me two or three goes to get it right.
This just requires a quick snap onto the top of the GPIO pins. It also includes wiring to use 3 buttons on the GPIO pins not being used by the TFT.
If you have multiple Raspberry’s or even multiple Unix hosts , you will start to get frustrated at constantly having to enter a password every time you copy files between them or access these devices from another Unix host.
There is an easy way to disable this with RSA key pairs.
On each host, enter ssh-keygen
Hit enter to keep the default directory.
Passphrase. Leave blank and hit enter
Your keys are created under .ssh/
Now you need to copy over your key to all the other hosts with; ssh-copy-id -i .ssh/id_rsa.pub username@serverip
Once this is done, you should now be able to copy files or access each device without having to use passwords.
You still need to enter a password when trying to connect from a host that hasn’t got a key.
Notro has done some great work with developing drivers for different models of TFT displays for the Pi.
He successfully created one for the Adafruit 2.2″ TFT . This is a great little display and is very clear.
Here is my first attempt at getting PiBBot balanced.
This is a very early prototype, confirming that all the hardware works and the I can get the time of the main loop down to 20ms, which was easy.
This prototype also helped me get my head around the code and math need to balance.
I am waiting on the delivery of some hardware to build my final and larger version. Which will have a battery pack placed up high, this will help a lot with the balancing.
The main components used to balance this prototype;
MinIMU-9 v2 Gyro, Accelerometer, and Compass (L3GD20 and LSM303DLHC Carrier)
Solarbotics GM6 120:1 Mini Gear Motor Offset Output
Dual H-Bridge Motor Driver
315MHz RF M4 Receiver
The receiver allowed my to change the PID values in real time.