-
Notifications
You must be signed in to change notification settings - Fork 0
/
The_code_of_forward_and_inverse_kinematics.ino
87 lines (66 loc) · 2.29 KB
/
The_code_of_forward_and_inverse_kinematics.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <Servo.h> //Arduino servo motor library
//configure the servo motors
Servo servo1;
Servo servo2;
const float pi = 3.1415926;
const float L1 = 8; // Length between the first motor and the second motor (in cm)
const float L2 = 5; // Length between the second motor and the end of arm (in cm)
float x;
float y;
float q1; // Angle of the first motor (in radian)
float q1_deg; // Angle of the first motor (in degree)
float q2; // Angle of the second motor (in radian)
float q2_deg; // Angle of the second motor (in degree)
float q; // Sum of in radian (q1+q2), it called theta
float q_deg; // Sum of in degree (q1_deg + q2_deg)
float x_sq ; // X square
float y_sq ; // // Y square
float L1_sq ; // L1 square
float L2_sq ; // L2 square
const int btn1 = 6;
const int btn2 = 7;
int btn1value = LOW; // For forward kinematics
int btn2value = LOW; // For inverse kinematics
void setup() {
servo1.attach(2);
servo2.attach(3);
pinMode (btn1 ,INPUT);
pinMode (btn2 ,INPUT);
Serial.begin(9600);
}
void loop() {
btn1value = digitalRead(btn1);
btn1value = digitalRead(btn2);
if (btn1value == HIGH) // if button 1 is pressed (case of forward kinematic)
{
//assume the values of the angles
q1_deg = 50;
q2_deg = 35;
q_deg = q1_deg + q2_deg;
q1 = q1_deg*pi/180;// convert the angle from degree to radian
q2 = q2_deg*pi/180;// convert the angle from degree to radian
q = q_deg*pi/180; // convert the angle from degree to radian
x = L1*cos(q1) + L2*cos(q); // The expected value with respect to X axis
y = L1*sin(q1) + L2*sin(q); // The expected value with respect to Y axis
servo1.write(q1_deg);
servo2.write(q2_deg);
}
if (btn2value == HIGH) // if button 2 pressed (case of inverse kinematic)
{
//assume the point on X and Y axes, and the value of theta
x = 5;
y = 5;
q_deg = 115;
q = q_deg*pi/180; // convert the angle from degree to radian
x_sq = pow(x,2);
y_sq = pow(y,2);
L1_sq = pow(L1,2);
L2_sq = pow(L2,2);
q1 = acos((x_sq + y_sq - (L1_sq + L2_sq))/(2*L1*L2));
q2 = q - q1 ;
q1_deg = q1*180/pi ; // convert the angle from radian to degree
q2_deg = q2*180/pi; // convert the angle from radian to degree
servo1.write(q1_deg);
servo2.write(q2_deg);
}
}