2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Wed Apr 10 20:06:29 2013 +0000
Revision:
34:e1678450feec
Parent:
33:a49197572737
Child:
60:5058465904e0
gains a bit more decent

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 22:6e3218cf75f8 7
madcowswe 25:b16f1045108f 8 namespace MotorControl
madcowswe 25:b16f1045108f 9 {
madcowswe 22:6e3218cf75f8 10
madcowswe 22:6e3218cf75f8 11 float fwdcmd = 0;
madcowswe 25:b16f1045108f 12 float omegacmd = 0;
madcowswe 22:6e3218cf75f8 13
madcowswe 25:b16f1045108f 14 void motor_control_isr()
madcowswe 25:b16f1045108f 15 {
madcowswe 25:b16f1045108f 16
madcowswe 22:6e3218cf75f8 17 MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B);
madcowswe 25:b16f1045108f 18
madcowswe 25:b16f1045108f 19 float testspeed = 0.2;
madcowswe 33:a49197572737 20 float Fcrit = 1.75;
madcowswe 34:e1678450feec 21 float Pcrit = 10*0.5;
madcowswe 33:a49197572737 22 float Pgain = Pcrit*0.45;
madcowswe 33:a49197572737 23 float Igain = 1.2f*Pgain*Fcrit*0.2;
madcowswe 33:a49197572737 24
madcowswe 33:a49197572737 25 float testrot = 0.5*PI;
madcowswe 33:a49197572737 26 float Pcrit_rot = 10;
madcowswe 33:a49197572737 27 float Pgain_rot = Pcrit_rot*0.45f;
madcowswe 33:a49197572737 28 float Fcrit_rot = 1.75f;
madcowswe 34:e1678450feec 29 float Igain_rot = 1.2f*Pgain_rot*Fcrit_rot*0.1;
madcowswe 33:a49197572737 30
madcowswe 33:a49197572737 31 //float Dgain =
madcowswe 25:b16f1045108f 32 static float lastT = SystemTime.read();
madcowswe 22:6e3218cf75f8 33 static float lastright = right_encoder.getTicks() * ENCODER_M_PER_TICK;
madcowswe 22:6e3218cf75f8 34 static float lastleft = left_encoder.getTicks() * ENCODER_M_PER_TICK;
madcowswe 25:b16f1045108f 35
madcowswe 25:b16f1045108f 36 static float thetafiltstate = 0;
madcowswe 25:b16f1045108f 37 static float fwdfiltstate = 0;
madcowswe 25:b16f1045108f 38
madcowswe 25:b16f1045108f 39 float currright = right_encoder.getTicks() * ENCODER_M_PER_TICK;
madcowswe 25:b16f1045108f 40 float dright = currright - lastright;
madcowswe 25:b16f1045108f 41 lastright = currright;
madcowswe 25:b16f1045108f 42
madcowswe 25:b16f1045108f 43 float currleft = left_encoder.getTicks() * ENCODER_M_PER_TICK;
madcowswe 25:b16f1045108f 44 float dleft = currleft - lastleft;
madcowswe 25:b16f1045108f 45 lastleft = currleft;
madcowswe 25:b16f1045108f 46
madcowswe 25:b16f1045108f 47 float currtime = SystemTime.read();
madcowswe 25:b16f1045108f 48 float dt = currtime - lastT;
madcowswe 25:b16f1045108f 49 lastT = currtime;
madcowswe 25:b16f1045108f 50
madcowswe 25:b16f1045108f 51 thetafiltstate = MOTORCONTROLLER_FILTER_K * thetafiltstate + (1-MOTORCONTROLLER_FILTER_K) * ((dright-dleft)/(dt*ENCODER_WHEELBASE));
madcowswe 25:b16f1045108f 52 fwdfiltstate = MOTORCONTROLLER_FILTER_K * fwdfiltstate + (1-MOTORCONTROLLER_FILTER_K) * ((dright+dleft)/(2.0f*dt));
madcowswe 25:b16f1045108f 53
madcowswe 25:b16f1045108f 54 float errfwd = fwdfiltstate - fwdcmd;
madcowswe 25:b16f1045108f 55 float errtheta = thetafiltstate - omegacmd;
madcowswe 33:a49197572737 56
madcowswe 33:a49197572737 57 static float fwdIstate = 0;
madcowswe 33:a49197572737 58 static float rotIstate = 0;
madcowswe 33:a49197572737 59 fwdIstate += errfwd;
madcowswe 33:a49197572737 60 rotIstate += errtheta;
madcowswe 33:a49197572737 61
madcowswe 33:a49197572737 62 float actuatefwd = errfwd*Pgain + fwdIstate*Igain;
madcowswe 33:a49197572737 63 float actuaterot = errtheta*Pgain_rot + rotIstate*Igain_rot;
madcowswe 25:b16f1045108f 64
madcowswe 33:a49197572737 65 float actuateleft = actuatefwd - (actuaterot*ENCODER_WHEELBASE/2.0f);
madcowswe 33:a49197572737 66 float actuateright = actuatefwd + (actuaterot*ENCODER_WHEELBASE/2.0f);
madcowswe 25:b16f1045108f 67
madcowswe 33:a49197572737 68 mleft(max(min(actuateleft, MOTOR_MAX_POWER), -MOTOR_MAX_POWER));
madcowswe 33:a49197572737 69 mright(max(min(actuateright, MOTOR_MAX_POWER), -MOTOR_MAX_POWER));
madcowswe 25:b16f1045108f 70
madcowswe 22:6e3218cf75f8 71 }
madcowswe 22:6e3218cf75f8 72
madcowswe 22:6e3218cf75f8 73 }