EMG check. met knopjes en toetsenboard emg signalen simuleren om de code te testen. groepje 12

Dependencies:   MODSERIAL QEI mbed

Fork of EMG_check by sibren vuurberg

Committer:
sivuu
Date:
Fri Oct 28 14:06:13 2016 +0000
Revision:
27:be2be9600e2d
Parent:
26:a6b8bad5b0e2
Child:
28:edfab1cb48df
samengevoegde if loops maar een else loop werkt niet;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sivuu 12:7903c0e55cd7 1 #include "mbed.h" //standaard bieb mbed
sivuu 12:7903c0e55cd7 2 #include "QEI.h" //bieb voor encoderfuncties in c++
sivuu 12:7903c0e55cd7 3 #include "MODSERIAL.h" //bieb voor modserial
sivuu 2:4dcdf7755a04 4 InterruptIn sw3(SW3);
sivuu 21:1f0fae4687af 5 DigitalIn sw2(SW2);
sivuu 17:f44f41cab151 6 DigitalIn encoder1A(D13);
sivuu 12:7903c0e55cd7 7 DigitalIn encoder1B(D12);
sivuu 17:f44f41cab151 8 DigitalIn encoder2A(D11);
sivuu 17:f44f41cab151 9 DigitalIn encoder2B(D10);
sivuu 17:f44f41cab151 10 DigitalIn button_cw(D9);
sivuu 17:f44f41cab151 11 DigitalIn button_ccw(PTC12);
sivuu 12:7903c0e55cd7 12 MODSERIAL pc(USBTX, USBRX);
sivuu 12:7903c0e55cd7 13 DigitalOut richting_motor1(D4);
sivuu 4:2b3fbd7ef1cf 14 PwmOut pwm_motor1(D5);
sivuu 12:7903c0e55cd7 15 DigitalOut richting_motor2(D7);
sivuu 4:2b3fbd7ef1cf 16 PwmOut pwm_motor2(D6);
sivuu 14:4b934ac37656 17
sivuu 14:4b934ac37656 18
sivuu 14:4b934ac37656 19
sivuu 12:7903c0e55cd7 20 int n = 0; //start van de teller wordt op nul gesteld
sivuu 2:4dcdf7755a04 21
sivuu 3:34f7c16a6a7f 22 void SwitchN() { // maakt simpele functie die 1 bij n optelt
sivuu 2:4dcdf7755a04 23 n++;
sivuu 2:4dcdf7755a04 24 }
sivuu 23:010d8edccc5b 25
sivuu 14:4b934ac37656 26 //constanten voor de encoder
sivuu 14:4b934ac37656 27 const int CW = 2.5; //definitie rechtsom 'lage waarde'
sivuu 17:f44f41cab151 28 const int CCW =-1; //definitie linksom 'hoge waarde'
sivuu 17:f44f41cab151 29 const float gearboxratio=131.25; // gearboxratio van encoder naar motor
sivuu 24:2b47887bfd78 30 const float rev_rond=64.0; // aantal revoluties per omgang van de encoder
sivuu 17:f44f41cab151 31
sivuu 17:f44f41cab151 32 volatile double current_position_motor1 = 0; // current position is 0 op het begin
sivuu 17:f44f41cab151 33 volatile double previous_position_motor1 = 0; // previous position is 0 op het begin
sivuu 17:f44f41cab151 34 volatile double current_position_motor2 = 0;
sivuu 17:f44f41cab151 35 volatile double previous_position_motor2 = 0;
sivuu 17:f44f41cab151 36 volatile bool tickerflag; //bool zorgt er voor dat de tickerflag alleen 1 of 0 kan zijn (true or false)
sivuu 21:1f0fae4687af 37 volatile double snelheid_motor1; // snelheid van motor1 wordt later berekend door waardes uit de encoder is in rad/s
sivuu 21:1f0fae4687af 38 volatile double snelheid_motor2; // snelheid van motor2 wordt later berekend door waardes uit de encoder is in rad/s
sivuu 21:1f0fae4687af 39 double ticker_TS=0.1; // zorgt voor een tijdstap van de ticker van 0.1 seconde
sivuu 17:f44f41cab151 40 volatile double timepassed=0; //de waarde van hoeveel tijd er verstreken is
sivuu 17:f44f41cab151 41 Ticker t; // maakt ticker t aan
sivuu 22:7c4902b80c16 42 volatile double value1_resetbutton = 0; // deze value wordt gebruikt zodat als er bij de reset button na het bereiken van de waarde nul. De motor stopt met draaien.
sivuu 21:1f0fae4687af 43 volatile double value2_resetbutton = 0;
sivuu 17:f44f41cab151 44
sivuu 17:f44f41cab151 45 void tickerfunctie() // maakt een ticker functie aan
sivuu 17:f44f41cab151 46 {
sivuu 17:f44f41cab151 47 tickerflag = 1; // het enige wat deze functie doet is zorgen dat tickerflag 1 is
sivuu 17:f44f41cab151 48 }
sivuu 2:4dcdf7755a04 49
sivuu 0:b7cb5d3978b5 50 int main()
sivuu 0:b7cb5d3978b5 51 {
sivuu 12:7903c0e55cd7 52 pc.baud(115200); // zorgt voor de link voor putty, 115200 is snelheid
sivuu 24:2b47887bfd78 53 QEI Encoder2(D12,D13, NC, rev_rond,QEI::X4_ENCODING); // maakt een encoder aan! D12/D13 ingangen, rev_rond zijn aantal pulsen per revolutie! Bovenaan in te stellen.
sivuu 24:2b47887bfd78 54 QEI Encoder1(D10,D11, NC, rev_rond,QEI::X4_ENCODING);
sivuu 14:4b934ac37656 55 float counts_encoder1; //variabele counts aanmaken
sivuu 22:7c4902b80c16 56 float rev_counts_motor1; //variabele motor rondjes aanmaken in radialen!!
sivuu 14:4b934ac37656 57 float counts_encoder2;
sivuu 14:4b934ac37656 58 float rev_counts_motor2;
sivuu 22:7c4902b80c16 59 t.attach(&tickerfunctie,ticker_TS); // attacht de ticker met een tick van 0.1 seconde (gelijk aan de tijdstap)
sivuu 22:7c4902b80c16 60 volatile float voltage_motor1=0.27; //pwm is de pulse with tussen geen ampere en wel ampere motor 1
sivuu 27:be2be9600e2d 61 volatile float voltage_motor2=1;//pwm is de pulse with tussen geen ampere en wel ampere motor 1
sivuu 12:7903c0e55cd7 62
sivuu 3:34f7c16a6a7f 63 while (true) { // zorgt er voor dat de code oneindig doorgelopen wordt
sivuu 2:4dcdf7755a04 64
sivuu 12:7903c0e55cd7 65 sw3.fall(&SwitchN); // zorgt er voor dat void switch wordt gedaan als switch 3 wordt ingedrukt
sivuu 12:7903c0e55cd7 66
sivuu 22:7c4902b80c16 67 while(sw2==0 && rev_counts_motor1<0 && value1_resetbutton >= 0){ // zorgt er voor dat de loop wordt doorlopen als switch 2 wordt ingedrukt, aantal rondejs van de motor kleiner is dan 0 en de value resetbutton groter of gelijk is aan 0
sivuu 21:1f0fae4687af 68 pc.printf("switch 2 is ingedrukt \r\n");
sivuu 24:2b47887bfd78 69 pc.printf("aantal rondjes motor 1 is kleiner dan 0\r\n");
sivuu 22:7c4902b80c16 70 richting_motor1 = 1; // zorgt er voor dat de motor tegenovergestelde richting gaat draaien zodat het totaal aantal rondjes gedraaid op 0 komt te staan
sivuu 22:7c4902b80c16 71 pwm_motor1 = voltage_motor1; // zorgt er voor dat er een ampere is voor de motor om te draaien
sivuu 21:1f0fae4687af 72 pc.printf("richting motor is %f \r\n", richting_motor1);
sivuu 21:1f0fae4687af 73 pc.printf("voltage motor 1 is %f \r\n", voltage_motor1);
sivuu 24:2b47887bfd78 74 counts_encoder1 = Encoder1.getPulses(); // zorgt er voor dat het aantal rondjes geteld worden
sivuu 22:7c4902b80c16 75 rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); // zorgt er voor dat rev counts niet het aantal pulsen is maar het aantal rondjes
sivuu 21:1f0fae4687af 76 pc.printf("rondjes motor is %f \r\n", rev_counts_motor1);
sivuu 22:7c4902b80c16 77 value1_resetbutton = 1; // veranderd de value1_resetbutton in 1 hierdoor kan er niet meer in de andere loop gekomen worden maar nog wel in dezelfde. dit voorkomt dat als de loop 0 heeft bereikt die daar blijft staan en niet oneindig om nul blijft draaien.
sivuu 21:1f0fae4687af 78 }
sivuu 22:7c4902b80c16 79 while (sw2==0 && rev_counts_motor1>0 && value1_resetbutton <=0){ // werkt het zelfde als de vorige loop maar dan met tegengestelde richting.
sivuu 24:2b47887bfd78 80 pc.printf("aantal rondjes motor 1 is groter dan 0 \r\n");
sivuu 21:1f0fae4687af 81 richting_motor1 = 0;
sivuu 21:1f0fae4687af 82 pwm_motor1 = voltage_motor1;
sivuu 21:1f0fae4687af 83 pc.printf("richting motor is %f \r\n", richting_motor1);
sivuu 21:1f0fae4687af 84 pc.printf("voltage motor 1 is %f \r\n", voltage_motor1);
sivuu 21:1f0fae4687af 85 counts_encoder1 = Encoder1.getPulses();
sivuu 21:1f0fae4687af 86 rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond);
sivuu 21:1f0fae4687af 87 pc.printf("rondjes motor is %f \r\n", rev_counts_motor1);
sivuu 21:1f0fae4687af 88 value1_resetbutton = -1;
sivuu 21:1f0fae4687af 89 }
sivuu 22:7c4902b80c16 90 while(sw2==0 && rev_counts_motor2<0 && value2_resetbutton >= 0){ // werkt het zelfde maar dan voor motor2
sivuu 21:1f0fae4687af 91 pc.printf("aantal rondjes motor 2 is kleiner dan 0\r\n");
sivuu 21:1f0fae4687af 92 richting_motor2 = 0;
sivuu 21:1f0fae4687af 93 pwm_motor2 = voltage_motor2;
sivuu 24:2b47887bfd78 94 pwm_motor1 = 0;
sivuu 21:1f0fae4687af 95 pc.printf("richting motor 2 is %f \r\n", richting_motor2);
sivuu 21:1f0fae4687af 96 pc.printf("voltage motor 2 is %f \r\n", voltage_motor2);
sivuu 24:2b47887bfd78 97 counts_encoder2 = Encoder2.getPulses();
sivuu 21:1f0fae4687af 98 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond);
sivuu 21:1f0fae4687af 99 pc.printf("rondjes motor 2is %f \r\n", rev_counts_motor2);
sivuu 24:2b47887bfd78 100 value2_resetbutton = 1;
sivuu 21:1f0fae4687af 101 }
sivuu 21:1f0fae4687af 102 while (sw2==0 && rev_counts_motor2>0 && value2_resetbutton <=0){
sivuu 24:2b47887bfd78 103 pc.printf("aantal rondjes motor 2 is groter dan 0\r\n");
sivuu 21:1f0fae4687af 104 richting_motor2 = 1;
sivuu 21:1f0fae4687af 105 pwm_motor2 = voltage_motor2;
sivuu 24:2b47887bfd78 106 pwm_motor1=0;
sivuu 21:1f0fae4687af 107 pc.printf("richting motor 2is %f \r\n", richting_motor2);
sivuu 21:1f0fae4687af 108 pc.printf("voltage motor 2 is %f \r\n", voltage_motor2);
sivuu 21:1f0fae4687af 109 counts_encoder2 = Encoder2.getPulses();
sivuu 21:1f0fae4687af 110 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond);
sivuu 21:1f0fae4687af 111 pc.printf("rondjes motor2 is %f \r\n", rev_counts_motor2);
sivuu 21:1f0fae4687af 112 value2_resetbutton = -1;
sivuu 21:1f0fae4687af 113 }
sivuu 0:b7cb5d3978b5 114
sivuu 27:be2be9600e2d 115 if (n%2==0 && button_cw==0 && tickerflag == 1) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande
sivuu 3:34f7c16a6a7f 116 {
sivuu 3:34f7c16a6a7f 117 pc.printf("n is even \n\r"); // print lijn "n is even"
sivuu 4:2b3fbd7ef1cf 118 pc.printf("up \n\r"); // print lijn "up"
sivuu 22:7c4902b80c16 119 richting_motor1 = 1; // geeft richting van motor 1 aan
sivuu 22:7c4902b80c16 120 pwm_motor1 = voltage_motor1; // geeft pulse lengte tussen ampere uit en aan dit bepaald de snelheid
sivuu 15:706e18b43dd6 121 counts_encoder1 = Encoder1.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 122 rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); //berekenen van het aantal rondjes van motor. Gedeeld door gearboxratio en rev rond, om naar motorrondjes te gaan in plaats van pulsen van encoder!
sivuu 14:4b934ac37656 123 pc.printf("motor rondjes omhoog: %f \r\n", rev_counts_motor1); //weergeven
sivuu 22:7c4902b80c16 124 value1_resetbutton = 0; // reset de value van de resetbutton zodat als je hebt gereset en dan weer gaat draaien dat je gewoon normaal kan resetten.
sivuu 3:34f7c16a6a7f 125 }
sivuu 7:9dc08a9a5991 126
sivuu 27:be2be9600e2d 127 else if (n%2!=0 && button_cw==0 && tickerflag ==1) // als s is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
sivuu 3:34f7c16a6a7f 128 {
sivuu 3:34f7c16a6a7f 129 pc.printf("n is odd \n\r"); // print lijn "n is odd"
sivuu 3:34f7c16a6a7f 130 pc.printf("left \n\r"); // print lijn "left"
sivuu 22:7c4902b80c16 131 richting_motor2 = 1; // motor 2 richting
sivuu 22:7c4902b80c16 132 pwm_motor2 = voltage_motor2; // geeft pulse lengte tussen ampere uit en aan dit bepaald de snelheid
sivuu 15:706e18b43dd6 133 counts_encoder2 = Encoder2.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 134 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); //weergeven van het aantal rondjes
sivuu 14:4b934ac37656 135 pc.printf("motor rondjes omhoog: %f \r\n", rev_counts_motor2);
sivuu 21:1f0fae4687af 136 value2_resetbutton = 0;
sivuu 4:2b3fbd7ef1cf 137 }
sivuu 19:58c24260e08d 138
sivuu 27:be2be9600e2d 139
sivuu 27:be2be9600e2d 140 if (n%2==0 && button_ccw==0 && tickerflag == 1) // als d is ingedrukt en n is even dan gebeurd het volgende
sivuu 3:34f7c16a6a7f 141 {
sivuu 3:34f7c16a6a7f 142 pc.printf("n is even \n\r"); // print lijn "n is even"
sivuu 3:34f7c16a6a7f 143 pc.printf("down \n\r"); // print lijn "down"
sivuu 12:7903c0e55cd7 144 richting_motor1 = 0;
sivuu 19:58c24260e08d 145 pwm_motor1 = voltage_motor1;
sivuu 15:706e18b43dd6 146 counts_encoder1 = Encoder1.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 147 rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); //weergeven van het aantal rondjes
sivuu 14:4b934ac37656 148 pc.printf("motor rondjes omhoog: %f \r\n", rev_counts_motor1);
sivuu 27:be2be9600e2d 149 value1_resetbutton = 0;
sivuu 27:be2be9600e2d 150 }
sivuu 12:7903c0e55cd7 151
sivuu 27:be2be9600e2d 152 else if ( n%2!=0 && button_ccw==0 && tickerflag==1) // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
sivuu 3:34f7c16a6a7f 153 {
sivuu 3:34f7c16a6a7f 154 pc.printf("n is odd \n\r"); // print lijn "n is odd"
sivuu 3:34f7c16a6a7f 155 pc.printf("right \n\r"); // print lijn "right"
sivuu 12:7903c0e55cd7 156 richting_motor2 = 0;
sivuu 19:58c24260e08d 157 pwm_motor2 = voltage_motor2;
sivuu 15:706e18b43dd6 158 counts_encoder2 = Encoder2.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 159 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); //weergeven van het aantal rondjes
sivuu 21:1f0fae4687af 160 pc.printf("motor rondjes naar rechts: %f \r\n", rev_counts_motor2);
sivuu 21:1f0fae4687af 161 value2_resetbutton = 0;
sivuu 12:7903c0e55cd7 162 }
sivuu 27:be2be9600e2d 163
sivuu 17:f44f41cab151 164 previous_position_motor1 = current_position_motor1;
sivuu 17:f44f41cab151 165 current_position_motor1 = rev_counts_motor1;
sivuu 17:f44f41cab151 166 previous_position_motor2 = current_position_motor2;
sivuu 17:f44f41cab151 167 current_position_motor2 = rev_counts_motor2;
sivuu 17:f44f41cab151 168
sivuu 17:f44f41cab151 169 snelheid_motor1 = ((current_position_motor1 - previous_position_motor1)*6.28318530718) / ticker_TS;
sivuu 17:f44f41cab151 170 pc.printf("snelheid motor 1 is: %f \r\n", snelheid_motor1);
sivuu 17:f44f41cab151 171
sivuu 17:f44f41cab151 172 snelheid_motor2 = ((current_position_motor2 - previous_position_motor2)*6.28318530718) / ticker_TS;
sivuu 17:f44f41cab151 173 pc.printf("snelheid motor 2 is: %f \r\n", snelheid_motor2);
sivuu 17:f44f41cab151 174
sivuu 21:1f0fae4687af 175 if (abs(snelheid_motor1) > 3.0){
sivuu 21:1f0fae4687af 176 voltage_motor1 = voltage_motor1-0.005;
sivuu 20:44d3f0e6e75a 177 pc.printf("motor1 draaid te snel voltage is nu %f \r\n",voltage_motor1);
sivuu 20:44d3f0e6e75a 178 }
sivuu 21:1f0fae4687af 179 else if (abs(snelheid_motor1) < 3.0 && snelheid_motor1 != 0){
sivuu 21:1f0fae4687af 180 voltage_motor1 = voltage_motor1+0.005;
sivuu 20:44d3f0e6e75a 181 pc.printf("motor1 draaid te langzaam voltage is nu %f \r\n",voltage_motor1);
sivuu 20:44d3f0e6e75a 182 }
sivuu 20:44d3f0e6e75a 183 if (abs(snelheid_motor2) > 5.0){
sivuu 21:1f0fae4687af 184 voltage_motor2 = voltage_motor2-0.005;
sivuu 20:44d3f0e6e75a 185 pc.printf("motor1 draaid te snel voltage is nu %f \r\n",voltage_motor2);
sivuu 20:44d3f0e6e75a 186 }
sivuu 20:44d3f0e6e75a 187 else if (abs(snelheid_motor2) < 5.0 && snelheid_motor2 != 0){
sivuu 21:1f0fae4687af 188 voltage_motor2 = voltage_motor2+0.005;
sivuu 20:44d3f0e6e75a 189 pc.printf("motor1 draaid te langzaam voltage is nu %f \r\n",voltage_motor2);
sivuu 20:44d3f0e6e75a 190 }
sivuu 20:44d3f0e6e75a 191
sivuu 17:f44f41cab151 192 tickerflag = 0;
sivuu 27:be2be9600e2d 193
sivuu 27:be2be9600e2d 194 else {
sivuu 24:2b47887bfd78 195 pc.printf("rondjes motor1 is %f: \n\r",rev_counts_motor1);
sivuu 24:2b47887bfd78 196 counts_encoder1 = Encoder1.getPulses(); //tellen van de pulsen in
sivuu 24:2b47887bfd78 197 rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond);
sivuu 24:2b47887bfd78 198 counts_encoder2 = Encoder2.getPulses(); //tellen van de pulsen in
sivuu 24:2b47887bfd78 199 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond);
sivuu 12:7903c0e55cd7 200 pwm_motor2=0;
sivuu 12:7903c0e55cd7 201 pwm_motor1=0;
sivuu 27:be2be9600e2d 202 }
sivuu 27:be2be9600e2d 203
sivuu 26:a6b8bad5b0e2 204 if (rev_counts_motor1 <-3.4 && richting_motor1 == 0){
sivuu 26:a6b8bad5b0e2 205 pwm_motor1=0;
sivuu 26:a6b8bad5b0e2 206 }
sivuu 26:a6b8bad5b0e2 207 else if(rev_counts_motor1 >3.4 && richting_motor1 == 1){
sivuu 26:a6b8bad5b0e2 208 pwm_motor1 =0;
sivuu 26:a6b8bad5b0e2 209 }
sivuu 26:a6b8bad5b0e2 210
sivuu 26:a6b8bad5b0e2 211 if (rev_counts_motor2 < -4.0 && richting_motor2 == 1){
sivuu 26:a6b8bad5b0e2 212 pwm_motor2=0;
sivuu 26:a6b8bad5b0e2 213 }
sivuu 26:a6b8bad5b0e2 214 else if (rev_counts_motor2 > 4.0 && richting_motor2 == 0){
sivuu 26:a6b8bad5b0e2 215 pwm_motor2==0;
sivuu 27:be2be9600e2d 216 }
sivuu 0:b7cb5d3978b5 217 }
sivuu 27:be2be9600e2d 218 }