Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL- mbed-dsp mbed
Project_main.cpp
- Committer:
- Hooglugt
- Date:
- 2014-10-03
- Revision:
- 24:c6073b9efd5b
- Parent:
- 23:8d9a623dd713
- Child:
- 25:ec41b972b250
File content as of revision 24:c6073b9efd5b:
#include "mbed.h"
#include "MODSERIAL.h"
#include "HIDScope.h"
#define TIMEB4NEXTCHOICE 2
//Define objects
AnalogIn emg0(PTB1); //Analog input
PwmOut emgfloat(PTD4);//Float voor EMG-waarde
PwmOut red(LED_RED); //PWM output
PwmOut green(LED_GREEN);
PwmOut blue(LED_BLUE);
int direction = 0;
int force = 0;
Ticker log_timer;
MODSERIAL pc(USBTX,USBRX);
HIDScope scope(2);
/** Looper function
* functions used for Ticker and Timeout should be of type void <name>(void)
* i.e. no input arguments, no output arguments.
* if you want to change a variable that you use in other places (for example in main)
* you will have to make that variable global in order to be able to reach it both from
* the function called at interrupt time, and in the main function.
* To make a variable global, define it under the includes.
* variables that are changed in the interrupt routine (written to) should be made
* 'volatile' to let the compiler know that those values may change outside the current context.
* i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value"
* in the example below, the variable is not re-used in the main function, and is thus declared
* local in the looper function only.
**/
void looper()
{
/*variable to store value in*/
uint16_t emg_value;
/*put raw emg value both in emgfloat and in emg_value*/
emgfloat.write(emg0.read()); // read float value (0..1 = 0..3.3V)
emg_value = emg0.read_u16(); // read direct ADC result (0..4096 = 0..3.3V)
/*send value to PC. Line below is used to prevent buffer overrun */
if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30)
//pc.printf("%u\n",emg_value);
scope.set(0,emg_value);
scope.set(1,emgfloat.read());
scope.send();
/**When not using the LED, the above could also have been done this way:
* pc.printf("%u\n", emg0.read_u16());
*/
}
int main()
{
/*setup baudrate. Choose the same in your program on PC side*/
pc.baud(115200);
/*set the period for the PWM to the emgfloat PTD4*/
emgfloat.period_ms(2);
/**Here you attach the 'void looper(void)' function to the Ticker object
* The looper() function will be called every 0.001 seconds.
* Please mind that the parentheses after looper are omitted when using attach.
*/
log_timer.attach(looper, 0.001);
enum Direction {LEFT, MID, RIGHT,UNSET};
Direction direc = UNSET;
enum Force {WEAK, NORMAL, STRONG,UNSET};
Force force = UNSET;
goto directionchoice;
while(1) { //Loop keuze DIRECTION
directionchoice:
for(int i=1; i<4; i++) {
if(i==1) { //red
red=0;
green=1;
blue=1;
for (int lag=0; lag<20; lag++) {
if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
direc = LEFT;
blue = 0;
green = 0;
red=1;
pc.printf("LEFT");
wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
goto forcechoice;
} else {
wait(0.1);
}
}
}
if(i==2) { //green
red =1;
green=0;
blue=1;
for (int lag=0; lag<20; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
direction = MID;
blue = 0;
green = 1;
red=0;
pc.printf("MID");
wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
goto forcechoice;
} else {
wait(0.1);
}
}
}
if(i==3) { //blue
red=1;
green=1;
blue=0;
for (int lag=0; lag<20; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
direction = 3;
blue = 1;
green = 0;
red=0;
pc.printf("C");
wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
goto forcechoice;
} else {
wait(0.1);
}
}
}
}
}
while(1) { //Loop keuze FORCE
forcechoice:
for(int i=1; i<4; i++) {
if(i==1) { //red
red=0;
green=1;
blue=1;
for (int lag=0; lag<20; lag++) {
if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
force = 1;
blue = 0;
green = 0;
red=1;
pc.printf("D");
wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
goto choicesmade;
} else {
wait(0.1);
}
}
}
if(i==2) { //green
red =1;
green=0;
blue=1;
for (int lag=0; lag<20; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
force = 2;
blue = 0;
green = 1;
red=0;
pc.printf("E");
wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
goto choicesmade;
} else {
wait(0.1);
}
}
}
if(i==3) { //blue
red=1;
green=1;
blue=0;
for (int lag=0; lag<20; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
force = 3;
blue = 1;
green = 0;
red=0;
pc.printf("F");
wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
goto choicesmade;
} else {
wait(0.1);
}
}
}
}
}
choicesmade:
red = 0;
green = 0;
blue = 0;
}