PID controller in max?

Hugobox's icon

Hi!
I'm trying to control a remote controlled helicopter with max. Tracking with a camera works and sending new coordinates works but the helicopter always overshoots and ends up circling around the target destination. so I was wondering if anybody had implemented a PID controller in Max?
Thanks!

mr_mapes's icon

PID eh, that takes me back.... Haven't touched that since toying with vacuum furnace temperature controls! I think I still remember the maths behind them, simple JS code too I reckon. I'll see what I can come up with

Hugobox's icon

according to wikipedia, the pseudocode for a PID looks like that, not sure how to implement it tough:

previous_error = setpoint - actual_position
integral = 0
start:
error = setpoint - actual_position
integral = integral + (error*dt)
derivative = (error - previous_error)/dt
output = (Kp*error) + (Ki*integral) + (Kd*derivative)
previous_error = error
wait(dt)
goto start

mr_mapes's icon

Yeah that's pretty much the form. Try this on for size - I've just fed the output into an accumulator to drive the *actualposition* value for test purposes though how you're choosing to use the error output is down to you. The JS code is attached to a comment box.

Max Patch
Copy patch and select New From Clipboard in Max.

I'd like to see if anyone else has any suggestions or improvements to this or my JS coding purely for my own learning benefit!

Hugobox's icon

Thanks! I'll give this a try!

mr_mapes's icon

Just so you know, I encountered a few initialisation problems where the numbers started coming out of the JS waaaaay too big and causing errors elsewhere - can be rectified by carefully initialising the patch. It does work though :)

Strophe's icon

Hi Hugobox, did you fix your problem ?
I'm looking for a subpatch for a PID thing to control a position of a motor, and 'im not very good with math, the equation PID is very complicated for me...
Have you something working ?

Regards, thanks.

Strophe.