DC motor control with PID

@Gibby623

What controller are you using for he wheels?
Not sure I understand your question, please elaborate
If you mean motor driver,here it is: Pololu - Dual VNH2SP30 Motor Driver Carrier MD03A

why is mine so bad... Sigh...
I understand your frustration, :wink: I have been pursuing this quest nearly for 2 years
Please post photos of your bot, together with the code, I will look for possible obvious reasons

i will do this later,if i success[ch65292]i'll show my pictures.
i got a question ,why do you put the battery on the top[ch65311]
i think that if you put it on the bottom,you can controll the balance better.

i think that if you put it on the bottom,you can controll the balance better.

I know nothing about balancing robots, but I would tend to think having the mass at the top would be easier to balance. Try to balance a pencil on your hand, then try it with a weight on top (blob of clay), then a weight near the hand - you'll find its easier with the weight at the top. I imagine the same would be true for a robot (ie, inverted pendulum)...

:slight_smile:

Quote:

i think that if you put it on the bottom,you can controll the balance better.

I know nothing about balancing robots, but I would tend to think having the mass at the top would be easier to balance. Try to balance a pencil on your hand, then try it with a weight on top (blob of clay), then a weight near the hand - you'll find its easier with the weight at the top. I imagine the same would be true for a robot (ie, inverted pendulum)...

Your are right cr0sh
the robot acts as an inverted pendulum and works better when the weight is high (it increases intertia and allows more reaction time).
Try to balance a broom in the palm of your hand and see which side is easier to balance.
However, PID parameters need different tuning

// Kalman filter module

float Q_angle = 0.001;
float Q_gyro = 0.003;
float R_angle = 0.03;

Hi Kas, awesome work on the project! My mate at uni was showing this to me today looks cool, great progress!

I am also working with an IMU a 6dof one and just wondering for your kalman filter above, how did you find the covariance matrices for measurement error and process error? lke what did you do and stuff?
Because im upto that aswell, i have my state space of the kalman filter, just dont understand how to get the covariance matrix.
Any help would be appreciated.

Thanks!!

Kalman filter module works pretty well but is still a Black Box for me
I tried hard to understand and finally gave up. :o

This code is a modified version from the AeroQuad project from Ted Carancho
http://code.google.com/p/aeroquad/

I spent some hours playing with Q_angle, Q_gyro and R_angle, and finally reverted to the original parameters

Some additional lectures for the brave:
http://academic.csuohio.edu/simond/courses/eec644/kalman.pdf

http://forum.sparkfun.com/viewtopic.php?t=6186

Kas,

I am confused, on the let's make Robots site, you stated that thanks to Dallaby your robot balances. Now, Dallaby uses a complementary filter. You mention you are using a Kalman filter. So, is this the case, if so what is the contribution from Dallaby?

I am asking because I am trying to start similar project and not sure whether to spend more time trying to be able to run the kalman routines or go ahead with the complementary filter. Several sites talks very good about this simpler approach.

Thanks !

Kas,
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284650649/0#0

How do you add photos?

Hi josev,

This is a long story
I made several unsuccessfull attemps to build an acceptable balancing bot, both with Kalman or complementary filter; I finally gave up 6 months ago.
Reading, Dallaby blog gave me new ideas for the project, mainly for hardware (I was using continuous rotation servos).
Dallaby uses a single axis accelerometer which is OK small angles, together with a very simple and efficient code.
I chose the full blown path, but again, without visiting his site, I wouldn't be writing to you right now. ::slight_smile:

The above Kalman code is really convenient and self contained
Feed it with Acc angle, Gyro speed and loop time, it will spit back the filtered angle right away
Just make sure that data are scaled and your loop time is accurate/constant,
mine is running 100 Hz, see above "loop timing control".

Good luck, keep us informed

Kas,
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284650649/0#0

How do you add photos?

Easy :wink:

  • Go to http://imgur.com/
  • Upload you photo(s)
  • copy the "Message board" line
  • paste it here

Gibby623,
A bit dubious about your PID function
Why do you have "PID" and "PID_comp"?? is "a" a tuning factor (as my "K") ??
I would have expected PID to be positive or negative according to the bot being tilted forward or backward
Also if your loop is fixed time, you can omit the dt factor

anycase, here is my PID code:

