Moet dit er bij

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

Committer:
BasB
Date:
Tue Oct 29 15:57:06 2019 +0000
Revision:
5:4d8b85b7cfc4
Parent:
4:1e8da6b5f147
Child:
6:e7e39d116ed0
(Voorlopig) werkende state machine calibratie met omega als switches

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BasB 0:335646ab45c0 1 #include "mbed.h"
BasB 0:335646ab45c0 2 #include "HIDScope.h"
BasB 0:335646ab45c0 3 #include "QEI.h"
BasB 0:335646ab45c0 4 #include "MODSERIAL.h"
BasB 0:335646ab45c0 5 #include "BiQuad.h"
BasB 0:335646ab45c0 6 #include "FastPWM.h"
BasB 0:335646ab45c0 7
BasB 0:335646ab45c0 8 // Button and potmeter1 control
BasB 0:335646ab45c0 9 InterruptIn button1(D11);
BasB 0:335646ab45c0 10 InterruptIn button2(D10);
BasB 0:335646ab45c0 11 InterruptIn buttonsw2(SW2);
BasB 0:335646ab45c0 12 InterruptIn buttonsw3(SW3);
BasB 0:335646ab45c0 13 AnalogIn potmeter1(A0);
BasB 0:335646ab45c0 14 AnalogIn potmeter2(A1);
BasB 0:335646ab45c0 15 AnalogIn potmeter3(A2);
BasB 0:335646ab45c0 16 AnalogIn potmeter4(A3);
BasB 2:7ea5ae2287a7 17
BasB 0:335646ab45c0 18 // Encoder
BasB 0:335646ab45c0 19 DigitalIn encA1(D9);
BasB 0:335646ab45c0 20 DigitalIn encB1(D8);
BasB 0:335646ab45c0 21 DigitalIn encA2(D13);
BasB 0:335646ab45c0 22 DigitalIn encB2(D13);
BasB 0:335646ab45c0 23 QEI encoder1(D9,D8,NC,64,QEI::X4_ENCODING); //Encoding motor 1
BasB 0:335646ab45c0 24 QEI encoder2(D13,D12,NC,64,QEI::X4_ENCODING); //Encoding motor 2
BasB 0:335646ab45c0 25 float Ts = 0.01; //Sample time
BasB 0:335646ab45c0 26 float motor1angle; //Measured angle motor 1
BasB 0:335646ab45c0 27 float motor2angle; //Measured angle motor 2
BasB 2:7ea5ae2287a7 28 float potmeter;
BasB 4:1e8da6b5f147 29 float omega1; //velocity rad/s motor 1
BasB 0:335646ab45c0 30 float omega2; //Velocity rad/s motor2
BasB 0:335646ab45c0 31 float deg2rad=0.0174532; //Conversion factor degree to rad
BasB 0:335646ab45c0 32 float rad2deg=57.29578; //Conversion factor rad to degree
BasB 0:335646ab45c0 33
BasB 0:335646ab45c0 34
BasB 0:335646ab45c0 35 // Motor
BasB 0:335646ab45c0 36 DigitalOut motor2Direction(D4);
BasB 0:335646ab45c0 37 FastPWM motor2Power(D5);
BasB 0:335646ab45c0 38 DigitalOut motor1Direction(D7);
BasB 0:335646ab45c0 39 FastPWM motor1Power(D6);
BasB 0:335646ab45c0 40
BasB 0:335646ab45c0 41 volatile int motortoggle = 1; //Toggle to stop motors
BasB 0:335646ab45c0 42
BasB 0:335646ab45c0 43 //Motorcontrol
BasB 0:335646ab45c0 44 bool motordir1;
BasB 0:335646ab45c0 45 bool motordir2;
BasB 0:335646ab45c0 46 float motor1ref=0.1745;
BasB 0:335646ab45c0 47 float motor2ref=0.0873;
BasB 0:335646ab45c0 48 double controlsignal1;
BasB 0:335646ab45c0 49 double controlsignal2;
BasB 0:335646ab45c0 50 double pi2= 6.283185;
BasB 0:335646ab45c0 51 float motor1error; //motor 1 error
BasB 0:335646ab45c0 52 float motor2error;
BasB 0:335646ab45c0 53 float Kp=0.27;
BasB 0:335646ab45c0 54 float Ki=0.35;
BasB 0:335646ab45c0 55 float Kd=0.1;
BasB 0:335646ab45c0 56 float u_p1;
BasB 0:335646ab45c0 57 float u_p2;
BasB 0:335646ab45c0 58 float u_i1;
BasB 0:335646ab45c0 59 float u_i2;
BasB 0:335646ab45c0 60
BasB 0:335646ab45c0 61 //Windup control
BasB 0:335646ab45c0 62 float ux1;
BasB 0:335646ab45c0 63 float ux2;
BasB 0:335646ab45c0 64 float up1; //Proportional contribution motor 1
BasB 0:335646ab45c0 65 float up2; //Proportional contribution motor 2
BasB 0:335646ab45c0 66 float ek1;
BasB 0:335646ab45c0 67 float ek2;
BasB 0:335646ab45c0 68 float ei1= 0.0; //Error integral motor 1
BasB 0:335646ab45c0 69 float ei2=0.0; //Error integral motor 2
BasB 0:335646ab45c0 70 float Ka= 1.0; //Integral windup gain
BasB 0:335646ab45c0 71
BasB 0:335646ab45c0 72 //RKI
BasB 0:335646ab45c0 73 float Vx=0.0; //Desired linear velocity x direction
BasB 0:335646ab45c0 74 float Vy=0.0; //Desired linear velocity y direction
BasB 0:335646ab45c0 75 float q1=0.0f*deg2rad; //Angle of first joint [rad]
BasB 0:335646ab45c0 76 float q2=-135.0f*deg2rad; //Angle of second joint [rad]
BasB 0:335646ab45c0 77 float q1dot; //Velocity of first joint [rad/s]
BasB 0:335646ab45c0 78 float q2dot; //Velocity of second joint [rad/s]
BasB 0:335646ab45c0 79 float l1=26.0; //Distance base-link [cm]
BasB 0:335646ab45c0 80 float l2=62.0; //Distance link-endpoint [cm]
BasB 0:335646ab45c0 81 float xe; //Endpoint x position [cm]
BasB 0:335646ab45c0 82 float ye; //Endpoint y position [cm]
BasB 0:335646ab45c0 83
BasB 0:335646ab45c0 84 //Hidscope
BasB 0:335646ab45c0 85 HIDScope scope(6); //Going to send x channels of data. To access data go to 'http:/localhost:18082/' after starting HIDScope application.
BasB 0:335646ab45c0 86
BasB 3:6e28b992b99e 87 //State maschine
BasB 4:1e8da6b5f147 88 enum Motor_States{motor_wait , motor_cal1_start , motor_cal1 , motor_cal2_start , motor_cal2 , motor_encoderset};
BasB 3:6e28b992b99e 89 Motor_States motor_curr_state;
BasB 3:6e28b992b99e 90 bool motor_state_changed = true;
BasB 3:6e28b992b99e 91 bool motor_cal1_done = false;
BasB 3:6e28b992b99e 92 bool motor_cal2_done = false;
BasB 3:6e28b992b99e 93
BasB 3:6e28b992b99e 94 bool button1_pressed = false;
BasB 3:6e28b992b99e 95 bool button2_pressed = false;
BasB 3:6e28b992b99e 96
BasB 0:335646ab45c0 97 // PC connection
BasB 0:335646ab45c0 98 MODSERIAL pc(USBTX, USBRX);
BasB 0:335646ab45c0 99
BasB 0:335646ab45c0 100 // Intializing tickers
BasB 0:335646ab45c0 101 Ticker motorTicker;
BasB 0:335646ab45c0 102 Ticker controlTicker;
BasB 0:335646ab45c0 103 Ticker directionTicker;
BasB 0:335646ab45c0 104 Ticker encoderTicker;
BasB 0:335646ab45c0 105 Ticker scopeTicker;
BasB 3:6e28b992b99e 106 Ticker tickGlobal; //Global ticker
BasB 0:335646ab45c0 107
BasB 0:335646ab45c0 108 const float PWM_period = 1e-6;
BasB 0:335646ab45c0 109
BasB 0:335646ab45c0 110 volatile int counts1; // Encoder counts
BasB 0:335646ab45c0 111 volatile int counts2;
BasB 0:335646ab45c0 112 volatile int countsPrev1 = 0;
BasB 0:335646ab45c0 113 volatile int countsPrev2 = 0;
BasB 0:335646ab45c0 114 volatile int deltaCounts1;
BasB 0:335646ab45c0 115 volatile int deltaCounts2;
BasB 0:335646ab45c0 116
BasB 0:335646ab45c0 117 float factorin = 6.23185/64; // Convert encoder counts to angle in rad
BasB 0:335646ab45c0 118 float gearratio = 131.25; // Gear ratio of gearbox
BasB 0:335646ab45c0 119
BasB 3:6e28b992b99e 120 void button1Press()
BasB 3:6e28b992b99e 121 {
BasB 3:6e28b992b99e 122 button1_pressed = true;
BasB 3:6e28b992b99e 123 }
BasB 0:335646ab45c0 124
BasB 0:335646ab45c0 125
BasB 3:6e28b992b99e 126 // Ticker Functions
BasB 0:335646ab45c0 127 void readEncoder()
BasB 0:335646ab45c0 128 {
BasB 0:335646ab45c0 129 counts1 = encoder1.getPulses();
BasB 0:335646ab45c0 130 deltaCounts1 = counts1 - countsPrev1;
BasB 0:335646ab45c0 131 countsPrev1 = counts1;
BasB 0:335646ab45c0 132
BasB 0:335646ab45c0 133 counts2 = encoder2.getPulses();
BasB 0:335646ab45c0 134 deltaCounts2 = counts2 - countsPrev2;
BasB 0:335646ab45c0 135 countsPrev2 = counts2;
BasB 0:335646ab45c0 136 }
BasB 0:335646ab45c0 137
BasB 4:1e8da6b5f147 138 void do_motor_cal1_start(){
BasB 4:1e8da6b5f147 139 // Entry function
BasB 4:1e8da6b5f147 140 if ( motor_state_changed == true ) {
BasB 4:1e8da6b5f147 141 motor_state_changed = false;
BasB 4:1e8da6b5f147 142 // More functions
BasB 4:1e8da6b5f147 143 }
BasB 4:1e8da6b5f147 144
BasB 4:1e8da6b5f147 145 //Do stuff
BasB 4:1e8da6b5f147 146 motor1angle = (counts1 * factorin / gearratio); // Angle of motor shaft in rad
BasB 4:1e8da6b5f147 147 omega1 = deltaCounts1 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 4:1e8da6b5f147 148 controlsignal1=0.10f;
BasB 4:1e8da6b5f147 149 motor1Power.write(abs(controlsignal1*motortoggle));
BasB 4:1e8da6b5f147 150 motor1Direction=0;
BasB 4:1e8da6b5f147 151
BasB 4:1e8da6b5f147 152 // State transition guard
BasB 4:1e8da6b5f147 153 if ( abs(omega1) >= 1.5f ){
BasB 4:1e8da6b5f147 154 motor_curr_state = motor_cal1;
BasB 4:1e8da6b5f147 155 motor_state_changed = true;
BasB 4:1e8da6b5f147 156 // More functions
BasB 4:1e8da6b5f147 157 }
BasB 4:1e8da6b5f147 158
BasB 4:1e8da6b5f147 159 }
BasB 4:1e8da6b5f147 160
BasB 3:6e28b992b99e 161 void do_motorCalibration1() {
BasB 3:6e28b992b99e 162 // Entry function
BasB 3:6e28b992b99e 163 if ( motor_state_changed == true ) {
BasB 3:6e28b992b99e 164 motor_state_changed = false;
BasB 3:6e28b992b99e 165 // More functions
BasB 3:6e28b992b99e 166 }
BasB 3:6e28b992b99e 167
BasB 3:6e28b992b99e 168 // Do stuff until end condition is met
BasB 1:a76fd17e18b3 169 motor1angle = (counts1 * factorin / gearratio); // Angle of motor shaft in rad
BasB 0:335646ab45c0 170 omega1 = deltaCounts1 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 2:7ea5ae2287a7 171 controlsignal1=0.0980f;
BasB 0:335646ab45c0 172 motor1Power.write(abs(controlsignal1*motortoggle));
BasB 2:7ea5ae2287a7 173 motor1Direction=0;
BasB 3:6e28b992b99e 174
BasB 3:6e28b992b99e 175 // State transition guard
BasB 4:1e8da6b5f147 176 if ( abs(omega1) <= 0.5f ) {
BasB 4:1e8da6b5f147 177 motor_curr_state = motor_cal2_start;
BasB 3:6e28b992b99e 178 motor_state_changed = true;
BasB 3:6e28b992b99e 179 // More functions
BasB 3:6e28b992b99e 180 }
BasB 3:6e28b992b99e 181 }
BasB 3:6e28b992b99e 182
BasB 5:4d8b85b7cfc4 183 void do_motor_cal2_start(){
BasB 5:4d8b85b7cfc4 184 // Entry function
BasB 5:4d8b85b7cfc4 185 if ( motor_state_changed == true ) {
BasB 5:4d8b85b7cfc4 186 motor_state_changed = false;
BasB 5:4d8b85b7cfc4 187 // More functions
BasB 5:4d8b85b7cfc4 188 }
BasB 5:4d8b85b7cfc4 189 //stuff
BasB 5:4d8b85b7cfc4 190 motor1angle = (counts1 * factorin / gearratio); // Angle of motor shaft in rad
BasB 5:4d8b85b7cfc4 191 omega1 = deltaCounts1 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 5:4d8b85b7cfc4 192 controlsignal1=0.10f;
BasB 5:4d8b85b7cfc4 193 motor1Power.write(abs(controlsignal1*motortoggle));
BasB 5:4d8b85b7cfc4 194 motor1Direction=0;
BasB 5:4d8b85b7cfc4 195
BasB 5:4d8b85b7cfc4 196 motor2angle = (counts2 * factorin / gearratio); // Angle of motor shaft in rad
BasB 5:4d8b85b7cfc4 197 omega2 = deltaCounts2 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 5:4d8b85b7cfc4 198 controlsignal2=0.10f;
BasB 5:4d8b85b7cfc4 199 motor2Power.write(abs(controlsignal2*motortoggle));
BasB 5:4d8b85b7cfc4 200 motor2Direction=0;
BasB 5:4d8b85b7cfc4 201
BasB 5:4d8b85b7cfc4 202 // State transition guard
BasB 5:4d8b85b7cfc4 203 if ( abs(omega2) >= 1.5f ) {
BasB 5:4d8b85b7cfc4 204 motor_curr_state = motor_cal2;
BasB 5:4d8b85b7cfc4 205 motor_state_changed = true;
BasB 5:4d8b85b7cfc4 206 // More functions
BasB 5:4d8b85b7cfc4 207 }
BasB 5:4d8b85b7cfc4 208 }
BasB 4:1e8da6b5f147 209
BasB 3:6e28b992b99e 210 void do_motorCalibration2(){
BasB 3:6e28b992b99e 211 // Entry function
BasB 3:6e28b992b99e 212 if ( motor_state_changed == true ) {
BasB 3:6e28b992b99e 213 motor_state_changed = false;
BasB 3:6e28b992b99e 214 // More functions
BasB 1:a76fd17e18b3 215 }
BasB 1:a76fd17e18b3 216
BasB 3:6e28b992b99e 217 // Do stuff until end condition is met
BasB 1:a76fd17e18b3 218 motor1angle = (counts1 * factorin / gearratio); // Angle of motor shaft in rad
BasB 1:a76fd17e18b3 219 omega1 = deltaCounts1 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 5:4d8b85b7cfc4 220 controlsignal1=0.10f;
BasB 1:a76fd17e18b3 221 motor1Power.write(abs(controlsignal1*motortoggle));
BasB 5:4d8b85b7cfc4 222 motor1Direction=0;
BasB 0:335646ab45c0 223
BasB 0:335646ab45c0 224 motor2angle = (counts2 * factorin / gearratio); // Angle of motor shaft in rad
BasB 5:4d8b85b7cfc4 225 omega2 = deltaCounts2 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 5:4d8b85b7cfc4 226 controlsignal2=0.10f;
BasB 0:335646ab45c0 227 motor2Power.write(abs(controlsignal2*motortoggle));
BasB 5:4d8b85b7cfc4 228 motor2Direction=0;
BasB 3:6e28b992b99e 229
BasB 3:6e28b992b99e 230 // State transition guard
BasB 5:4d8b85b7cfc4 231 if ( abs(omega2) <= 0.5f ) {
BasB 3:6e28b992b99e 232 motor_curr_state = motor_encoderset;
BasB 3:6e28b992b99e 233 motor_state_changed = true;
BasB 3:6e28b992b99e 234 // More functions
BasB 3:6e28b992b99e 235 }
BasB 0:335646ab45c0 236 }
BasB 0:335646ab45c0 237
BasB 3:6e28b992b99e 238 void do_motor_Encoder_Set(){
BasB 3:6e28b992b99e 239 // Entry function
BasB 3:6e28b992b99e 240 if ( motor_state_changed == true ) {
BasB 3:6e28b992b99e 241 motor_state_changed = false;
BasB 3:6e28b992b99e 242 // More functions
BasB 3:6e28b992b99e 243 }
BasB 3:6e28b992b99e 244
BasB 3:6e28b992b99e 245 // Do stuff until end condition is met
BasB 3:6e28b992b99e 246
BasB 3:6e28b992b99e 247
BasB 3:6e28b992b99e 248 // State transition guard
BasB 3:6e28b992b99e 249 if ( omega2 <= 0.5f ) {
BasB 3:6e28b992b99e 250 motor_curr_state = motor_encoderset;
BasB 3:6e28b992b99e 251 motor_state_changed = true;
BasB 3:6e28b992b99e 252 // More functions
BasB 3:6e28b992b99e 253 }
BasB 3:6e28b992b99e 254 }
BasB 3:6e28b992b99e 255
BasB 3:6e28b992b99e 256 void do_motor_wait(){
BasB 3:6e28b992b99e 257 // Entry function
BasB 3:6e28b992b99e 258 if ( motor_state_changed == true ) {
BasB 3:6e28b992b99e 259 motor_state_changed = false;
BasB 3:6e28b992b99e 260 // More functions
BasB 3:6e28b992b99e 261 }
BasB 3:6e28b992b99e 262
BasB 3:6e28b992b99e 263 // Do nothing until end condition is met
BasB 3:6e28b992b99e 264
BasB 3:6e28b992b99e 265 // State transition guard
BasB 3:6e28b992b99e 266 if ( button1_pressed ) {
BasB 3:6e28b992b99e 267 button1_pressed = false;
BasB 4:1e8da6b5f147 268 motor_curr_state = motor_cal1_start; //Beginnen met calibratie
BasB 3:6e28b992b99e 269 motor_state_changed = true;
BasB 3:6e28b992b99e 270 // More functions
BasB 3:6e28b992b99e 271 }
BasB 3:6e28b992b99e 272 }
BasB 3:6e28b992b99e 273
BasB 0:335646ab45c0 274 void toggleMotor()
BasB 0:335646ab45c0 275 {
BasB 0:335646ab45c0 276 motortoggle = !motortoggle;
BasB 0:335646ab45c0 277 }
BasB 0:335646ab45c0 278
BasB 3:6e28b992b99e 279 void motor_state_machine()
BasB 3:6e28b992b99e 280 {
BasB 3:6e28b992b99e 281 switch(motor_curr_state) {
BasB 3:6e28b992b99e 282 case motor_wait:
BasB 3:6e28b992b99e 283 do_motor_wait();
BasB 3:6e28b992b99e 284 break;
BasB 4:1e8da6b5f147 285 case motor_cal1_start:
BasB 4:1e8da6b5f147 286 do_motor_cal1_start();
BasB 4:1e8da6b5f147 287 break;
BasB 3:6e28b992b99e 288 case motor_cal1:
BasB 3:6e28b992b99e 289 do_motorCalibration1();
BasB 3:6e28b992b99e 290 break;
BasB 4:1e8da6b5f147 291 case motor_cal2_start:
BasB 4:1e8da6b5f147 292 do_motor_cal2_start();
BasB 4:1e8da6b5f147 293 break;
BasB 3:6e28b992b99e 294 case motor_cal2:
BasB 3:6e28b992b99e 295 do_motorCalibration2();
BasB 3:6e28b992b99e 296 break;
BasB 3:6e28b992b99e 297 case motor_encoderset:
BasB 3:6e28b992b99e 298 do_motor_Encoder_Set();
BasB 3:6e28b992b99e 299 break;
BasB 3:6e28b992b99e 300 }
BasB 3:6e28b992b99e 301 }
BasB 3:6e28b992b99e 302
BasB 3:6e28b992b99e 303 // Global loop of program
BasB 3:6e28b992b99e 304 void tickGlobalFunc()
BasB 3:6e28b992b99e 305 {
BasB 3:6e28b992b99e 306 //sampleSignal();
BasB 3:6e28b992b99e 307 //emg_state_machine();
BasB 3:6e28b992b99e 308 motor_state_machine();
BasB 4:1e8da6b5f147 309 readEncoder();
BasB 3:6e28b992b99e 310 // controller();
BasB 3:6e28b992b99e 311 // outputToMotors();
BasB 3:6e28b992b99e 312 }
BasB 3:6e28b992b99e 313
BasB 3:6e28b992b99e 314
BasB 0:335646ab45c0 315 int main()
BasB 0:335646ab45c0 316 {
BasB 0:335646ab45c0 317 pc.baud(115200);
BasB 0:335646ab45c0 318 pc.printf("\r\nStarting...\r\n\r\n");
BasB 0:335646ab45c0 319 motor1Power.period(PWM_period);
BasB 0:335646ab45c0 320 motor2Power.period(PWM_period);
BasB 2:7ea5ae2287a7 321
BasB 3:6e28b992b99e 322 motor_curr_state = motor_wait; // Start off in EMG Wait state
BasB 3:6e28b992b99e 323 tickGlobal.attach( &tickGlobalFunc, Ts );
BasB 0:335646ab45c0 324
BasB 4:1e8da6b5f147 325 button1.fall(&button1Press);
BasB 0:335646ab45c0 326
BasB 0:335646ab45c0 327 while (true) {
BasB 2:7ea5ae2287a7 328 pc.printf("Omega1: %f Omega 2: %f controlsignal1: %f \r\n", omega1, omega2, controlsignal1);
BasB 4:1e8da6b5f147 329 pc.printf("Currentstate: %i motor_cal1: %i\r\n",motor_curr_state, motor_cal1);
BasB 0:335646ab45c0 330 wait(0.5);
BasB 0:335646ab45c0 331 }
BasB 0:335646ab45c0 332 }