PID tuning II (lots of words, no pictures, but very interesting!)

I’ve been PID tuning for the past couple of days, and it’s been very tricky (for that read “effing PITA”).

When Phoebe had her centre of gravity dangling between her legs (Gentlemen, behave!), she was virtually self levelling – equal power to all blades and gravity would do the rest.

But with the rebuild, her centre of gravity, though still below the plane of the propellers, isn’t much below.  And that does make her very sensitive – a good thing as her reaction to disturbances will be super quick but only once I’ve got the PIDs tuned..

Thankfully, one of the reader’s, David, commented about mathematical tuning called the Ziegler-Nichols method.  It’s not perfect as this “we have a chip-on-our-shoulder about Z-N PID tuning and suggest you pay us for our tuner instead” article describes in detail.  However, the systems they name which should work match the dynamics of a quadcopter.  The Z-N algorithm takes away two thirds of the tuning, and makes very clear and simple what you’re looking for in the remaining “third”.  I strongly recommend you read it before carrying on reading here – it’s only a very small Wiki stub article.

The aim is to find the magical Ku, the ultimate gain, and the frequency of stable wobbles @ Ku. From that you can determined Kp, Ki and Kd for whatever kind of behaviour you might be looking for – mellow, jumpy or normal.  Sounds too good to be true, doesn’t it!

And indeed it is, but it’s no fault of Ziegler-Nichols that it doesn’t work reliably.  As my testing progressed, and the battery voltage reduces as a result, so do the resulting Ku values, and that can only be blamed on the ESC for not increasing the pulse widths of the PWM signal feeding the motors to compensate as it detects the reducing supply voltage of its battery.

The reducing supply voltage does change the speed of rotation since that driven directly by the PWM switching between the 3 phases of the motors coils.  But it does affect the power applied to those coils.

The ESC should be able to compensate for the reduced power based upon the fact motors have a manufacturer Kv value relating rpm to voltage supplied linearly. The ESC PWM signal to the motors is effectively dividing the input voltage from the batteries to a lower voltage. So if the ESC tracked the input voltage, it could increase the PWM pulse width to the motors to compensate. But I suspect my ESCs don’t do that leading to the every decreasing Ku values I’m seeing.

Now I’ve never liked the idea of a microcontroller ESC sitting between the Raspberry Pi and the motors – I don’t like the separation of control – it’s like Dalek’s serving tea to Winston Churchill in the second World War – you just can’t trust them. And I think now I understand enough about what’s going on that I could make my own ESCs that are better. Here’s the ingredients list:

  • Adafruit i2c 16-channel PWM servo driver which conveniently I have in stock
  • A rewrite of the PWM code to drive 3-phase PWM per motor (12 channel for four 3-phase motors)
  • 12 MOSFETs to switch the LiPo voltage across the motor phases according to the supplied PWM
  • 12 opto-isolators to keep the Raspberry Pi / Adafruit PWM safely isolated from the LiPo battery current across the MOSFETs / motors
  • An ADC converter tracking the voltage level of the LiPo where the value is used to increase the PWM pulse width in line with the decreasing voltage of the battery over time – this could also be used for an auto-landing / return home trigger for Phoebe as her batteries start running low.

The one missing piece of the puzzle is the connection between the ESC to motor PWM used to ‘reduce’ the power supplied.  But, again, thanks to another comment conversation with David, it should be possible through a combination of audio spectrum analysis to determine rpm, combined with the motor’s Kv to infer the ESC to motor PWM power on / off ratio.

I’m really quite excited about this, thanks David. Having written all this up, I definitely owe you a couple of beers.

Sadly, I do still need to sort out the PID tuning for the tetchy Phoebe, but this was an interesting diversion, and has got me thinking about how to make Phoebe the 100% Raspberry Pi Quadcopter controller I’d always hoped to achieve.  And that’s very exciting!

P.S. Just realized that the home grown ESC will need 2 PWM feeds per motors – the first provide the 3 phase output to a given motor at the required rotation speed, and the second uses Kv combined with the rotation speed to ensure the right voltage is supplied to the motors to run at that speed effectively / efficiently.

5 thoughts on “PID tuning II (lots of words, no pictures, but very interesting!)

  1. Hi!
    First of all, thanks for sharing and telling the tales of building a quadcopter.
    I appreciate following your steps , but now I want to build my own quad. I was looking for information about ESCs and then I saw that lots of them use an Atmega (Arduino) microcontroller and there are custom firmwares that boost the performance of them. My question is, did you already test them? I think you can even edit the firmware to your needs.

    • I didn’t try any other ESCs – I just bought the DJI 30A ESCs to match my DJI F450 Quad frame kit. I have bought different ESCs (Black Sheep with SimonK firmware) for my next Quad, but I still probably won’t mess with the firmware unless I have to, I’m more interested in the Raspberry Pi Flight Controller

  2. It would be really cool indeed to get Raspberry Pi only controlled quad! In fact, If one is not using the *funky* features of ESCs (like braking, reversing, etc.), why have them at all?

    Looking forward to a 100% build! 🙂

    p.s. Never seen or heard of “adaptive” PWM signal that takes into account the input voltage, but would quite exciting to see one in action. I suppose this would allow the flying capabilities/stability would last longer (because you’d still get the correct RPM/thrust even with lower voltage). And it would also mean that the range of operation could determine “live”: At some point, the battery will be so low, that the PWM will not be able to compensate anymore, indicatating that that particular RPM cannot be reached anymore. From that, one could set alarms, or trigger a “return home and land” procedure. It could eve detect for instance that the wind (when outside) is getting too strong, that she cannot cope with it…

    • I don’t think thing’s like braking & reversing would be that hard to implement in software – simply the change in PID target from forward to hover could well do the job – it all depends how you tune your PIDs.

      I think the ESCs offer setting like mellow, norma and frantic – and again, this would be relatively easy to do by some dynamic averaging of target changes in software – again a very simply addition to the code.

    • Just remembered that some PIDs use inductive feedback from the motors – i.e. if the motor is under- or overshooting the spin defined by the PWM, then there is a detectable feedback signal from the motors caused by the ‘inductor’ that is the motor coil passing past the static magnets. Perhaps my ESCs don’t use that?

      I guess a direct feedback mechanism like this would be preferable to reliance on an arbitrary Kv?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.