Alle subfiles samengevoegd
Dependencies: HIDScope biquadFilter mbed MODSERIAL FastPWM QEI
main.cpp@5:b4a0301ec8cc, 2016-10-25 (annotated)
- Committer:
- s1600907
- Date:
- Tue Oct 25 07:03:16 2016 +0000
- Revision:
- 5:b4a0301ec8cc
- Parent:
- 4:05c5da1c8b08
- Child:
- 6:00011220b82b
angle limits adjusted
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s1600907 | 0:c96cf9760185 | 1 | #include "mbed.h" |
s1600907 | 0:c96cf9760185 | 2 | #include "HIDScope.h" |
s1600907 | 0:c96cf9760185 | 3 | #include "BiQuad.h" |
s1600907 | 0:c96cf9760185 | 4 | #include "math.h" |
s1600907 | 1:e1267e72ade8 | 5 | #include "MODSERIAL.h" |
s1600907 | 0:c96cf9760185 | 6 | #define SERIAL_BAUD 115200 |
s1600907 | 0:c96cf9760185 | 7 | |
s1600907 | 1:e1267e72ade8 | 8 | MODSERIAL pc(USBTX,USBRX); |
s1600907 | 0:c96cf9760185 | 9 | //EMG aansluitingen |
s1600907 | 0:c96cf9760185 | 10 | AnalogIn emg0( A0 ); //Biceps Rechts |
s1600907 | 2:92593c9d6146 | 11 | AnalogIn emg1( A1 ); //Bicpes Links |
s1600907 | 2:92593c9d6146 | 12 | AnalogIn emg2( A2 ); //Upper leg |
s1600907 | 0:c96cf9760185 | 13 | //HIDScope |
s1600907 | 5:b4a0301ec8cc | 14 | //HIDScope scope( 6 ); // aantal channels op de HIDscope |
s1600907 | 0:c96cf9760185 | 15 | //Button om Calibratie te beëindigen |
s1600907 | 0:c96cf9760185 | 16 | InterruptIn Button1(PTC6); // DIT WORDT EEN ANDERE BUTTON |
s1600907 | 0:c96cf9760185 | 17 | |
s1600907 | 0:c96cf9760185 | 18 | // Tickers |
s1600907 | 0:c96cf9760185 | 19 | Ticker sample_timer; |
s1600907 | 0:c96cf9760185 | 20 | Ticker TickerCalculationsForTheta; |
s1600907 | 0:c96cf9760185 | 21 | |
s1600907 | 0:c96cf9760185 | 22 | //BiQuad |
s1600907 | 0:c96cf9760185 | 23 | // making the biquads and chains; imported from matlab |
s1600907 | 0:c96cf9760185 | 24 | BiQuadChain bqc; //Chain for right biceps |
s1600907 | 0:c96cf9760185 | 25 | BiQuadChain bqc2; //Chain for left biceps |
s1600907 | 0:c96cf9760185 | 26 | BiQuadChain bqc3; //Chain for Upper leg |
s1600907 | 0:c96cf9760185 | 27 | // 1 to 3 are for right biceps |
s1600907 | 0:c96cf9760185 | 28 | BiQuad bq1( 9.14969e-01, -1.82994e+00, 9.14969e-01, -1.82269e+00, 8.37182e-01 ); |
s1600907 | 0:c96cf9760185 | 29 | BiQuad bq2( 9.91104e-01, -1.60364e+00, 9.91104e-01, -1.60364e+00, 9.82207e-01 ); |
s1600907 | 0:c96cf9760185 | 30 | BiQuad bq3( 8.76555e-05, 1.75311e-04, 8.76555e-05, -1.97334e+00, 9.73695e-01 ); |
s1600907 | 0:c96cf9760185 | 31 | // 4 to 6 are for left biceps |
s1600907 | 0:c96cf9760185 | 32 | BiQuad bq4( 9.14969e-01, -1.82994e+00, 9.14969e-01, -1.82269e+00, 8.37182e-01 ); |
s1600907 | 0:c96cf9760185 | 33 | BiQuad bq5( 9.91104e-01, -1.60364e+00, 9.91104e-01, -1.60364e+00, 9.82207e-01 ); |
s1600907 | 0:c96cf9760185 | 34 | BiQuad bq6( 8.76555e-05, 1.75311e-04, 8.76555e-05, -1.97334e+00, 9.73695e-01 ); |
s1600907 | 0:c96cf9760185 | 35 | // 7 to 9 are for upper leg |
s1600907 | 0:c96cf9760185 | 36 | BiQuad bq7( 9.14969e-01, -1.82994e+00, 9.14969e-01, -1.82269e+00, 8.37182e-01 ); |
s1600907 | 0:c96cf9760185 | 37 | BiQuad bq8( 9.91104e-01, -1.60364e+00, 9.91104e-01, -1.60364e+00, 9.82207e-01 ); |
s1600907 | 0:c96cf9760185 | 38 | BiQuad bq9( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 ); |
s1600907 | 0:c96cf9760185 | 39 | |
s1600907 | 1:e1267e72ade8 | 40 | |
s1600907 | 0:c96cf9760185 | 41 | // Variabelen voor filter |
s1600907 | 0:c96cf9760185 | 42 | // spier EMG niet boven treshhold |
s1600907 | 0:c96cf9760185 | 43 | volatile bool BicepsLeft = false; |
s1600907 | 0:c96cf9760185 | 44 | volatile bool BicepsRight = false; |
s1600907 | 0:c96cf9760185 | 45 | volatile bool Leg = false; |
s1600907 | 0:c96cf9760185 | 46 | |
s1600907 | 0:c96cf9760185 | 47 | // Variabelen voor MotorHoekBerekenen |
s1600907 | 1:e1267e72ade8 | 48 | volatile double x = 0.0; // beginpositie x en y |
s1600907 | 1:e1267e72ade8 | 49 | volatile double y = 305.5; |
s1600907 | 0:c96cf9760185 | 50 | volatile const double pi = 3.14159265359; |
s1600907 | 3:c524afedf863 | 51 | volatile double Theta1Gear = 60.0; // Beginpositie op 60 graden |
s1600907 | 3:c524afedf863 | 52 | volatile double Theta2Gear = 60.0; |
s1600907 | 1:e1267e72ade8 | 53 | volatile double Theta1 = Theta1Gear*42/12; // Beginpositie van MotorHoek |
s1600907 | 1:e1267e72ade8 | 54 | volatile double Theta2 = Theta2Gear*42/12; |
s1600907 | 0:c96cf9760185 | 55 | double Fr_Speed_Theta = 100.0; // frequentie in Hz waarmee theta wordt uigerekend |
s1600907 | 0:c96cf9760185 | 56 | bool Calibration = true; // beginnen met calibreren |
s1600907 | 0:c96cf9760185 | 57 | volatile bool Stepper_State = false; // false = we zijn niet bezig met flippen |
s1600907 | 0:c96cf9760185 | 58 | |
s1600907 | 0:c96cf9760185 | 59 | /////////////////////// functies voor filters ///////////////////////////////////////////// |
s1600907 | 0:c96cf9760185 | 60 | void sample() |
s1600907 | 0:c96cf9760185 | 61 | { |
s1600907 | 0:c96cf9760185 | 62 | /* Sample function |
s1600907 | 0:c96cf9760185 | 63 | samples the emg0 (of the rightBiceps) |
s1600907 | 0:c96cf9760185 | 64 | samples the emg1 (of the leftBiceps) |
s1600907 | 0:c96cf9760185 | 65 | samples the emg2 (of the Upper leg) |
s1600907 | 0:c96cf9760185 | 66 | filter the singals |
s1600907 | 0:c96cf9760185 | 67 | sends it to HIDScope |
s1600907 | 0:c96cf9760185 | 68 | */ |
s1600907 | 0:c96cf9760185 | 69 | |
s1600907 | 0:c96cf9760185 | 70 | // Rechts Biceps |
s1600907 | 0:c96cf9760185 | 71 | double inRechts = emg0.read(); // EMG signal |
s1600907 | 0:c96cf9760185 | 72 | double FilterRechts = bqc.step(inRechts); // High pass + Notch (50 HZ) |
s1600907 | 0:c96cf9760185 | 73 | double RectifyRechts = fabs(FilterRechts); // Rectify |
s1600907 | 0:c96cf9760185 | 74 | double outRechts = bq3.step(RectifyRechts); // Low pass |
s1600907 | 2:92593c9d6146 | 75 | |
s1600907 | 0:c96cf9760185 | 76 | // Links Biceps |
s1600907 | 0:c96cf9760185 | 77 | double inLinks = emg1.read(); // EMG signal |
s1600907 | 0:c96cf9760185 | 78 | double FilterLinks = bqc2.step(inLinks); // High pass + Notch (50 HZ) |
s1600907 | 0:c96cf9760185 | 79 | double RectifyLinks = fabs(FilterLinks); // Rectify |
s1600907 | 0:c96cf9760185 | 80 | double outLinks = bq6.step(RectifyLinks); // Low pass |
s1600907 | 0:c96cf9760185 | 81 | |
s1600907 | 0:c96cf9760185 | 82 | // Upper leg |
s1600907 | 0:c96cf9760185 | 83 | double inBeen = emg2.read(); // EMG signal |
s1600907 | 0:c96cf9760185 | 84 | double FilterBeen = bqc3.step(inBeen); // High pass + Notch (50 HZ) |
s1600907 | 0:c96cf9760185 | 85 | double RectifyBeen = fabs(FilterBeen); // Rectify |
s1600907 | 2:92593c9d6146 | 86 | double outBeen = bq9.step(RectifyBeen); // Low pass |
s1600907 | 0:c96cf9760185 | 87 | |
s1600907 | 0:c96cf9760185 | 88 | |
s1600907 | 0:c96cf9760185 | 89 | /* EMG signal in channel 0 (the first channel) |
s1600907 | 0:c96cf9760185 | 90 | and the filtered EMG signal in channel 1 (the second channel) |
s1600907 | 0:c96cf9760185 | 91 | of the HIDscope */ |
s1600907 | 5:b4a0301ec8cc | 92 | /*scope.set(0,inRechts); |
s1600907 | 0:c96cf9760185 | 93 | scope.set(1,outRechts); |
s1600907 | 2:92593c9d6146 | 94 | scope.set(2,inLinks); |
s1600907 | 0:c96cf9760185 | 95 | scope.set(3,outLinks); |
s1600907 | 0:c96cf9760185 | 96 | scope.set(4,inBeen); |
s1600907 | 0:c96cf9760185 | 97 | scope.set(5,outBeen); |
s1600907 | 5:b4a0301ec8cc | 98 | */ |
s1600907 | 0:c96cf9760185 | 99 | // To indicate that the function is working, the LED is on |
s1600907 | 1:e1267e72ade8 | 100 | if (outRechts >= 0.04){ |
s1600907 | 0:c96cf9760185 | 101 | BicepsRight = true; |
s1600907 | 0:c96cf9760185 | 102 | } |
s1600907 | 0:c96cf9760185 | 103 | else{ |
s1600907 | 0:c96cf9760185 | 104 | BicepsRight = false; |
s1600907 | 0:c96cf9760185 | 105 | } |
s1600907 | 2:92593c9d6146 | 106 | |
s1600907 | 0:c96cf9760185 | 107 | // If Biceps links is actuated then: |
s1600907 | 1:e1267e72ade8 | 108 | if (outLinks >= 0.04){ |
s1600907 | 0:c96cf9760185 | 109 | BicepsLeft = true; |
s1600907 | 0:c96cf9760185 | 110 | } |
s1600907 | 0:c96cf9760185 | 111 | else{ |
s1600907 | 0:c96cf9760185 | 112 | BicepsLeft = false; |
s1600907 | 0:c96cf9760185 | 113 | } |
s1600907 | 0:c96cf9760185 | 114 | // If upper leg is actuated then: |
s1600907 | 0:c96cf9760185 | 115 | if (outBeen >= 0.01){ |
s1600907 | 0:c96cf9760185 | 116 | Leg = true; |
s1600907 | 0:c96cf9760185 | 117 | } |
s1600907 | 0:c96cf9760185 | 118 | else{ |
s1600907 | 0:c96cf9760185 | 119 | Leg = false; |
s1600907 | 0:c96cf9760185 | 120 | } |
s1600907 | 0:c96cf9760185 | 121 | } |
s1600907 | 0:c96cf9760185 | 122 | |
s1600907 | 0:c96cf9760185 | 123 | ///////////////////// functies voor motorhoekberekenen ////////////////////////// |
s1600907 | 4:05c5da1c8b08 | 124 | // vorige x opslaan |
s1600907 | 0:c96cf9760185 | 125 | double Calc_Prev_x () { |
s1600907 | 0:c96cf9760185 | 126 | double Prev_x = x; |
s1600907 | 0:c96cf9760185 | 127 | //pc.printf("prev x = %f\r\n", Prev_x); |
s1600907 | 0:c96cf9760185 | 128 | return Prev_x; |
s1600907 | 0:c96cf9760185 | 129 | } |
s1600907 | 0:c96cf9760185 | 130 | |
s1600907 | 4:05c5da1c8b08 | 131 | // vorige y opslaan |
s1600907 | 0:c96cf9760185 | 132 | double Calc_Prev_y () { |
s1600907 | 0:c96cf9760185 | 133 | double Prev_y = y; |
s1600907 | 0:c96cf9760185 | 134 | //pc.printf("prev y = %f\r\n", Prev_y); |
s1600907 | 0:c96cf9760185 | 135 | return Prev_y; |
s1600907 | 0:c96cf9760185 | 136 | } |
s1600907 | 0:c96cf9760185 | 137 | |
s1600907 | 4:05c5da1c8b08 | 138 | // vorige Theta1Gear opslaan |
s1600907 | 4:05c5da1c8b08 | 139 | double Calc_Prev_Theta1_Gear () { |
s1600907 | 4:05c5da1c8b08 | 140 | double Prev_Theta1_Gear = Theta1Gear; |
s1600907 | 4:05c5da1c8b08 | 141 | return Prev_Theta1_Gear; |
s1600907 | 4:05c5da1c8b08 | 142 | } |
s1600907 | 4:05c5da1c8b08 | 143 | |
s1600907 | 4:05c5da1c8b08 | 144 | //vorige Theta2Gear opslaan |
s1600907 | 4:05c5da1c8b08 | 145 | double Calc_Prev_Theta2_Gear () { |
s1600907 | 4:05c5da1c8b08 | 146 | double Prev_Theta2_Gear = Theta2Gear; |
s1600907 | 4:05c5da1c8b08 | 147 | return Prev_Theta2_Gear; |
s1600907 | 4:05c5da1c8b08 | 148 | } |
s1600907 | 4:05c5da1c8b08 | 149 | |
s1600907 | 2:92593c9d6146 | 150 | void FunctieFlipper(); // declarate function, function discribed below |
s1600907 | 0:c96cf9760185 | 151 | // berekenen van de nieuwe x en y waardes |
s1600907 | 0:c96cf9760185 | 152 | bool CalcXY (bool BicepsLeft, bool BicepsRight, bool Leg, bool Stepper_State) { |
s1600907 | 1:e1267e72ade8 | 153 | double Step = 5/Fr_Speed_Theta ; //10 mm per seconde afleggen |
s1600907 | 0:c96cf9760185 | 154 | |
s1600907 | 0:c96cf9760185 | 155 | if (BicepsLeft==true && BicepsRight==true && Leg==true && Stepper_State==false) { |
s1600907 | 0:c96cf9760185 | 156 | Stepper_State = true; // we zijn aan het flipper dus geen andere dingen doen |
s1600907 | 2:92593c9d6146 | 157 | FunctieFlipper() ; |
s1600907 | 0:c96cf9760185 | 158 | } |
s1600907 | 0:c96cf9760185 | 159 | else if (BicepsLeft==true && BicepsRight==false && Leg==false && Stepper_State==false) { |
s1600907 | 0:c96cf9760185 | 160 | x = x - Step; |
s1600907 | 0:c96cf9760185 | 161 | } |
s1600907 | 0:c96cf9760185 | 162 | else if (BicepsLeft==false && BicepsRight==true && Leg==false && Stepper_State==false) { |
s1600907 | 0:c96cf9760185 | 163 | x = x + Step; // naar Right bewegen |
s1600907 | 0:c96cf9760185 | 164 | } |
s1600907 | 0:c96cf9760185 | 165 | else if (BicepsLeft==true && BicepsRight==true && Leg==false && Stepper_State==false) { |
s1600907 | 0:c96cf9760185 | 166 | y = y + Step; // naar voren bewegen |
s1600907 | 0:c96cf9760185 | 167 | } |
s1600907 | 0:c96cf9760185 | 168 | else if (BicepsLeft==false && BicepsRight==true && Leg==true && Stepper_State==false) { |
s1600907 | 0:c96cf9760185 | 169 | y = y - Step; // naar achter bewegen |
s1600907 | 0:c96cf9760185 | 170 | } |
s1600907 | 0:c96cf9760185 | 171 | else if (BicepsLeft==false && BicepsRight==false && Leg==false || Stepper_State==true) { |
s1600907 | 0:c96cf9760185 | 172 | } |
s1600907 | 0:c96cf9760185 | 173 | |
s1600907 | 0:c96cf9760185 | 174 | // Grenswaardes LET OP: ARMEN MISSCHIEN GEBLOKKEERD DOOR BALK AAN DE BINNENKANT |
s1600907 | 0:c96cf9760185 | 175 | if (x > 200) { |
s1600907 | 0:c96cf9760185 | 176 | x = 200; |
s1600907 | 0:c96cf9760185 | 177 | } |
s1600907 | 0:c96cf9760185 | 178 | else if (x < -200) { |
s1600907 | 0:c96cf9760185 | 179 | x = -200; |
s1600907 | 0:c96cf9760185 | 180 | } |
s1600907 | 0:c96cf9760185 | 181 | if (y > 306) { |
s1600907 | 0:c96cf9760185 | 182 | y = 306; |
s1600907 | 0:c96cf9760185 | 183 | } |
s1600907 | 0:c96cf9760185 | 184 | else if (y < 50) { |
s1600907 | 0:c96cf9760185 | 185 | y = 50; // GOKJE, UITPROBEREN |
s1600907 | 0:c96cf9760185 | 186 | } |
s1600907 | 5:b4a0301ec8cc | 187 | //pc.printf("x = %f, y = %f\r\n", x, y); |
s1600907 | 1:e1267e72ade8 | 188 | |
s1600907 | 2:92593c9d6146 | 189 | //scope.set(2,x); |
s1600907 | 2:92593c9d6146 | 190 | //scope.set(3,y); |
s1600907 | 0:c96cf9760185 | 191 | |
s1600907 | 0:c96cf9760185 | 192 | return Stepper_State; |
s1600907 | 0:c96cf9760185 | 193 | } |
s1600907 | 0:c96cf9760185 | 194 | |
s1600907 | 0:c96cf9760185 | 195 | // diagonaal berekenen voor linker arm |
s1600907 | 0:c96cf9760185 | 196 | double CalcDia1 (double x, double y) { |
s1600907 | 0:c96cf9760185 | 197 | double a = 50.0; // de afstand van gekozen assenstelsel tot de armas (assenstelsel precies in het midden) KEERTJE NAMETEN |
s1600907 | 0:c96cf9760185 | 198 | double BV1 = sqrt(pow((a+x),2) + pow(y,2)); // diagonaal (afstand van armas tot locatie) berekenen |
s1600907 | 0:c96cf9760185 | 199 | double Dia1 = pow(BV1,2)/(2*BV1); // berekenen van de afstand oorsprong tot diagonaal |
s1600907 | 0:c96cf9760185 | 200 | |
s1600907 | 0:c96cf9760185 | 201 | //pc.printf("dia = %f, x = %f, y= %f\r\n", Dia1, x, y); |
s1600907 | 0:c96cf9760185 | 202 | return Dia1; |
s1600907 | 0:c96cf9760185 | 203 | } |
s1600907 | 0:c96cf9760185 | 204 | |
s1600907 | 0:c96cf9760185 | 205 | // diagonaal berekenen voor rechter arm |
s1600907 | 0:c96cf9760185 | 206 | double CalcDia2 (double x, double y) { |
s1600907 | 0:c96cf9760185 | 207 | double a = 50.0; |
s1600907 | 0:c96cf9760185 | 208 | double BV2 = sqrt(pow((x-a),2) + pow(y,2)); // zelfde nog een keer doen maar nu voor de rechter arm |
s1600907 | 0:c96cf9760185 | 209 | double Dia2 = pow(BV2,2)/(2*BV2); |
s1600907 | 0:c96cf9760185 | 210 | |
s1600907 | 0:c96cf9760185 | 211 | //pc.printf("dia = %f, x = %f, y= %f\r\n", Dia2, x, y); |
s1600907 | 0:c96cf9760185 | 212 | return Dia2; |
s1600907 | 0:c96cf9760185 | 213 | } |
s1600907 | 0:c96cf9760185 | 214 | |
s1600907 | 0:c96cf9760185 | 215 | // calculate Theta1 |
s1600907 | 4:05c5da1c8b08 | 216 | void CalcTheta1 (double Dia1) { |
s1600907 | 0:c96cf9760185 | 217 | double a = 50.0; |
s1600907 | 0:c96cf9760185 | 218 | double Bar = 200.0; // lengte van de armen |
s1600907 | 0:c96cf9760185 | 219 | |
s1600907 | 0:c96cf9760185 | 220 | // Hoek berekenen van het grote tandwiel (gear) |
s1600907 | 0:c96cf9760185 | 221 | if (x > -a) { |
s1600907 | 0:c96cf9760185 | 222 | Theta1Gear = pi - atan(y/(x+a)) - acos(Dia1/Bar); |
s1600907 | 0:c96cf9760185 | 223 | } |
s1600907 | 0:c96cf9760185 | 224 | else if (x > -a) { |
s1600907 | 0:c96cf9760185 | 225 | Theta1Gear = pi - (pi + atan(y/(x+a))) - acos(Dia1/Bar); |
s1600907 | 0:c96cf9760185 | 226 | } |
s1600907 | 0:c96cf9760185 | 227 | else { // als x=-a |
s1600907 | 0:c96cf9760185 | 228 | Theta1Gear = 0.5*pi - acos(Dia1/Bar); |
s1600907 | 0:c96cf9760185 | 229 | } |
s1600907 | 3:c524afedf863 | 230 | Theta1Gear = Theta1Gear*180.0/pi; // veranderen van radialen naar graden |
s1600907 | 0:c96cf9760185 | 231 | |
s1600907 | 0:c96cf9760185 | 232 | // omrekenen van grote tandwiel naar motortandwiel |
s1600907 | 0:c96cf9760185 | 233 | Theta1 = Theta1Gear*42.0/12.0; // grote tandwiel heeft 42 tanden. Motortandwiel heeft er 12. |
s1600907 | 0:c96cf9760185 | 234 | |
s1600907 | 5:b4a0301ec8cc | 235 | pc.printf("thetaMotor = %f, thetaGear = %f\r\n", Theta1, Theta1Gear); |
s1600907 | 0:c96cf9760185 | 236 | } |
s1600907 | 0:c96cf9760185 | 237 | |
s1600907 | 4:05c5da1c8b08 | 238 | void CalcTheta2 (double Dia2) { |
s1600907 | 0:c96cf9760185 | 239 | double a = 50.0; |
s1600907 | 0:c96cf9760185 | 240 | double Bar = 200.0; // lengte van de armen |
s1600907 | 0:c96cf9760185 | 241 | double Prev_Theta2_Gear = Theta2Gear; |
s1600907 | 4:05c5da1c8b08 | 242 | |
s1600907 | 0:c96cf9760185 | 243 | // Hoek berekenen van het grote tandwiel (gear) |
s1600907 | 0:c96cf9760185 | 244 | if (x < a) { |
s1600907 | 0:c96cf9760185 | 245 | Theta2Gear = pi + atan(y/(x-a)) - acos(Dia2/Bar); |
s1600907 | 0:c96cf9760185 | 246 | } |
s1600907 | 0:c96cf9760185 | 247 | else if (x > a) { |
s1600907 | 0:c96cf9760185 | 248 | Theta2Gear = pi - (pi - atan(y/(x-a))) - acos(Dia2/Bar); |
s1600907 | 0:c96cf9760185 | 249 | } |
s1600907 | 0:c96cf9760185 | 250 | else { // als x=a |
s1600907 | 0:c96cf9760185 | 251 | Theta2Gear = 0.5*pi - acos(Dia2/Bar); |
s1600907 | 0:c96cf9760185 | 252 | } |
s1600907 | 3:c524afedf863 | 253 | Theta2Gear = Theta2Gear*180/pi; // veranderen van radialen naar graden |
s1600907 | 0:c96cf9760185 | 254 | |
s1600907 | 4:05c5da1c8b08 | 255 | // omrekenen van grote tandwiel naar motortandwiel |
s1600907 | 4:05c5da1c8b08 | 256 | Theta2 = Theta2Gear*42.0/12.0; // grote tandwiel heeft 42 tanden. Motortandwiel heeft er 12. |
s1600907 | 4:05c5da1c8b08 | 257 | |
s1600907 | 5:b4a0301ec8cc | 258 | pc.printf("thetaMotor = %f, thetaGear = %f\r\n", Theta2, Theta2Gear); |
s1600907 | 4:05c5da1c8b08 | 259 | } |
s1600907 | 4:05c5da1c8b08 | 260 | |
s1600907 | 4:05c5da1c8b08 | 261 | // als een van de hoeken te groot wordt, zet dan alles terug naar de vorige positie |
s1600907 | 4:05c5da1c8b08 | 262 | void AngleLimits (double Prev_Theta1_Gear, double Prev_Theta2_Gear, double Prev_x, double Prev_y) { |
s1600907 | 4:05c5da1c8b08 | 263 | double MaxThetaGear = 100.0; // de hoek voordat arm het opstakel raakt (max hoek is 107.62 tussen arm en opstakel, maken er 100 van voor veiligheid) |
s1600907 | 4:05c5da1c8b08 | 264 | |
s1600907 | 4:05c5da1c8b08 | 265 | if (Theta1Gear >= MaxThetaGear || Theta2Gear >= MaxThetaGear) { |
s1600907 | 4:05c5da1c8b08 | 266 | Theta1Gear = Prev_Theta1_Gear; |
s1600907 | 0:c96cf9760185 | 267 | Theta2Gear = Prev_Theta2_Gear; |
s1600907 | 0:c96cf9760185 | 268 | x = Prev_x; |
s1600907 | 0:c96cf9760185 | 269 | y = Prev_y; |
s1600907 | 4:05c5da1c8b08 | 270 | |
s1600907 | 4:05c5da1c8b08 | 271 | Theta1 = Theta1Gear*42.0/12.0; |
s1600907 | 4:05c5da1c8b08 | 272 | Theta2 = Theta2Gear*42.0/12.0; |
s1600907 | 0:c96cf9760185 | 273 | } |
s1600907 | 5:b4a0301ec8cc | 274 | pc.printf("thetaMotor = %f, thetaGear = %f\r\n", Theta2, Theta2Gear); |
s1600907 | 0:c96cf9760185 | 275 | } |
s1600907 | 0:c96cf9760185 | 276 | |
s1600907 | 0:c96cf9760185 | 277 | void CalculationsForTheta () { |
s1600907 | 1:e1267e72ade8 | 278 | sample(); |
s1600907 | 0:c96cf9760185 | 279 | double Prev_x = Calc_Prev_x (); |
s1600907 | 0:c96cf9760185 | 280 | double Prev_y = Calc_Prev_y (); |
s1600907 | 4:05c5da1c8b08 | 281 | double Calc_Prev_Theta1_Gear (); |
s1600907 | 4:05c5da1c8b08 | 282 | double Calc_Prev_Theta2_Gear (); |
s1600907 | 0:c96cf9760185 | 283 | bool Stepper_State = CalcXY (BicepsLeft, BicepsRight, Leg, Stepper_State); |
s1600907 | 0:c96cf9760185 | 284 | double Dia1 = CalcDia1 (x, y); |
s1600907 | 0:c96cf9760185 | 285 | double Dia2 = CalcDia2 (x, y); |
s1600907 | 4:05c5da1c8b08 | 286 | CalcTheta1 (Dia1); |
s1600907 | 4:05c5da1c8b08 | 287 | CalcTheta2 (Dia2); |
s1600907 | 4:05c5da1c8b08 | 288 | AngleLimits (Theta1Gear, Theta2Gear, Prev_x, Prev_y); |
s1600907 | 2:92593c9d6146 | 289 | |
s1600907 | 5:b4a0301ec8cc | 290 | //scope.send(); |
s1600907 | 0:c96cf9760185 | 291 | } |
s1600907 | 0:c96cf9760185 | 292 | |
s1600907 | 0:c96cf9760185 | 293 | // als de button ingedrukt wordt dan stoppen we met calibreren |
s1600907 | 0:c96cf9760185 | 294 | void EndCalibration () { |
s1600907 | 0:c96cf9760185 | 295 | Calibration = false; |
s1600907 | 0:c96cf9760185 | 296 | } |
s1600907 | 0:c96cf9760185 | 297 | |
s1600907 | 2:92593c9d6146 | 298 | //////////////// functies voor aansturing motoren /////////////////////////////// |
s1600907 | 2:92593c9d6146 | 299 | void FunctieFlipper () { |
s1600907 | 2:92593c9d6146 | 300 | Stepper_State = false; |
s1600907 | 1:e1267e72ade8 | 301 | } |
s1600907 | 0:c96cf9760185 | 302 | |
s1600907 | 0:c96cf9760185 | 303 | ////////////////////////// main loop //////////////////////////////////////////// |
s1600907 | 0:c96cf9760185 | 304 | int main() |
s1600907 | 0:c96cf9760185 | 305 | { |
s1600907 | 0:c96cf9760185 | 306 | pc.baud(SERIAL_BAUD); |
s1600907 | 0:c96cf9760185 | 307 | pc.printf("\r\n Nieuwe code uitproberen :) \r\n"); |
s1600907 | 0:c96cf9760185 | 308 | |
s1600907 | 0:c96cf9760185 | 309 | /* making the biquad chain for filtering the emg |
s1600907 | 0:c96cf9760185 | 310 | notch and high pass */ |
s1600907 | 0:c96cf9760185 | 311 | bqc.add( &bq1 ).add( &bq2 ); |
s1600907 | 0:c96cf9760185 | 312 | bqc2.add( &bq4 ).add( &bq5 ); |
s1600907 | 0:c96cf9760185 | 313 | bqc3.add( &bq7 ).add( &bq8 ); |
s1600907 | 0:c96cf9760185 | 314 | |
s1600907 | 0:c96cf9760185 | 315 | // Calibreren |
s1600907 | 0:c96cf9760185 | 316 | while (Calibration == true) { |
s1600907 | 1:e1267e72ade8 | 317 | //potmeter dingen aanpassen |
s1600907 | 0:c96cf9760185 | 318 | |
s1600907 | 0:c96cf9760185 | 319 | pc.printf("calibreren is bezig\r\n"); |
s1600907 | 0:c96cf9760185 | 320 | Button1.fall(&EndCalibration); |
s1600907 | 0:c96cf9760185 | 321 | } |
s1600907 | 0:c96cf9760185 | 322 | |
s1600907 | 0:c96cf9760185 | 323 | /**Attach the 'sample' function to the timer 'sample_timer'. |
s1600907 | 0:c96cf9760185 | 324 | * this ensures that 'sample' is executed every 0.002 seconds = 500 Hz |
s1600907 | 0:c96cf9760185 | 325 | */ |
s1600907 | 1:e1267e72ade8 | 326 | // sample_timer.attach(&sample, 0.002); |
s1600907 | 2:92593c9d6146 | 327 | |
s1600907 | 0:c96cf9760185 | 328 | // ticker voor hoek berekenen aanzetten |
s1600907 | 5:b4a0301ec8cc | 329 | TickerCalculationsForTheta.attach(&CalculationsForTheta,1); //0.002 |
s1600907 | 2:92593c9d6146 | 330 | |
s1600907 | 0:c96cf9760185 | 331 | //empty loop, sample() is executed periodically |
s1600907 | 1:e1267e72ade8 | 332 | while(1) { |
s1600907 | 1:e1267e72ade8 | 333 | |
s1600907 | 1:e1267e72ade8 | 334 | } |
s1600907 | 0:c96cf9760185 | 335 | } |