FINAL VERSION

Dependencies:   biquadFilter MODSERIAL QEI Servo mbed

Fork of Robot_Battle_met_ARVID by Gaston Gabriël

Committer:
cmaas
Date:
Thu Nov 01 17:29:01 2018 +0000
Revision:
10:2325e545ce11
Parent:
9:40c9a18c3430
Child:
11:4ee7f6d482f4
PID + Kinematics working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cmaas 9:40c9a18c3430 1 // EMG + KINEMATICS + PID + MOTOR CONTROL
cmaas 9:40c9a18c3430 2
cmaas 9:40c9a18c3430 3 //----------------~INITIATING-------------------------
aschut 0:90750f158475 4 #include "mbed.h"
cmaas 9:40c9a18c3430 5
cmaas 9:40c9a18c3430 6 // EMG -- DEPENDENCIES
aschut 0:90750f158475 7 #include <iostream>
gastongab 2:0a8622662f6d 8 #include "BiQuad.h"
gastongab 2:0a8622662f6d 9 #include "BiQuadchains_zelfbeun.h"
gastongab 2:0a8622662f6d 10 #include "MODSERIAL.h"
gastongab 2:0a8622662f6d 11
cmaas 9:40c9a18c3430 12 // KINEMATICS -- DEPENDENCIES
cmaas 9:40c9a18c3430 13 #include "stdio.h"
cmaas 9:40c9a18c3430 14 #define _USE_MATH_DEFINES
cmaas 9:40c9a18c3430 15 #include <math.h>
cmaas 9:40c9a18c3430 16 #define M_PI 3.14159265358979323846 /* pi */
cmaas 9:40c9a18c3430 17
cmaas 9:40c9a18c3430 18 // PID CONTROLLER -- DEPENDENCIES
cmaas 9:40c9a18c3430 19 #include "BiQuad.h"
cmaas 9:40c9a18c3430 20 #include "QEI.h"
cmaas 9:40c9a18c3430 21 //#include "HIDScope.h"
cmaas 9:40c9a18c3430 22
cmaas 9:40c9a18c3430 23
cmaas 9:40c9a18c3430 24 // GENERAL PIN DEFENITIONS
gastongab 2:0a8622662f6d 25 MODSERIAL pc(USBTX, USBRX);
gastongab 2:0a8622662f6d 26
cmaas 9:40c9a18c3430 27 // EMG -- PIN DEFENITIONS
aschut 0:90750f158475 28 DigitalOut gpo(D0);
aschut 0:90750f158475 29
cmaas 9:40c9a18c3430 30 DigitalIn button2(SW3);
aschut 0:90750f158475 31 DigitalIn button1(SW2); //or SW2
aschut 0:90750f158475 32
aschut 0:90750f158475 33 DigitalOut led1(LED_GREEN);
aschut 0:90750f158475 34 DigitalOut led2(LED_RED);
aschut 0:90750f158475 35 DigitalOut led3(LED_BLUE);
aschut 0:90750f158475 36
gastongab 2:0a8622662f6d 37 //EMG tickers, these tickers are called in the mainscript with fsample 500Hz, also sends to HIDscope with same fsample
gastongab 2:0a8622662f6d 38 Ticker sample_ticker; //ticker for filtering pref. with 1000Hz, define in tick.attach
gastongab 4:c7be673eb4a1 39 Ticker threshold_check_ticker;
gastongab 2:0a8622662f6d 40 Timer t; //timer try out for Astrid
gastongab 2:0a8622662f6d 41 Timer timer_calibration; //timer for EMG calibration
gastongab 2:0a8622662f6d 42
gastongab 2:0a8622662f6d 43 //Input system
gastongab 2:0a8622662f6d 44 AnalogIn emg1(A0); //right biceps
gastongab 2:0a8622662f6d 45 AnalogIn emg2(A1); //right triceps
gastongab 2:0a8622662f6d 46 AnalogIn emg3(A2); //left biceps
gastongab 2:0a8622662f6d 47 AnalogIn emg4(A3); //left triceps
gastongab 2:0a8622662f6d 48
cmaas 9:40c9a18c3430 49
cmaas 9:40c9a18c3430 50 // PID CONTROLLER -- PIN DEFENITIONS
cmaas 10:2325e545ce11 51 AnalogIn button3(A4);
cmaas 10:2325e545ce11 52 AnalogIn button4(A5);
cmaas 9:40c9a18c3430 53
cmaas 9:40c9a18c3430 54 DigitalOut directionpin1(D7); // motor 1
cmaas 9:40c9a18c3430 55 DigitalOut directionpin2(D4); // motor 2
cmaas 9:40c9a18c3430 56 DigitalOut directionpin3(D13); // motor 3
cmaas 9:40c9a18c3430 57 PwmOut pwmpin1(D6); // motor 1
cmaas 9:40c9a18c3430 58 PwmOut pwmpin2(D5); // motor 2
cmaas 9:40c9a18c3430 59 PwmOut pwmpin3(D12); // motor 3
cmaas 9:40c9a18c3430 60
cmaas 9:40c9a18c3430 61 QEI encoder1 (D9, D8, NC, 8400, QEI::X4_ENCODING);
cmaas 9:40c9a18c3430 62 QEI encoder2 (D11, D10, NC, 8400, QEI::X4_ENCODING); // motor 2
cmaas 9:40c9a18c3430 63 QEI encoder3 (D3, D2, NC, 8400, QEI::X4_ENCODING); // motor 3
cmaas 9:40c9a18c3430 64 // HIDScope scope(2);
cmaas 9:40c9a18c3430 65
cmaas 9:40c9a18c3430 66 // PID - TICKERS
cmaas 9:40c9a18c3430 67 Ticker ref_rot;
cmaas 9:40c9a18c3430 68 Ticker show_counts;
cmaas 9:40c9a18c3430 69 Ticker Scope_Data;
cmaas 9:40c9a18c3430 70
cmaas 10:2325e545ce11 71 //------------------------GLOBALS-----------------------------
cmaas 9:40c9a18c3430 72 // GLOBALS EMG
gastongab 2:0a8622662f6d 73 //Filtered EMG signals from the end of the chains
gastongab 4:c7be673eb4a1 74 volatile double emg1_filtered, emg2_filtered, emg3_filtered, emg4_filtered;
gastongab 4:c7be673eb4a1 75 int i = 0;
gastongab 2:0a8622662f6d 76
cmaas 9:40c9a18c3430 77 //Define doubles for calibration and ticker
cmaas 9:40c9a18c3430 78 double ts = 0.001; //tijdsstap
cmaas 9:40c9a18c3430 79 double calibration_time = 55; //time EMG calibration should take
cmaas 9:40c9a18c3430 80
cmaas 9:40c9a18c3430 81 volatile double temp_highest_emg1 = 0; //highest detected value right biceps
cmaas 9:40c9a18c3430 82 volatile double temp_highest_emg2 = 0;
cmaas 9:40c9a18c3430 83 volatile double temp_highest_emg3 = 0;
cmaas 9:40c9a18c3430 84 volatile double temp_highest_emg4 = 0;
cmaas 9:40c9a18c3430 85
cmaas 9:40c9a18c3430 86 //Doubles for calculation threshold
cmaas 9:40c9a18c3430 87 double biceps_p_t = 0.4; //set threshold at percentage of highest value
cmaas 9:40c9a18c3430 88 double triceps_p_t = 0.5; //set threshold at percentage of highest value
cmaas 9:40c9a18c3430 89 volatile double threshold1;
cmaas 9:40c9a18c3430 90 volatile double threshold2;
cmaas 9:40c9a18c3430 91 volatile double threshold3;
cmaas 9:40c9a18c3430 92 volatile double threshold4;
cmaas 9:40c9a18c3430 93
cmaas 9:40c9a18c3430 94 // thresholdreads bools
cmaas 9:40c9a18c3430 95 int bicepsR;
cmaas 9:40c9a18c3430 96 int tricepsR;
cmaas 9:40c9a18c3430 97 int bicepsL;
cmaas 9:40c9a18c3430 98 int tricepsL;
cmaas 9:40c9a18c3430 99
cmaas 9:40c9a18c3430 100 // VARIABLES ROBOT KINEMATICS
cmaas 9:40c9a18c3430 101 // constants
cmaas 9:40c9a18c3430 102 const float la = 0.256; // lengte actieve arm
cmaas 9:40c9a18c3430 103 const float lp = 0.21; // lengte passieve arm
cmaas 9:40c9a18c3430 104 const float rp = 0.052; // straal van midden end effector tot hoekpunt
cmaas 9:40c9a18c3430 105 const float rm = 0.23; // straal van global midden tot motor
cmaas 9:40c9a18c3430 106 const float a = 0.09; // zijde van de driehoek
cmaas 9:40c9a18c3430 107 const float xas = 0.40; // afstand van motor 1 tot motor 3
cmaas 9:40c9a18c3430 108 const float yas = 0.346; // afstand van xas tot motor 2
cmaas 9:40c9a18c3430 109 const float thetap = 0; // rotatiehoek van de end effector
cmaas 9:40c9a18c3430 110
cmaas 9:40c9a18c3430 111
cmaas 9:40c9a18c3430 112 // motor locatie
cmaas 9:40c9a18c3430 113 const int a1x = 0; //x locatie motor 1
cmaas 9:40c9a18c3430 114 const int a1y = 0; //y locatie motor 1
cmaas 9:40c9a18c3430 115 const float a2x = (0.5)*xas; // x locatie motor 2
cmaas 9:40c9a18c3430 116 const float a2y = yas; // y locatie motor 2
cmaas 9:40c9a18c3430 117 const float a3x = xas; // x locatie motor 3
cmaas 9:40c9a18c3430 118 const int a3y = 0; // y locatie motor 3
cmaas 9:40c9a18c3430 119
cmaas 9:40c9a18c3430 120 // script voor het bepalen van de desired position aan de hand van emg (1/0)
cmaas 9:40c9a18c3430 121
cmaas 9:40c9a18c3430 122 // EMG OUTPUT
cmaas 9:40c9a18c3430 123 int EMGxplus;
cmaas 9:40c9a18c3430 124 int EMGxmin ;
cmaas 9:40c9a18c3430 125 int EMGyplus;
cmaas 9:40c9a18c3430 126 int EMGymin ;
cmaas 9:40c9a18c3430 127
cmaas 9:40c9a18c3430 128 // Dit moet experimenteel geperfectioneerd worden
cmaas 9:40c9a18c3430 129 float tijdstap = 0.005; //nu wss heel langzaam, kan miss omhoog KEER V GEEFT VERANDERING IN POSITIE
cmaas 9:40c9a18c3430 130 float v = 0.1; // snelheid kan wss ook hoger
cmaas 9:40c9a18c3430 131
cmaas 9:40c9a18c3430 132 float px = 0.2; //starting x // BOUNDARIES
cmaas 9:40c9a18c3430 133 float py = 0.155; // starting y // BOUNDARIES
cmaas 9:40c9a18c3430 134
cmaas 9:40c9a18c3430 135 // verschil horizontale as met de actieve arm
cmaas 9:40c9a18c3430 136 float da1 = 1.619685; // verschil a1 hoek en motor
cmaas 9:40c9a18c3430 137 float da2 = -0.609780;
cmaas 9:40c9a18c3430 138 float da3 = 3.372859;
cmaas 9:40c9a18c3430 139
cmaas 9:40c9a18c3430 140 // limits (since no forward kinematics)
cmaas 9:40c9a18c3430 141 float upperxlim = 0.275; //36, 0.04, 0.315, -0.085niet helemaal naar requierments ff kijken of ie groter kan
cmaas 9:40c9a18c3430 142 float lowerxlim = 0.10;
cmaas 9:40c9a18c3430 143 float upperylim = 0.225;
cmaas 9:40c9a18c3430 144 float lowerylim = 0.03; //0.03 is goed
cmaas 9:40c9a18c3430 145
cmaas 9:40c9a18c3430 146 // VARIABLES PID CONTROLLER
cmaas 9:40c9a18c3430 147 double PI = M_PI;// CHANGE THIS INTO M_PI
cmaas 9:40c9a18c3430 148 double Kp = 14; //200 , 50
cmaas 9:40c9a18c3430 149 double Ki = 0; //1, 0.5
cmaas 9:40c9a18c3430 150 double Kd = 3; //200, 10
cmaas 9:40c9a18c3430 151 double Ts = 0.1; // Sample time in seconds
cmaas 9:40c9a18c3430 152 double reference_rotation; //define as radians
cmaas 9:40c9a18c3430 153 double motor_position;
cmaas 9:40c9a18c3430 154 bool AlwaysTrue;
cmaas 9:40c9a18c3430 155
cmaas 9:40c9a18c3430 156 //----------------FUNCTIONS--------------------------
cmaas 9:40c9a18c3430 157
cmaas 9:40c9a18c3430 158 // ~~~~~~~~~~~~~~~~~~~EMG FUNCTIONS~~~~~~~~~~~~~~~~~~
gastongab 7:88fa84742b3c 159 void emgsample()
gastongab 7:88fa84742b3c 160 {
gastongab 2:0a8622662f6d 161 //All EMG signal through Highpass
gastongab 2:0a8622662f6d 162 double emgread1 = emg1.read();
gastongab 2:0a8622662f6d 163 double emgread2 = emg2.read();
gastongab 2:0a8622662f6d 164 double emgread3 = emg3.read();
gastongab 2:0a8622662f6d 165 double emgread4 = emg4.read();
gastongab 7:88fa84742b3c 166
gastongab 2:0a8622662f6d 167 double emg1_highpassed = highp1.step(emgread1);
gastongab 2:0a8622662f6d 168 double emg2_highpassed = highp2.step(emgread2);
gastongab 2:0a8622662f6d 169 double emg3_highpassed = highp3.step(emgread3);
gastongab 2:0a8622662f6d 170 double emg4_highpassed = highp4.step(emgread4);
gastongab 7:88fa84742b3c 171
gastongab 2:0a8622662f6d 172 //All EMG highpassed through Notch
gastongab 2:0a8622662f6d 173 double emg1_notched = notch1.step(emg1_highpassed);
gastongab 2:0a8622662f6d 174 double emg2_notched = notch2.step(emg2_highpassed);
gastongab 2:0a8622662f6d 175 double emg3_notched = notch3.step(emg3_highpassed);
gastongab 2:0a8622662f6d 176 double emg4_notched = notch4.step(emg4_highpassed);
gastongab 7:88fa84742b3c 177
gastongab 2:0a8622662f6d 178 //All EMG notched rectify
gastongab 2:0a8622662f6d 179 double emg1_abs = abs(emg1_notched);
gastongab 2:0a8622662f6d 180 double emg2_abs = abs(emg2_notched);
gastongab 2:0a8622662f6d 181 double emg3_abs = abs(emg3_notched);
gastongab 2:0a8622662f6d 182 double emg4_abs = abs(emg4_notched);
gastongab 7:88fa84742b3c 183
gastongab 2:0a8622662f6d 184 //All EMG abs into lowpass
gastongab 2:0a8622662f6d 185 emg1_filtered = lowp1.step(emg1_abs);
gastongab 2:0a8622662f6d 186 emg2_filtered = lowp2.step(emg2_abs);
gastongab 2:0a8622662f6d 187 emg3_filtered = lowp3.step(emg3_abs);
gastongab 2:0a8622662f6d 188 emg4_filtered = lowp4.step(emg4_abs);
gastongab 7:88fa84742b3c 189 }
gastongab 7:88fa84742b3c 190
gastongab 7:88fa84742b3c 191 void CalibrationEMG()
gastongab 7:88fa84742b3c 192 {
gastongab 7:88fa84742b3c 193 //static float samples = calibration_time/ts;
gastongab 7:88fa84742b3c 194 while(timer_calibration<55) {
gastongab 7:88fa84742b3c 195 if(timer_calibration>0 && timer_calibration<10) {
gastongab 7:88fa84742b3c 196 led1=!led1;
gastongab 7:88fa84742b3c 197 if(emg1_filtered>temp_highest_emg1) {
gastongab 7:88fa84742b3c 198 temp_highest_emg1= emg1_filtered;
gastongab 7:88fa84742b3c 199 pc.printf("Temp1 = %f \r\n",temp_highest_emg1);
gastongab 7:88fa84742b3c 200 }
gastongab 7:88fa84742b3c 201 }
gastongab 7:88fa84742b3c 202 if(timer_calibration>10 && timer_calibration<15) {
gastongab 7:88fa84742b3c 203 led1=0;
gastongab 7:88fa84742b3c 204 led2=0;
gastongab 7:88fa84742b3c 205 led3=0;
gastongab 7:88fa84742b3c 206 }
gastongab 7:88fa84742b3c 207 if(timer_calibration>15 && timer_calibration<25) {
gastongab 7:88fa84742b3c 208 led2=!led2;
gastongab 7:88fa84742b3c 209 if(emg2_filtered>temp_highest_emg2) {
gastongab 7:88fa84742b3c 210 temp_highest_emg2= emg2_filtered;
gastongab 7:88fa84742b3c 211 pc.printf("Temp2 = %f \r\n",temp_highest_emg2);
gastongab 7:88fa84742b3c 212 }
gastongab 7:88fa84742b3c 213 }
gastongab 7:88fa84742b3c 214 if(timer_calibration>25 && timer_calibration<30) {
gastongab 7:88fa84742b3c 215 led1=0;
gastongab 7:88fa84742b3c 216 led2=0;
gastongab 7:88fa84742b3c 217 led3=0;
gastongab 7:88fa84742b3c 218 }
gastongab 7:88fa84742b3c 219 if(timer_calibration>30 && timer_calibration<40) {
gastongab 7:88fa84742b3c 220 led3=!led3;
gastongab 7:88fa84742b3c 221 if(emg3_filtered>temp_highest_emg3) {
gastongab 7:88fa84742b3c 222 temp_highest_emg3= emg3_filtered;
gastongab 7:88fa84742b3c 223 pc.printf("Temp3 = %f \r\n",temp_highest_emg3);
gastongab 7:88fa84742b3c 224 }
gastongab 7:88fa84742b3c 225 }
gastongab 7:88fa84742b3c 226 if(timer_calibration>40 && timer_calibration<45) {
gastongab 7:88fa84742b3c 227 led1=0;
gastongab 7:88fa84742b3c 228 led2=0;
gastongab 7:88fa84742b3c 229 led3=0;
gastongab 7:88fa84742b3c 230 }
gastongab 7:88fa84742b3c 231 if(timer_calibration>45 && timer_calibration<55) {
gastongab 7:88fa84742b3c 232 led2=!led2;
gastongab 7:88fa84742b3c 233 led3=!led3;
gastongab 7:88fa84742b3c 234 if(emg4_filtered>temp_highest_emg4) {
gastongab 7:88fa84742b3c 235 temp_highest_emg4= emg4_filtered;
gastongab 7:88fa84742b3c 236 pc.printf("Temp4 = %f \r\n",temp_highest_emg4);
gastongab 7:88fa84742b3c 237 }
gastongab 7:88fa84742b3c 238 }
gastongab 7:88fa84742b3c 239 led1=1;
gastongab 7:88fa84742b3c 240 led2=1;
gastongab 7:88fa84742b3c 241 led3=1;
gastongab 2:0a8622662f6d 242 }
cmaas 9:40c9a18c3430 243 /*
gastongab 2:0a8622662f6d 244 pc.printf("Highest value right biceps= %f \r\n", temp_highest_emg1);
gastongab 2:0a8622662f6d 245 pc.printf("Highest value right triceps= %f \r\n", temp_highest_emg2);
gastongab 2:0a8622662f6d 246 pc.printf("Highest value left biceps= %f \r\n", temp_highest_emg3);
gastongab 2:0a8622662f6d 247 pc.printf("Highest value left triceps= %f \r\n", temp_highest_emg4);
cmaas 9:40c9a18c3430 248 */
gastongab 7:88fa84742b3c 249
gastongab 6:f55ab7e38a7f 250 threshold1 = temp_highest_emg1*biceps_p_t; //Right Biceps
gastongab 6:f55ab7e38a7f 251 threshold2 = temp_highest_emg2*triceps_p_t; //Right Triceps
gastongab 6:f55ab7e38a7f 252 threshold3 = temp_highest_emg3*biceps_p_t; //Left Biceps
gastongab 7:88fa84742b3c 253 threshold4 = temp_highest_emg4*triceps_p_t; //Left Triceps
gastongab 2:0a8622662f6d 254 }
gastongab 2:0a8622662f6d 255
gastongab 4:c7be673eb4a1 256 //Check if emg_filtered has reached their threshold
gastongab 7:88fa84742b3c 257 void threshold_check()
gastongab 7:88fa84742b3c 258 {
gastongab 7:88fa84742b3c 259
gastongab 2:0a8622662f6d 260 //EMG1 threshold check
gastongab 7:88fa84742b3c 261 if(emg1_filtered>threshold1) {
gastongab 4:c7be673eb4a1 262 bicepsR = 1;
gastongab 7:88fa84742b3c 263 } else {
gastongab 4:c7be673eb4a1 264 bicepsR= 0;
gastongab 7:88fa84742b3c 265 }
gastongab 2:0a8622662f6d 266 //EMG2 threshold check
gastongab 7:88fa84742b3c 267 if(emg2_filtered>threshold2) {
gastongab 4:c7be673eb4a1 268 tricepsR = 1;
gastongab 7:88fa84742b3c 269 } else {
gastongab 4:c7be673eb4a1 270 tricepsR= 0;
gastongab 7:88fa84742b3c 271 }
gastongab 2:0a8622662f6d 272 //EMG3 threshold check
gastongab 7:88fa84742b3c 273 if(emg3_filtered>threshold3) {
gastongab 4:c7be673eb4a1 274 bicepsL = 1;
gastongab 7:88fa84742b3c 275 } else {
gastongab 4:c7be673eb4a1 276 bicepsL= 0;
gastongab 7:88fa84742b3c 277 }
gastongab 2:0a8622662f6d 278 //EMG4 threshold check
gastongab 7:88fa84742b3c 279 if(emg4_filtered>threshold4) {
gastongab 4:c7be673eb4a1 280 tricepsL = 1;
gastongab 7:88fa84742b3c 281 } else {
gastongab 4:c7be673eb4a1 282 tricepsL= 0;
gastongab 7:88fa84742b3c 283 }
gastongab 7:88fa84742b3c 284
gastongab 7:88fa84742b3c 285 /*
gastongab 4:c7be673eb4a1 286 pc.printf("Biceps Right = %i", bicepsR);
gastongab 7:88fa84742b3c 287 pc.printf("Triceps Right = %i",tricepsR);
gastongab 4:c7be673eb4a1 288 pc.printf("Biceps Left = %i", bicepsL);
gastongab 4:c7be673eb4a1 289 pc.printf("Triceps Left = %i", tricepsL);
gastongab 4:c7be673eb4a1 290 */
gastongab 2:0a8622662f6d 291 }
aschut 0:90750f158475 292
gastongab 4:c7be673eb4a1 293 //Activate ticker for Movement state, filtering and Threshold checking
gastongab 7:88fa84742b3c 294 void movement_ticker_activator()
gastongab 7:88fa84742b3c 295 {
gastongab 4:c7be673eb4a1 296 sample_ticker.attach(&emgsample, ts);
gastongab 4:c7be673eb4a1 297 threshold_check_ticker.attach(&threshold_check, ts);
gastongab 7:88fa84742b3c 298 }
gastongab 7:88fa84742b3c 299 void movement_ticker_deactivator()
gastongab 7:88fa84742b3c 300 {
gastongab 4:c7be673eb4a1 301 sample_ticker.detach();
gastongab 4:c7be673eb4a1 302 threshold_check_ticker.detach();
gastongab 7:88fa84742b3c 303 }
gastongab 7:88fa84742b3c 304
cmaas 9:40c9a18c3430 305 // ~~~~~~~~~~~~~~ROBOT KINEMATICS ~~~~~~~~~~~~~~~~~~
cmaas 9:40c9a18c3430 306
cmaas 9:40c9a18c3430 307 // functie x positie
cmaas 9:40c9a18c3430 308 float positionx(int EMGxplus,int EMGxmin)
cmaas 9:40c9a18c3430 309 {
cmaas 9:40c9a18c3430 310 float EMGx = EMGxplus - EMGxmin;
cmaas 9:40c9a18c3430 311
cmaas 9:40c9a18c3430 312 float verplaatsingx = EMGx * tijdstap * v;
cmaas 9:40c9a18c3430 313 float pxnieuw = px + verplaatsingx;
cmaas 9:40c9a18c3430 314 // x limit
cmaas 9:40c9a18c3430 315 if (pxnieuw <= upperxlim && pxnieuw >= lowerxlim) {
cmaas 9:40c9a18c3430 316 px = pxnieuw;
cmaas 9:40c9a18c3430 317 } else {
cmaas 9:40c9a18c3430 318 if (pxnieuw >= lowerxlim) {
cmaas 9:40c9a18c3430 319 px = upperxlim;
cmaas 9:40c9a18c3430 320 } else {
cmaas 9:40c9a18c3430 321 px = lowerxlim;
cmaas 9:40c9a18c3430 322 }
cmaas 9:40c9a18c3430 323 }
cmaas 9:40c9a18c3430 324 //printf("X eindpunt (%f) en verplaatsing: (%f)\n\r",px,verplaatsingx);
cmaas 9:40c9a18c3430 325 return px;
cmaas 9:40c9a18c3430 326 }
cmaas 9:40c9a18c3430 327
cmaas 9:40c9a18c3430 328
cmaas 9:40c9a18c3430 329 // functie y positie
cmaas 9:40c9a18c3430 330 float positiony(int EMGyplus,int EMGymin)
cmaas 9:40c9a18c3430 331 {
cmaas 9:40c9a18c3430 332 float EMGy = EMGyplus - EMGymin;
cmaas 9:40c9a18c3430 333
cmaas 9:40c9a18c3430 334 float verplaatsingy = EMGy * tijdstap * v;
cmaas 9:40c9a18c3430 335 float pynieuw = py + verplaatsingy;
cmaas 9:40c9a18c3430 336
cmaas 9:40c9a18c3430 337 // y limit
cmaas 9:40c9a18c3430 338 if (pynieuw <= upperylim && pynieuw >= lowerylim) {
cmaas 9:40c9a18c3430 339 py = pynieuw;
cmaas 9:40c9a18c3430 340 } else {
cmaas 9:40c9a18c3430 341 if (pynieuw >= lowerylim) {
cmaas 9:40c9a18c3430 342 py = upperylim;
cmaas 9:40c9a18c3430 343 } else {
cmaas 9:40c9a18c3430 344 py = lowerylim;
cmaas 9:40c9a18c3430 345 }
cmaas 9:40c9a18c3430 346 }
cmaas 9:40c9a18c3430 347 //printf("Y eindpunt (%f) en verplaatsing: (%f) \n\r",py,verplaatsingy);
cmaas 9:40c9a18c3430 348 return (py);
cmaas 9:40c9a18c3430 349 }
cmaas 9:40c9a18c3430 350
cmaas 9:40c9a18c3430 351
cmaas 9:40c9a18c3430 352 //~~~~~~~~~~~~CALCULATIING MOTOR ANGLES ~~~~~~~~
cmaas 9:40c9a18c3430 353 // arm 1 --> reference angle motor 1
cmaas 9:40c9a18c3430 354 float hoek1(float px, float py) // input: ref x, ref y
cmaas 9:40c9a18c3430 355 {
cmaas 9:40c9a18c3430 356 float c1x = px - rp * cos(thetap +(M_PI/6)); // x locatie hoekpunt end-effector
cmaas 9:40c9a18c3430 357 float c1y = py - rp*sin(thetap+(M_PI/6)); // y locatie hoekpunt end-effector
cmaas 9:40c9a18c3430 358 float alpha1 = atan2((c1y-a1y),(c1x-a1x)); // hoek tussen horizontaal en lijn van motor naar bijbehorende end-effector punt
cmaas 9:40c9a18c3430 359 float psi1 = acos(( pow(la,2)-pow(lp,2)+pow((c1x-a1x),2)+pow((c1y-a1y),2))/(2*la*sqrt(pow ((c1x-a1x),2)+pow((c1y-a1y),2) ))); //Hoek tussen lijn van motor naar bijbehorende end=effector punt en actieve arm
cmaas 9:40c9a18c3430 360 float a1 = alpha1 + psi1 - da1; //hoek tussen horizontaal en actieve arm
cmaas 9:40c9a18c3430 361 //printf("arm 1 = %f \n\r",a1);
cmaas 9:40c9a18c3430 362 return a1;
cmaas 9:40c9a18c3430 363 }
cmaas 9:40c9a18c3430 364
cmaas 9:40c9a18c3430 365 // arm 2 --> reference angle motor 2
cmaas 9:40c9a18c3430 366 float hoek2(float px, float py)
cmaas 9:40c9a18c3430 367 {
cmaas 9:40c9a18c3430 368 float c2x = px - rp * cos(thetap -(M_PI/2));
cmaas 9:40c9a18c3430 369 float c2y = py - rp*sin(thetap-(M_PI/2));
cmaas 9:40c9a18c3430 370 float alpha2 = atan2((c2y-a2y),(c2x-a2x));
cmaas 9:40c9a18c3430 371 float psi2 = acos(( pow(la,2)-pow(lp,2)+pow((c2x-a2x),2)+pow((c2y-a2y),2))/(2*la*sqrt(pow ((c2x-a2x),2)+pow((c2y-a2y),2) )));
cmaas 9:40c9a18c3430 372 float a2 = alpha2 + psi2 - da2;
cmaas 9:40c9a18c3430 373 //printf("arm 2 = %f \n\r",a2);
cmaas 9:40c9a18c3430 374 return a2;
cmaas 9:40c9a18c3430 375 }
cmaas 9:40c9a18c3430 376
cmaas 9:40c9a18c3430 377 //arm 3 --> reference angle motor 3
cmaas 9:40c9a18c3430 378 float hoek3(float px, float py)
cmaas 9:40c9a18c3430 379 {
cmaas 9:40c9a18c3430 380 float c3x = px - rp * cos(thetap +(5*M_PI/6));
cmaas 9:40c9a18c3430 381 float c3y = py - rp*sin(thetap+(5*M_PI/6));
cmaas 9:40c9a18c3430 382 float alpha3 = atan2((c3y-a3y),(c3x-a3x));
cmaas 9:40c9a18c3430 383 float psi3 = acos(( pow(la,2)-pow(lp,2)+pow((c3x-a3x),2)+pow((c3y-a3y),2))/(2*la*sqrt(pow ((c3x-a3x),2)+pow((c3y-a3y),2) )));
cmaas 9:40c9a18c3430 384 float a3 = alpha3 + psi3 - da3;
cmaas 9:40c9a18c3430 385 //printf("arm 3 = %f \n\r",a3);
cmaas 9:40c9a18c3430 386 return a3;
cmaas 9:40c9a18c3430 387 }
cmaas 9:40c9a18c3430 388
cmaas 9:40c9a18c3430 389 // ~~~~~~~~~~~~~~PID CONTROLLER~~~~~~~~~~~~~~~~~~
cmaas 9:40c9a18c3430 390
cmaas 9:40c9a18c3430 391 double PID_controller(double error)
cmaas 9:40c9a18c3430 392 {
cmaas 9:40c9a18c3430 393 static double error_integral = 0;
cmaas 9:40c9a18c3430 394 static double error_prev = error; // initialization with this value only done once!
cmaas 9:40c9a18c3430 395 static BiQuad LowPassFilter(0.0640, 0.1279, 0.0640, -1.1683, 0.4241);
cmaas 9:40c9a18c3430 396
cmaas 9:40c9a18c3430 397 // Proportional part:
cmaas 9:40c9a18c3430 398 double u_k = Kp * error;
cmaas 9:40c9a18c3430 399
cmaas 9:40c9a18c3430 400 // Integral part
cmaas 9:40c9a18c3430 401 error_integral = error_integral + error * Ts;
cmaas 9:40c9a18c3430 402 double u_i = Ki * error_integral;
cmaas 9:40c9a18c3430 403
cmaas 9:40c9a18c3430 404 // Derivative part
cmaas 9:40c9a18c3430 405 double error_derivative = (error - error_prev)/Ts;
cmaas 9:40c9a18c3430 406 double filtered_error_derivative = LowPassFilter.step(error_derivative);
cmaas 9:40c9a18c3430 407 double u_d = Kd * filtered_error_derivative;
cmaas 9:40c9a18c3430 408 error_prev = error;
cmaas 9:40c9a18c3430 409
cmaas 9:40c9a18c3430 410 // Sum all parts and return it
cmaas 9:40c9a18c3430 411 return u_k + u_i + u_d;
cmaas 9:40c9a18c3430 412 }
cmaas 9:40c9a18c3430 413
cmaas 9:40c9a18c3430 414
cmaas 9:40c9a18c3430 415 // DIRECTON AND SPEED CONTROL
cmaas 9:40c9a18c3430 416 void moter_control(double u)
cmaas 9:40c9a18c3430 417 {
cmaas 9:40c9a18c3430 418
cmaas 9:40c9a18c3430 419 directionpin1= u > 0.0f; //eithertrueor false
cmaas 9:40c9a18c3430 420 if (fabs(u)> 0.7f) {
cmaas 9:40c9a18c3430 421 u = 0.7f;
cmaas 9:40c9a18c3430 422 } else {
cmaas 9:40c9a18c3430 423 u= u;
cmaas 9:40c9a18c3430 424 }
cmaas 9:40c9a18c3430 425 pwmpin1= fabs(u); //pwmduty cycle canonlybepositive, floatingpoint absolute value
cmaas 9:40c9a18c3430 426 }
cmaas 9:40c9a18c3430 427
cmaas 9:40c9a18c3430 428 void moter2_control(double u)
cmaas 9:40c9a18c3430 429 {
cmaas 9:40c9a18c3430 430 directionpin2= u > 0.0f; //eithertrueor false
cmaas 9:40c9a18c3430 431 if (fabs(u)> 0.7f) {
cmaas 9:40c9a18c3430 432 u = 0.7f;
cmaas 9:40c9a18c3430 433 } else {
cmaas 9:40c9a18c3430 434 u= u;
cmaas 9:40c9a18c3430 435 }
cmaas 9:40c9a18c3430 436 pwmpin2= fabs(u); //pwmduty cycle canonlybepositive, floatingpoint absolute value
cmaas 9:40c9a18c3430 437 }
cmaas 9:40c9a18c3430 438
cmaas 9:40c9a18c3430 439 void moter3_control(double u)
cmaas 9:40c9a18c3430 440 {
cmaas 9:40c9a18c3430 441 directionpin3= u > 0.0f; //eithertrueor false
cmaas 9:40c9a18c3430 442 if (fabs(u)> 0.7f) {
cmaas 9:40c9a18c3430 443 u = 0.7f;
cmaas 9:40c9a18c3430 444 } else {
cmaas 9:40c9a18c3430 445 u= u;
cmaas 9:40c9a18c3430 446 }
cmaas 9:40c9a18c3430 447 pwmpin3 = fabs(u); //pwmduty cycle canonlybepositive, floatingpoint absolute value
cmaas 9:40c9a18c3430 448 }
cmaas 9:40c9a18c3430 449
cmaas 9:40c9a18c3430 450 // CONTROLLING THE MOTOR
cmaas 9:40c9a18c3430 451 void Motor_mover()
cmaas 9:40c9a18c3430 452 {
cmaas 9:40c9a18c3430 453 double motor_position = encoder1.getPulses(); //output in counts
cmaas 9:40c9a18c3430 454 double reference_rotation = hoek1(px, py);
cmaas 9:40c9a18c3430 455 double error = reference_rotation - motor_position*(2*PI)/8400;
cmaas 9:40c9a18c3430 456 double u = PID_controller(error);
cmaas 9:40c9a18c3430 457 moter_control(u);
cmaas 9:40c9a18c3430 458
cmaas 9:40c9a18c3430 459 double motor_position2 = encoder2.getPulses(); //output in counts
cmaas 9:40c9a18c3430 460 double reference_rotation2 = hoek2(px, py);
cmaas 9:40c9a18c3430 461 double error_2 = reference_rotation2 - motor_position2*(2*PI)/8400;
cmaas 9:40c9a18c3430 462 double u_2 = PID_controller(error_2);
cmaas 9:40c9a18c3430 463 moter2_control(u_2);
cmaas 9:40c9a18c3430 464
cmaas 9:40c9a18c3430 465 double motor_position3 = encoder3.getPulses(); //output in counts
cmaas 9:40c9a18c3430 466 double reference_rotation3 = hoek3(px, py);
cmaas 9:40c9a18c3430 467 double error_3 = reference_rotation3 - motor_position3*(2*PI)/8400;
cmaas 9:40c9a18c3430 468 double u_3 = PID_controller(error_3);
cmaas 9:40c9a18c3430 469 moter3_control(u_3);
cmaas 9:40c9a18c3430 470
cmaas 9:40c9a18c3430 471
cmaas 9:40c9a18c3430 472 }
cmaas 9:40c9a18c3430 473
cmaas 9:40c9a18c3430 474
cmaas 9:40c9a18c3430 475
cmaas 9:40c9a18c3430 476 //-------------------- STATE MACHINE --------------------------
gastongab 7:88fa84742b3c 477 enum states {MOTORS_OFF,CALIBRATION,HOMING,DEMO,MOVEMENT,CLICK};
gastongab 2:0a8622662f6d 478 states currentState = MOTORS_OFF; //Chosen startingposition for states
aschut 0:90750f158475 479 bool stateChanged = true; // Make sure the initialization of first state is executed
aschut 0:90750f158475 480
aschut 0:90750f158475 481 void ProcessStateMachine(void)
aschut 0:90750f158475 482 {
gastongab 7:88fa84742b3c 483 switch (currentState) {
gastongab 7:88fa84742b3c 484 case MOTORS_OFF:
gastongab 7:88fa84742b3c 485 // Actions
gastongab 7:88fa84742b3c 486 if (stateChanged) {
gastongab 7:88fa84742b3c 487 // state initialization: rood
gastongab 7:88fa84742b3c 488 led1 = 1;
gastongab 7:88fa84742b3c 489 led2 = 0;
gastongab 7:88fa84742b3c 490 led3 = 1;
gastongab 7:88fa84742b3c 491 wait (1);
gastongab 7:88fa84742b3c 492 stateChanged = false;
gastongab 7:88fa84742b3c 493 }
gastongab 7:88fa84742b3c 494
gastongab 7:88fa84742b3c 495 // State transition logic: Als button 1 word ingedrukt --> calibratie, anders motor uithouden
gastongab 7:88fa84742b3c 496 if (!button1) {
gastongab 7:88fa84742b3c 497 currentState = CALIBRATION;
gastongab 7:88fa84742b3c 498 stateChanged = true;
gastongab 7:88fa84742b3c 499 } else if (!button2) {
gastongab 7:88fa84742b3c 500 currentState = HOMING ;
gastongab 7:88fa84742b3c 501 stateChanged = true;
gastongab 7:88fa84742b3c 502 } else {
gastongab 7:88fa84742b3c 503 currentState = MOTORS_OFF;
gastongab 7:88fa84742b3c 504 stateChanged = true;
gastongab 7:88fa84742b3c 505 }
gastongab 7:88fa84742b3c 506
gastongab 7:88fa84742b3c 507 break;
gastongab 7:88fa84742b3c 508
gastongab 7:88fa84742b3c 509 case CALIBRATION:
gastongab 7:88fa84742b3c 510 // Actions
gastongab 7:88fa84742b3c 511 if (stateChanged) {
gastongab 7:88fa84742b3c 512 // state initialization: oranje
gastongab 7:88fa84742b3c 513 temp_highest_emg1 = 0; //highest detected value right biceps
gastongab 7:88fa84742b3c 514 temp_highest_emg2 = 0;
gastongab 7:88fa84742b3c 515 temp_highest_emg3 = 0;
gastongab 7:88fa84742b3c 516 temp_highest_emg4 = 0;
gastongab 7:88fa84742b3c 517
gastongab 7:88fa84742b3c 518 timer_calibration.reset();
gastongab 7:88fa84742b3c 519 timer_calibration.start();
gastongab 7:88fa84742b3c 520
gastongab 7:88fa84742b3c 521 sample_ticker.attach(&emgsample, ts);
gastongab 7:88fa84742b3c 522 CalibrationEMG();
gastongab 7:88fa84742b3c 523 sample_ticker.detach();
gastongab 7:88fa84742b3c 524 timer_calibration.stop();
aschut 0:90750f158475 525
gastongab 7:88fa84742b3c 526
gastongab 7:88fa84742b3c 527 stateChanged = false;
gastongab 7:88fa84742b3c 528 }
gastongab 7:88fa84742b3c 529
gastongab 7:88fa84742b3c 530 // State transition logic: automatisch terug naar motors off.
gastongab 7:88fa84742b3c 531
gastongab 7:88fa84742b3c 532 currentState = MOTORS_OFF;
gastongab 7:88fa84742b3c 533 stateChanged = true;
gastongab 7:88fa84742b3c 534 break;
gastongab 7:88fa84742b3c 535
gastongab 7:88fa84742b3c 536 case HOMING:
gastongab 7:88fa84742b3c 537 // Actions
gastongab 7:88fa84742b3c 538 if (stateChanged) {
gastongab 7:88fa84742b3c 539 // state initialization: green
gastongab 7:88fa84742b3c 540 t.reset();
gastongab 7:88fa84742b3c 541 t.start();
gastongab 7:88fa84742b3c 542 led1 = 0;
gastongab 7:88fa84742b3c 543 led2 = 1;
gastongab 7:88fa84742b3c 544 led3 = 1;
gastongab 7:88fa84742b3c 545 wait (1);
gastongab 7:88fa84742b3c 546
gastongab 7:88fa84742b3c 547 stateChanged = false;
gastongab 7:88fa84742b3c 548 }
gastongab 7:88fa84742b3c 549
gastongab 7:88fa84742b3c 550 // State transition logic: naar DEMO (button1), naar MOVEMENT(button2)
gastongab 7:88fa84742b3c 551 if (!button1) {
gastongab 7:88fa84742b3c 552 currentState = DEMO;
gastongab 7:88fa84742b3c 553 stateChanged = true;
gastongab 7:88fa84742b3c 554 } else if (!button2) {
gastongab 7:88fa84742b3c 555 currentState = MOVEMENT ;
gastongab 7:88fa84742b3c 556 stateChanged = true;
gastongab 7:88fa84742b3c 557 } else if (t>300) {
gastongab 7:88fa84742b3c 558 t.stop();
gastongab 7:88fa84742b3c 559 t.reset();
gastongab 7:88fa84742b3c 560 currentState = MOTORS_OFF ;
gastongab 7:88fa84742b3c 561 stateChanged = true;
gastongab 7:88fa84742b3c 562 } else {
gastongab 7:88fa84742b3c 563 currentState = HOMING ;
gastongab 7:88fa84742b3c 564 stateChanged = true;
gastongab 7:88fa84742b3c 565 }
gastongab 7:88fa84742b3c 566 break;
gastongab 7:88fa84742b3c 567
aschut 0:90750f158475 568 case DEMO:
gastongab 7:88fa84742b3c 569 // Actions
gastongab 7:88fa84742b3c 570 if (stateChanged) {
gastongab 7:88fa84742b3c 571 // state initialization: light blue
gastongab 7:88fa84742b3c 572 led1 = 0;
gastongab 7:88fa84742b3c 573 led2 = 1;
gastongab 7:88fa84742b3c 574 led3 = 0;
gastongab 7:88fa84742b3c 575 wait (1);
gastongab 7:88fa84742b3c 576
gastongab 7:88fa84742b3c 577 stateChanged = false;
gastongab 7:88fa84742b3c 578 }
gastongab 7:88fa84742b3c 579
gastongab 7:88fa84742b3c 580 // State transition logic: automatisch terug naar HOMING
gastongab 7:88fa84742b3c 581 currentState = HOMING;
gastongab 7:88fa84742b3c 582 stateChanged = true;
gastongab 7:88fa84742b3c 583 break;
gastongab 7:88fa84742b3c 584
gastongab 7:88fa84742b3c 585 case MOVEMENT:
gastongab 7:88fa84742b3c 586 // Actions
gastongab 7:88fa84742b3c 587 if (stateChanged) {
gastongab 7:88fa84742b3c 588 // state initialization: purple
gastongab 7:88fa84742b3c 589 //t.reset();
gastongab 7:88fa84742b3c 590 //t.start();
gastongab 7:88fa84742b3c 591
gastongab 7:88fa84742b3c 592 led1 = 1;
gastongab 7:88fa84742b3c 593 led2 = 0;
gastongab 7:88fa84742b3c 594 led3 = 0;
gastongab 7:88fa84742b3c 595 wait(2);
gastongab 7:88fa84742b3c 596
gastongab 7:88fa84742b3c 597 movement_ticker_activator();
gastongab 7:88fa84742b3c 598
gastongab 7:88fa84742b3c 599 led1 = 0;
gastongab 7:88fa84742b3c 600 led2 = 0;
gastongab 7:88fa84742b3c 601 led3 = 0;
gastongab 7:88fa84742b3c 602 wait(2);
gastongab 7:88fa84742b3c 603
gastongab 7:88fa84742b3c 604
gastongab 7:88fa84742b3c 605 stateChanged = false;
gastongab 7:88fa84742b3c 606 }
gastongab 7:88fa84742b3c 607
gastongab 7:88fa84742b3c 608 // State transition logic: naar CLICK (button1), naar MOTORS_OFF(button2) anders naar MOVEMENT
gastongab 7:88fa84742b3c 609 if (!button1) {
gastongab 7:88fa84742b3c 610 movement_ticker_deactivator();
gastongab 7:88fa84742b3c 611 currentState = CLICK;
gastongab 7:88fa84742b3c 612 stateChanged = true;
gastongab 7:88fa84742b3c 613 } else if (!button2) {
gastongab 7:88fa84742b3c 614 movement_ticker_deactivator();
gastongab 7:88fa84742b3c 615 currentState = MOTORS_OFF ;
gastongab 7:88fa84742b3c 616 stateChanged = true;
gastongab 7:88fa84742b3c 617 } else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0) { //this check if person is idle for more than 300seconds
gastongab 7:88fa84742b3c 618 t.start();
gastongab 7:88fa84742b3c 619 } else if (bicepsR==1 || tricepsR==1 || bicepsL==1 || tricepsL==1) {
gastongab 7:88fa84742b3c 620 t.stop();
gastongab 7:88fa84742b3c 621 t.reset();
gastongab 7:88fa84742b3c 622 }
gastongab 7:88fa84742b3c 623
gastongab 7:88fa84742b3c 624 if(t>20) {
gastongab 7:88fa84742b3c 625 movement_ticker_deactivator();
gastongab 7:88fa84742b3c 626 t.stop();
gastongab 7:88fa84742b3c 627 t.reset();
gastongab 7:88fa84742b3c 628 currentState = HOMING ;
gastongab 7:88fa84742b3c 629 stateChanged = true;
gastongab 7:88fa84742b3c 630 }
gastongab 7:88fa84742b3c 631 // here ends the idle checking mode
gastongab 7:88fa84742b3c 632 else {
gastongab 7:88fa84742b3c 633 //For every muscle a different colour if threshold is passed
gastongab 7:88fa84742b3c 634 if(bicepsR==1) {
gastongab 7:88fa84742b3c 635 led1 = 0;
gastongab 7:88fa84742b3c 636 led2 = 1;
gastongab 7:88fa84742b3c 637 led3 = 1;
gastongab 7:88fa84742b3c 638 } else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ) {
gastongab 7:88fa84742b3c 639 led1 = 1;
gastongab 7:88fa84742b3c 640 led2 = 1;
gastongab 7:88fa84742b3c 641 led3 = 1;
gastongab 7:88fa84742b3c 642 }
gastongab 7:88fa84742b3c 643 if(tricepsR==1) {
gastongab 7:88fa84742b3c 644 led1 = 1;
gastongab 7:88fa84742b3c 645 led2 = 0;
gastongab 7:88fa84742b3c 646 led3 = 1;
gastongab 7:88fa84742b3c 647 } else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ) {
gastongab 7:88fa84742b3c 648 led1 = 1;
gastongab 7:88fa84742b3c 649 led2 = 1;
gastongab 7:88fa84742b3c 650 led3 = 1;
gastongab 7:88fa84742b3c 651 }
gastongab 7:88fa84742b3c 652 if(bicepsL==1) {
gastongab 7:88fa84742b3c 653 led1 = 1;
gastongab 7:88fa84742b3c 654 led2 = 1;
gastongab 7:88fa84742b3c 655 led3 = 0;
gastongab 7:88fa84742b3c 656 } else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ) {
gastongab 7:88fa84742b3c 657 led1 = 1;
gastongab 7:88fa84742b3c 658 led2 = 1;
gastongab 7:88fa84742b3c 659 led3 = 1;
gastongab 7:88fa84742b3c 660 }
gastongab 7:88fa84742b3c 661 if(tricepsL==1) {
gastongab 7:88fa84742b3c 662 led1 = 1;
gastongab 7:88fa84742b3c 663 led2 = 0;
gastongab 7:88fa84742b3c 664 led3 = 0;
gastongab 7:88fa84742b3c 665 } else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ) {
gastongab 7:88fa84742b3c 666 led1 = 1;
gastongab 7:88fa84742b3c 667 led2 = 1;
gastongab 7:88fa84742b3c 668 led3 = 1;
gastongab 7:88fa84742b3c 669 }
gastongab 7:88fa84742b3c 670 }
gastongab 7:88fa84742b3c 671
gastongab 7:88fa84742b3c 672 break;
gastongab 7:88fa84742b3c 673
aschut 0:90750f158475 674 case CLICK:
gastongab 7:88fa84742b3c 675 // Actions
gastongab 7:88fa84742b3c 676 if (stateChanged) {
gastongab 7:88fa84742b3c 677 // state initialization: blue
gastongab 7:88fa84742b3c 678 led1 = 1;
gastongab 7:88fa84742b3c 679 led2 = 1;
gastongab 7:88fa84742b3c 680 led3 = 0;
gastongab 7:88fa84742b3c 681 wait (1);
aschut 0:90750f158475 682
gastongab 7:88fa84742b3c 683 stateChanged = false;
gastongab 7:88fa84742b3c 684 }
gastongab 7:88fa84742b3c 685
gastongab 7:88fa84742b3c 686 // State transition logic: automatisch terug naar MOVEMENT.
gastongab 7:88fa84742b3c 687
gastongab 7:88fa84742b3c 688 currentState = MOVEMENT;
gastongab 7:88fa84742b3c 689 stateChanged = true;
gastongab 7:88fa84742b3c 690 break;
gastongab 7:88fa84742b3c 691
gastongab 4:c7be673eb4a1 692 }
aschut 0:90750f158475 693 }
gastongab 7:88fa84742b3c 694
cmaas 9:40c9a18c3430 695 // --------------------------MAIN--------------------
cmaas 9:40c9a18c3430 696
cmaas 9:40c9a18c3430 697
aschut 0:90750f158475 698 int main()
aschut 0:90750f158475 699 {
gastongab 4:c7be673eb4a1 700 //BiQuad Chain add
gastongab 4:c7be673eb4a1 701 highp1.add( &highp1_1 ).add( &highp1_2 );
gastongab 4:c7be673eb4a1 702 notch1.add( &notch1_1 ).add( &notch1_2 );
gastongab 4:c7be673eb4a1 703 lowp1.add( &lowp1_1 ).add(&lowp1_2);
gastongab 7:88fa84742b3c 704
gastongab 4:c7be673eb4a1 705 highp2.add( &highp2_1 ).add( &highp2_2 );
gastongab 4:c7be673eb4a1 706 notch2.add( &notch2_1 ).add( &notch2_2 );
gastongab 7:88fa84742b3c 707 lowp2.add( &lowp2_1 ).add(&lowp2_2);
gastongab 7:88fa84742b3c 708
gastongab 4:c7be673eb4a1 709 highp3.add( &highp3_1 ).add( &highp3_2 );
gastongab 4:c7be673eb4a1 710 notch3.add( &notch3_1 ).add( &notch3_2 );
gastongab 4:c7be673eb4a1 711 lowp3.add( &lowp3_1 ).add(&lowp3_2);
gastongab 7:88fa84742b3c 712
gastongab 4:c7be673eb4a1 713 highp4.add( &highp4_1 ).add( &highp4_2 );
gastongab 4:c7be673eb4a1 714 notch4.add( &notch4_1 ).add( &notch4_2 );
gastongab 4:c7be673eb4a1 715 lowp4.add( &lowp4_1 ).add(&lowp4_2);
gastongab 7:88fa84742b3c 716
gastongab 2:0a8622662f6d 717 pc.baud(115200);
aschut 0:90750f158475 718 led1 = 1;
aschut 0:90750f158475 719 led2 = 1;
aschut 0:90750f158475 720 led3 = 1;
cmaas 9:40c9a18c3430 721
cmaas 9:40c9a18c3430 722 pwmpin1.period_us(60); // setup motor
cmaas 10:2325e545ce11 723 ref_rot.attach(Motor_mover, 0.001);// HAS TO GO TO STATE MACHINE
gastongab 7:88fa84742b3c 724
gastongab 7:88fa84742b3c 725 while (true) {
cmaas 10:2325e545ce11 726 //ProcessStateMachine();
cmaas 10:2325e545ce11 727
cmaas 10:2325e545ce11 728 if (button2 == false) {
cmaas 10:2325e545ce11 729 wait(0.01f);
cmaas 10:2325e545ce11 730
cmaas 10:2325e545ce11 731 // berekenen positie
cmaas 10:2325e545ce11 732 float px = positionx(1,0); // EMG: +x, -x
cmaas 10:2325e545ce11 733 float py = positiony(0,0); // EMG: +y, -y
cmaas 10:2325e545ce11 734 //printf("positie (%f,%f)\n\r",px,py);
cmaas 10:2325e545ce11 735 }
cmaas 10:2325e545ce11 736
cmaas 10:2325e545ce11 737 if (button1 == false) {
cmaas 10:2325e545ce11 738 wait(0.01f);
cmaas 10:2325e545ce11 739 // berekenen positie
cmaas 10:2325e545ce11 740 float px = positionx(0,1); // EMG: +x, -x
cmaas 10:2325e545ce11 741 float py = positiony(0,0); // EMG: +y, -y
cmaas 10:2325e545ce11 742 //printf("positie (%f,%f)\n\r",px,py);
cmaas 10:2325e545ce11 743 }
cmaas 10:2325e545ce11 744
cmaas 10:2325e545ce11 745 if (button3 == false) {
cmaas 10:2325e545ce11 746 wait(0.01f);
cmaas 10:2325e545ce11 747 // berekenen positie
cmaas 10:2325e545ce11 748 float px = positionx(0,0); // EMG: +x, -x
cmaas 10:2325e545ce11 749 float py = positiony(1,0); // EMG: +y, -y
cmaas 10:2325e545ce11 750 //printf("positie (%f,%f)\n\r",px,py);
cmaas 10:2325e545ce11 751 }
cmaas 10:2325e545ce11 752
cmaas 10:2325e545ce11 753 if (button4 == false) {
cmaas 10:2325e545ce11 754 wait(0.01f);
cmaas 10:2325e545ce11 755 // berekenen positie
cmaas 10:2325e545ce11 756 float px = positionx(0,0); // EMG: +x, -x
cmaas 10:2325e545ce11 757 float py = positiony(0,1); // EMG: +y, -y
cmaas 10:2325e545ce11 758 //printf("positie (%f,%f)\n\r",px,py);
cmaas 10:2325e545ce11 759 }
gastongab 7:88fa84742b3c 760
aschut 0:90750f158475 761 }
gastongab 7:88fa84742b3c 762
aschut 0:90750f158475 763 }
aschut 0:90750f158475 764
aschut 0:90750f158475 765
aschut 0:90750f158475 766
aschut 0:90750f158475 767
aschut 0:90750f158475 768
aschut 0:90750f158475 769