2014 Eurobot fork
Dependencies: mbed-rtos mbed QEI
Processes/MotorControl/MotorControl.cpp@63:c2c6269767b8, 2013-04-14 (annotated)
- Committer:
- madcowswe
- Date:
- Sun Apr 14 14:32:43 2013 +0000
- Revision:
- 63:c2c6269767b8
- Parent:
- 62:78d99b781f02
- Child:
- 64:c979fb1cd3b5
Workinf feed forward both turn and fwd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 22:6e3218cf75f8 | 1 | |
madcowswe | 22:6e3218cf75f8 | 2 | #include "MainMotor.h" |
madcowswe | 22:6e3218cf75f8 | 3 | #include "Encoder.h" |
madcowswe | 22:6e3218cf75f8 | 4 | #include "globals.h" |
madcowswe | 22:6e3218cf75f8 | 5 | #include <algorithm> |
madcowswe | 28:4e20b44251c6 | 6 | #include "system.h" |
madcowswe | 62:78d99b781f02 | 7 | #include "supportfuncs.h" |
madcowswe | 22:6e3218cf75f8 | 8 | |
madcowswe | 25:b16f1045108f | 9 | namespace MotorControl |
madcowswe | 25:b16f1045108f | 10 | { |
madcowswe | 22:6e3218cf75f8 | 11 | |
madcowswe | 60:5058465904e0 | 12 | volatile float fwdcmd = 0; |
madcowswe | 60:5058465904e0 | 13 | volatile float omegacmd = 0; |
madcowswe | 22:6e3218cf75f8 | 14 | |
madcowswe | 62:78d99b781f02 | 15 | volatile float mfwdpowdbg = 0; |
madcowswe | 62:78d99b781f02 | 16 | volatile float mrotpowdbg = 0; |
madcowswe | 62:78d99b781f02 | 17 | |
madcowswe | 62:78d99b781f02 | 18 | MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B); |
madcowswe | 62:78d99b781f02 | 19 | |
madcowswe | 25:b16f1045108f | 20 | void motor_control_isr() |
madcowswe | 25:b16f1045108f | 21 | { |
madcowswe | 60:5058465904e0 | 22 | |
madcowswe | 62:78d99b781f02 | 23 | const float power_per_dc_m_per_s = 1.64f; |
madcowswe | 62:78d99b781f02 | 24 | const float hysteresis_pwr = 0.16f; |
madcowswe | 25:b16f1045108f | 25 | |
madcowswe | 25:b16f1045108f | 26 | float testspeed = 0.2; |
madcowswe | 33:a49197572737 | 27 | float Fcrit = 1.75; |
madcowswe | 63:c2c6269767b8 | 28 | float Pcrit = 10; |
madcowswe | 33:a49197572737 | 29 | float Pgain = Pcrit*0.45; |
madcowswe | 63:c2c6269767b8 | 30 | float Igain = 1.2f*Pgain*Fcrit*0.05; |
madcowswe | 33:a49197572737 | 31 | |
madcowswe | 33:a49197572737 | 32 | float testrot = 0.5*PI; |
madcowswe | 63:c2c6269767b8 | 33 | float Pcrit_rot = 20; |
madcowswe | 33:a49197572737 | 34 | float Pgain_rot = Pcrit_rot*0.45f; |
madcowswe | 33:a49197572737 | 35 | float Fcrit_rot = 1.75f; |
madcowswe | 34:e1678450feec | 36 | float Igain_rot = 1.2f*Pgain_rot*Fcrit_rot*0.1; |
madcowswe | 33:a49197572737 | 37 | |
madcowswe | 33:a49197572737 | 38 | //float Dgain = |
madcowswe | 25:b16f1045108f | 39 | static float lastT = SystemTime.read(); |
madcowswe | 22:6e3218cf75f8 | 40 | static float lastright = right_encoder.getTicks() * ENCODER_M_PER_TICK; |
madcowswe | 22:6e3218cf75f8 | 41 | static float lastleft = left_encoder.getTicks() * ENCODER_M_PER_TICK; |
madcowswe | 25:b16f1045108f | 42 | |
madcowswe | 25:b16f1045108f | 43 | static float thetafiltstate = 0; |
madcowswe | 25:b16f1045108f | 44 | static float fwdfiltstate = 0; |
madcowswe | 25:b16f1045108f | 45 | |
madcowswe | 25:b16f1045108f | 46 | float currright = right_encoder.getTicks() * ENCODER_M_PER_TICK; |
madcowswe | 25:b16f1045108f | 47 | float dright = currright - lastright; |
madcowswe | 25:b16f1045108f | 48 | lastright = currright; |
madcowswe | 25:b16f1045108f | 49 | |
madcowswe | 25:b16f1045108f | 50 | float currleft = left_encoder.getTicks() * ENCODER_M_PER_TICK; |
madcowswe | 25:b16f1045108f | 51 | float dleft = currleft - lastleft; |
madcowswe | 25:b16f1045108f | 52 | lastleft = currleft; |
madcowswe | 25:b16f1045108f | 53 | |
madcowswe | 25:b16f1045108f | 54 | float currtime = SystemTime.read(); |
madcowswe | 25:b16f1045108f | 55 | float dt = currtime - lastT; |
madcowswe | 63:c2c6269767b8 | 56 | //dt = 0.05; //TODO: HACK! |
madcowswe | 25:b16f1045108f | 57 | lastT = currtime; |
madcowswe | 25:b16f1045108f | 58 | |
madcowswe | 25:b16f1045108f | 59 | thetafiltstate = MOTORCONTROLLER_FILTER_K * thetafiltstate + (1-MOTORCONTROLLER_FILTER_K) * ((dright-dleft)/(dt*ENCODER_WHEELBASE)); |
madcowswe | 25:b16f1045108f | 60 | fwdfiltstate = MOTORCONTROLLER_FILTER_K * fwdfiltstate + (1-MOTORCONTROLLER_FILTER_K) * ((dright+dleft)/(2.0f*dt)); |
madcowswe | 25:b16f1045108f | 61 | |
madcowswe | 60:5058465904e0 | 62 | float errfwd = fwdcmd - fwdfiltstate; |
madcowswe | 60:5058465904e0 | 63 | float errtheta = omegacmd - thetafiltstate; |
madcowswe | 33:a49197572737 | 64 | |
madcowswe | 33:a49197572737 | 65 | static float fwdIstate = 0; |
madcowswe | 33:a49197572737 | 66 | static float rotIstate = 0; |
madcowswe | 33:a49197572737 | 67 | |
madcowswe | 63:c2c6269767b8 | 68 | float actuatefwd = errfwd*Pgain + fwdIstate*Igain;// + max(power_per_dc_m_per_s*abs(fwdcmd), hysteresis_pwr)*sgn(fwdcmd); |
madcowswe | 33:a49197572737 | 69 | float actuaterot = errtheta*Pgain_rot + rotIstate*Igain_rot; |
madcowswe | 25:b16f1045108f | 70 | |
madcowswe | 33:a49197572737 | 71 | float actuateleft = actuatefwd - (actuaterot*ENCODER_WHEELBASE/2.0f); |
madcowswe | 33:a49197572737 | 72 | float actuateright = actuatefwd + (actuaterot*ENCODER_WHEELBASE/2.0f); |
madcowswe | 63:c2c6269767b8 | 73 | |
madcowswe | 63:c2c6269767b8 | 74 | float lff = fwdcmd - omegacmd*ENCODER_WHEELBASE/2.0f; |
madcowswe | 63:c2c6269767b8 | 75 | float rff = fwdcmd + omegacmd*ENCODER_WHEELBASE/2.0f; |
madcowswe | 25:b16f1045108f | 76 | |
madcowswe | 63:c2c6269767b8 | 77 | mleft(max(min(actuateleft + max(power_per_dc_m_per_s*abs(lff), hysteresis_pwr)*sgn(lff), MOTOR_MAX_POWER), -MOTOR_MAX_POWER)); |
madcowswe | 63:c2c6269767b8 | 78 | mright(max(min(actuateright + max(power_per_dc_m_per_s*abs(rff), hysteresis_pwr)*sgn(rff), MOTOR_MAX_POWER), -MOTOR_MAX_POWER)); |
madcowswe | 62:78d99b781f02 | 79 | |
madcowswe | 63:c2c6269767b8 | 80 | if (!(abs(actuateleft) > MOTOR_MAX_POWER || abs(actuateright) > MOTOR_MAX_POWER)){ |
madcowswe | 63:c2c6269767b8 | 81 | fwdIstate += errfwd; |
madcowswe | 63:c2c6269767b8 | 82 | rotIstate += errtheta; |
madcowswe | 63:c2c6269767b8 | 83 | } |
madcowswe | 63:c2c6269767b8 | 84 | |
madcowswe | 63:c2c6269767b8 | 85 | //mfwdpowdbg = 0;//fwdfiltstate;//; |
madcowswe | 63:c2c6269767b8 | 86 | //mrotpowdbg = rotIstate*Igain_rot;//thetafiltstate;//; |
madcowswe | 25:b16f1045108f | 87 | |
madcowswe | 22:6e3218cf75f8 | 88 | } |
madcowswe | 22:6e3218cf75f8 | 89 | |
madcowswe | 22:6e3218cf75f8 | 90 | } |