Shop OBEX P1 Docs P2 Docs Learn Events
Simple Solution? — Parallax Forums

Simple Solution?

teganburnsteganburns Posts: 134
edited 2014-07-15 06:59 in Propeller 1
I'm currently tuning a PID controller (For the first time) on my tricopter and from what I've read you start with a P controller. Then you set you gain to a value that will get you there the quickest but without overshooting.
My problem is that that any whole number is an overshot and when i try and use something like "3/4" or "1/2" it always rounds it down to zero. I have read about floating points a little but I feel like there is an easier way to solve this.
Any ideas? Thanks

Comments

  • jmgjmg Posts: 15,173
    edited 2014-07-14 13:13
    Try fixed point, - if you have 32 bit integers, there is usually more than enough dynamic range to multiply by 10,16,64,100,128 (or whatever you decide).
    The LSB effect becomes a smaller fraction in each case, and you need only to check that no numeric overflow occurs during the calculation.
  • teganburnsteganburns Posts: 134
    edited 2014-07-14 13:27
    I think I understand what you are saying but can you give me an example?
  • kwinnkwinn Posts: 8,697
    edited 2014-07-14 17:15
    teganburns wrote: »
    I think I understand what you are saying but can you give me an example?

    Lets say the range of numbers you are working with is 0 to 100 in steps of 0.1, and you need a P of 0.3 for optimum control. Multiply them by 10 and you now have a range of 0 to 1000 and a P of 3. Do all the calculations and then divide the result by 10. This works even better if the multiplier is a power of 2 so shifts can be used in place of multiplies and divides.

    If it is possible I do all the calculations using the binary numbers received directly from the sensors.
  • teganburnsteganburns Posts: 134
    edited 2014-07-14 17:18
    Ok thanks that's what I though but wasn't sure.
    kwinn wrote: »

    If it is possible I do all the calculations using the binary numbers received directly from the sensors.

    What's the advantage to that?
  • abecedarianabecedarian Posts: 312
    edited 2014-07-14 19:09
    teganburns wrote: »
    Ok thanks that's what I though but wasn't sure.


    What's the advantage to that?
    Any math you can do that is a native typedef, i.e. byte, char, int, and doesn't require conversion to another type, or the math only needs multiply or divide by 2, 4, 8 and so on, means easy math and maybe the processor can keep from doing fixed point or even worse, floating point, which is even more processor intensive, and requires more memory to handle.

    So, in short, if your numbers have decimal points, multiply them so the decimal / fraction goes away: 0.1 becomes 1, 0.123 becomes 123, et cetera. Also, try to use the intrinsic data type when possible so the compiler doesn't have to cast an 8 bit char to 32 bit integer.

    In some instances storing four 8 bit char's (or bytes or gasp... Booleans) can take up as much memory as four 32 bit integers.


    I'll shut up now and let the pro's correct me. :)
  • kwinnkwinn Posts: 8,697
    edited 2014-07-14 20:59
    teganburns wrote: »
    Ok thanks that's what I though but wasn't sure.


    What's the advantage to that?

    As abecedarian posted the advantage is not having to do a lot of conversions. That can save cpu cycles you may need for something else. A pid loop calculation does not care what the units are. It takes an input measurement, compares it to the desired measurement (set point), and uses the difference ( error signal ) to calculate a change to the output signal that controls the input signal. If you have binary numbers coming in and binary numbers going out why not skip the conversions and do all the calculations in binary?
  • Heater.Heater. Posts: 21,230
    edited 2014-07-14 22:11
    Because calculating 1 divided by 3 using only binary integers is beyond the ken of most people :)
  • kwinnkwinn Posts: 8,697
    edited 2014-07-15 06:59
    Heater. wrote: »
    Because calculating 1 divided by 3 using only binary integers is beyond the ken of most people :)

    Which is why we have objects and math routines written by helpful generous people who do ken it.
Sign In or Register to comment.