3

I'm trying to build a two-wheeled balancing robot for fun. I have all of the hardware built and put together, and I think I have it coded as well. I'm using an IMU with gyro and accelerometers to find my tilt angle with a complimentary filter for smoothing the signal. The input signal from the IMU seems pretty smooth, as in less than 0.7 variance + or - the actual tilt angle.

My IMU sampling rate is 50 Hz and I do a PID calculation at 50 Hz too, which I think should be fast enough.

Basically, I'm using the PID library found at PID Library .

When I set the P value to something low then the wheels go in the right direction.

When I set the P value to something large then I get an output like the graph.

Enter image description here

Jonas
  • 121,568
  • 97
  • 310
  • 388
Steven McConnon
  • 2,650
  • 2
  • 16
  • 21
  • 1
    You need to cut down on the amount of code in your question, no one is going to read all that. Please try to narrow it down, and it will be much easier to help you. If more is needed, people will tell you what they need. – Some programmer dude May 21 '13 at 07:25
  • Steven, I find your project fascinating! I assume IMU is Inertial Measurement Unit? @JoachimPileborg above is correct, lots of code and hard to deduce logic without full knowledge about your hardware. I see you are starting with PID = 0,0,0. My suspicion these values should be different. When P is high you say the wheels go in the wrong direction, and that sounds like your software logic is faulty. I suggest you describe the project on a web site if possible then post a link here. I for one am interested. Good luck. – user2019047 May 21 '13 at 16:06
  • Thanks @JoachimPileborg, I removed the code and added a graph to make it easier to see what I'm asking. – Steven McConnon May 22 '13 at 02:47
  • After working on a QuadCoptor for a few months tuning the PID controller was one of the hardest things. For PID values we used P=0.35 and I think I=0.55, D=0.65. Before setting I and D you want P to be able to oscillateclose to Setpoint and then I will turn that oscillating down and make it more exact to the Setpoint after a few rotations. (QuadCoptor weighted 4.5lbs and provided 12-13lbs of thrust) – Steven10172 May 22 '13 at 14:09
  • 1
    You may want to have a look at questions with the [pid](http://robotics.stackexchange.com/questions/tagged/pid) tag over on [robotics](http://robotics.stackexchange.com), since we are starting to build up a number of good PID control question (as opposed to PID here, which is supposed to be for *process id* questions). If this didn't already have some answers here I would suggest this get migrated over to *robotics* (where I am a protem mod) as it would be a good fit for us. – Mark Booth Jun 05 '13 at 19:09

4 Answers4

1

From the graph it looks like your system is not stable. I hope you have tested each subsystem of your robot before directly going for tuning. Which means that both sensors and actuators are responding properly and with acceptable error. Once each subsytem is calibrated properly for external error. You can start tuning. Once this done is you can start with valid value of P may be (0.5) to first achieve proper response time, you will need to do some trials here, them increment I slowly to cut down steady state error if any and use D only when required(in case of oscillation). I would suggest to handle P,I and D one by one instead of tweaking all at one time. Also during the testing you will need to continuously monitor the your sensor and actuator data to see if they are in acceptable range.

praks411
  • 1,972
  • 16
  • 23
  • This was the answer, specifically unit testing each subsystem individually before going for tuning. Turns out I had a bad IMU that was giving incorrect data. – Steven McConnon Aug 06 '21 at 08:42
1

As Praks Wrote, your system looks as if it is either unstable or at perhaps marginally stable.

Generally Two wheeled robots can be quite difficult to control as they are inherently unstable without a controller.

I would personally try A PD controller at first, and if you have problems with setpoint accuracy i would use a PID, but just remember that if you want to have a Differential gain in your controller (The D part) it is extremely important that you have a very smooth signal.

Also, the values of the controller greatly depends on your hardware setup (Weight and weight distribution of the robot, motor coefficients and voltage levels) and the units you use internally in your software for the control signals (eg. mV V, degrees/radians). This entails that it will almost be impossible for anybody to guess the correct parameters for you.

What a control engineer could do would be to make a mathematical model of the robot and analyse the pole/zero locations.

If you have any experience with control theory you can take a look at the following paper, and see if it makes sense to you.

http://dspace.mit.edu/bitstream/handle/1721.1/69500/775672333.pdf

Sigurd V
  • 5,077
  • 4
  • 18
  • 27
1

There are many heuristic rules to PID tuning out there, but what most people fail to realize is that PID tuning should not be an heuristic process, but should based on math and science.

What @Sigurd V said is correct: "What a control engineer could do would be to make a mathematical model...", and this can get as complicated as you want. But now a days there are many software tools that can help you automate all the math stuff and get you your desired PID gains quite easily.

Assuming all your hardware is in good shape you can use a free online tool like PidTuner to input your data and get near to optimal PID gains. I have personally used it and achieved good results. Use these as an starting point and then tune manually if required.

0

If you haven't already, I'd suggest you do a search on the terms Arduino PID (obvious suggestion but lots of people have been down this road). I remember when that PID library was being written, the author posted quite a bit with tutorials, etc. (example). Also I came across this PIDAutotuneLibrary.

I wrote my own PID routines but also had a heck of a time tuning and never got it quite right.

spring
  • 18,009
  • 15
  • 80
  • 160