![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Forward Kinematics
Dependencies: MODSERIAL Matrix mbed
main.cpp@3:c006b3e9a41c, 2018-10-31 (annotated)
- Committer:
- MAHCSnijders
- Date:
- Wed Oct 31 08:28:57 2018 +0000
- Revision:
- 3:c006b3e9a41c
- Parent:
- 2:92aeffa6b16e
- Child:
- 4:6db7291caa6d
Changed eta to theta (naming difference with inverse kinematics)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MAHCSnijders | 0:6fa73e77d49c | 1 | #include "mbed.h" |
MAHCSnijders | 0:6fa73e77d49c | 2 | #include "math.h" |
MAHCSnijders | 0:6fa73e77d49c | 3 | #include "Matrix.h" |
MAHCSnijders | 0:6fa73e77d49c | 4 | |
MAHCSnijders | 0:6fa73e77d49c | 5 | // Stuff die waarschijnlijk weg kan?? |
MAHCSnijders | 0:6fa73e77d49c | 6 | const float L0 = 0.15; // Length between two motors [meter] |
MAHCSnijders | 0:6fa73e77d49c | 7 | const float L1 = 0.10; // Length first beam from right motor2 [meter] |
MAHCSnijders | 0:6fa73e77d49c | 8 | const float L2 = 0.30; // Length second beam from right motor2 [meter] |
MAHCSnijders | 0:6fa73e77d49c | 9 | const float L3 = 0.15; // Length beam between L2 and L4 [meter] |
MAHCSnijders | 0:6fa73e77d49c | 10 | const float L4 = 0.30; // Length first beam from left motor1 [meter] |
MAHCSnijders | 0:6fa73e77d49c | 11 | const float L5 = 0.35; // Length from L3 to end-effector [meter] |
MAHCSnijders | 0:6fa73e77d49c | 12 | const double PI = 3.14159265359; |
MAHCSnijders | 0:6fa73e77d49c | 13 | |
MAHCSnijders | 0:6fa73e77d49c | 14 | // DEZE MOET ER NOG WEL IN!!! |
MAHCSnijders | 0:6fa73e77d49c | 15 | const float L6 = 1.0; // Length beam between frame 0 and motor 1 [meter] |
MAHCSnijders | 0:6fa73e77d49c | 16 | volatile static float Pe_x_cur; // Current x-coordinate of end-effector from frame 0 [meter] |
MAHCSnijders | 0:6fa73e77d49c | 17 | volatile static float Pe_y_cur; // Current y-coordinate of end-effector from frame 0 [meter] |
MAHCSnijders | 0:6fa73e77d49c | 18 | volatile float motor_angle1; // Current angle of motor 1 (left) based on kinematics [rad] |
MAHCSnijders | 0:6fa73e77d49c | 19 | volatile float motor_angle2; // Current angle of motor 2 (right) based on kinematics [rad] |
MAHCSnijders | 0:6fa73e77d49c | 20 | |
MAHCSnijders | 0:6fa73e77d49c | 21 | |
MAHCSnijders | 0:6fa73e77d49c | 22 | // Useful stuff |
MAHCSnijders | 0:6fa73e77d49c | 23 | Ticker ForwardKinematics_ticker; |
MAHCSnijders | 0:6fa73e77d49c | 24 | |
MAHCSnijders | 0:6fa73e77d49c | 25 | void ForwardKinematics() |
MAHCSnijders | 0:6fa73e77d49c | 26 | { |
MAHCSnijders | 0:6fa73e77d49c | 27 | // Calculation of position joint 1 expressed in frame 0 |
MAHCSnijders | 0:6fa73e77d49c | 28 | float J1x_0 = L6 + L0 + L1*cos(motor_angle2); |
MAHCSnijders | 0:6fa73e77d49c | 29 | float J1y_0 = L1*sin(motor_angle2); |
MAHCSnijders | 0:6fa73e77d49c | 30 | |
MAHCSnijders | 0:6fa73e77d49c | 31 | // Calculation of position joint 3 expressed in frame 0 |
MAHCSnijders | 0:6fa73e77d49c | 32 | float J3x_0 = L6 + L4*cos(motor_angle1); |
MAHCSnijders | 0:6fa73e77d49c | 33 | float J3y_0 = L4*sin(motor_angle1); |
MAHCSnijders | 0:6fa73e77d49c | 34 | |
MAHCSnijders | 0:6fa73e77d49c | 35 | // Calculation of Joint 2 expressed in frame 2 |
MAHCSnijders | 0:6fa73e77d49c | 36 | float m_y = J3y_0 - J1y_0; |
MAHCSnijders | 0:6fa73e77d49c | 37 | float m_x = J1x_0 - J3x_0; |
MAHCSnijders | 0:6fa73e77d49c | 38 | float m = sqrt(pow(m_y,2) + pow(m_x,2)); // Radius between Joint 1 and 3 |
MAHCSnijders | 0:6fa73e77d49c | 39 | float delta = acos(- ( pow(m,2) - pow(L2,2) - pow(L3,2))/(2*L2*L3) ); |
MAHCSnijders | 0:6fa73e77d49c | 40 | float mu = acos( (pow(L2,2) - pow(L3,2) + pow(m,2))/(2*m*L2) ); // Angle between L2 and m |
MAHCSnijders | 0:6fa73e77d49c | 41 | |
MAHCSnijders | 0:6fa73e77d49c | 42 | float t_y = J3y_0; |
MAHCSnijders | 0:6fa73e77d49c | 43 | float t_x = (L0 + L6) - J3x_0; |
MAHCSnijders | 0:6fa73e77d49c | 44 | float t = sqrt(pow(t_y,2) + pow(t_x,2)); // Radius between frame 1 and Joint 3 |
MAHCSnijders | 0:6fa73e77d49c | 45 | float phi = acos( (pow(L1,2) - pow(t,2) + pow(m,2))/(2*m*L1) ); // Angle between L1 and m |
MAHCSnijders | 0:6fa73e77d49c | 46 | |
MAHCSnijders | 0:6fa73e77d49c | 47 | float q2 = PI - mu - phi; // Angle that L2 makes in frame 2 |
MAHCSnijders | 2:92aeffa6b16e | 48 | float J2x_2 = L2*cos(q2); |
MAHCSnijders | 2:92aeffa6b16e | 49 | float J2y_2 = L2*sin(q2); |
MAHCSnijders | 0:6fa73e77d49c | 50 | |
MAHCSnijders | 1:3dfde431f833 | 51 | // Calculation of Joint 2 expressed in frame 0 |
MAHCSnijders | 0:6fa73e77d49c | 52 | float J1x_1 = L1*cos(motor_angle2); // Joint 1 expressed in frame 1 |
MAHCSnijders | 0:6fa73e77d49c | 53 | float J1y_1 = L1*sin(motor_angle2); |
MAHCSnijders | 2:92aeffa6b16e | 54 | float J2x_0 = J2x_2*cos(motor_angle2) - J2y_2 * sin(motor_angle2) + J1x_1 + L0 + L6; // Joint 2 expressed in frame 0 |
MAHCSnijders | 2:92aeffa6b16e | 55 | float J2y_0 = J2x_2*sin(motor_angle2) + J2y_2 * cos(motor_angle2) + J1y_1; |
MAHCSnijders | 2:92aeffa6b16e | 56 | |
MAHCSnijders | 2:92aeffa6b16e | 57 | // Calculation of End-effector |
MAHCSnijders | 2:92aeffa6b16e | 58 | float f_x = J2x_0 - J3x_0; |
MAHCSnijders | 2:92aeffa6b16e | 59 | float f_y = J2y_0; |
MAHCSnijders | 2:92aeffa6b16e | 60 | float f = sqrt(pow(f_x,2) + pow(f_y,2)); // Radius between motor 1 and Joint 2 |
MAHCSnijders | 2:92aeffa6b16e | 61 | float xhi = acos( -(pow(f,2) - pow(L3,2) - pow(L4,2))/(2*L3*L4) ); // Angle between L3 and L4 |
MAHCSnijders | 2:92aeffa6b16e | 62 | float omega = PI - xhi; // Angle between L4 and L5 |
MAHCSnijders | 2:92aeffa6b16e | 63 | float n = sqrt(pow(L4,2) + pow(L5,2) - 2*L4*L5*cos(omega)); // Radius between end effector and motor 1 |
MAHCSnijders | 0:6fa73e77d49c | 64 | |
MAHCSnijders | 3:c006b3e9a41c | 65 | float theta = acos( (pow(L4,2) - pow(L5,2) + pow(n,2))/(2*n*L4) ); // Angle between n and L4 |
MAHCSnijders | 3:c006b3e9a41c | 66 | float rho = PI - theta - motor_angle1; // Angle between n and L4 |
MAHCSnijders | 2:92aeffa6b16e | 67 | |
MAHCSnijders | 2:92aeffa6b16e | 68 | float Pe_x = L6 - n*cos(rho); // y-coordinate end-effector |
MAHCSnijders | 2:92aeffa6b16e | 69 | float Pe_y = n*sin(rho); // x-coordinate end-effector |
MAHCSnijders | 2:92aeffa6b16e | 70 | |
MAHCSnijders | 2:92aeffa6b16e | 71 | // NEEDS TO RETURN END-EFFECTOR COORDINATES |
MAHCSnijders | 2:92aeffa6b16e | 72 | // return Pe_x; |
MAHCSnijders | 2:92aeffa6b16e | 73 | // return Pe_y; |
MAHCSnijders | 0:6fa73e77d49c | 74 | } |
MAHCSnijders | 0:6fa73e77d49c | 75 | |
MAHCSnijders | 0:6fa73e77d49c | 76 | |
MAHCSnijders | 0:6fa73e77d49c | 77 | int main() |
MAHCSnijders | 0:6fa73e77d49c | 78 | { |
MAHCSnijders | 0:6fa73e77d49c | 79 | while (true) { |
MAHCSnijders | 0:6fa73e77d49c | 80 | ForwardKinematics_ticker.attach(ForwardKinematics,2); |
MAHCSnijders | 0:6fa73e77d49c | 81 | } |
MAHCSnijders | 0:6fa73e77d49c | 82 | } |