float K = 1.4;
int   Kp = 3;                      
int   Ki = 1;                   
int   Kd = 6;  
int last_error = 0;
int integrated_error = 0;
int pTerm = 0, iTerm = 0, dTerm = 0;

int updatePid(int targetPosition, int currentPosition)   {
  int error = targetPosition - currentPosition; 
  pTerm = Kp * error;
  integrated_error += error;                                       
  iTerm = Ki * constrain(integrated_error, -GUARD_GAIN, GUARD_GAIN);
  dTerm = Kd * (error - last_error);                            
  last_error = error;
  return -constrain(K*(pTerm + iTerm + dTerm), -255, 255);
}

This function accepts 2 parameters

  • target position (normally = 0)
  • current position = the angle obtained from Kalman or complementary filter
    the function returns an integer value, between -255 and +255

Please let me know which Acc and Gyro models are buit on your specific Sparkfun board

Why do you have "PID" and "PID_comp"?? is "a" a tuning factor (as my "K") ??

Yes, one motor responses quicker/more than the other. I know this is sad, I didn't know it accepts -255 to 255. I though it was only 0-255.

Also if your loop is fixed time, you can omit the dt factor

I am unsure how you can exclude dt, my dt is not constant. How do you make it constant?

How did you find your P, I and D values? Well, their gains, kp, ki, kd?

Please let me know which Acc and Gyro models are buit on your specific Sparkfun board

I am using the generic 5DOF sensor which has a ADXL335 and IDG500, I think.

Forgot to add, I uploaded my pictures in my topic, to avoid cluttering your topic with my ugly system

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284650649/0#0
Your bot is really sophisticated, you should have spent a lot of time on this project

Is there any reason why the gyro is placed at the bottom? Intuitively I would think it was better to place it near the 'top'.

Feel free to correct me, I read somewhere it eliminates uncertainty with the readings.

You are right, we want to measure gravity angles, not movements.
IMU board should be located near the weels (axe of rotation) to reduce noise sensors

Hardware: 2 x 29:1 12V Gearmotors, SN754410NE H-Bridge, Arduino Nano 3.0, Sparkfun 5DOF IMU.

For the expensive parts (IMU, motors), You have the right hardware :slight_smile:

Yes, one motor responses quicker/more than the other. I know this is sad, I didn't know it accepts -255 to 255. I though it was only 0-255.

My left motor is 10% slower than the left one, that the nature of the beasts, this is adjusted within function "Drive_Motor(int torque)" in the Motor Module, this adjustment is not PID related.
The motors accept only 0 to 255, the sign determines the direction of rotation
Again, this will be treated in "Drive_Motor(int torque)"

Gibby663, I suspect that your signal aquisition and processing is not adequate.
For you and possibly additional lurkers, I am willing to start from the very begining and rebuild the bot together
I am sure you already know most of the info to come, just look for the details
For convenience, you will have to remove you breadboard from the bot (I did)

Ready ??? OK!!!

Disclaimer:
I invented nothing
I read A LOT and went by trial and error

__ ** Balancing robot for dummies **__

Part one: sensor, setup and fixed time loop

Stay tuned, I will open, later this afternoon, a new thread with a more relevant title than "DC motor control with PID". :sunglasses:

Gibby663, I suspect that your signal aquisition and processing is not adequate.

I suspect that too!!!

Your bot is really sophisticated, you should have spent a lot of time on this project

What do you mean sorry? It is rather generic though; IMU, H-bridge, microcontroller...

For convenience, you will have to remove you breadboard from the bot (I did)

Do you mean I should??? I don't have much time to solder everything to PCB. Does the breadboard introduce a lot of noise?

Thanks for taking some time to help :slight_smile:

This is the new topic I just opened for further discussions
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284738418/0#0

Please do not post anymore in this thread

Thanks

I'd love to build smething like this, only I don't have the money. (or intelligence!)

I'd love to build smething like this, only I don't have the money. (or intelligence!)

Begin with a small project (or part of the project) and then advance slowly to more advanced projects.

I am planning on building a similar robot soon, so I was wondering where you got the wheels? (A UK supplier would be nice!)

Hi onions,

This thread is not active anymore, please join us here: Balancing robot for dummies - General / Exhibition - Arduino Forum