Motor + potmerer + EMG + led

Dependencies:   HIDScope MODSERIAL mbed

Fork of EMG_Motor_LED by Michel Vos

Committer:
CasperK
Date:
Fri Sep 28 10:53:57 2018 +0000
Revision:
2:735ca8577f31
Parent:
1:aa505856416d
Child:
3:f00ddd66fe39
Changing motor directions with scaled potmeter values, switch doesn't work yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CasperK 0:9922b502cbc3 1 #include "mbed.h"
CasperK 0:9922b502cbc3 2 #include "MODSERIAL.h"
CasperK 0:9922b502cbc3 3 #include "HIDScope.h"
CasperK 0:9922b502cbc3 4
CasperK 0:9922b502cbc3 5 PwmOut pwmpin(D6);
CasperK 0:9922b502cbc3 6 PwmOut led(D10);
CasperK 0:9922b502cbc3 7 AnalogIn potmeter(A5);
CasperK 0:9922b502cbc3 8 DigitalIn button(D2);
CasperK 0:9922b502cbc3 9 DigitalOut directionpin(D5);
CasperK 0:9922b502cbc3 10
CasperK 0:9922b502cbc3 11 HIDScope scope(2);
CasperK 0:9922b502cbc3 12 Ticker ticker;
CasperK 2:735ca8577f31 13 enum states{forward, stop, backwards};
CasperK 2:735ca8577f31 14 states CurrentState;
CasperK 0:9922b502cbc3 15
CasperK 0:9922b502cbc3 16 volatile float x;
CasperK 0:9922b502cbc3 17 volatile float y;
CasperK 2:735ca8577f31 18 volatile float p = potmeter;
CasperK 0:9922b502cbc3 19
CasperK 0:9922b502cbc3 20 void sendData() {
CasperK 0:9922b502cbc3 21 scope.set(0,potmeter); //set the potmeter data to the first scope
CasperK 1:aa505856416d 22 scope.set(1,x);
CasperK 1:aa505856416d 23 scope.send(); //send the datapoints of the 2 motors
CasperK 0:9922b502cbc3 24 }
CasperK 0:9922b502cbc3 25
CasperK 2:735ca8577f31 26 int main() {
CasperK 2:735ca8577f31 27
CasperK 2:735ca8577f31 28 // float u = -0.3f; //determineusefulvalue, -0.3f is justanexample
CasperK 2:735ca8577f31 29 // directionpin = u > 0; //if bigger than 0, gives true, otherwise gives false
CasperK 1:aa505856416d 30 x = 1; //placeholder value for potmeter of second motor
CasperK 0:9922b502cbc3 31
CasperK 2:735ca8577f31 32 float scaled_potmeter = (p*2)-1; //scale potmeter from 0-1 to (-1 to 1)
CasperK 2:735ca8577f31 33 float c; //value to be used in negative scaled potmeter value
CasperK 2:735ca8577f31 34 bool forward = scaled_potmeter > 0;
CasperK 2:735ca8577f31 35 bool stop = scaled_potmeter == 0;
CasperK 2:735ca8577f31 36 bool backwards = scaled_potmeter < 0;
CasperK 2:735ca8577f31 37
CasperK 1:aa505856416d 38 pwmpin.period_us(60); //60 microseconds PWM period, 16.7 kHz
CasperK 1:aa505856416d 39 led.period_us(60); //60 microseconds
CasperK 1:aa505856416d 40 ticker.attach(&sendData, 0.005f); //send data to hidscope at 200Hz
CasperK 0:9922b502cbc3 41
CasperK 2:735ca8577f31 42 while (true) {
CasperK 2:735ca8577f31 43 switch (CurrentState){
CasperK 2:735ca8577f31 44 case forward:
CasperK 2:735ca8577f31 45 directionpin = true;
CasperK 2:735ca8577f31 46 pwmpin.write(scaled_potmeter); //pwm of motor is potmeter value
CasperK 2:735ca8577f31 47 led.write(scaled_potmeter); //led is potmeter value
CasperK 2:735ca8577f31 48 break;
CasperK 2:735ca8577f31 49 case stop:
CasperK 2:735ca8577f31 50 // do nothing
CasperK 2:735ca8577f31 51 break;
CasperK 2:735ca8577f31 52 case backwards:
CasperK 2:735ca8577f31 53 directionpin = false;
CasperK 2:735ca8577f31 54 c = scaled_potmeter*-1;
CasperK 2:735ca8577f31 55 pwmpin.write(c); //pwm of motor is potmeter value
CasperK 2:735ca8577f31 56 led.write(c); //led is potmeter value
CasperK 2:735ca8577f31 57 break;
CasperK 2:735ca8577f31 58 }
CasperK 2:735ca8577f31 59 wait(1.0f);
CasperK 0:9922b502cbc3 60 }
CasperK 2:735ca8577f31 61 }
CasperK 2:735ca8577f31 62
CasperK 2:735ca8577f31 63