Motor control

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

Committer:
BasB
Date:
Mon Oct 21 08:57:45 2019 +0000
Revision:
13:048458947701
Parent:
12:23b94b5dcc60
Child:
14:0afc46ad1b99
Namen van de variabelen aangepast, voor het toevoegen van motor 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
freek100 0:e4858e2df9c7 1 #include "mbed.h"
freek100 3:07fedd2e252c 2 #include "HIDScope.h"
freek100 0:e4858e2df9c7 3 #include "QEI.h"
freek100 0:e4858e2df9c7 4 #include "MODSERIAL.h"
freek100 3:07fedd2e252c 5 #include "BiQuad.h"
freek100 0:e4858e2df9c7 6 #include "FastPWM.h"
freek100 0:e4858e2df9c7 7
freek100 6:1c0b6e55e900 8 // Button and potmeter1 control
freek100 0:e4858e2df9c7 9 InterruptIn button1(D11);
freek100 0:e4858e2df9c7 10 InterruptIn button2(D10);
BasB 10:b871d1b05787 11 InterruptIn buttonsw2(SW2);
BasB 10:b871d1b05787 12 InterruptIn buttonsw3(SW3);
freek100 6:1c0b6e55e900 13 AnalogIn potmeter1(A0);
freek100 6:1c0b6e55e900 14 AnalogIn potmeter2(A1);
freek100 8:c6c94d55b088 15 AnalogIn potmeter3(A2);
BasB 9:08a7a8e59a6a 16 AnalogIn potmeter4(A3);
freek100 0:e4858e2df9c7 17 // Encoder
BasB 13:048458947701 18 DigitalIn encA1(D13);
BasB 13:048458947701 19 DigitalIn encB1(D12);
BasB 13:048458947701 20 QEI encoder1(D13,D12,NC,64,QEI::X4_ENCODING);
freek100 2:d7286c36595f 21 float Ts = 0.01;
BasB 13:048458947701 22 float motor1angle;
BasB 13:048458947701 23 float omega1;
freek100 1:08e8cc33fcae 24
freek100 0:e4858e2df9c7 25
freek100 0:e4858e2df9c7 26 // Motor
freek100 0:e4858e2df9c7 27 DigitalOut motor2Direction(D4);
freek100 0:e4858e2df9c7 28 FastPWM motor2Power(D5);
freek100 0:e4858e2df9c7 29 DigitalOut motor1Direction(D7);
freek100 0:e4858e2df9c7 30 FastPWM motor1Power(D6);
freek100 0:e4858e2df9c7 31
freek100 5:17aa878564d0 32 volatile int motor1Toggle = 1;
freek100 5:17aa878564d0 33
freek100 0:e4858e2df9c7 34 //Motorcontrol
BasB 13:048458947701 35 bool motordir1;
BasB 13:048458947701 36 float motor1ref= 0;
BasB 13:048458947701 37 double controlsignal1;
freek100 0:e4858e2df9c7 38 double potValue;
freek100 1:08e8cc33fcae 39 double pi2= 6.283185;
BasB 13:048458947701 40 float motor1error; //e = error
BasB 13:048458947701 41 float Kp=0.27;
BasB 13:048458947701 42 float Ki=0.35;
BasB 13:048458947701 43 float u_p1;
BasB 13:048458947701 44 float u_i1;
freek100 3:07fedd2e252c 45
freek100 11:94a4dd7ed05c 46 //Windup control
BasB 13:048458947701 47 float ux1;
BasB 13:048458947701 48 float up1;
BasB 13:048458947701 49 float ek1;
BasB 13:048458947701 50 float ei1= 0;
freek100 11:94a4dd7ed05c 51 float Ka= 1;
freek100 11:94a4dd7ed05c 52
freek100 4:e7d50c6a7c53 53 //Hidscope
freek100 4:e7d50c6a7c53 54 HIDScope scope(3); //Going to send 3 channels of data. To access data go to 'http:/localhost:18082/' after starting HIDScope application.
freek100 0:e4858e2df9c7 55 // PC connection
freek100 0:e4858e2df9c7 56 MODSERIAL pc(USBTX, USBRX);
freek100 0:e4858e2df9c7 57
freek100 0:e4858e2df9c7 58 // Intializing tickers
freek100 0:e4858e2df9c7 59 Ticker motorTicker;
freek100 0:e4858e2df9c7 60 Ticker controlTicker;
freek100 0:e4858e2df9c7 61 Ticker directionTicker;
freek100 1:08e8cc33fcae 62 Ticker encoderTicker;
freek100 4:e7d50c6a7c53 63 Ticker scopeTicker;
freek100 0:e4858e2df9c7 64
freek100 0:e4858e2df9c7 65 const float PWM_period = 1e-6;
freek100 0:e4858e2df9c7 66
BasB 13:048458947701 67 volatile int counts1; // Encoder counts
BasB 13:048458947701 68 volatile int countsPrev1 = 0;
BasB 13:048458947701 69 volatile int deltaCounts1;
freek100 0:e4858e2df9c7 70
freek100 0:e4858e2df9c7 71 float factorin = 6.23185/64; // Convert encoder counts to angle in rad
freek100 0:e4858e2df9c7 72 float gearratio = 131.25; // Gear ratio of gearbox
freek100 0:e4858e2df9c7 73
freek100 2:d7286c36595f 74
BasB 13:048458947701 75 float PID_controller1(float motor1error){
BasB 13:048458947701 76 static float error_integral1=0;
freek100 11:94a4dd7ed05c 77 //static float e_prev=e;
freek100 2:d7286c36595f 78
freek100 2:d7286c36595f 79 //Proportional part:
BasB 13:048458947701 80 u_p1=Kp*motor1error;
freek100 2:d7286c36595f 81
freek100 2:d7286c36595f 82 //Integral part
BasB 13:048458947701 83 error_integral1=error_integral1+ei1*Ts;
BasB 13:048458947701 84 u_i1=Ki*error_integral1;
freek100 2:d7286c36595f 85
freek100 11:94a4dd7ed05c 86 // Sum and limit
BasB 13:048458947701 87 up1= u_p1+u_i1;
BasB 13:048458947701 88 if (up1>1){
BasB 13:048458947701 89 controlsignal1=1;}
BasB 13:048458947701 90 else if (up1<-1){
BasB 13:048458947701 91 controlsignal1=-1;}
freek100 11:94a4dd7ed05c 92 else {
BasB 13:048458947701 93 controlsignal1=up1;}
freek100 11:94a4dd7ed05c 94
freek100 11:94a4dd7ed05c 95 // To prevent windup
BasB 13:048458947701 96 ux1= up1-controlsignal1;
BasB 13:048458947701 97 ek1= Ka*ux1;
BasB 13:048458947701 98 ei1= motor1error-ek1;
freek100 11:94a4dd7ed05c 99 //Return
BasB 13:048458947701 100 return controlsignal1;
freek100 2:d7286c36595f 101 }
freek100 2:d7286c36595f 102
freek100 4:e7d50c6a7c53 103
freek100 4:e7d50c6a7c53 104 void readEncoder()
freek100 4:e7d50c6a7c53 105 {
BasB 13:048458947701 106 counts1 = encoder1.getPulses();
BasB 13:048458947701 107 deltaCounts1 = counts1 - countsPrev1;
freek100 4:e7d50c6a7c53 108
BasB 13:048458947701 109 countsPrev1 = counts1;
freek100 4:e7d50c6a7c53 110 }
BasB 9:08a7a8e59a6a 111
BasB 9:08a7a8e59a6a 112 void togglehoek(){
freek100 11:94a4dd7ed05c 113
BasB 13:048458947701 114 motor1ref= 0.5*pi2+motor1ref;
freek100 11:94a4dd7ed05c 115 // static float t = 0;
freek100 11:94a4dd7ed05c 116 // refangle= pi2/3.0f*sin(5.0f*t)*motor1Toggle;
freek100 11:94a4dd7ed05c 117 //t+=0.01;
BasB 9:08a7a8e59a6a 118 }
BasB 9:08a7a8e59a6a 119
freek100 0:e4858e2df9c7 120 void motorControl()
freek100 0:e4858e2df9c7 121 {
freek100 11:94a4dd7ed05c 122 //togglehoek();
freek100 11:94a4dd7ed05c 123 button1.fall(&togglehoek);
BasB 13:048458947701 124 motor1angle = counts1 * factorin / gearratio; // Angle of motor shaft in rad
BasB 13:048458947701 125 omega1 = deltaCounts1 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
freek100 6:1c0b6e55e900 126 potValue= potmeter1.read();
freek100 11:94a4dd7ed05c 127 //refangle= (potValue*2*pi2)-pi2;
BasB 13:048458947701 128 motor1error=motor1ref-motor1angle;
freek100 2:d7286c36595f 129
BasB 13:048458947701 130 controlsignal1=PID_controller1(motor1error);
BasB 13:048458947701 131
BasB 13:048458947701 132 if (controlsignal1<0){
BasB 13:048458947701 133 motordir1= 0;}
BasB 10:b871d1b05787 134 else {
BasB 13:048458947701 135 motordir1= 1;}
BasB 13:048458947701 136 motor1Power.write(abs(controlsignal1));
BasB 13:048458947701 137 motor1Direction= motordir1;
freek100 0:e4858e2df9c7 138 }
freek100 0:e4858e2df9c7 139
BasB 10:b871d1b05787 140 void Plotje(){
BasB 13:048458947701 141 scope.set(0,motor1ref); //gewenste hoek
BasB 13:048458947701 142 scope.set(1,motor1angle); //Gemeten hoek
BasB 13:048458947701 143 scope.set(2,motor1error); //verschil in gewenste en gemeten hoek
freek100 1:08e8cc33fcae 144
freek100 4:e7d50c6a7c53 145 scope.send(); //send what's in scope memory to PC
freek100 1:08e8cc33fcae 146 }
freek100 0:e4858e2df9c7 147
freek100 5:17aa878564d0 148 void toggleMotor()
freek100 5:17aa878564d0 149 {
freek100 5:17aa878564d0 150 motor1Toggle = !motor1Toggle;
freek100 5:17aa878564d0 151 }
freek100 4:e7d50c6a7c53 152
freek100 0:e4858e2df9c7 153 int main()
freek100 0:e4858e2df9c7 154 {
freek100 0:e4858e2df9c7 155 pc.baud(115200);
freek100 0:e4858e2df9c7 156 pc.printf("\r\nStarting...\r\n\r\n");
freek100 0:e4858e2df9c7 157
freek100 0:e4858e2df9c7 158 motor1Power.period(PWM_period);
freek100 0:e4858e2df9c7 159 motorTicker.attach(motorControl, 0.01);
freek100 4:e7d50c6a7c53 160 scopeTicker.attach(Plotje, 0.01);
freek100 2:d7286c36595f 161 encoderTicker.attach(readEncoder, Ts);
freek100 5:17aa878564d0 162
freek100 5:17aa878564d0 163 button2.fall(&toggleMotor);
BasB 9:08a7a8e59a6a 164
freek100 0:e4858e2df9c7 165 while (true) {
freek100 0:e4858e2df9c7 166
freek100 7:20a802dfe664 167 //pc.printf("Potmeter: %d \r\n", potValue,);
freek100 7:20a802dfe664 168 //pc.printf("Counts: %i DeltaCounts: %i\r\n", counts, deltaCounts);
BasB 13:048458947701 169 pc.printf("Angle: %f Omega: %f\r\n", motor1angle, omega1);
BasB 13:048458947701 170 pc.printf("refangle: %f Error: %f \r\n",motor1ref, motor1error);
BasB 10:b871d1b05787 171 pc.printf("Kp: %f Ki: %f \r\n", Kp, Ki);
freek100 6:1c0b6e55e900 172
freek100 0:e4858e2df9c7 173 wait(0.5);
freek100 0:e4858e2df9c7 174 }
freek100 0:e4858e2df9c7 175 }