** Balancing robot for dummies **
Part five (final): DC motor control and V1 code
The updatePid function (see part 4) returns a -255 +255 range value for the motors commands
0 stop (bot is vertical)
+255 fast forward
-255 fast backward
Motor controllers only understand 0 +255 range values
This is taken care of in the Drive_Motor code
int Drive_Motor(int torque) {
if (torque >= 0) { // drive motors forward
digitalWrite(InA_R, LOW);
digitalWrite(InB_R, HIGH);
digitalWrite(InA_L, LOW);
digitalWrite(InB_L, HIGH);
} else { // drive motors backward
digitalWrite(InA_R, HIGH);
digitalWrite(InB_R, LOW);
digitalWrite(InA_L, HIGH);
digitalWrite(InB_L, LOW);
torque = abs(torque);
}
analogWrite(PWM_R,torque);
analogWrite(PWM_L,torque * 0.9); // motors are not built equal...
Please refer to http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282384853/0
for additional informations
The first part of this tutorial is now terminated (finally... :-X :-X :-X)
I am impressed by the number of time this thread has been viewed, I hope the info was clear and detailed enough
If you started your own project or intend to do so in the near future, please jump in now and just leave a comment here
KasBot V1 complete code file:
** EDIT ** can't find the trick to upload a file any hint ???
KasBot V1 is fully functional and should balance for ever
Just modify the code according to your own gyro sensitivity
int getGyroRate() { // ARef=3.3V, Gyro sensitivity=2mV/(deg/sec)
return int(sensorValue[GYR_Y] * 4.583333333); // in quid/sec:(1024/360)/1024 * 3.3/0.002)
}
My gyro (IDG300) is 2mV/deg/sec
For Accelerators, adjust gravity offset in calibrateSensors()
PID parameters should also be tuned according to your own bot geometry and weight
More to come:
V1.5 will bring "utilities" for live tuning without re uploading the code
V1.7 will replace Arduino 10 bit ADC with MPC3208 12 bit ADC (increased resolution)
V2.0 should combine encoders info in PID to suppress potential forward/backward drifting
V3.0 should bring RC remote control features (Xbee technology)
V4.0 will add IR Ping))) sensor for obstacle avoidance
V5.0 will add... your own ideas

Long (probably endless) roadmap :