I'm in the process of writing my own simple quadcopter controller for experimental use, and I'm having trouble getting my head around how to convert from the degrees which my PID controller demands to an appropriate 1k-2k range for PWM output. For example, take the roll axis on a '+' configured 'copter (pseudo-code):
setpoint = scaleToRange(receiver.rollValue, -30, 30); //scale the command (1000-2000) to between -30 and 30 degrees, as that's the maximum roll permitted.
demandedRoll = rollPID.calculate(setpoint, imu.currentRoll, PID_params);
/// the part I'm having trouble with
motorLeft_command = receiver.throttle - rollPWM;
motorRight_command = receiver.throttle + rollPWM;
How do I take the roll demanded by my PID controller and convert it to a value useful to the motors, that is to say, where does rollPWM
come from? My first instinct is to use a simple linear relationship, i.e.:
rollPWM = scaleToRange(demandedRoll, MinValue=receiver.throttle/2, MaxValue=2000-receiver.throttle);
//don't let it go beyond 50% of throttle on low end, and the ESC's max on the high end.
However this seems far too simplistic to work.
Or should I be doing more calculations before everything goes through PID control? Any help would be great.