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:
- 26:9b43d9cb1fb2
- Parent:
- 25:ec41b972b250
- Child:
- 27:54167d54b0c5
- Child:
- 29:7523e4a8e000
File content as of revision 26:9b43d9cb1fb2:
#include "mbed.h"
#include "MODSERIAL.h"
#include "HIDScope.h"
#define TIMEB4NEXTCHOICE 2
#define TIMEBETWEENBLINK 20
//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()
{
pc.baud(115200); //baudrate instellen
emgfloat.period_ms(2); //sets period for the PWM to the emgfloat PTD4
log_timer.attach(looper, 0.001); // The looper() function will be called every 0.001 seconds (with the ticker object)
goto directionchoice; // goes to first while(1) for the deciding the direction
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<TIMEBETWEENBLINK; lag++) {
if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
direction = 1;
blue = 0;
green = 0;
red=1;
pc.printf("links ");
wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
goto forcechoice; // goes to second while(1) for the deciding the force
} else {
wait(0.1);
}
}
}
if(i==2) { //green
red =1;
green=0;
blue=1;
for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
direction = 2;
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<TIMEBETWEENBLINK; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
direction = 3;
blue = 1;
green = 0;
red=0;
pc.printf("rechts ");
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<TIMEBETWEENBLINK; lag++) {
if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
force = 1;
blue = 0;
green = 0;
red=1;
pc.printf("zwak ");
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<TIMEBETWEENBLINK; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
force = 2;
blue = 0;
green = 1;
red=0;
pc.printf("normaal ");
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<TIMEBETWEENBLINK; lag++) {
if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
force = 3;
blue = 1;
green = 0;
red=0;
pc.printf("sterk ");
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; // wit lampje
if(direction == 0 || force == 0) {
pc.printf("error");
}
if(direction == 1 && force == 1) { // links zwak
pc.printf("links zwak");
}
if(direction == 1 && force == 2) { // links normaal
pc.printf("links normaal");
}
if(direction == 1 && force == 3) { // links sterk
pc.printf("links sterk");
}
if(direction == 2 && force == 1) { // mid zwak
pc.printf("mid zwak");
}
if(direction == 2 && force == 2) { // mid normaal
pc.printf("mid normaal");
}
if(direction == 2 && force == 3) { // mid sterk
pc.printf("mid sterk");
}
if(direction == 3 && force == 1) { // rechts zwak
pc.printf("rechts zwak");
}
if(direction == 3 && force == 2) { // rechts normaal
pc.printf("rechts normaal");
}
if(direction == 3 && force == 3) { // rechts sterk
pc.printf("rechts sterk");
}
}