Go Down

### Topic: PD of PID in a balancing robot (Read 8234 times)previous topic - next topic

#### ironbot

#30
##### Apr 28, 2012, 09:52 pm
PeterH, I'm little bit confused that you say we don't need accel, actually without filtering, if using only gyro output, the big drift I'd have and change of set-point that happens, I get nothing, I tried it long before this experiment!

It is impossible in my practice to not to combine output of gyro and accel, and yet balance the bot! I've read so many works (of other people already done it) and everybody is doing it this way, writing that impossible to use only gyro!

Of course, I'm in no way on an authority to claim anything in control, just I report what I've read. Actually if you personally can do it, is because of your skills that I unfortunately don't have, all i have is a background in math and a lot of reading online about this project, and still with a bot that can't balance really

#### ironbot

#31
##### Apr 28, 2012, 10:00 pm

In your initial post code, there is some stuff after that, which I don't understand:
Code: [Select]
`...    analogWrite(RPWM, -output);  }  [color=green]// probably not good enough, but understood up to here  ;)[/color]    output = -1 * output;    if(output >= 0)  {    digitalWrite(RDIRA, HIGH);    digitalWrite(RDIRB, LOW);    analogWrite(RPWM, output);  }  else  {    digitalWrite(RDIRA, LOW);    digitalWrite(RDIRB, HIGH);    analogWrite(RPWM, -output);  }  }//if(!(angle>30 || angle < -30))  else  {    analogWrite(LPWM, 0);    analogWrite(RPWM, 0);  }`

I assume output should represent the speed you want your robot to move. It does not need to be exactly proportional, but you should work on that a bit, too.
- What's the minimum/maximum speed your bot can do ? For smooth and easy contol, motor should react immediately with slow speed. Max speed should rather tell you when to give up.
- What's the dynamic behavior of your balancing rod when it falls ? angle / angular velocity  over time ?
- I think you don't need sin/cos even (5.7° = 0.1 * R is sufficient around your "set point") but a bit of math won't hurt.
- Did you consider that the more your sensor has left the 0° position, the more moving the motor will accelerate it ?

oh, sorry, I just got that you are pointing this:
Code: [Select]
`  output = -1 * output;`
well, I just changed motors and swapped the left/right wires of motors and one of them started to turn not in the right direction, instead of changing in hardware, I added that line to just turn the output sign to the opposite one, which in turn effects on the 'if' decision and I got the motor turning again on the right direction.

I set 255 to get max. speed and it is claimed by the seller that this motor turns 300rpm, but I did not pass any motor identification process to give a reliable data of the speed.

dynamic behavior  - I'm not sure if I got what you mean by that, when it falls: it takes some 10 deg and only by then the motor reaction happens, which is late, and robot starts to fall, and motors get to turn so fast, until it falls completely and motors stop

I think you don't need sin/cos even - also didn't get you! sorry!

the more moving the motor will accelerate it ? - no, I don't feel that, but I'd submit a behavioral video, so you all who are kindly helping see it, that is better than me describing it, yet with my English which is noisy

#### PeterH

#32
##### Apr 29, 2012, 12:23 am

PeterH, I'm little bit confused that you say we don't need accel, actually without filtering, if using only gyro output, the big drift I'd have and change of set-point that happens, I get nothing, I tried it long before this experiment!

It is impossible in my practice to not to combine output of gyro and accel, and yet balance the bot! I've read so many works (of other people already done it) and everybody is doing it this way, writing that impossible to use only gyro!

Of course, I'm in no way on an authority to claim anything in control, just I report what I've read. Actually if you personally can do it, is because of your skills that I unfortunately don't have, all i have is a background in math and a lot of reading online about this project, and still with a bot that can't balance really

If you are in contact with somebody who has actually done it, then follow their advice. I haven't - I am only telling you my understanding of the theory.

In my opinion, the accelerometer will not do what you intend it to do. The tilt calculations are based on measuring very small lateral accelerations that occur when the sensor is not perfectly level. This only works on a stationary object, and of course your bot is not stationary. As soon as your bot starts to move sideways the sensor will get a big horizontal acceleration that swamps the tiny signal you were using to calculate the direction of gravity.

Now, you can try using that instead of calculating the lean angle, if you like. As soon as the bot starts to fall to the left you move the wheels in the direction it's falling. To get that working reliably you'd really need to be able to work out and control the acceleration caused by your motors and model the relationship between the movement of the base and the top of your bot. Although it would be possible to do, it'd look nothing like the algorithm you have got there and would be quite hard to get working.

I think you are better off with the approach you're using, of trying to measure the orientation of the bot. The primary sensor for doing this is the gyro. You have it sitting in front of you; I don't. What signal do you get off the gyro in practice? What is the minimum angular rate you can read from it, and how much latency is there in reading it?

