hoi

Dependencies:   mbed QEI biquadFilter MODSERIAL

Committer:
CasperBerkhout
Date:
Thu Nov 02 15:30:30 2017 +0000
Revision:
6:b0b15eb27de1
Parent:
5:9e8847a0492c
Child:
7:c78b25ef3c7b
Kinematics added, not working correctly

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CasperBerkhout 0:4141aef83f4b 1 #include "QEI.h"
CasperBerkhout 0:4141aef83f4b 2 #include "math.h"
CasperBerkhout 0:4141aef83f4b 3 #include "mbed.h"
CasperBerkhout 0:4141aef83f4b 4 //#include "HIDScope.h" //set mbed library version to 119 for HIDScope to work
CasperBerkhout 0:4141aef83f4b 5 #include "MODSERIAL.h"
CasperBerkhout 0:4141aef83f4b 6 #include "BiQuad.h"
CasperBerkhout 0:4141aef83f4b 7
CasperBerkhout 0:4141aef83f4b 8 //left pot to set reference position.
CasperBerkhout 0:4141aef83f4b 9 //right pot to set Kp, right pot sets Ki when (right)button is pressed down
CasperBerkhout 0:4141aef83f4b 10
CasperBerkhout 0:4141aef83f4b 11
CasperBerkhout 0:4141aef83f4b 12 //--------------object creation------------------
CasperBerkhout 0:4141aef83f4b 13 Serial pc(USBTX, USBRX);
CasperBerkhout 0:4141aef83f4b 14 //Use X4 encoding.
CasperBerkhout 0:4141aef83f4b 15 //QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING);
CasperBerkhout 0:4141aef83f4b 16 //Use X2 encoding by default.
CasperBerkhout 0:4141aef83f4b 17 QEI enc1(D13, D12, NC, 32); //enable the encoder
CasperBerkhout 0:4141aef83f4b 18 QEI enc2(D15, D14, NC, 32); //enable the encoder
CasperBerkhout 0:4141aef83f4b 19 PwmOut M1_speed(D6);
CasperBerkhout 0:4141aef83f4b 20 PwmOut M2_speed(D5);
CasperBerkhout 0:4141aef83f4b 21 DigitalOut M1_direction(D7);
CasperBerkhout 0:4141aef83f4b 22 DigitalOut M2_direction(D4);
CasperBerkhout 0:4141aef83f4b 23 AnalogIn potmeter(A0); //left pot
CasperBerkhout 0:4141aef83f4b 24 AnalogIn potmeter2(A1); //right pot
CasperBerkhout 1:d7299175a12e 25 InterruptIn button1(D2); //hardware interrupt for stopping motors - right button
CasperBerkhout 1:d7299175a12e 26 DigitalIn LimSW1(D9);
CasperBerkhout 1:d7299175a12e 27 DigitalIn LimSW2(D8);
CasperBerkhout 1:d7299175a12e 28 DigitalIn HomStart(D3); // - left button
CasperBerkhout 1:d7299175a12e 29
CasperBerkhout 4:de0923cf6bcc 30 BiQuad bqlowpass(0.4354, 0.8709, 0.4354, 0.5179, 0.2238);
CasperBerkhout 0:4141aef83f4b 31 //create scope objects - note: script won't run when HID usb port is not connected
CasperBerkhout 0:4141aef83f4b 32 //HIDScope scope(5); //set # of channels
CasperBerkhout 1:d7299175a12e 33
CasperBerkhout 1:d7299175a12e 34 Ticker motor_update1;
CasperBerkhout 1:d7299175a12e 35 Ticker motor_update2;
CasperBerkhout 1:d7299175a12e 36 Ticker error_update;
CasperBerkhout 0:4141aef83f4b 37
CasperBerkhout 0:4141aef83f4b 38
CasperBerkhout 0:4141aef83f4b 39 //-----------------variable decleration----------------
CasperBerkhout 0:4141aef83f4b 40 int pwm_freq = 500;
CasperBerkhout 0:4141aef83f4b 41 float set_speed;
CasperBerkhout 0:4141aef83f4b 42 float reference_pos;
CasperBerkhout 0:4141aef83f4b 43
CasperBerkhout 0:4141aef83f4b 44
CasperBerkhout 0:4141aef83f4b 45
CasperBerkhout 0:4141aef83f4b 46 double M1_home;
CasperBerkhout 0:4141aef83f4b 47 double M1_error_pos = 0;
CasperBerkhout 1:d7299175a12e 48 float M1_Kp = 2;
CasperBerkhout 1:d7299175a12e 49 float M1_Ki = 4;
CasperBerkhout 3:455b79d42636 50 float M1_Kd = 0.19;
CasperBerkhout 0:4141aef83f4b 51 double M1_e_int=0;
CasperBerkhout 0:4141aef83f4b 52 double M1_e_prior=0;
CasperBerkhout 0:4141aef83f4b 53
CasperBerkhout 0:4141aef83f4b 54 double M2_home;
CasperBerkhout 0:4141aef83f4b 55 double M2_error_pos = 0;
CasperBerkhout 4:de0923cf6bcc 56 float M2_Kp = 1.5;
CasperBerkhout 6:b0b15eb27de1 57 float M2_Ki = 2.5;
CasperBerkhout 1:d7299175a12e 58 float M2_Kd = 0;
CasperBerkhout 0:4141aef83f4b 59 double M2_e_int=0;
CasperBerkhout 0:4141aef83f4b 60 double M2_e_prior=0;
CasperBerkhout 0:4141aef83f4b 61
CasperBerkhout 2:c7369b41f7ae 62 double setpoint = 0;
CasperBerkhout 2:c7369b41f7ae 63
CasperBerkhout 2:c7369b41f7ae 64 double M1_rel_pos;
CasperBerkhout 2:c7369b41f7ae 65 double M2_rel_pos;
CasperBerkhout 5:9e8847a0492c 66 double M2_reference_pos;
CasperBerkhout 2:c7369b41f7ae 67
CasperBerkhout 6:b0b15eb27de1 68 double q1_step;
CasperBerkhout 6:b0b15eb27de1 69 double q2_step;
CasperBerkhout 6:b0b15eb27de1 70
CasperBerkhout 6:b0b15eb27de1 71 float Ts = 0.01; //500hz sample freq
CasperBerkhout 0:4141aef83f4b 72
CasperBerkhout 1:d7299175a12e 73 bool M1homflag;
CasperBerkhout 1:d7299175a12e 74 bool M2homflag;
CasperBerkhout 1:d7299175a12e 75 bool Homstartflag;
CasperBerkhout 1:d7299175a12e 76
CasperBerkhout 1:d7299175a12e 77
CasperBerkhout 0:4141aef83f4b 78
CasperBerkhout 0:4141aef83f4b 79
CasperBerkhout 6:b0b15eb27de1 80 void kinematica(double q1, double q2, double vdx, double vdy, double &q1_new, double &q2_new){
CasperBerkhout 0:4141aef83f4b 81 double q1_dot = (2.5*cos(q1+q2))/(sin(q2)) *vdx + (2.5*sin(q1+q2))/(sin(q2))*vdy;
CasperBerkhout 0:4141aef83f4b 82 double q2_dot = -(0.3*cos(q1+q2)+0.4*cos(q1))/(0.12*sin(q2))*vdx -(0.3*sin(q1+q2)+0.4*sin(q2))/(0.12*sin(q2))*vdy;
CasperBerkhout 0:4141aef83f4b 83 q1_new = q1 +q1_dot*Ts;
CasperBerkhout 6:b0b15eb27de1 84 q2_new = q2 +(q2_dot+q1_dot)*Ts;
CasperBerkhout 0:4141aef83f4b 85 return;
CasperBerkhout 0:4141aef83f4b 86 }
CasperBerkhout 0:4141aef83f4b 87
CasperBerkhout 0:4141aef83f4b 88 float PID(double e, const double Kp, const double Ki, const double Kd, double Ts, double &e_int, double &e_prior){ //PID calculator
CasperBerkhout 0:4141aef83f4b 89 e_int += Ts*e;
CasperBerkhout 0:4141aef83f4b 90 double e_diff = (e-e_prior)/Ts;
CasperBerkhout 0:4141aef83f4b 91 e_prior = e;
CasperBerkhout 0:4141aef83f4b 92 double e_diff_filter = bqlowpass.step(e_diff);
CasperBerkhout 0:4141aef83f4b 93 return(Kp*e+Ki*e_int+Kd*e_diff_filter);
CasperBerkhout 0:4141aef83f4b 94 }
CasperBerkhout 0:4141aef83f4b 95
CasperBerkhout 0:4141aef83f4b 96 void M1_control(){
CasperBerkhout 0:4141aef83f4b 97
CasperBerkhout 0:4141aef83f4b 98 //call PID func and set new motor speed
CasperBerkhout 0:4141aef83f4b 99 set_speed = PID(M1_error_pos,M1_Kp,M1_Ki,M1_Kd,Ts,M1_e_int,M1_e_prior);
CasperBerkhout 0:4141aef83f4b 100 if(set_speed > 0){
CasperBerkhout 0:4141aef83f4b 101 M1_speed.write(abs(set_speed));
CasperBerkhout 0:4141aef83f4b 102 M1_direction.write(0);
CasperBerkhout 0:4141aef83f4b 103 }
CasperBerkhout 0:4141aef83f4b 104 else if (set_speed < 0){
CasperBerkhout 0:4141aef83f4b 105 M1_speed.write(abs(set_speed));
CasperBerkhout 0:4141aef83f4b 106 M1_direction.write(1);
CasperBerkhout 0:4141aef83f4b 107 }
CasperBerkhout 0:4141aef83f4b 108 else{M1_speed.write(0);}
CasperBerkhout 4:de0923cf6bcc 109
CasperBerkhout 0:4141aef83f4b 110 }
CasperBerkhout 0:4141aef83f4b 111
CasperBerkhout 0:4141aef83f4b 112 void M2_control(){
CasperBerkhout 0:4141aef83f4b 113 set_speed = PID(M2_error_pos,M2_Kp,M2_Ki,M2_Kd,Ts,M2_e_int,M2_e_prior);
CasperBerkhout 0:4141aef83f4b 114 if(set_speed > 0){
CasperBerkhout 0:4141aef83f4b 115 M2_speed.write(abs(set_speed));
CasperBerkhout 0:4141aef83f4b 116 M2_direction.write(0);
CasperBerkhout 0:4141aef83f4b 117 }
CasperBerkhout 0:4141aef83f4b 118 else if (set_speed < 0){
CasperBerkhout 0:4141aef83f4b 119 M2_speed.write(abs(set_speed));
CasperBerkhout 0:4141aef83f4b 120 M2_direction.write(1);
CasperBerkhout 0:4141aef83f4b 121 }
CasperBerkhout 0:4141aef83f4b 122 else{M2_speed.write(0);}
CasperBerkhout 5:9e8847a0492c 123 //pc.printf("M2 speed = %f, M2 direction = %i, M2 pos error = %f, M2 setpoint = %f\n\r",set_speed,M2_direction.read(),M2_error_pos,M2_reference_pos);
CasperBerkhout 5:9e8847a0492c 124 //pc.printf("M2 integral = %f position error = %f\n\r", M2_e_int,M2_error_pos);
CasperBerkhout 0:4141aef83f4b 125 }
CasperBerkhout 0:4141aef83f4b 126
CasperBerkhout 2:c7369b41f7ae 127 void homing_system () {
CasperBerkhout 2:c7369b41f7ae 128 LimSW1.mode(PullDown);
CasperBerkhout 2:c7369b41f7ae 129 LimSW2.mode(PullDown);
CasperBerkhout 2:c7369b41f7ae 130 M1_speed.write(0);
CasperBerkhout 2:c7369b41f7ae 131 M2_speed.write(0);
CasperBerkhout 5:9e8847a0492c 132
CasperBerkhout 2:c7369b41f7ae 133
CasperBerkhout 2:c7369b41f7ae 134 while(1){
CasperBerkhout 3:455b79d42636 135 if (HomStart.read() == 0){
CasperBerkhout 6:b0b15eb27de1 136 setpoint += 0.005; //move setpoint 0.2 radian per second (at 100hz)
CasperBerkhout 4:de0923cf6bcc 137 pc.printf("Homing... \n\r");
CasperBerkhout 4:de0923cf6bcc 138 }
CasperBerkhout 4:de0923cf6bcc 139
CasperBerkhout 4:de0923cf6bcc 140 double M1_rel_pos = enc1.getPulses()/32.0/131.25*2.0*3.1416;
CasperBerkhout 4:de0923cf6bcc 141 double M2_rel_pos = enc2.getPulses()/32.0/131.25*2.0*3.1416;
CasperBerkhout 3:455b79d42636 142 if(LimSW1.read() == 0){
CasperBerkhout 5:9e8847a0492c 143 M1_error_pos = 1.2*setpoint - M1_rel_pos;
CasperBerkhout 3:455b79d42636 144 }
CasperBerkhout 3:455b79d42636 145 if(LimSW2.read() == 0){
CasperBerkhout 5:9e8847a0492c 146 M2_error_pos = - setpoint - M2_rel_pos;
CasperBerkhout 3:455b79d42636 147 }
CasperBerkhout 3:455b79d42636 148 M1_control();
CasperBerkhout 3:455b79d42636 149 M2_control();
CasperBerkhout 4:de0923cf6bcc 150
CasperBerkhout 4:de0923cf6bcc 151
CasperBerkhout 2:c7369b41f7ae 152 if(LimSW1.read() == 1){
CasperBerkhout 2:c7369b41f7ae 153 M1_error_pos = 0;
CasperBerkhout 2:c7369b41f7ae 154 M1_speed.write(0);
CasperBerkhout 2:c7369b41f7ae 155 M1_home = enc1.getPulses()/32.0/131.25*2.0*3.1416; //in radians
CasperBerkhout 2:c7369b41f7ae 156 }
CasperBerkhout 2:c7369b41f7ae 157 if (LimSW2.read() == 1) {
CasperBerkhout 2:c7369b41f7ae 158 M2_error_pos = 0;
CasperBerkhout 2:c7369b41f7ae 159 M2_speed.write(0);
CasperBerkhout 2:c7369b41f7ae 160 M2_home = enc2.getPulses()/32.0/131.25*2.0*3.1416; //in radians
CasperBerkhout 2:c7369b41f7ae 161 }
CasperBerkhout 2:c7369b41f7ae 162
CasperBerkhout 2:c7369b41f7ae 163 if (LimSW1.read() == 1 && LimSW2.read() ==1) {
CasperBerkhout 2:c7369b41f7ae 164 pc.printf("Homing finished \n\r");
CasperBerkhout 2:c7369b41f7ae 165 M1_speed.write(0);
CasperBerkhout 2:c7369b41f7ae 166 M2_speed.write(0);
CasperBerkhout 2:c7369b41f7ae 167 wait(0.5);
CasperBerkhout 2:c7369b41f7ae 168 M1_home = enc1.getPulses()/32.0/131.25*2.0*3.1416; //in radians
CasperBerkhout 2:c7369b41f7ae 169 M2_home = enc2.getPulses()/32.0/131.25*2.0*3.1416; //in radians
CasperBerkhout 5:9e8847a0492c 170 break;
CasperBerkhout 5:9e8847a0492c 171 //while(1); //stop after homing.
CasperBerkhout 2:c7369b41f7ae 172 }
CasperBerkhout 5:9e8847a0492c 173 pc.printf("M2 error = %f M2 reference = %f\n\r", M2_error_pos,M2_reference_pos);
CasperBerkhout 4:de0923cf6bcc 174 wait(0.01);
CasperBerkhout 2:c7369b41f7ae 175 }
CasperBerkhout 0:4141aef83f4b 176
CasperBerkhout 2:c7369b41f7ae 177 }
CasperBerkhout 0:4141aef83f4b 178
CasperBerkhout 0:4141aef83f4b 179 void scopesend(){
CasperBerkhout 0:4141aef83f4b 180
CasperBerkhout 0:4141aef83f4b 181
CasperBerkhout 0:4141aef83f4b 182
CasperBerkhout 0:4141aef83f4b 183 }
CasperBerkhout 0:4141aef83f4b 184 void StopMotors(){
CasperBerkhout 0:4141aef83f4b 185 while(1){
CasperBerkhout 0:4141aef83f4b 186 M1_speed.write(0);
CasperBerkhout 0:4141aef83f4b 187 M2_speed.write(0);
CasperBerkhout 0:4141aef83f4b 188 }
CasperBerkhout 0:4141aef83f4b 189 }
CasperBerkhout 0:4141aef83f4b 190
CasperBerkhout 0:4141aef83f4b 191 void geterror(){
CasperBerkhout 0:4141aef83f4b 192 double M1_rel_pos = enc1.getPulses()/32.0/131.25*2.0*3.1416; //relative position in radians
CasperBerkhout 0:4141aef83f4b 193 double M2_rel_pos = enc2.getPulses()/32.0/131.25*2.0*3.1416; //relative position in radians
CasperBerkhout 2:c7369b41f7ae 194
CasperBerkhout 2:c7369b41f7ae 195 float Arm1_home = 122.0/180.0*3.1416;//home position of large link attached to base in radians - 112 degrees
CasperBerkhout 2:c7369b41f7ae 196 float Arm2_home = 19.0/180.0*3.1416;//home position of small link attached to base
CasperBerkhout 0:4141aef83f4b 197
CasperBerkhout 2:c7369b41f7ae 198 double M1_actual_pos = Arm1_home + (M1_rel_pos - M1_home)/2; //!!!!!!!Gearing ratio of timing belt = 2!!!!!!!!!! - Also known as angle theta
CasperBerkhout 2:c7369b41f7ae 199 double M2_actual_pos = Arm2_home + (M2_rel_pos - M2_home)/2; //!!!!!!!Gearing ratio of timing belt = 2!!!!!!!!!! - Also known as angle Alpha
CasperBerkhout 0:4141aef83f4b 200
CasperBerkhout 0:4141aef83f4b 201 double q1 = M1_actual_pos;
CasperBerkhout 6:b0b15eb27de1 202 double q2 = -(q1 + M2_actual_pos); //see drawing
CasperBerkhout 6:b0b15eb27de1 203
CasperBerkhout 6:b0b15eb27de1 204 double vdx = potmeter.read();
CasperBerkhout 6:b0b15eb27de1 205 double vdy = -potmeter2.read();
CasperBerkhout 0:4141aef83f4b 206
CasperBerkhout 6:b0b15eb27de1 207 kinematica(q1,q2,vdx,vdy,q1_step,q2_step);
CasperBerkhout 6:b0b15eb27de1 208
CasperBerkhout 6:b0b15eb27de1 209 double M1_reference_pos = q1_step; //should cover the right range - radians
CasperBerkhout 6:b0b15eb27de1 210 double M2_reference_pos = -(q1_step+q2_step);
CasperBerkhout 6:b0b15eb27de1 211
CasperBerkhout 6:b0b15eb27de1 212
CasperBerkhout 6:b0b15eb27de1 213
CasperBerkhout 0:4141aef83f4b 214
CasperBerkhout 3:455b79d42636 215
CasperBerkhout 6:b0b15eb27de1 216 pc.printf("VDX = %f, q2 = %f, q2_step = %f, M2_actual = %f, M2_reference \n\r",vdx, q2,q2_step,M2_actual_pos, M2_reference_pos);
CasperBerkhout 6:b0b15eb27de1 217
CasperBerkhout 3:455b79d42636 218 if(M1_reference_pos > Arm1_home){
CasperBerkhout 3:455b79d42636 219 M1_reference_pos = Arm1_home;
CasperBerkhout 3:455b79d42636 220 }
CasperBerkhout 3:455b79d42636 221 else{
CasperBerkhout 3:455b79d42636 222 M1_error_pos = M1_reference_pos - M1_actual_pos;
CasperBerkhout 3:455b79d42636 223 }
CasperBerkhout 5:9e8847a0492c 224 if(M2_reference_pos < Arm2_home){
CasperBerkhout 3:455b79d42636 225 M2_reference_pos = Arm2_home;
CasperBerkhout 3:455b79d42636 226 }
CasperBerkhout 3:455b79d42636 227 else{
CasperBerkhout 3:455b79d42636 228 M2_error_pos = M2_reference_pos - M2_actual_pos;
CasperBerkhout 3:455b79d42636 229 }
CasperBerkhout 0:4141aef83f4b 230 }
CasperBerkhout 0:4141aef83f4b 231
CasperBerkhout 0:4141aef83f4b 232 int main() {
CasperBerkhout 5:9e8847a0492c 233 button1.fall(StopMotors);
CasperBerkhout 0:4141aef83f4b 234 //initialize serial comm and set motor PWM freq
CasperBerkhout 0:4141aef83f4b 235 M1_speed.period(1.0/pwm_freq);
CasperBerkhout 0:4141aef83f4b 236 M2_speed.period(1.0/pwm_freq);
CasperBerkhout 0:4141aef83f4b 237 pc.baud(115200);
CasperBerkhout 1:d7299175a12e 238 pc.printf("starting homing function now. Push button to start procedure \n\r");
CasperBerkhout 0:4141aef83f4b 239 //commence homing procedure
CasperBerkhout 0:4141aef83f4b 240 homing_system();
CasperBerkhout 1:d7299175a12e 241 pc.printf("Setting home position complete\n\r");
CasperBerkhout 0:4141aef83f4b 242 //attach all interrupt
CasperBerkhout 1:d7299175a12e 243 pc.printf("attaching interrupt tickers now \n\r");
CasperBerkhout 5:9e8847a0492c 244 //stop motor interrupt
CasperBerkhout 1:d7299175a12e 245 motor_update1.attach(&M1_control,0.01);
CasperBerkhout 1:d7299175a12e 246 motor_update2.attach(&M2_control,0.01);
CasperBerkhout 1:d7299175a12e 247 error_update.attach(&geterror,0.01);
CasperBerkhout 0:4141aef83f4b 248
CasperBerkhout 1:d7299175a12e 249 pc.printf("initialization complete - position control of motors now active\n\r");
CasperBerkhout 0:4141aef83f4b 250
CasperBerkhout 0:4141aef83f4b 251 while(1){
CasperBerkhout 0:4141aef83f4b 252
CasperBerkhout 0:4141aef83f4b 253
CasperBerkhout 0:4141aef83f4b 254 }
CasperBerkhout 0:4141aef83f4b 255
CasperBerkhout 0:4141aef83f4b 256 }