C code PID controller, where to start?

Firmware/software/electronics/mechanics
Post Reply
fberg
Beginner
Posts: 3
Joined: Wed Nov 12, 2014 3:08 pm

C code PID controller, where to start?

Post by fberg »

Hi!

I am looking to implement my own PID control scheme for the crazyflie for a school project.
Preferably i would like to do this using c code since that is what is taught in class.
I have looked through the firmware code but its a bit overwhelming, any recommendations on how to start?

Thanks

/Johan
whoenig
Expert
Posts: 395
Joined: Mon Oct 27, 2014 2:55 am

Re: C code PID controller, where to start?

Post by whoenig »

This is a fairly ambitious project. The control scheme needs to run fairly fast in order to work, so you would need to run it on board on the crazyflie. On the other hand, if you finish that, you will learn a lot! I would recommend the following steps:

1. Make sure that you can compile and flash the original firmware.
2. Reduce the firmware to the bare minimum (e.g. remove everything except of the main function)
3. Readd functionality
a) Just blinking LEDs
b) Communication (if you don't have JTAG for debugging)
c) device drivers for the sensors you are interested in
4. Once you get to the control part, try to fix the crazyflie on one axis and concentrate on one axis at a time only (e.g. roll).

The controller you need is actually not just one PID controller. You will need sensor fusion and a cascade of multiple controllers.
fberg
Beginner
Posts: 3
Joined: Wed Nov 12, 2014 3:08 pm

Re: C code PID controller, where to start?

Post by fberg »

Thank you!

Yes it could become quite a large project, we definitely have the necessary skills in control theory and sensor fusion to do this but we are somewhat lacking in c programming. It would therefore be very useful to avoid coding it all from scratch. We were thinking about simply replacing the existing PID controller in the firmware with another (perhaps an LQ controller). Would this be possible to do given the quadrocopters processor, or would it have to be super efficiently coded in order to be fast enough?

Would it be easier to do this on the PC side, i.e writing python code instead and leaving the firmware in the quad as is?
whoenig
Expert
Posts: 395
Joined: Mon Oct 27, 2014 2:55 am

Re: C code PID controller, where to start?

Post by whoenig »

I wouldn't do it from scratch either. Step 2 in my previous list was more or less only there to understand the rest of the firmware. If you already understand how FreeRTOS works etc., you can go ahead and replace the controller code only.

I don't know what an LQ controller includes exactly. However, the onboard processor is quite powerful (32bit, FPU, 72Mhz) and should be sufficient. Someone measured that the current firmware only uses about 60% of its computing power so far.

Writing it on the PC side doesn't work really well. Basically, the latency is too high to run the control loop fast enough. I have a prototype for that and whenever a packet drops the flie would become unstable. However, you could implement your controller in simulation first and then port it to C later on.
fberg
Beginner
Posts: 3
Joined: Wed Nov 12, 2014 3:08 pm

Re: C code PID controller, where to start?

Post by fberg »

Okey, the LQ controller will probably not be a lot more computationally heavy than a PID, i'm more worried about the efficiency of our code...

Anyways, thank you very much for the help :)
Post Reply