Poep Hoofd / Mbed 2 deprecated PoolRobot_Code

Dependencies:   HIDScope mbed MODSERIAL QEI

Committer:
Alex_Kyrl
Date:
Fri Oct 27 11:55:22 2017 +0000
Revision:
9:22d79a4a0324
Parent:
8:0b7925095416
Child:
10:bb9a00d656c4
Child:
11:dd1976534a03
new inverse kinematics

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 9:22d79a4a0324 6 DigitalIn a(D3); //buttons for testing
Alex_Kyrl 9:22d79a4a0324 7 DigitalIn b(D2);
Alex_Kyrl 9:22d79a4a0324 8 double cont = 0 ;
Alex_Kyrl 9:22d79a4a0324 9
Alex_Kyrl 9:22d79a4a0324 10 HIDScope scope(4); // 4 channels of data
poephoofd 0:2d9dae739559 11 Ticker MainTicker;
poephoofd 0:2d9dae739559 12 MODSERIAL pc(USBTX, USBRX);
poephoofd 0:2d9dae739559 13
Alex_Kyrl 8:0b7925095416 14 /*****************************************************************/
Alex_Kyrl 8:0b7925095416 15 //Initialize Analog EMG inputs:
Alex_Kyrl 8:0b7925095416 16
Alex_Kyrl 9:22d79a4a0324 17
Alex_Kyrl 9:22d79a4a0324 18 EMG EMG_bi_r(A0); // Move the endpoint to the right (plus direction)
Alex_Kyrl 9:22d79a4a0324 19 EMG EMG_bi_l(A1); // Move the endpoint to the left (minus direction)
Alex_Kyrl 9:22d79a4a0324 20 EMG EMG_tri_r(A2); // Move the endpoint forward (plus direction)
Alex_Kyrl 9:22d79a4a0324 21 EMG EMG_tri_l(A3); // Move the endpoint backward (minus direction)
Alex_Kyrl 8:0b7925095416 22
Alex_Kyrl 8:0b7925095416 23 /****************************************************/
Alex_Kyrl 9:22d79a4a0324 24 //Initialise Motors:
Alex_Kyrl 8:0b7925095416 25
Alex_Kyrl 9:22d79a4a0324 26 Motor motor2(D13 , D12 , D7 , D6 , 50000 , 180 , 0.6 );
Alex_Kyrl 9:22d79a4a0324 27 Motor motor1(D11 , D10 , D4 , D5 , 50000 , 180 , 0.2 );
poephoofd 0:2d9dae739559 28
Alex_Kyrl 8:0b7925095416 29 /*****************************************************/
Alex_Kyrl 8:0b7925095416 30 // Set control signals:
poephoofd 0:2d9dae739559 31
Alex_Kyrl 9:22d79a4a0324 32 //x direction is the righ/left movement
Alex_Kyrl 9:22d79a4a0324 33 //y direction is forward/backward movement
Alex_Kyrl 9:22d79a4a0324 34
Alex_Kyrl 9:22d79a4a0324 35 double get_X_control_signal(){
Alex_Kyrl 9:22d79a4a0324 36 double emg_right = EMG_bi_r.filter();
Alex_Kyrl 9:22d79a4a0324 37 double emg_left = EMG_bi_l.filter();
Alex_Kyrl 9:22d79a4a0324 38 // TODO: Tune emg to velocity mapping
Alex_Kyrl 9:22d79a4a0324 39 return emg_right - emg_left;
Alex_Kyrl 9:22d79a4a0324 40
Alex_Kyrl 9:22d79a4a0324 41 }
Alex_Kyrl 8:0b7925095416 42
Alex_Kyrl 8:0b7925095416 43
Alex_Kyrl 9:22d79a4a0324 44 double get_Y_control_signal(){
Alex_Kyrl 9:22d79a4a0324 45 double emg_fwd= EMG_tri_r.filter();
Alex_Kyrl 9:22d79a4a0324 46 double emg_bwd= EMG_tri_l.filter();
Alex_Kyrl 9:22d79a4a0324 47 // TODO: `Tune emg to velocity mapping
Alex_Kyrl 9:22d79a4a0324 48 return cont;//emg_fwd - emg_bwd;
Alex_Kyrl 8:0b7925095416 49
Alex_Kyrl 8:0b7925095416 50 }
Alex_Kyrl 8:0b7925095416 51
Alex_Kyrl 8:0b7925095416 52 /******************************************************/
Alex_Kyrl 9:22d79a4a0324 53 //set speed of setpoints
Alex_Kyrl 9:22d79a4a0324 54 void control_motors()
Alex_Kyrl 9:22d79a4a0324 55 {
Alex_Kyrl 9:22d79a4a0324 56 int row_J =2 , row_Speed=2 , column_J =2;
Alex_Kyrl 9:22d79a4a0324 57 float speed_setpoint[row_J] , J_inv[row_J][column_J] , speed[row_Speed];
Alex_Kyrl 9:22d79a4a0324 58
Alex_Kyrl 9:22d79a4a0324 59 speed[0] = 0;//get_X_control_signal();
Alex_Kyrl 9:22d79a4a0324 60 speed[1] = get_Y_control_signal();
Alex_Kyrl 9:22d79a4a0324 61
Alex_Kyrl 9:22d79a4a0324 62 float theta_1 = 2*3.14*motor1.set_angle()/360 , theta_2 = 2*3.14*motor2.set_angle()/360;
Alex_Kyrl 9:22d79a4a0324 63 float L1 = 0.48;
Alex_Kyrl 9:22d79a4a0324 64 float L2 = 0.84;
Alex_Kyrl 9:22d79a4a0324 65
Alex_Kyrl 9:22d79a4a0324 66 J_inv[0][0] = -sin(theta_1 + theta_2)/(L1*cos(theta_2)) ;
Alex_Kyrl 9:22d79a4a0324 67 J_inv[0][1] = cos(theta_1 + theta_2)/(L1*cos(theta_2)) ;
Alex_Kyrl 9:22d79a4a0324 68 J_inv[1][0] = (L2*sin(theta_1 + theta_2) + L1*cos(theta_1))/(L1*L2*cos(theta_2)) ;
Alex_Kyrl 9:22d79a4a0324 69 J_inv[1][1] = -(L2*cos(theta_1 + theta_2) - L1*sin(theta_1))/(L1*L2*cos(theta_2)) ;
Alex_Kyrl 9:22d79a4a0324 70
Alex_Kyrl 9:22d79a4a0324 71 // Initializing elements of matrix mult to 0.
Alex_Kyrl 9:22d79a4a0324 72 for(int i = 0; i < row_J; ++i)
Alex_Kyrl 9:22d79a4a0324 73 {
Alex_Kyrl 9:22d79a4a0324 74 speed_setpoint[i] = 0;
Alex_Kyrl 9:22d79a4a0324 75 }
Alex_Kyrl 8:0b7925095416 76
Alex_Kyrl 9:22d79a4a0324 77 // Multiplying matrix firstMatrix and secondMatrix and storing in array mult.
Alex_Kyrl 9:22d79a4a0324 78 for(int i = 0; i < row_J; ++i)
Alex_Kyrl 9:22d79a4a0324 79 {
Alex_Kyrl 9:22d79a4a0324 80 for(int k=0; k<column_J; ++k)
Alex_Kyrl 9:22d79a4a0324 81 {
Alex_Kyrl 9:22d79a4a0324 82 speed_setpoint[i] += J_inv[i][k] * speed[k];
Alex_Kyrl 9:22d79a4a0324 83 }
Alex_Kyrl 9:22d79a4a0324 84 }
Alex_Kyrl 9:22d79a4a0324 85
Alex_Kyrl 9:22d79a4a0324 86 scope.set(0, theta_1*360/(2*3.14));
Alex_Kyrl 9:22d79a4a0324 87 scope.set(1, cont);
Alex_Kyrl 9:22d79a4a0324 88 scope.set(2, speed_setpoint[0]);
Alex_Kyrl 9:22d79a4a0324 89 scope.set(3, speed_setpoint[1]);
Alex_Kyrl 9:22d79a4a0324 90 float time = 0.002 ;
Alex_Kyrl 9:22d79a4a0324 91 motor1.Control_angle(theta_1*360/(2*3.14) + speed_setpoint[0]*time*360/(2*3.14));
Alex_Kyrl 9:22d79a4a0324 92 motor2.Control_angle(theta_2*360/(2*3.14) + speed_setpoint[1]*time*360/(2*3.14));
Alex_Kyrl 9:22d79a4a0324 93 }
Alex_Kyrl 9:22d79a4a0324 94
Alex_Kyrl 9:22d79a4a0324 95 /******************************************************/
Alex_Kyrl 8:0b7925095416 96 // Ticker Function:
Alex_Kyrl 9:22d79a4a0324 97
Alex_Kyrl 9:22d79a4a0324 98 void mainTicker()
poephoofd 0:2d9dae739559 99 {
Alex_Kyrl 9:22d79a4a0324 100
Alex_Kyrl 9:22d79a4a0324 101
Alex_Kyrl 9:22d79a4a0324 102 control_motors();
poephoofd 0:2d9dae739559 103
Alex_Kyrl 9:22d79a4a0324 104
Alex_Kyrl 9:22d79a4a0324 105
Alex_Kyrl 9:22d79a4a0324 106
Alex_Kyrl 9:22d79a4a0324 107
Alex_Kyrl 9:22d79a4a0324 108 // scope.set(0, x_control_signal);
Alex_Kyrl 9:22d79a4a0324 109 // scope.set(1, motor2.set_angle());
poephoofd 0:2d9dae739559 110 scope.send();
Alex_Kyrl 8:0b7925095416 111
poephoofd 2:a08bff88216d 112 }
poephoofd 2:a08bff88216d 113
Alex_Kyrl 8:0b7925095416 114
Alex_Kyrl 8:0b7925095416 115 /***************************************************/
Alex_Kyrl 9:22d79a4a0324 116 //Main Function:
Alex_Kyrl 8:0b7925095416 117
poephoofd 2:a08bff88216d 118 int main(void)
poephoofd 2:a08bff88216d 119 {
Alex_Kyrl 8:0b7925095416 120
Alex_Kyrl 9:22d79a4a0324 121 double sample_time= 0.002; //fs = 500Hz
Alex_Kyrl 9:22d79a4a0324 122 pc.baud(115200); //Set Baud rate for Serial communication
Alex_Kyrl 9:22d79a4a0324 123 MainTicker.attach(&mainTicker, sample_time); //Attach time based interrupt
poephoofd 0:2d9dae739559 124
Alex_Kyrl 9:22d79a4a0324 125
poephoofd 2:a08bff88216d 126 while(true)
poephoofd 2:a08bff88216d 127 {
Alex_Kyrl 8:0b7925095416 128 if(a==0){
Alex_Kyrl 9:22d79a4a0324 129 cont+=0.1;
Alex_Kyrl 8:0b7925095416 130 wait(0.1);
Alex_Kyrl 8:0b7925095416 131 }
Alex_Kyrl 8:0b7925095416 132 if(b==0){
Alex_Kyrl 9:22d79a4a0324 133 cont-=0.1;
Alex_Kyrl 8:0b7925095416 134 wait(0.1);
Alex_Kyrl 8:0b7925095416 135 }
poephoofd 2:a08bff88216d 136 }
Alex_Kyrl 8:0b7925095416 137
poephoofd 2:a08bff88216d 138
Alex_Kyrl 8:0b7925095416 139 //return 0;
poephoofd 0:2d9dae739559 140 }