Aukie Hooglugt / Mbed 2 deprecated EMG_Project

Dependencies:   HIDScope MODSERIAL- mbed-dsp mbed

Committer:
Hooglugt
Date:
Fri Oct 03 12:40:15 2014 +0000
Revision:
26:9b43d9cb1fb2
Parent:
25:ec41b972b250
Child:
27:54167d54b0c5
Child:
29:7523e4a8e000
script iets netter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hooglugt 23:8d9a623dd713 1 #include "mbed.h"
Hooglugt 23:8d9a623dd713 2 #include "MODSERIAL.h"
Hooglugt 23:8d9a623dd713 3 #include "HIDScope.h"
Hooglugt 24:c6073b9efd5b 4 #define TIMEB4NEXTCHOICE 2
Hooglugt 26:9b43d9cb1fb2 5 #define TIMEBETWEENBLINK 20
Hooglugt 23:8d9a623dd713 6
Hooglugt 23:8d9a623dd713 7 //Define objects
Hooglugt 23:8d9a623dd713 8 AnalogIn emg0(PTB1); //Analog input
Hooglugt 23:8d9a623dd713 9 PwmOut emgfloat(PTD4);//Float voor EMG-waarde
Hooglugt 23:8d9a623dd713 10 PwmOut red(LED_RED); //PWM output
Hooglugt 23:8d9a623dd713 11 PwmOut green(LED_GREEN);
Hooglugt 23:8d9a623dd713 12 PwmOut blue(LED_BLUE);
Hooglugt 23:8d9a623dd713 13 int direction = 0;
Hooglugt 23:8d9a623dd713 14 int force = 0;
Hooglugt 23:8d9a623dd713 15
Hooglugt 23:8d9a623dd713 16 Ticker log_timer;
Hooglugt 23:8d9a623dd713 17 MODSERIAL pc(USBTX,USBRX);
Hooglugt 23:8d9a623dd713 18 HIDScope scope(2);
Hooglugt 23:8d9a623dd713 19
Hooglugt 23:8d9a623dd713 20 /** Looper function
Hooglugt 23:8d9a623dd713 21 * functions used for Ticker and Timeout should be of type void <name>(void)
Hooglugt 23:8d9a623dd713 22 * i.e. no input arguments, no output arguments.
Hooglugt 23:8d9a623dd713 23 * if you want to change a variable that you use in other places (for example in main)
Hooglugt 23:8d9a623dd713 24 * you will have to make that variable global in order to be able to reach it both from
Hooglugt 23:8d9a623dd713 25 * the function called at interrupt time, and in the main function.
Hooglugt 23:8d9a623dd713 26 * To make a variable global, define it under the includes.
Hooglugt 23:8d9a623dd713 27 * variables that are changed in the interrupt routine (written to) should be made
Hooglugt 23:8d9a623dd713 28 * 'volatile' to let the compiler know that those values may change outside the current context.
Hooglugt 23:8d9a623dd713 29 * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value"
Hooglugt 23:8d9a623dd713 30 * in the example below, the variable is not re-used in the main function, and is thus declared
Hooglugt 23:8d9a623dd713 31 * local in the looper function only.
Hooglugt 23:8d9a623dd713 32 **/
Hooglugt 23:8d9a623dd713 33 void looper()
Hooglugt 23:8d9a623dd713 34 {
Hooglugt 23:8d9a623dd713 35 /*variable to store value in*/
Hooglugt 23:8d9a623dd713 36 uint16_t emg_value;
Hooglugt 23:8d9a623dd713 37 /*put raw emg value both in emgfloat and in emg_value*/
Hooglugt 23:8d9a623dd713 38 emgfloat.write(emg0.read()); // read float value (0..1 = 0..3.3V)
Hooglugt 23:8d9a623dd713 39 emg_value = emg0.read_u16(); // read direct ADC result (0..4096 = 0..3.3V)
Hooglugt 23:8d9a623dd713 40 /*send value to PC. Line below is used to prevent buffer overrun */
Hooglugt 23:8d9a623dd713 41 if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30)
Hooglugt 24:c6073b9efd5b 42 //pc.printf("%u\n",emg_value);
Hooglugt 25:ec41b972b250 43 scope.set(0,emg_value);
Hooglugt 23:8d9a623dd713 44 scope.set(1,emgfloat.read());
Hooglugt 23:8d9a623dd713 45 scope.send();
Hooglugt 23:8d9a623dd713 46 /**When not using the LED, the above could also have been done this way:
Hooglugt 23:8d9a623dd713 47 * pc.printf("%u\n", emg0.read_u16());
Hooglugt 23:8d9a623dd713 48 */
Hooglugt 23:8d9a623dd713 49 }
Hooglugt 23:8d9a623dd713 50
Hooglugt 23:8d9a623dd713 51
Hooglugt 23:8d9a623dd713 52
Hooglugt 23:8d9a623dd713 53 int main()
Hooglugt 23:8d9a623dd713 54 {
Hooglugt 25:ec41b972b250 55 pc.baud(115200); //baudrate instellen
Hooglugt 25:ec41b972b250 56 emgfloat.period_ms(2); //sets period for the PWM to the emgfloat PTD4
Hooglugt 25:ec41b972b250 57 log_timer.attach(looper, 0.001); // The looper() function will be called every 0.001 seconds (with the ticker object)
Hooglugt 26:9b43d9cb1fb2 58
Hooglugt 25:ec41b972b250 59 goto directionchoice; // goes to first while(1) for the deciding the direction
Hooglugt 26:9b43d9cb1fb2 60
Hooglugt 23:8d9a623dd713 61 while(1) { //Loop keuze DIRECTION
Hooglugt 25:ec41b972b250 62 directionchoice:
Hooglugt 23:8d9a623dd713 63 for(int i=1; i<4; i++) {
Hooglugt 23:8d9a623dd713 64 if(i==1) { //red
Hooglugt 23:8d9a623dd713 65 red=0;
Hooglugt 23:8d9a623dd713 66 green=1;
Hooglugt 23:8d9a623dd713 67 blue=1;
Hooglugt 26:9b43d9cb1fb2 68 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 25:ec41b972b250 69 if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 25:ec41b972b250 70 direction = 1;
Hooglugt 24:c6073b9efd5b 71 blue = 0;
Hooglugt 24:c6073b9efd5b 72 green = 0;
Hooglugt 24:c6073b9efd5b 73 red=1;
Hooglugt 26:9b43d9cb1fb2 74 pc.printf("links ");
Hooglugt 25:ec41b972b250 75 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
Hooglugt 25:ec41b972b250 76 goto forcechoice; // goes to second while(1) for the deciding the force
Hooglugt 23:8d9a623dd713 77 } else {
Hooglugt 23:8d9a623dd713 78 wait(0.1);
Hooglugt 23:8d9a623dd713 79 }
Hooglugt 23:8d9a623dd713 80 }
Hooglugt 23:8d9a623dd713 81 }
Hooglugt 23:8d9a623dd713 82 if(i==2) { //green
Hooglugt 23:8d9a623dd713 83 red =1;
Hooglugt 23:8d9a623dd713 84 green=0;
Hooglugt 23:8d9a623dd713 85 blue=1;
Hooglugt 26:9b43d9cb1fb2 86 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 23:8d9a623dd713 87 if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 25:ec41b972b250 88 direction = 2;
Hooglugt 24:c6073b9efd5b 89 blue = 0;
Hooglugt 24:c6073b9efd5b 90 green = 1;
Hooglugt 24:c6073b9efd5b 91 red=0;
Hooglugt 26:9b43d9cb1fb2 92 pc.printf("mid ");
Hooglugt 25:ec41b972b250 93 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
Hooglugt 23:8d9a623dd713 94 goto forcechoice;
Hooglugt 23:8d9a623dd713 95 } else {
Hooglugt 23:8d9a623dd713 96 wait(0.1);
Hooglugt 23:8d9a623dd713 97 }
Hooglugt 23:8d9a623dd713 98 }
Hooglugt 23:8d9a623dd713 99 }
Hooglugt 23:8d9a623dd713 100 if(i==3) { //blue
Hooglugt 23:8d9a623dd713 101 red=1;
Hooglugt 23:8d9a623dd713 102 green=1;
Hooglugt 23:8d9a623dd713 103 blue=0;
Hooglugt 26:9b43d9cb1fb2 104 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 23:8d9a623dd713 105 if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 23:8d9a623dd713 106 direction = 3;
Hooglugt 24:c6073b9efd5b 107 blue = 1;
Hooglugt 24:c6073b9efd5b 108 green = 0;
Hooglugt 24:c6073b9efd5b 109 red=0;
Hooglugt 26:9b43d9cb1fb2 110 pc.printf("rechts ");
Hooglugt 25:ec41b972b250 111 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
Hooglugt 23:8d9a623dd713 112 goto forcechoice;
Hooglugt 23:8d9a623dd713 113 } else {
Hooglugt 23:8d9a623dd713 114 wait(0.1);
Hooglugt 23:8d9a623dd713 115 }
Hooglugt 23:8d9a623dd713 116 }
Hooglugt 23:8d9a623dd713 117 }
Hooglugt 25:ec41b972b250 118 }
Hooglugt 23:8d9a623dd713 119 }
Hooglugt 23:8d9a623dd713 120 while(1) { //Loop keuze FORCE
Hooglugt 25:ec41b972b250 121 forcechoice:
Hooglugt 23:8d9a623dd713 122 for(int i=1; i<4; i++) {
Hooglugt 23:8d9a623dd713 123 if(i==1) { //red
Hooglugt 23:8d9a623dd713 124 red=0;
Hooglugt 23:8d9a623dd713 125 green=1;
Hooglugt 23:8d9a623dd713 126 blue=1;
Hooglugt 26:9b43d9cb1fb2 127 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 23:8d9a623dd713 128 if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 23:8d9a623dd713 129 force = 1;
Hooglugt 24:c6073b9efd5b 130 blue = 0;
Hooglugt 24:c6073b9efd5b 131 green = 0;
Hooglugt 24:c6073b9efd5b 132 red=1;
Hooglugt 26:9b43d9cb1fb2 133 pc.printf("zwak ");
Hooglugt 25:ec41b972b250 134 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
Hooglugt 23:8d9a623dd713 135 goto choicesmade;
Hooglugt 23:8d9a623dd713 136 } else {
Hooglugt 23:8d9a623dd713 137 wait(0.1);
Hooglugt 23:8d9a623dd713 138 }
Hooglugt 23:8d9a623dd713 139 }
Hooglugt 23:8d9a623dd713 140 }
Hooglugt 23:8d9a623dd713 141 if(i==2) { //green
Hooglugt 23:8d9a623dd713 142 red =1;
Hooglugt 23:8d9a623dd713 143 green=0;
Hooglugt 23:8d9a623dd713 144 blue=1;
Hooglugt 26:9b43d9cb1fb2 145 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 23:8d9a623dd713 146 if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 23:8d9a623dd713 147 force = 2;
Hooglugt 24:c6073b9efd5b 148 blue = 0;
Hooglugt 24:c6073b9efd5b 149 green = 1;
Hooglugt 24:c6073b9efd5b 150 red=0;
Hooglugt 26:9b43d9cb1fb2 151 pc.printf("normaal ");
Hooglugt 25:ec41b972b250 152 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
Hooglugt 23:8d9a623dd713 153 goto choicesmade;
Hooglugt 23:8d9a623dd713 154 } else {
Hooglugt 23:8d9a623dd713 155 wait(0.1);
Hooglugt 23:8d9a623dd713 156 }
Hooglugt 23:8d9a623dd713 157 }
Hooglugt 23:8d9a623dd713 158 }
Hooglugt 23:8d9a623dd713 159 if(i==3) { //blue
Hooglugt 23:8d9a623dd713 160 red=1;
Hooglugt 23:8d9a623dd713 161 green=1;
Hooglugt 23:8d9a623dd713 162 blue=0;
Hooglugt 26:9b43d9cb1fb2 163 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 23:8d9a623dd713 164 if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 23:8d9a623dd713 165 force = 3;
Hooglugt 24:c6073b9efd5b 166 blue = 1;
Hooglugt 24:c6073b9efd5b 167 green = 0;
Hooglugt 24:c6073b9efd5b 168 red=0;
Hooglugt 26:9b43d9cb1fb2 169 pc.printf("sterk ");
Hooglugt 25:ec41b972b250 170 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
Hooglugt 23:8d9a623dd713 171 goto choicesmade;
Hooglugt 23:8d9a623dd713 172 } else {
Hooglugt 23:8d9a623dd713 173 wait(0.1);
Hooglugt 23:8d9a623dd713 174 }
Hooglugt 23:8d9a623dd713 175 }
Hooglugt 23:8d9a623dd713 176 }
Hooglugt 23:8d9a623dd713 177 }
Hooglugt 23:8d9a623dd713 178 }
Hooglugt 25:ec41b972b250 179 choicesmade:
Hooglugt 25:ec41b972b250 180
Hooglugt 26:9b43d9cb1fb2 181 red = 0;
Hooglugt 26:9b43d9cb1fb2 182 green = 0;
Hooglugt 26:9b43d9cb1fb2 183 blue = 0; // wit lampje
Hooglugt 26:9b43d9cb1fb2 184
Hooglugt 25:ec41b972b250 185 if(direction == 0 || force == 0) {
Hooglugt 25:ec41b972b250 186 pc.printf("error");
Hooglugt 25:ec41b972b250 187 }
Hooglugt 25:ec41b972b250 188 if(direction == 1 && force == 1) { // links zwak
Hooglugt 25:ec41b972b250 189 pc.printf("links zwak");
Hooglugt 25:ec41b972b250 190 }
Hooglugt 25:ec41b972b250 191 if(direction == 1 && force == 2) { // links normaal
Hooglugt 25:ec41b972b250 192 pc.printf("links normaal");
Hooglugt 25:ec41b972b250 193 }
Hooglugt 25:ec41b972b250 194 if(direction == 1 && force == 3) { // links sterk
Hooglugt 25:ec41b972b250 195 pc.printf("links sterk");
Hooglugt 25:ec41b972b250 196 }
Hooglugt 25:ec41b972b250 197 if(direction == 2 && force == 1) { // mid zwak
Hooglugt 25:ec41b972b250 198 pc.printf("mid zwak");
Hooglugt 25:ec41b972b250 199 }
Hooglugt 25:ec41b972b250 200 if(direction == 2 && force == 2) { // mid normaal
Hooglugt 25:ec41b972b250 201 pc.printf("mid normaal");
Hooglugt 25:ec41b972b250 202 }
Hooglugt 25:ec41b972b250 203 if(direction == 2 && force == 3) { // mid sterk
Hooglugt 25:ec41b972b250 204 pc.printf("mid sterk");
Hooglugt 25:ec41b972b250 205 }
Hooglugt 25:ec41b972b250 206 if(direction == 3 && force == 1) { // rechts zwak
Hooglugt 25:ec41b972b250 207 pc.printf("rechts zwak");
Hooglugt 25:ec41b972b250 208 }
Hooglugt 25:ec41b972b250 209 if(direction == 3 && force == 2) { // rechts normaal
Hooglugt 25:ec41b972b250 210 pc.printf("rechts normaal");
Hooglugt 25:ec41b972b250 211 }
Hooglugt 25:ec41b972b250 212 if(direction == 3 && force == 3) { // rechts sterk
Hooglugt 25:ec41b972b250 213 pc.printf("rechts sterk");
Hooglugt 25:ec41b972b250 214 }
Hooglugt 25:ec41b972b250 215 }