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 21 09:23:28 2016 +0000
Revision:
19:58c24260e08d
Parent:
17:f44f41cab151
Child:
20:44d3f0e6e75a
met werkende feedbackloop voor motor1 een kant op

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 17:f44f41cab151 5 DigitalIn encoder1A(D13);
sivuu 12:7903c0e55cd7 6 DigitalIn encoder1B(D12);
sivuu 17:f44f41cab151 7 DigitalIn encoder2A(D11);
sivuu 17:f44f41cab151 8 DigitalIn encoder2B(D10);
sivuu 17:f44f41cab151 9 DigitalIn button_cw(D9);
sivuu 17:f44f41cab151 10 DigitalIn button_ccw(PTC12);
sivuu 12:7903c0e55cd7 11 MODSERIAL pc(USBTX, USBRX);
sivuu 12:7903c0e55cd7 12 DigitalOut richting_motor1(D4);
sivuu 4:2b3fbd7ef1cf 13 PwmOut pwm_motor1(D5);
sivuu 12:7903c0e55cd7 14 DigitalOut richting_motor2(D7);
sivuu 4:2b3fbd7ef1cf 15 PwmOut pwm_motor2(D6);
sivuu 14:4b934ac37656 16
sivuu 14:4b934ac37656 17
sivuu 14:4b934ac37656 18
sivuu 12:7903c0e55cd7 19 int n = 0; //start van de teller wordt op nul gesteld
sivuu 2:4dcdf7755a04 20
sivuu 3:34f7c16a6a7f 21 void SwitchN() { // maakt simpele functie die 1 bij n optelt
sivuu 2:4dcdf7755a04 22 n++;
sivuu 2:4dcdf7755a04 23 }
sivuu 12:7903c0e55cd7 24
sivuu 17:f44f41cab151 25
sivuu 17:f44f41cab151 26
sivuu 14:4b934ac37656 27 //constanten voor de encoder
sivuu 14:4b934ac37656 28 const int CW = 2.5; //definitie rechtsom 'lage waarde'
sivuu 17:f44f41cab151 29 const int CCW =-1; //definitie linksom 'hoge waarde'
sivuu 17:f44f41cab151 30 const float gearboxratio=131.25; // gearboxratio van encoder naar motor
sivuu 14:4b934ac37656 31 const int rev_rond=32; // aantal revoluties per omgang van de encoder
sivuu 17:f44f41cab151 32
sivuu 17:f44f41cab151 33 volatile double current_position_motor1 = 0; // current position is 0 op het begin
sivuu 17:f44f41cab151 34 volatile double previous_position_motor1 = 0; // previous position is 0 op het begin
sivuu 17:f44f41cab151 35 volatile double current_position_motor2 = 0;
sivuu 17:f44f41cab151 36 volatile double previous_position_motor2 = 0;
sivuu 17:f44f41cab151 37 volatile bool tickerflag; //bool zorgt er voor dat de tickerflag alleen 1 of 0 kan zijn (true or false)
sivuu 17:f44f41cab151 38 volatile double snelheid_motor1;
sivuu 17:f44f41cab151 39 volatile double snelheid_motor2;
sivuu 19:58c24260e08d 40 double ticker_TS=0.1; // zorgt voor een tijdstap van de ticker van 0.01 seconde
sivuu 17:f44f41cab151 41 volatile double timepassed=0; //de waarde van hoeveel tijd er verstreken is
sivuu 17:f44f41cab151 42 Ticker t; // maakt ticker t aan
sivuu 17:f44f41cab151 43
sivuu 17:f44f41cab151 44 void tickerfunctie() // maakt een ticker functie aan
sivuu 17:f44f41cab151 45 {
sivuu 17:f44f41cab151 46 tickerflag = 1; // het enige wat deze functie doet is zorgen dat tickerflag 1 is
sivuu 17:f44f41cab151 47 }
sivuu 2:4dcdf7755a04 48
sivuu 0:b7cb5d3978b5 49 int main()
sivuu 0:b7cb5d3978b5 50 {
sivuu 12:7903c0e55cd7 51 pc.baud(115200); // zorgt voor de link voor putty, 115200 is snelheid
sivuu 17:f44f41cab151 52 QEI Encoder2(D12,D13, NC, rev_rond); // maakt een encoder aan! D12/D13 ingangen, rev_rond zijn aantal pulsen per revolutie! Bovenaan in te stellen.
sivuu 17:f44f41cab151 53 QEI Encoder1(D10,D11, NC, rev_rond);
sivuu 14:4b934ac37656 54 float counts_encoder1; //variabele counts aanmaken
sivuu 14:4b934ac37656 55 float rev_counts_motor1; //variabele motor rondjes aanmaken in radialen!!
sivuu 14:4b934ac37656 56 float counts_encoder2;
sivuu 14:4b934ac37656 57 float rev_counts_motor2;
sivuu 17:f44f41cab151 58 t.attach(&tickerfunctie,ticker_TS); // attacht de ticker met een tick van 0.01 seconde (gelijk aan de tijdstap)
sivuu 19:58c24260e08d 59 volatile float voltage_motor1=1; //voltingang motor 1
sivuu 19:58c24260e08d 60 volatile float voltage_motor2=1;//voltingang motor 2
sivuu 12:7903c0e55cd7 61
sivuu 3:34f7c16a6a7f 62 while (true) { // zorgt er voor dat de code oneindig doorgelopen wordt
sivuu 2:4dcdf7755a04 63
sivuu 12:7903c0e55cd7 64 sw3.fall(&SwitchN); // zorgt er voor dat void switch wordt gedaan als switch 3 wordt ingedrukt
sivuu 12:7903c0e55cd7 65
sivuu 0:b7cb5d3978b5 66
sivuu 12:7903c0e55cd7 67
sivuu 12:7903c0e55cd7 68 if (button_cw==0) // als s ingedrukt wordt gebeurd het volgende
sivuu 3:34f7c16a6a7f 69 {
sivuu 17:f44f41cab151 70 if ( tickerflag == 1) // als de ticker flag 1 is gaat dit loopje lopen.
sivuu 17:f44f41cab151 71 {
sivuu 3:34f7c16a6a7f 72 if (n%2==0) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande
sivuu 3:34f7c16a6a7f 73 {
sivuu 3:34f7c16a6a7f 74 pc.printf("n is even \n\r"); // print lijn "n is even"
sivuu 4:2b3fbd7ef1cf 75 pc.printf("up \n\r"); // print lijn "up"
sivuu 12:7903c0e55cd7 76 richting_motor1 = 1;
sivuu 19:58c24260e08d 77 pwm_motor1 = voltage_motor1;
sivuu 15:706e18b43dd6 78 counts_encoder1 = Encoder1.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 79 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 80 pc.printf("motor rondjes omhoog: %f \r\n", rev_counts_motor1); //weergeven
sivuu 12:7903c0e55cd7 81
sivuu 3:34f7c16a6a7f 82 }
sivuu 7:9dc08a9a5991 83
sivuu 3:34f7c16a6a7f 84 else // als s is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
sivuu 3:34f7c16a6a7f 85 {
sivuu 3:34f7c16a6a7f 86 pc.printf("n is odd \n\r"); // print lijn "n is odd"
sivuu 3:34f7c16a6a7f 87 pc.printf("left \n\r"); // print lijn "left"
sivuu 12:7903c0e55cd7 88 richting_motor2 = 1;
sivuu 19:58c24260e08d 89 pwm_motor2 = voltage_motor2;
sivuu 15:706e18b43dd6 90 counts_encoder2 = Encoder2.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 91 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); //weergeven van het aantal rondjes
sivuu 14:4b934ac37656 92 pc.printf("motor rondjes omhoog: %f \r\n", rev_counts_motor2);
sivuu 4:2b3fbd7ef1cf 93 }
sivuu 19:58c24260e08d 94
sivuu 17:f44f41cab151 95 previous_position_motor1 = current_position_motor1;
sivuu 17:f44f41cab151 96 current_position_motor1 = rev_counts_motor1;
sivuu 17:f44f41cab151 97 previous_position_motor2 = current_position_motor2;
sivuu 17:f44f41cab151 98 current_position_motor2 = rev_counts_motor2;
sivuu 17:f44f41cab151 99
sivuu 17:f44f41cab151 100 snelheid_motor1 = ((current_position_motor1 - previous_position_motor1)*6.28318530718) / ticker_TS;
sivuu 17:f44f41cab151 101 pc.printf("snelheid motor 1 is: %f \r\n", snelheid_motor1);
sivuu 17:f44f41cab151 102
sivuu 17:f44f41cab151 103 snelheid_motor2 = ((current_position_motor2 - previous_position_motor2)*6.28318530718) / ticker_TS;
sivuu 17:f44f41cab151 104 pc.printf("snelheid motor 2 is: %f \r\n", snelheid_motor2);
sivuu 17:f44f41cab151 105
sivuu 19:58c24260e08d 106 if (snelheid_motor1 > 5.2 && snelheid_motor1 != 0){
sivuu 19:58c24260e08d 107 voltage_motor1 = voltage_motor1-0.02;
sivuu 19:58c24260e08d 108 pc.printf("motor1 draaid te snel voltage is nu %f \r\n",voltage_motor1);
sivuu 19:58c24260e08d 109 }
sivuu 19:58c24260e08d 110 else if (snelheid_motor1 < 5.2 && snelheid_motor1 != 0){
sivuu 19:58c24260e08d 111 voltage_motor1 = voltage_motor1+0.02;
sivuu 19:58c24260e08d 112 pc.printf("motor1 draaid te langzaam voltage is nu %f \r\n",voltage_motor1);
sivuu 19:58c24260e08d 113 }
sivuu 17:f44f41cab151 114 tickerflag = 0;
sivuu 17:f44f41cab151 115 }
sivuu 3:34f7c16a6a7f 116 }
sivuu 12:7903c0e55cd7 117 else if (button_ccw==0) // als d ingedrukt wordt gebeurd het volgende
sivuu 3:34f7c16a6a7f 118 {
sivuu 17:f44f41cab151 119 if ( tickerflag == 1) // als de ticker flag 1 is gaat dit loopje lopen.
sivuu 17:f44f41cab151 120 {
sivuu 3:34f7c16a6a7f 121 if (n%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende
sivuu 3:34f7c16a6a7f 122 {
sivuu 3:34f7c16a6a7f 123 pc.printf("n is even \n\r"); // print lijn "n is even"
sivuu 3:34f7c16a6a7f 124 pc.printf("down \n\r"); // print lijn "down"
sivuu 12:7903c0e55cd7 125 richting_motor1 = 0;
sivuu 19:58c24260e08d 126 pwm_motor1 = voltage_motor1;
sivuu 15:706e18b43dd6 127 counts_encoder1 = Encoder1.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 128 rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); //weergeven van het aantal rondjes
sivuu 14:4b934ac37656 129 pc.printf("motor rondjes omhoog: %f \r\n", rev_counts_motor1);
sivuu 12:7903c0e55cd7 130
sivuu 12:7903c0e55cd7 131
sivuu 3:34f7c16a6a7f 132 }
sivuu 3:34f7c16a6a7f 133 else // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
sivuu 3:34f7c16a6a7f 134 {
sivuu 3:34f7c16a6a7f 135 pc.printf("n is odd \n\r"); // print lijn "n is odd"
sivuu 3:34f7c16a6a7f 136 pc.printf("right \n\r"); // print lijn "right"
sivuu 12:7903c0e55cd7 137 richting_motor2 = 0;
sivuu 19:58c24260e08d 138 pwm_motor2 = voltage_motor2;
sivuu 15:706e18b43dd6 139 counts_encoder2 = Encoder2.getPulses(); //tellen van de pulsen in
sivuu 14:4b934ac37656 140 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); //weergeven van het aantal rondjes
sivuu 14:4b934ac37656 141 pc.printf("motor rondjes naar rechts: %f \r\n", rev_counts_motor2);
sivuu 12:7903c0e55cd7 142
sivuu 12:7903c0e55cd7 143 }
sivuu 17:f44f41cab151 144 previous_position_motor1 = current_position_motor1;
sivuu 17:f44f41cab151 145 current_position_motor1 = rev_counts_motor1;
sivuu 17:f44f41cab151 146 previous_position_motor2 = current_position_motor2;
sivuu 17:f44f41cab151 147 current_position_motor2 = rev_counts_motor2;
sivuu 17:f44f41cab151 148
sivuu 17:f44f41cab151 149 snelheid_motor1 = ((current_position_motor1 - previous_position_motor1)*6.28318530718) / ticker_TS;
sivuu 17:f44f41cab151 150 pc.printf("snelheid motor 1 is: %f \r\n", snelheid_motor1);
sivuu 17:f44f41cab151 151
sivuu 17:f44f41cab151 152 snelheid_motor2 = ((current_position_motor2 - previous_position_motor2)*6.28318530718) / ticker_TS;
sivuu 17:f44f41cab151 153 pc.printf("snelheid motor 2 is: %f \r\n", snelheid_motor2);
sivuu 17:f44f41cab151 154
sivuu 17:f44f41cab151 155 tickerflag = 0;
sivuu 17:f44f41cab151 156 }
sivuu 12:7903c0e55cd7 157 }
sivuu 12:7903c0e55cd7 158 else{
sivuu 15:706e18b43dd6 159 // pc.printf("motor staat stil \n\r");
sivuu 12:7903c0e55cd7 160 pwm_motor2=0;
sivuu 12:7903c0e55cd7 161 pwm_motor1=0;
sivuu 12:7903c0e55cd7 162 }
sivuu 12:7903c0e55cd7 163
sivuu 3:34f7c16a6a7f 164
sivuu 0:b7cb5d3978b5 165 }
sivuu 3:34f7c16a6a7f 166 }
sivuu 3:34f7c16a6a7f 167