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