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 mbed MODSERIAL QEI
main.cpp@11:dd1976534a03, 2017-10-30 (annotated)
- Committer:
- Alex_Kyrl
- Date:
- Mon Oct 30 15:32:27 2017 +0000
- Revision:
- 11:dd1976534a03
- Parent:
- 9:22d79a4a0324
- Child:
- 12:69a9cf74583e
Testing motor PWM min
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:dd1976534a03 | 10 | HIDScope scope(6); // 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 | 11:dd1976534a03 | 26 | Motor motor2(D13 , D12 , D7 , D6 , 50000 , 180 , 0.5 ); |
Alex_Kyrl | 11:dd1976534a03 | 27 | Motor motor1(D11 , D10 , D4 , D5 , 50000 , 180 , 0.5 ); |
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 | 11:dd1976534a03 | 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 | 11:dd1976534a03 | 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 | 11:dd1976534a03 | 85 | float time = 0.002 ; |
Alex_Kyrl | 9:22d79a4a0324 | 86 | |
Alex_Kyrl | 9:22d79a4a0324 | 87 | scope.set(0, theta_1*360/(2*3.14)); |
Alex_Kyrl | 9:22d79a4a0324 | 88 | scope.set(1, cont); |
Alex_Kyrl | 11:dd1976534a03 | 89 | scope.set(2, theta_1*360/(2*3.14) + speed_setpoint[0]*time*360/(2*3.14)); |
Alex_Kyrl | 11:dd1976534a03 | 90 | scope.set(3, theta_2*360/(2*3.14) + speed_setpoint[1]*time*360/(2*3.14)); |
Alex_Kyrl | 11:dd1976534a03 | 91 | |
Alex_Kyrl | 11:dd1976534a03 | 92 | |
Alex_Kyrl | 11:dd1976534a03 | 93 | |
Alex_Kyrl | 11:dd1976534a03 | 94 | scope.set(4, motor1.Control_angle(theta_1*360/(2*3.14) + speed_setpoint[0]*time*360/(2*3.14)) ); |
Alex_Kyrl | 11:dd1976534a03 | 95 | scope.set(5, motor2.Control_angle(theta_2*360/(2*3.14) + speed_setpoint[1]*time*360/(2*3.14)) ); |
Alex_Kyrl | 11:dd1976534a03 | 96 | |
Alex_Kyrl | 9:22d79a4a0324 | 97 | } |
Alex_Kyrl | 9:22d79a4a0324 | 98 | |
Alex_Kyrl | 9:22d79a4a0324 | 99 | /******************************************************/ |
Alex_Kyrl | 8:0b7925095416 | 100 | // Ticker Function: |
Alex_Kyrl | 9:22d79a4a0324 | 101 | |
Alex_Kyrl | 9:22d79a4a0324 | 102 | void mainTicker() |
poephoofd | 0:2d9dae739559 | 103 | { |
Alex_Kyrl | 9:22d79a4a0324 | 104 | |
Alex_Kyrl | 9:22d79a4a0324 | 105 | |
Alex_Kyrl | 9:22d79a4a0324 | 106 | control_motors(); |
poephoofd | 0:2d9dae739559 | 107 | |
Alex_Kyrl | 9:22d79a4a0324 | 108 | |
Alex_Kyrl | 9:22d79a4a0324 | 109 | // scope.set(0, x_control_signal); |
Alex_Kyrl | 9:22d79a4a0324 | 110 | // scope.set(1, motor2.set_angle()); |
poephoofd | 0:2d9dae739559 | 111 | scope.send(); |
Alex_Kyrl | 8:0b7925095416 | 112 | |
poephoofd | 2:a08bff88216d | 113 | } |
poephoofd | 2:a08bff88216d | 114 | |
Alex_Kyrl | 8:0b7925095416 | 115 | |
Alex_Kyrl | 8:0b7925095416 | 116 | /***************************************************/ |
Alex_Kyrl | 9:22d79a4a0324 | 117 | //Main Function: |
Alex_Kyrl | 8:0b7925095416 | 118 | |
poephoofd | 2:a08bff88216d | 119 | int main(void) |
poephoofd | 2:a08bff88216d | 120 | { |
Alex_Kyrl | 8:0b7925095416 | 121 | |
Alex_Kyrl | 9:22d79a4a0324 | 122 | double sample_time= 0.002; //fs = 500Hz |
Alex_Kyrl | 9:22d79a4a0324 | 123 | pc.baud(115200); //Set Baud rate for Serial communication |
Alex_Kyrl | 9:22d79a4a0324 | 124 | MainTicker.attach(&mainTicker, sample_time); //Attach time based interrupt |
poephoofd | 0:2d9dae739559 | 125 | |
Alex_Kyrl | 9:22d79a4a0324 | 126 | |
poephoofd | 2:a08bff88216d | 127 | while(true) |
poephoofd | 2:a08bff88216d | 128 | { |
Alex_Kyrl | 8:0b7925095416 | 129 | if(a==0){ |
Alex_Kyrl | 9:22d79a4a0324 | 130 | cont+=0.1; |
Alex_Kyrl | 8:0b7925095416 | 131 | wait(0.1); |
Alex_Kyrl | 8:0b7925095416 | 132 | } |
Alex_Kyrl | 8:0b7925095416 | 133 | if(b==0){ |
Alex_Kyrl | 9:22d79a4a0324 | 134 | cont-=0.1; |
Alex_Kyrl | 8:0b7925095416 | 135 | wait(0.1); |
Alex_Kyrl | 8:0b7925095416 | 136 | } |
poephoofd | 2:a08bff88216d | 137 | } |
Alex_Kyrl | 8:0b7925095416 | 138 | |
poephoofd | 2:a08bff88216d | 139 | |
Alex_Kyrl | 8:0b7925095416 | 140 | //return 0; |
poephoofd | 0:2d9dae739559 | 141 | } |