Step 45CODE (Just balances nothing else!)
Open this sketch (attached as a text file to paste into an arduino sketch and save)
NOTE:
Updated Feb 25th 2012.
It works with Arduino V22. They have now released a newer version (V1.0) with a new softserial library with slightly different softserial commands so it might not work with very latest version of Arduino.
As when we tweaked the IMU tester: alter the value of 338 in this line of code (if you need to) in for the value you wrote down for accsum when running IMU tester and holding your board level: x_accdeg = (float)((accsum - 338) * 0.862);
Recompile the balance code with this new value in and you are now ready to try it out.
Now you have tested your IMU, tested your motors (and so have a working deadman switch) and are confident from video and photos that IMU is mounted correctly AND that motors are not wired up back-to-front, then you are ready to try to get it to balance.
This sketch, attached as a text file, just balances the board - nothing else. Steering disabled so if you have no steering controls connected it doesnt matter at this stage.
The tip-start is working.
The deadman switch has to be working.
The overallgain has been fixed in the code so the gain potentiometer does not have to be attached.
Power it up (tilted), wait a few seconds for gyro to zero itself, press deadman button and hold it in, then bring it slowly level, hopefully when more or less level it should start to balance or at least try to.
| « Previous Step | Download PDFView All Steps | Next Step » |



























































































I built a robot using this setup a while back, but never managed to get it to work satisfactorily. I've now picked the project back up to see if I can get it working.
The problem I'm having is caused by the Level drifting very quickly, so the board never finds its level. It tries to level, but by the time it gets there, the Level variable is way off horizontal, so it can't balance.
Any idea what can cause this drift? It happens over a few seconds, and within 30 seconds or so instead of being 0 deg it's 400 deg.
This is a classic issue. The gyro is doing the short term balancing but it drifts so after 30 sec or so it still balances but at an odd angle. The accel is supposed to gradually correct this. What you describe is the accel acting against the gyro rather than with it (i.e. back to front).
Solutions, turn IMU over (current upper side downwards) or alter code:
Take this line:
x_accdeg = (float)((accsum - (338 + balancetrim))* (-0.862));
Change the sign before the 0.862. If +ve now change it to a minus sign or vice versa.
Alternative is to take this line in code:
angle = (float) ((1-aa) * (angle + gyroangledt)) - (aa * x_accdeg);
and again, change the sign so now reads opposite of what it was before:
angle = (float) ((1-aa) * (angle + gyroangledt)) + (aa * x_accdeg);
It makes the accel interact with the gyro readings in opposite way to whatever it is doing currently.
Hope this helps
John
I'd actually got past this point, but I'm still having trouble getting it to balance. It starts off well, then oscillates wildly out of control. I suspect the values in your sketch are not working well on this small frame (~500g).
I'm experimenting with overallgain, and also with the scaling variables in the balance_torque calculation (4.5 and 0.5), but I still haven't managed to make it balance.
Any further insights would be much appreciated.
Set all constants to zero
1) increase prportional gain constant (currently 4.5) until it more or less balances but starts to oscillate wildly. Then set the value to about 1/2 to 2/3 this value.
2) Increase derivative gain constant (0.5 at present) so that when you tilt it one way it rapidly corrects in a damped sort of way, i.e as it get back closer to balance point the force of the correction drops off so it rapidly corrects without overshooting or oscillating.
3) The I gain is more what tends to keep it in one place. Keep increasing it until you get oscillation then back off a bit. If too high and you push it off balance machine will roll along, regain balance, then roll back the other way as the I gain was too high. I gain in this sketch is actually the cur-speed multiplier value So that is the line of code you need to play with.
I am not an expert, whole theses are written on this, but this is one relatively simple explanation i found on the web.
John