Shop OBEX P1 Docs P2 Docs Learn Events
about PID control problem — Parallax Forums

about PID control problem

ggg558877ggg558877 Posts: 40
edited 2013-03-18 23:44 in Propeller 1
Hi everyone
now i have some problem about PID control
I'm trying to do is have an rc car go toward a heading if i want to go straight on heading angle = 0
so i set the setpoint = 0 and input is Current heading angle and get the output
my qusetion is the output value is the PWM pulse width to control the servo?
thanks

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-03-14 10:16
    I'm not sure if this is the right application for PID,

    What are you using for feedback?

    Do you have a digital compass?

    Or do you just want to set the servo to drive the car straight?

    The attached file looks like a template to be used with other code. You need a parent object to control the output and monitor sensors.

    Depending on what you want to do, you may not need the PID object at all. Very few of my projects require PID (and I have lots of Propeller projects).

    I think if you describe what you want to do, and what equipment you're using, we'll be better able to suggest where to look for information you may need.
  • ggg558877ggg558877 Posts: 40
    edited 2013-03-14 22:16
    Hi Duane Degn
    in my project i have use compass to get heading angle and gps to navigation
    and if i want go straight on heading angle = 0 maybe it can offset to heading angle = 30
    and I want to use PID let my car Correction on heading angle = 0
    and i use PID.spin calculated the PID output
    but i don't certain know is the output value is use in PWM pulse width to control the servo or ?
    thanks
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-03-14 22:37
    ggg558877 wrote: »
    Hi Duane Degn
    in my project i have use compass to get heading angle and gps to navigation
    and if i want go straight on heading angle = 0 maybe it can offset to heading angle = 30
    and I want to use PID let my car Correction on heading angle = 0
    and i use PID.spin calculated the PID output
    but i don't certain know is the output value is use in PWM pulse width to control the servo or ?
    thanks

    Can you program the car to move forward and in a circle? If not, put the compass, GPS and PID aside and develop a simple program to drive the car.

    One of the first tasks I try to accomplish with a new robot is to get to drive in a figure 8. There's a thread in the robot forum where forum members post videos of there robot performing a figure 8.

    Once I figured out how to control the robot enough to drive a figure 8, I start adding sensors and making the program more complicated. (Here's a figure 8 using a compass to sense when the robot should change directions and stop.)

    You still haven't given us information about the car you want to control. I assume it uses a servo to steer?

    What kind of Propeller board do you have?

    If you have a QuickStart board, you can try out my QuickStart servo tester. Post #15 of that thread has very simple servo demo. It may help you learn to control a servo if you don't already know how.
  • pedwardpedward Posts: 1,642
    edited 2013-03-15 00:01
    Your application is no different than mine at post http://forums.parallax.com/showthread.php/146228-Simple-and-cheap-encoder-idea?p=1167026&viewfull=1#post1167026

    I had a target encoder value I wanted to hit, and I used PID to make it work. The PID objects in OBEX didn't work correctly, so I had to improve them to make them work as intended.

    You have a compass, which is the current point, the set point is your desired heading. The trick is the servo. I was using a servo in my experiment too.

    You need to limit the bandwidth of the PID algorithm so your wheels aren't going bang-bang against the stops.

    I chose +-200 counts from the center position (1500). You can see in the code where I did this. I just take 1500 and add the value returned from the PID algorithm to get a velocity value. In your case the PID algorithm will tell the car to turn left or right, and slowly bring it back to center once you're on heading.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2013-03-15 12:14
    The PID object is very general and does not know what you intend to do with it so it helps to have an understanding of how it works.

    Very simply the output of the PID consists of three parts added up. One is proportional to the error, that is it equals the current error multiplied by a number (called the gain) the next part is the integral which adds the current error to an accumulator, this is multiplied by another gain and added to the output. Finally the derivative part is proportional to the rate of change in the error, this too has a gain.

    So the output depends on the way you measure the current system state as well as these gain terms. Bigger gains make this number bigger, different sensors would change it too.

    So most likely you will need to scale the output, offset it and probably limit it. You might get +/- 1000 out of the system but you might want to convert that to a different range you can send to your servos, that depends on your servo driver.

    I would also advise just putting the I and D gains to zero to start with and begin with a small P gain. Play with it and look at the very simple maths and the numbers it is generating and you will start to get it and as mentioned you may want to slow down the rate the loop repeats.

    Graham
  • pedwardpedward Posts: 1,642
    edited 2013-03-15 12:41
    The PID1_1 object is reasonably handy, but it doesn't seem to properly handle time scaling and weighting, so I added that into my edit of that. In the thread linked above, just the actual calculation routine is represented, since the rest didn't change. I also show an example of usage and further down the thread, how I offset and wrapped the output to make it useful as in a motion control.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2013-03-15 12:49
    You don't have the make the I and D errors actually meaningful numbers they just need to be proportional to these errors as any scaling for time gets undone by the arbitrary gains then applied.

    Graham
  • pedwardpedward Posts: 1,642
    edited 2013-03-15 13:00
    There's theory and there's operation, my changes were based on the latter. Until I added weighted scaling, the PID control was useless and ALWAYS overshot the setpoint. Now it decelerates when the error is less and it has less overshoot. More importantly, the overshoot corrects more quickly.

    I spent plenty of time looking at data streaming from the serial to not only tune the parameters, but adjust the algorithm.

    You need to implement time decay without requiring the algorithm be called synchronously, and this turns out to be a rather simple fix, so I did it.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2013-03-15 17:38
    All I am saying in theory and in practice is that it is no different than changing the gain as it is just another fixed scaling factor only it is based on the clock frequency.

    Graham
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2013-03-15 17:39
    p.s. I have implemented PID on the propeller (in assembly) so I am not just spouting theory.
  • tperkinstperkins Posts: 98
    edited 2013-03-15 18:49
    I confirm the attached document is freely publicly available.

    It may even be helpful, especially the table of coefficients on page 8, although it is geared towards relatively long period heat control.

    It may be that instead of using integral alone to provide "reset", that you need to implement a table of responses known to be contextually appropriate, and use PID to finesses errors.
  • ggg558877ggg558877 Posts: 40
    edited 2013-03-18 23:44
    thanks for Duane Degn, pedward , Graham and tperkins
    now i have understand how to use PID in my project
    thanks
Sign In or Register to comment.