Moet dit er bij

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

Committer:
BasB
Date:
Mon Oct 28 16:11:15 2019 +0000
Revision:
1:a76fd17e18b3
Parent:
0:335646ab45c0
Child:
2:7ea5ae2287a7
Opzet pre-testen

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 0:335646ab45c0 17 // Encoder
BasB 0:335646ab45c0 18 DigitalIn encA1(D9);
BasB 0:335646ab45c0 19 DigitalIn encB1(D8);
BasB 0:335646ab45c0 20 DigitalIn encA2(D13);
BasB 0:335646ab45c0 21 DigitalIn encB2(D13);
BasB 0:335646ab45c0 22 QEI encoder1(D9,D8,NC,64,QEI::X4_ENCODING); //Encoding motor 1
BasB 0:335646ab45c0 23 QEI encoder2(D13,D12,NC,64,QEI::X4_ENCODING); //Encoding motor 2
BasB 0:335646ab45c0 24 float Ts = 0.01; //Sample time
BasB 0:335646ab45c0 25 float motor1angle; //Measured angle motor 1
BasB 0:335646ab45c0 26 float motor2angle; //Measured angle motor 2
BasB 0:335646ab45c0 27 float omega1; //velocity rad/s motor 1
BasB 0:335646ab45c0 28 float omega2; //Velocity rad/s motor2
BasB 0:335646ab45c0 29 float deg2rad=0.0174532; //Conversion factor degree to rad
BasB 0:335646ab45c0 30 float rad2deg=57.29578; //Conversion factor rad to degree
BasB 0:335646ab45c0 31
BasB 0:335646ab45c0 32
BasB 0:335646ab45c0 33 // Motor
BasB 0:335646ab45c0 34 DigitalOut motor2Direction(D4);
BasB 0:335646ab45c0 35 FastPWM motor2Power(D5);
BasB 0:335646ab45c0 36 DigitalOut motor1Direction(D7);
BasB 0:335646ab45c0 37 FastPWM motor1Power(D6);
BasB 0:335646ab45c0 38
BasB 0:335646ab45c0 39 volatile int motortoggle = 1; //Toggle to stop motors
BasB 0:335646ab45c0 40
BasB 0:335646ab45c0 41 //Motorcontrol
BasB 0:335646ab45c0 42 bool motordir1;
BasB 0:335646ab45c0 43 bool motordir2;
BasB 0:335646ab45c0 44 float motor1ref=0.1745;
BasB 0:335646ab45c0 45 float motor2ref=0.0873;
BasB 0:335646ab45c0 46 double controlsignal1;
BasB 0:335646ab45c0 47 double controlsignal2;
BasB 0:335646ab45c0 48 double pi2= 6.283185;
BasB 0:335646ab45c0 49 float motor1error; //motor 1 error
BasB 0:335646ab45c0 50 float motor2error;
BasB 0:335646ab45c0 51 float Kp=0.27;
BasB 0:335646ab45c0 52 float Ki=0.35;
BasB 0:335646ab45c0 53 float Kd=0.1;
BasB 0:335646ab45c0 54 float u_p1;
BasB 0:335646ab45c0 55 float u_p2;
BasB 0:335646ab45c0 56 float u_i1;
BasB 0:335646ab45c0 57 float u_i2;
BasB 0:335646ab45c0 58
BasB 0:335646ab45c0 59 //Windup control
BasB 0:335646ab45c0 60 float ux1;
BasB 0:335646ab45c0 61 float ux2;
BasB 0:335646ab45c0 62 float up1; //Proportional contribution motor 1
BasB 0:335646ab45c0 63 float up2; //Proportional contribution motor 2
BasB 0:335646ab45c0 64 float ek1;
BasB 0:335646ab45c0 65 float ek2;
BasB 0:335646ab45c0 66 float ei1= 0.0; //Error integral motor 1
BasB 0:335646ab45c0 67 float ei2=0.0; //Error integral motor 2
BasB 0:335646ab45c0 68 float Ka= 1.0; //Integral windup gain
BasB 0:335646ab45c0 69
BasB 0:335646ab45c0 70 //RKI
BasB 0:335646ab45c0 71 float Vx=0.0; //Desired linear velocity x direction
BasB 0:335646ab45c0 72 float Vy=0.0; //Desired linear velocity y direction
BasB 0:335646ab45c0 73 float q1=0.0f*deg2rad; //Angle of first joint [rad]
BasB 0:335646ab45c0 74 float q2=-135.0f*deg2rad; //Angle of second joint [rad]
BasB 0:335646ab45c0 75 float q1dot; //Velocity of first joint [rad/s]
BasB 0:335646ab45c0 76 float q2dot; //Velocity of second joint [rad/s]
BasB 0:335646ab45c0 77 float l1=26.0; //Distance base-link [cm]
BasB 0:335646ab45c0 78 float l2=62.0; //Distance link-endpoint [cm]
BasB 0:335646ab45c0 79 float xe; //Endpoint x position [cm]
BasB 0:335646ab45c0 80 float ye; //Endpoint y position [cm]
BasB 0:335646ab45c0 81
BasB 0:335646ab45c0 82 //Hidscope
BasB 0:335646ab45c0 83 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 84
BasB 0:335646ab45c0 85 // PC connection
BasB 0:335646ab45c0 86 MODSERIAL pc(USBTX, USBRX);
BasB 0:335646ab45c0 87
BasB 0:335646ab45c0 88 // Intializing tickers
BasB 0:335646ab45c0 89 Ticker motorTicker;
BasB 0:335646ab45c0 90 Ticker controlTicker;
BasB 0:335646ab45c0 91 Ticker directionTicker;
BasB 0:335646ab45c0 92 Ticker encoderTicker;
BasB 0:335646ab45c0 93 Ticker scopeTicker;
BasB 0:335646ab45c0 94
BasB 0:335646ab45c0 95 const float PWM_period = 1e-6;
BasB 0:335646ab45c0 96
BasB 0:335646ab45c0 97 volatile int counts1; // Encoder counts
BasB 0:335646ab45c0 98 volatile int counts2;
BasB 0:335646ab45c0 99 volatile int countsPrev1 = 0;
BasB 0:335646ab45c0 100 volatile int countsPrev2 = 0;
BasB 0:335646ab45c0 101 volatile int deltaCounts1;
BasB 0:335646ab45c0 102 volatile int deltaCounts2;
BasB 0:335646ab45c0 103
BasB 0:335646ab45c0 104 float factorin = 6.23185/64; // Convert encoder counts to angle in rad
BasB 0:335646ab45c0 105 float gearratio = 131.25; // Gear ratio of gearbox
BasB 0:335646ab45c0 106
BasB 0:335646ab45c0 107
BasB 1:a76fd17e18b3 108 // Vanaf hier is het misschien belangrijk
BasB 0:335646ab45c0 109
BasB 0:335646ab45c0 110 void readEncoder()
BasB 0:335646ab45c0 111 {
BasB 0:335646ab45c0 112 counts1 = encoder1.getPulses();
BasB 0:335646ab45c0 113 deltaCounts1 = counts1 - countsPrev1;
BasB 0:335646ab45c0 114 countsPrev1 = counts1;
BasB 0:335646ab45c0 115
BasB 0:335646ab45c0 116 counts2 = encoder2.getPulses();
BasB 0:335646ab45c0 117 deltaCounts2 = counts2 - countsPrev2;
BasB 0:335646ab45c0 118 countsPrev2 = counts2;
BasB 0:335646ab45c0 119 }
BasB 0:335646ab45c0 120
BasB 1:a76fd17e18b3 121 void motorCalibration1()
BasB 0:335646ab45c0 122 {
BasB 0:335646ab45c0 123 button1.fall(&togglehoek);
BasB 1:a76fd17e18b3 124 motor1angle = (counts1 * factorin / gearratio); // Angle of motor shaft in rad
BasB 0:335646ab45c0 125 omega1 = deltaCounts1 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 1:a76fd17e18b3 126 float potmeter=potmeter1.read();
BasB 1:a76fd17e18b3 127 controlsignal1=potmeter;
BasB 0:335646ab45c0 128 motor1Power.write(abs(controlsignal1*motortoggle));
BasB 1:a76fd17e18b3 129 motor1Direction=1;
BasB 1:a76fd17e18b3 130 //Dit moet je doen zolang omega motor 1 > 0.iets
BasB 1:a76fd17e18b3 131 //State switch
BasB 1:a76fd17e18b3 132 //Dan motor 2 tot omega < 0.iets
BasB 1:a76fd17e18b3 133 }
BasB 1:a76fd17e18b3 134
BasB 1:a76fd17e18b3 135 void motorCalibration2(){
BasB 1:a76fd17e18b3 136
BasB 1:a76fd17e18b3 137 float potmeter=potmeter1.read();
BasB 1:a76fd17e18b3 138 button1.fall(&togglehoek);
BasB 1:a76fd17e18b3 139 motor1angle = (counts1 * factorin / gearratio); // Angle of motor shaft in rad
BasB 1:a76fd17e18b3 140 omega1 = deltaCounts1 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 1:a76fd17e18b3 141 controlsignal1=potmeter;
BasB 1:a76fd17e18b3 142 motor1Power.write(abs(controlsignal1*motortoggle));
BasB 1:a76fd17e18b3 143 motor1Direction=1;
BasB 0:335646ab45c0 144
BasB 0:335646ab45c0 145 motor2angle = (counts2 * factorin / gearratio); // Angle of motor shaft in rad
BasB 1:a76fd17e18b3 146 omega2 = deltaCounts2 / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
BasB 1:a76fd17e18b3 147 controlsignal2=potmeter;
BasB 0:335646ab45c0 148 motor2Power.write(abs(controlsignal2*motortoggle));
BasB 1:a76fd17e18b3 149 motor2Direction=1;
BasB 1:a76fd17e18b3 150 //Dit moet je doen zolang omega van motor 2 > 0.iets
BasB 0:335646ab45c0 151 }
BasB 0:335646ab45c0 152
BasB 0:335646ab45c0 153 void Plotje(){
BasB 0:335646ab45c0 154 scope.set(0,q1*rad2deg); //gewenste hoek
BasB 0:335646ab45c0 155 scope.set(1,motor1angle/5.5f*rad2deg); //Gemeten hoek
BasB 0:335646ab45c0 156 scope.set(2,motor1error/5.5f*rad2deg); //verschil in gewenste en gemeten hoek
BasB 0:335646ab45c0 157 scope.set(3,q2*rad2deg); //gewenste hoek
BasB 0:335646ab45c0 158 scope.set(4,motor2angle/5.5f*rad2deg); //Gemeten hoek
BasB 0:335646ab45c0 159 scope.set(5,motor2error/5.5f*rad2deg); //verschil in gewenste en gemeten hoek
BasB 0:335646ab45c0 160
BasB 0:335646ab45c0 161 scope.send(); //send what's in scope memory to PC
BasB 0:335646ab45c0 162 }
BasB 0:335646ab45c0 163
BasB 0:335646ab45c0 164 void toggleMotor()
BasB 0:335646ab45c0 165 {
BasB 0:335646ab45c0 166 motortoggle = !motortoggle;
BasB 0:335646ab45c0 167 }
BasB 0:335646ab45c0 168
BasB 0:335646ab45c0 169 int main()
BasB 0:335646ab45c0 170 {
BasB 0:335646ab45c0 171 pc.baud(115200);
BasB 0:335646ab45c0 172 pc.printf("\r\nStarting...\r\n\r\n");
BasB 0:335646ab45c0 173 motor1Power.period(PWM_period);
BasB 0:335646ab45c0 174 motor2Power.period(PWM_period);
BasB 1:a76fd17e18b3 175 motorTicker.attach(motorCalibration1, 0.01); //Dit moet je doen zolang omega < 0.iets
BasB 1:a76fd17e18b3 176 motorTicker.attack(motorCalibration2, 0.01);
BasB 0:335646ab45c0 177 scopeTicker.attach(Plotje, 0.01);
BasB 0:335646ab45c0 178 encoderTicker.attach(readEncoder, Ts);
BasB 0:335646ab45c0 179
BasB 0:335646ab45c0 180 button2.fall(&toggleMotor);
BasB 0:335646ab45c0 181
BasB 0:335646ab45c0 182 while (true) {
BasB 1:a76fd17e18b3 183 pc.printf("Omega1: %f Omega 2: %f Potmeter: %f \r\n", omega1, omega2, potmeter);
BasB 0:335646ab45c0 184 wait(0.5);
BasB 0:335646ab45c0 185 }
BasB 0:335646ab45c0 186 }