You are right that you need to cope with drift. If it was my project, the way I'd do that was to add a second feedback loop which tried to drive the amount of correction towards zero. In effect it would self-calibrate towards the vertical at startup, and inherently deal with gyro frift. But there's no point thinking about that until you have got the basic underlying feedback loop working. And that depends on know what your sensors are telling you. Hence I keep asking the question: what are you getting from that gyro?
I only provide help via the forum - please do not contact me for private consultancy.

#### ironbot

#33
##### Apr 29, 2012, 07:32 pm
PeterH thank you for this much support, my aim is to do the project to learn something and I'm ready to try different direction that I took at first. So let's do it the way you suggest, but I've doubt I fully understand you in this (to measure gyro).

Do you mean that I post here simply information like:
1. if I keep robot stationary, output of gyro is x
2. leaning forward fast to around 45deg I get y
3...

or what best way to answer your question "output of gyro" please?

#### michael_x

#34
##### May 02, 2012, 09:45 am

2. leaning forward fast to around 45deg I get

Don't bother about 30° or more: If your motor at full speed is able to catch that, it's nice, but you should rather get numbers (or a feeling, at least) about what happens and what your sensor says, when your bot begins leaving the balanced position. (In that region where degrees and displacement in x direction is nearly proportional...)

#### ironbot

#35
##### May 05, 2012, 10:12 am
Ok, thank you all for continuous support!

Just received my new sensors: a separate gyro and a separate accel (my current sensor as you see on photos, has both of them combined on one board).

I can't rely on my old sensor, not really sure about its health, she is too old!

With new ones I'd first build a new chassis, with the hope that it gives me room to easily apply changes as you suggest and instruct. This will take probably around 2 weeks and I'd be back here with a new bot which (hopefully) also helps to answer the questions you kindly put (how goes the output of gyro if you do this/that?) easy and fast.

Please, if you have suggestions for the new chassis I'd build, proportionality info., etc. let me know. What I know for now is that, I'd for instance build the vertical axis in around 70cm height, so that to have it falling not too fast, and keep the base in a way I can add and remove weight to easily change the moment of inertia. Any other suggestions will be very welcome, I will apply them in 3D modeling of chassis in software and finally cut with CNC.

Thank you again,

#### michael_x

#36
##### May 05, 2012, 02:24 pm
BTW: there is a dedicated Robotics thread http://arduino.cc/forum/index.php/board,14.0.html , discussing "balancing robot" similar to yours (1 ft high, two wheels) http://arduino.cc/forum/index.php/topic,60170.0.html

#### ironbot

#37
##### May 05, 2012, 10:11 pm
Man, I just visited the links and the video clip there: when he leans the bot even just a degree, motors react, in my version, I have some 5 deg before reaction, I must take record a video before destructing the current one and upload here!

#### PeterH

#38
##### May 06, 2012, 06:09 pm

Man, I just visited the links and the video clip there: when he leans the bot even just a degree, motors react, in my version, I have some 5 deg before reaction, I must take record a video before destructing the current one and upload here!

I think you're looking at the real problem now, but I'm still not clear whether the slow reaction is caused by the sensors, or your algorithm. I really think you need to write a sketch that just outputs the raw data from your gyro and accelerometer and find out how sensitive they are and how quickly they respond. Don't try to do any clever mapping or anything - just output the raw data as promptly and simply as you can.
I only provide help via the forum - please do not contact me for private consultancy.

#### michael_x

#39
##### May 06, 2012, 07:42 pm
The fact that you reported the motor producing a sound only on low values and started turning with higher pwm output and without load only, is something you should definitely try to solve.

#### ironbot

#40
##### May 07, 2012, 09:59 am
True, I'd approach it and some other problems in a totally new design, hopefully to bring it to 1 level higher totally.

#### hasuino

#41
##### Jul 31, 2013, 08:00 pm
although it's an old topic,I'd like to share my case>>

I used to have the same problem.. using PD only the robot wouldn't respond to small change in angle and when I increased the P term it went crazy,,,D term wasn't helpful at all and when increased the robot starts to vibrate !!

as mentioned in previous posts this maybe due to noisy data...and what they said is true
I zeroed the D term and put I term instead and it balanced!! just like that!!
I term was like magic!

#### vovk93

#42
##### May 04, 2016, 04:22 pmLast Edit: May 04, 2016, 04:29 pm by vovk93
Hello

First sorry for my broken english.

I made a self balancing robot with mpu-6050 gyroscope, l298n driver, 2x 500rpm motors, arduino uno and bluetooth modul hc-06.

Robot chasing a vertical position and driven via Bluetooth. But i have a problem. When robot comes on a different basis. It does not catch more vertical position but it drives left or right. PID values should be continuously adjusting...Could you please help me?

Add a link to the program:
https://drive.google.com/open?id=0Bz2RldWSUEhRdS1URVl1cm5ybGM

Thanks for all answers.

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy