This is some awesome robot code

Dependencies:   mbed-rtos mbed QEI

Fork of ICRSEurobot13 by Thomas Branch

Committer:
madcowswe
Date:
Sun Apr 14 12:57:04 2013 +0000
Revision:
62:78d99b781f02
Parent:
60:5058465904e0
Child:
63:c2c6269767b8
Basic fwd dc power feed forward working

Who changed what in which revision?

UserRevisionLine numberNew 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 34:e1678450feec 28 float Pcrit = 10*0.5;
madcowswe 33:a49197572737 29 float Pgain = Pcrit*0.45;
madcowswe 33:a49197572737 30 float Igain = 1.2f*Pgain*Fcrit*0.2;
madcowswe 33:a49197572737 31
madcowswe 33:a49197572737 32 float testrot = 0.5*PI;
madcowswe 33:a49197572737 33 float Pcrit_rot = 10;
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 62:78d99b781f02 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 fwdIstate += errfwd;
madcowswe 33:a49197572737 68 rotIstate += errtheta;
madcowswe 33:a49197572737 69
madcowswe 62:78d99b781f02 70 float actuatefwd = errfwd*Pgain + fwdIstate*Igain + max(power_per_dc_m_per_s*abs(fwdcmd), hysteresis_pwr)*sgn(fwdcmd);
madcowswe 33:a49197572737 71 float actuaterot = errtheta*Pgain_rot + rotIstate*Igain_rot;
madcowswe 25:b16f1045108f 72
madcowswe 33:a49197572737 73 float actuateleft = actuatefwd - (actuaterot*ENCODER_WHEELBASE/2.0f);
madcowswe 33:a49197572737 74 float actuateright = actuatefwd + (actuaterot*ENCODER_WHEELBASE/2.0f);
madcowswe 25:b16f1045108f 75
madcowswe 33:a49197572737 76 mleft(max(min(actuateleft, MOTOR_MAX_POWER), -MOTOR_MAX_POWER));
madcowswe 33:a49197572737 77 mright(max(min(actuateright, MOTOR_MAX_POWER), -MOTOR_MAX_POWER));
madcowswe 62:78d99b781f02 78
madcowswe 62:78d99b781f02 79 mfwdpowdbg = fwdIstate*Igain;
madcowswe 62:78d99b781f02 80 mrotpowdbg = rotIstate*Igain_rot;
madcowswe 25:b16f1045108f 81
madcowswe 22:6e3218cf75f8 82 }
madcowswe 22:6e3218cf75f8 83
madcowswe 22:6e3218cf75f8 84 }