Opgeschoonde code voor verslag

Dependencies:   Encoder HIDScope MODSERIAL mbed

Fork of TotalCodegr13V2 by Rianne Bulthuis

Committer:
riannebulthuis
Date:
Thu Oct 22 15:07:20 2015 +0000
Revision:
8:b219ca30967f
Parent:
7:22126f285d69
Child:
9:888ed3c72795
Cases homepos picontrol EMG 2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arunr 0:65ab9f79a4cc 1 #include "mbed.h"
arunr 0:65ab9f79a4cc 2 #include "encoder.h"
arunr 0:65ab9f79a4cc 3 #include "HIDScope.h"
riannebulthuis 1:7d5e6bc2b314 4 #include "MODSERIAL.h"
arunr 0:65ab9f79a4cc 5
riannebulthuis 8:b219ca30967f 6
riannebulthuis 8:b219ca30967f 7 // pins
arunr 0:65ab9f79a4cc 8 DigitalOut motor1_direction(D4);
arunr 0:65ab9f79a4cc 9 PwmOut motor1_speed(D5);
arunr 0:65ab9f79a4cc 10 DigitalIn button_1(PTC6); //counterclockwise
arunr 0:65ab9f79a4cc 11 DigitalIn button_2(PTA4); //clockwise
riannebulthuis 8:b219ca30967f 12 AnalogIn PotMeter1(A4);
arunr 5:b9d5d7311dac 13 AnalogIn EMG(A0);
arunr 5:b9d5d7311dac 14 Ticker controller;
arunr 5:b9d5d7311dac 15 Ticker ticker_regelaar;
arunr 6:1597888c9a56 16 Ticker EMG_Filter;
arunr 6:1597888c9a56 17 Ticker Scope;
arunr 0:65ab9f79a4cc 18 Encoder motor1(D12,D13);
arunr 5:b9d5d7311dac 19 HIDScope scope(3);
riannebulthuis 8:b219ca30967f 20 MODSERIAL pc(USBTX, USBRX);
arunr 0:65ab9f79a4cc 21
riannebulthuis 8:b219ca30967f 22
riannebulthuis 8:b219ca30967f 23
riannebulthuis 8:b219ca30967f 24 // Regelaar homeposition
riannebulthuis 8:b219ca30967f 25 #define SAMPLETIME_REGELAAR 0.005
riannebulthuis 2:866a8a9f2b93 26 volatile bool regelaar_ticker_flag;
riannebulthuis 8:b219ca30967f 27 void setregelaar_ticker_flag(){
riannebulthuis 2:866a8a9f2b93 28 regelaar_ticker_flag = true;
riannebulthuis 2:866a8a9f2b93 29 }
riannebulthuis 2:866a8a9f2b93 30
riannebulthuis 2:866a8a9f2b93 31 //define states
riannebulthuis 8:b219ca30967f 32 enum state {HOME, MOVE_MOTOR_1, BACKTOHOMEPOSITION, STOP};
arunr 4:b4530fb376dd 33 uint8_t state = HOME;
arunr 0:65ab9f79a4cc 34
riannebulthuis 8:b219ca30967f 35 // Berekening van de output shaft resolution. Het aantal counts per 1 graden verandering (PI-controller)
riannebulthuis 3:5f59cbe53d7d 36 const double g = 360; // Aantal graden 1 rotatie
riannebulthuis 3:5f59cbe53d7d 37 const double c = 4200; // Aantal counts 1 rotatie
riannebulthuis 3:5f59cbe53d7d 38 const double q = c/(g);
riannebulthuis 3:5f59cbe53d7d 39
riannebulthuis 3:5f59cbe53d7d 40 //PI-controller constante
riannebulthuis 3:5f59cbe53d7d 41 const double motor1_Kp = 2.0; // Dit is de proportionele gain motor 1
riannebulthuis 3:5f59cbe53d7d 42 const double motor1_Ki = 0.002; // Integrating gain m1.
riannebulthuis 3:5f59cbe53d7d 43 const double motor1_Ts = 0.01; // Time step m1
riannebulthuis 3:5f59cbe53d7d 44 double err_int_m1 = 0 ; // De integrating error op het beginstijdstip m1
riannebulthuis 3:5f59cbe53d7d 45
riannebulthuis 3:5f59cbe53d7d 46 // Reusable P controller
riannebulthuis 8:b219ca30967f 47 double Pc (double error, const double Kp){
riannebulthuis 3:5f59cbe53d7d 48 return motor1_Kp * error;
riannebulthuis 3:5f59cbe53d7d 49 }
riannebulthuis 3:5f59cbe53d7d 50
riannebulthuis 3:5f59cbe53d7d 51 // Measure the error and apply output to the plant
riannebulthuis 8:b219ca30967f 52 void motor1_controlP(){
riannebulthuis 3:5f59cbe53d7d 53 double referenceP1 = PotMeter1.read();
riannebulthuis 3:5f59cbe53d7d 54 double positionP1 = q*motor1.getPosition();
riannebulthuis 3:5f59cbe53d7d 55 double motorP1 = Pc(referenceP1 - positionP1, motor1_Kp);
riannebulthuis 3:5f59cbe53d7d 56 }
riannebulthuis 3:5f59cbe53d7d 57
riannebulthuis 3:5f59cbe53d7d 58 // Reusable PI controller
riannebulthuis 8:b219ca30967f 59 double PI (double error, const double Kp, const double Ki, const double Ts, double &err_int){
riannebulthuis 3:5f59cbe53d7d 60 err_int = err_int * Ts*error; // Dit is de fout die er door de integrator uit wordt gehaald. Deze wordt elke meting aangepast door het &-teken
riannebulthuis 3:5f59cbe53d7d 61 return motor1_Kp*error + motor1_Ki*err_int;
riannebulthuis 3:5f59cbe53d7d 62 } // De totale fout die wordt hersteld met behulp van PI control.
riannebulthuis 3:5f59cbe53d7d 63
arunr 5:b9d5d7311dac 64
arunr 5:b9d5d7311dac 65 // Filter1 = High pass filter tot 20 Hz
arunr 5:b9d5d7311dac 66 double fh1_v1=0, fh1_v2=0, fh2_v1=0, fh2_v2=0;
arunr 5:b9d5d7311dac 67 const double fh1_a1=-0.84909054461, fh1_a2=0.00000000000, fh1_b0= 1, fh1_b1=-1, fh1_b2=0;
arunr 5:b9d5d7311dac 68 const double fh2_a1=-1.82553264091, fh2_a2=0.85001416809, fh2_b0= 1, fh2_b1=-2, fh2_b2=1;
riannebulthuis 8:b219ca30967f 69
arunr 5:b9d5d7311dac 70 // Filter2 = Low pass filter na 60 Hz
arunr 5:b9d5d7311dac 71 double fl1_v1=0, fl1_v2=0, fl2_v1=0, fl2_v2=0;
arunr 5:b9d5d7311dac 72 const double fl1_a1=-0.66979455390, fl1_a2=0.00000000000, fl1_b0= 1, fl1_b1=1, fl1_b2=0;
arunr 5:b9d5d7311dac 73 const double fl2_a1=-1.55376616139, fl2_a2=0.68023470431, fl2_b0= 1, fl2_b1=2, fl2_b2=1;
riannebulthuis 8:b219ca30967f 74
arunr 5:b9d5d7311dac 75 // Filter3 = Notch filter at 50 Hz
arunr 5:b9d5d7311dac 76 double fno1_v1=0, fno1_v2=0, fno2_v1=0, fno2_v2=0, fno3_v1=0, fno3_v2=0;
arunr 5:b9d5d7311dac 77 const double fno1_a1 = -1.87934916386, fno1_a2= 0.97731851355, fno1_b0= 1, fno1_b1= -1.90090686046, fno1_b2= 1;
arunr 5:b9d5d7311dac 78 const double fno2_a1 = -1.88341028603, fno2_a2= 0.98825147717, fno2_b0= 1, fno2_b1= -1.90090686046, fno2_b2= 1;
arunr 5:b9d5d7311dac 79 const double fno3_a1 = -1.89635403726, fno3_a2= 0.98894004849, fno3_b0= 1, fno3_b1= -1.90090686046, fno3_b2= 1;
arunr 5:b9d5d7311dac 80
arunr 5:b9d5d7311dac 81 // Filter4 = Lowpass filter at 5 Hz
arunr 5:b9d5d7311dac 82 double flp1_v1=0, flp1_v2=0, flp2_v1=0, flp2_v2=0;
arunr 5:b9d5d7311dac 83 const double flp1_a1=-0.97922725527, flp1_a2=0.00000000000, flp1_b0= 1, flp1_b1=1, flp1_b2=0;
arunr 5:b9d5d7311dac 84 const double flp2_a1=-1.97879353121, flp2_a2=0.97922951943, flp2_b0= 1, flp2_b1=2, flp2_b2=1;
arunr 5:b9d5d7311dac 85
arunr 5:b9d5d7311dac 86 double y1, y2, y3, y4, y5, y6, y7, y8, y9, u1, u2, u3, u4, u5, u6, u7, u8, u9;
arunr 5:b9d5d7311dac 87 double final_filter1;
arunr 5:b9d5d7311dac 88
arunr 5:b9d5d7311dac 89 // Standaard formule voor het biquad filter
riannebulthuis 8:b219ca30967f 90 double biquad(double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2){
arunr 5:b9d5d7311dac 91 double v = u - a1*v1 - a2*v2;
arunr 5:b9d5d7311dac 92 double y = b0*v + b1*v1 + b2*v2;
arunr 5:b9d5d7311dac 93 v2=v1;
arunr 5:b9d5d7311dac 94 v1=v;
arunr 5:b9d5d7311dac 95 return y;
arunr 5:b9d5d7311dac 96 }
arunr 5:b9d5d7311dac 97
riannebulthuis 8:b219ca30967f 98 // script voor filters
riannebulthuis 8:b219ca30967f 99 void Filters(){
arunr 5:b9d5d7311dac 100 u1 = EMG.read();
arunr 5:b9d5d7311dac 101 //Highpass
arunr 5:b9d5d7311dac 102 y1 = biquad (u1, fh1_v1, fh1_v2, fh1_a1, fh1_a2, fh1_b0*0.924547, fh1_b1*0.924547, fh1_b2*0.924547);
arunr 5:b9d5d7311dac 103 u2 = y1;
arunr 5:b9d5d7311dac 104 y2 = biquad (u2, fh2_v1, fh2_v2, fh2_a1, fh2_a2, fh2_b0*0.918885, fh2_b1*0.918885, fh2_b2*0.918885);
arunr 5:b9d5d7311dac 105 //Lowpass
arunr 5:b9d5d7311dac 106 u3 = y2;
arunr 5:b9d5d7311dac 107 y3 = biquad (u3, fl1_v1, fl1_v2, fl1_a1, fl1_a2, fl1_b0*0.165103, fl1_b1*0.165103, fl1_b2*0.165103);
arunr 5:b9d5d7311dac 108 u4 = y3;
arunr 5:b9d5d7311dac 109 y4 = biquad (u4, fl2_v1, fl2_v2, fl2_a1, fl2_a2, fl2_b0*0.031617, fl2_b1*0.031617, fl2_b2*0.031617);
arunr 5:b9d5d7311dac 110 // Notch
arunr 5:b9d5d7311dac 111 u5 = y4;
arunr 5:b9d5d7311dac 112 y5 = biquad (u5, fno1_v1, fno1_v2, fno1_a1, fno1_a2, fno1_b0*1.004206, fno1_b1*1.004206, fno1_b2*1.004206);
arunr 5:b9d5d7311dac 113 u6 = y5;
arunr 5:b9d5d7311dac 114 y6 = biquad (u6, fno2_v1, fno2_v2, fno2_a1, fno2_a2, fno2_b0, fno2_b1, fno2_b2);
arunr 5:b9d5d7311dac 115 u7 = y6;
arunr 5:b9d5d7311dac 116 y7 = biquad (u7, fno3_v1, fno3_v2, fno3_a1, fno3_a2, fno3_b0*0.973227, fno3_b1*0.973227, fno3_b2*0.973227);
arunr 5:b9d5d7311dac 117 y8 = fabs (y7);
arunr 5:b9d5d7311dac 118 //smooth
arunr 5:b9d5d7311dac 119 u8 = y8;
arunr 5:b9d5d7311dac 120 y9 = biquad (u8, flp1_v1, flp1_v2, flp1_a1, flp1_a2, flp1_b0* 0.010386, flp1_b1* 0.010386, flp1_b2* 0.010386);
arunr 5:b9d5d7311dac 121 u9 = y9;
arunr 5:b9d5d7311dac 122 final_filter1 = biquad(u9, flp2_v1, flp2_v2, flp2_a1, flp2_a2, flp2_b0*0.000109, flp2_b1*0.000109, flp2_b2*0.000109);
arunr 6:1597888c9a56 123 }
arunr 5:b9d5d7311dac 124
arunr 5:b9d5d7311dac 125
riannebulthuis 8:b219ca30967f 126 void motor1_controlPI(){
riannebulthuis 3:5f59cbe53d7d 127 double referencePI1 = PotMeter1.read();
riannebulthuis 3:5f59cbe53d7d 128 double positionPI1 = q*motor1.getPosition();
riannebulthuis 3:5f59cbe53d7d 129 double motorPI1 = PI(referencePI1 - positionPI1, motor1_Kp, motor1_Ki, motor1_Ts, err_int_m1);
riannebulthuis 3:5f59cbe53d7d 130 }
riannebulthuis 3:5f59cbe53d7d 131
arunr 0:65ab9f79a4cc 132 const int pressed = 0;
arunr 0:65ab9f79a4cc 133
riannebulthuis 8:b219ca30967f 134 // constantes voor berekening homepositie
arunr 0:65ab9f79a4cc 135 double H;
arunr 0:65ab9f79a4cc 136 double P;
arunr 0:65ab9f79a4cc 137 double D;
arunr 0:65ab9f79a4cc 138
arunr 0:65ab9f79a4cc 139
riannebulthuis 8:b219ca30967f 140 void move_motor1()
riannebulthuis 8:b219ca30967f 141 {
riannebulthuis 8:b219ca30967f 142 if (final_filter1 > 0.03){
riannebulthuis 8:b219ca30967f 143 pc.printf("Moving clockwise \n\r");
riannebulthuis 8:b219ca30967f 144 motor1_direction = 1; //clockwise
riannebulthuis 8:b219ca30967f 145 motor1_speed = 0.4;
riannebulthuis 8:b219ca30967f 146 }
riannebulthuis 8:b219ca30967f 147 else if (button_2 == pressed){
riannebulthuis 8:b219ca30967f 148 pc.printf("Moving counterclockwise \n\r");
riannebulthuis 8:b219ca30967f 149 motor1_direction = 0; //counterclockwise
riannebulthuis 8:b219ca30967f 150 motor1_speed = 0.4;
riannebulthuis 8:b219ca30967f 151 }
riannebulthuis 8:b219ca30967f 152 else {
riannebulthuis 8:b219ca30967f 153 pc.printf("Not moving \n\r");
riannebulthuis 8:b219ca30967f 154 motor1_speed = 0;
riannebulthuis 8:b219ca30967f 155 }
arunr 0:65ab9f79a4cc 156 }
arunr 0:65ab9f79a4cc 157
arunr 0:65ab9f79a4cc 158 void movetohome(){
arunr 0:65ab9f79a4cc 159 P = motor1.getPosition();
arunr 4:b4530fb376dd 160
arunr 4:b4530fb376dd 161 if (P >= -28 && P <= 28){
arunr 0:65ab9f79a4cc 162 motor1_speed = 0;
arunr 0:65ab9f79a4cc 163 }
arunr 4:b4530fb376dd 164 else if (P > 24){
riannebulthuis 3:5f59cbe53d7d 165 motor1_direction = 1;
riannebulthuis 3:5f59cbe53d7d 166 motor1_speed = 0.1;
arunr 0:65ab9f79a4cc 167 }
arunr 4:b4530fb376dd 168 else if (P < -24){
riannebulthuis 3:5f59cbe53d7d 169 motor1_direction = 0;
riannebulthuis 3:5f59cbe53d7d 170 motor1_speed = 0.1;
arunr 0:65ab9f79a4cc 171 }
arunr 0:65ab9f79a4cc 172 }
arunr 0:65ab9f79a4cc 173
arunr 6:1597888c9a56 174 void HIDScope (){
riannebulthuis 8:b219ca30967f 175 scope.set (0, final_filter1);
riannebulthuis 8:b219ca30967f 176 scope.set(1, motor1.getPosition());
arunr 6:1597888c9a56 177 scope.send ();
arunr 4:b4530fb376dd 178 }
arunr 6:1597888c9a56 179
arunr 0:65ab9f79a4cc 180 int main()
riannebulthuis 2:866a8a9f2b93 181 {
arunr 0:65ab9f79a4cc 182 while (true) {
riannebulthuis 8:b219ca30967f 183 pc.baud(9600); //pc baud rate van de computer
riannebulthuis 8:b219ca30967f 184 EMG_Filter.attach_us(Filters, 1e3); //filters uitvoeren
riannebulthuis 8:b219ca30967f 185 Scope.attach_us(HIDScope, 1e3); //EMG en encoder signaal naar de hidscope sturen
riannebulthuis 1:7d5e6bc2b314 186
riannebulthuis 8:b219ca30967f 187 switch (state) { //zorgt voor het in goede volgorde uitvoeren van de cases
riannebulthuis 1:7d5e6bc2b314 188
arunr 4:b4530fb376dd 189 case HOME: //positie op 0 zetten voor arm 1
riannebulthuis 1:7d5e6bc2b314 190 {
arunr 4:b4530fb376dd 191 pc.printf("home\n\r");
riannebulthuis 8:b219ca30967f 192 H = motor1.getPosition();
arunr 4:b4530fb376dd 193 pc.printf("Home-position is %f\n\r", H);
riannebulthuis 8:b219ca30967f 194 state = MOVE_MOTOR_1;
riannebulthuis 8:b219ca30967f 195 wait(2);
riannebulthuis 1:7d5e6bc2b314 196 break;
riannebulthuis 1:7d5e6bc2b314 197 }
riannebulthuis 8:b219ca30967f 198
riannebulthuis 8:b219ca30967f 199 case MOVE_MOTOR_1: //motor kan cw en ccw bewegen a.d.h.v. buttons
riannebulthuis 1:7d5e6bc2b314 200 {
riannebulthuis 2:866a8a9f2b93 201 pc.printf("move_motor\n\r");
riannebulthuis 8:b219ca30967f 202 while (state == MOVE_MOTOR_1){
riannebulthuis 2:866a8a9f2b93 203 move_motor1();
riannebulthuis 2:866a8a9f2b93 204 if (button_1 == pressed && button_2 == pressed){
riannebulthuis 8:b219ca30967f 205 motor1_speed = 0;
riannebulthuis 2:866a8a9f2b93 206 state = BACKTOHOMEPOSITION;
riannebulthuis 2:866a8a9f2b93 207 }
riannebulthuis 2:866a8a9f2b93 208 }
riannebulthuis 2:866a8a9f2b93 209 wait (1);
riannebulthuis 2:866a8a9f2b93 210 break;
riannebulthuis 1:7d5e6bc2b314 211 }
riannebulthuis 1:7d5e6bc2b314 212
riannebulthuis 2:866a8a9f2b93 213 case BACKTOHOMEPOSITION: //motor gaat terug naar homeposition
riannebulthuis 1:7d5e6bc2b314 214 {
riannebulthuis 2:866a8a9f2b93 215 pc.printf("backhomeposition\n\r");
riannebulthuis 2:866a8a9f2b93 216 wait (1);
riannebulthuis 2:866a8a9f2b93 217
riannebulthuis 2:866a8a9f2b93 218 ticker_regelaar.attach(setregelaar_ticker_flag, SAMPLETIME_REGELAAR);
riannebulthuis 2:866a8a9f2b93 219 while(state == BACKTOHOMEPOSITION){
riannebulthuis 3:5f59cbe53d7d 220 movetohome();
riannebulthuis 2:866a8a9f2b93 221 while(regelaar_ticker_flag != true);
riannebulthuis 2:866a8a9f2b93 222 regelaar_ticker_flag = false;
riannebulthuis 2:866a8a9f2b93 223
riannebulthuis 2:866a8a9f2b93 224 pc.printf("pulsmotorposition1 %d", motor1.getPosition());
arunr 4:b4530fb376dd 225 pc.printf("referentie %f\n\r", H);
riannebulthuis 2:866a8a9f2b93 226
arunr 4:b4530fb376dd 227 if (P <=24 && P >= -24){
arunr 4:b4530fb376dd 228 pc.printf("pulsmotorposition1 %d", motor1.getPosition());
arunr 4:b4530fb376dd 229 pc.printf("referentie %f\n\r", H);
riannebulthuis 3:5f59cbe53d7d 230 state = STOP;
arunr 4:b4530fb376dd 231 pc.printf("Laatste positie %f\n\r", motor1.getPosition());
riannebulthuis 3:5f59cbe53d7d 232 break;
riannebulthuis 2:866a8a9f2b93 233 }
riannebulthuis 3:5f59cbe53d7d 234 }
riannebulthuis 3:5f59cbe53d7d 235 }
riannebulthuis 3:5f59cbe53d7d 236 case STOP:
riannebulthuis 3:5f59cbe53d7d 237 {
arunr 4:b4530fb376dd 238 static int c;
riannebulthuis 3:5f59cbe53d7d 239 while(state == STOP){
riannebulthuis 3:5f59cbe53d7d 240 motor1_speed = 0;
arunr 4:b4530fb376dd 241 if (c++ == 0){
riannebulthuis 3:5f59cbe53d7d 242 pc.printf("einde\n\r");
riannebulthuis 3:5f59cbe53d7d 243 }
riannebulthuis 3:5f59cbe53d7d 244 }
riannebulthuis 1:7d5e6bc2b314 245 break;
riannebulthuis 3:5f59cbe53d7d 246 }
riannebulthuis 3:5f59cbe53d7d 247 }
riannebulthuis 2:866a8a9f2b93 248 }
arunr 0:65ab9f79a4cc 249 }