Poep Hoofd / Mbed 2 deprecated PoolRobot_Code

Dependencies:   HIDScope mbed MODSERIAL QEI

Committer:
Alex_Kyrl
Date:
Fri Oct 20 14:38:14 2017 +0000
Revision:
8:0b7925095416
Parent:
7:c17f5473f4e1
Child:
9:22d79a4a0324
change motor to be angle control ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alex_Kyrl 6:452e301a105a 1 #include "EMG.h"
Alex_Kyrl 6:452e301a105a 2 #include "Motor.h"
poephoofd 0:2d9dae739559 3 #include "HIDScope.h"
poephoofd 2:a08bff88216d 4 #include "MODSERIAL.h"
Alex_Kyrl 8:0b7925095416 5 #include "iostream"
Alex_Kyrl 8:0b7925095416 6 DigitalIn a(D3);
Alex_Kyrl 8:0b7925095416 7 DigitalIn b(D4);
Alex_Kyrl 8:0b7925095416 8 HIDScope scope(2); // 4 channels of data
poephoofd 0:2d9dae739559 9 Ticker MainTicker;
poephoofd 0:2d9dae739559 10 MODSERIAL pc(USBTX, USBRX);
poephoofd 0:2d9dae739559 11
Alex_Kyrl 8:0b7925095416 12 /*****************************************************************/
Alex_Kyrl 8:0b7925095416 13 //Initialize Analog EMG inputs:
Alex_Kyrl 8:0b7925095416 14
Alex_Kyrl 8:0b7925095416 15
Alex_Kyrl 8:0b7925095416 16 EMG EMG_bi_r(A0);// Move the endpoint to the right (plus direction)
Alex_Kyrl 8:0b7925095416 17 EMG EMG_bi_l(A1);// Move the endpoint to the left (minus direction)
Alex_Kyrl 8:0b7925095416 18 EMG EMG_tri_r(A2);// Move the endpoint forward (plus direction)
Alex_Kyrl 8:0b7925095416 19 EMG EMG_tri_l(A3);// Move the endpoint backward (minus direction)
Alex_Kyrl 8:0b7925095416 20
Alex_Kyrl 8:0b7925095416 21 /****************************************************/
Alex_Kyrl 8:0b7925095416 22
Alex_Kyrl 8:0b7925095416 23 Motor motor2;
poephoofd 3:9c5aea210f1d 24
poephoofd 0:2d9dae739559 25
Alex_Kyrl 8:0b7925095416 26 /*****************************************************/
Alex_Kyrl 8:0b7925095416 27 // Set control signals:
poephoofd 0:2d9dae739559 28
Alex_Kyrl 8:0b7925095416 29 double x_control_signal=0 , y_control_signal; //x direction is the righ/left movement
Alex_Kyrl 8:0b7925095416 30
Alex_Kyrl 8:0b7925095416 31
Alex_Kyrl 8:0b7925095416 32 double make_X_control_signal(){
Alex_Kyrl 8:0b7925095416 33 double emg=EMG_bi_r.filter()/10;
Alex_Kyrl 8:0b7925095416 34 double emg2=EMG_bi_l.filter()/10;
Alex_Kyrl 8:0b7925095416 35
Alex_Kyrl 8:0b7925095416 36 if(abs(emg-emg2)>=0.0025)
Alex_Kyrl 8:0b7925095416 37 {
Alex_Kyrl 8:0b7925095416 38 return x_control_signal+emg2-emg;
Alex_Kyrl 8:0b7925095416 39 }
Alex_Kyrl 8:0b7925095416 40 else
Alex_Kyrl 8:0b7925095416 41 return x_control_signal;
Alex_Kyrl 8:0b7925095416 42
Alex_Kyrl 8:0b7925095416 43 }
Alex_Kyrl 8:0b7925095416 44
Alex_Kyrl 8:0b7925095416 45 /******************************************************/
Alex_Kyrl 8:0b7925095416 46
Alex_Kyrl 8:0b7925095416 47 // Ticker Function:
poephoofd 0:2d9dae739559 48 void ReadAndFilterEMG()
poephoofd 0:2d9dae739559 49 {
Alex_Kyrl 8:0b7925095416 50 //motor2.set_angle();
Alex_Kyrl 8:0b7925095416 51 //x_control_signal= make_X_control_signal();
poephoofd 0:2d9dae739559 52
Alex_Kyrl 8:0b7925095416 53 motor2.Control_angle(x_control_signal);
Alex_Kyrl 8:0b7925095416 54 scope.set(0, x_control_signal);
Alex_Kyrl 8:0b7925095416 55 scope.set(1, motor2.set_angle());
poephoofd 0:2d9dae739559 56 scope.send();
Alex_Kyrl 8:0b7925095416 57
poephoofd 2:a08bff88216d 58 }
poephoofd 2:a08bff88216d 59
Alex_Kyrl 8:0b7925095416 60
Alex_Kyrl 8:0b7925095416 61 /***************************************************/
Alex_Kyrl 8:0b7925095416 62
Alex_Kyrl 8:0b7925095416 63 //Main Function:
poephoofd 2:a08bff88216d 64 int main(void)
poephoofd 2:a08bff88216d 65 {
Alex_Kyrl 8:0b7925095416 66
Alex_Kyrl 8:0b7925095416 67 double sample_time= 0.002; //fs = 500Hz
poephoofd 2:a08bff88216d 68 pc.baud(115200); //Set Baud rate for Serial communication
Alex_Kyrl 6:452e301a105a 69 MainTicker.attach(&ReadAndFilterEMG, sample_time); //Attach time based interrupt
poephoofd 0:2d9dae739559 70
Alex_Kyrl 8:0b7925095416 71
poephoofd 2:a08bff88216d 72 while(true)
poephoofd 2:a08bff88216d 73 {
Alex_Kyrl 8:0b7925095416 74 if(a==0){
Alex_Kyrl 8:0b7925095416 75 x_control_signal+=10;
Alex_Kyrl 8:0b7925095416 76 wait(0.1);
Alex_Kyrl 8:0b7925095416 77 }
Alex_Kyrl 8:0b7925095416 78 if(b==0){
Alex_Kyrl 8:0b7925095416 79 x_control_signal-=10;
Alex_Kyrl 8:0b7925095416 80 wait(0.1);
Alex_Kyrl 8:0b7925095416 81 }
poephoofd 2:a08bff88216d 82 }
Alex_Kyrl 8:0b7925095416 83
poephoofd 2:a08bff88216d 84
Alex_Kyrl 8:0b7925095416 85 //return 0;
poephoofd 0:2d9dae739559 86 }