x and y should be changed to represent the valid inputs. Takes a setpoint x,y and transfers it to required motor angles
main.cpp@0:30f920202a5c, 2018-10-25 (annotated)
- Committer:
- Duif
- Date:
- Thu Oct 25 11:34:55 2018 +0000
- Revision:
- 0:30f920202a5c
- Child:
- 1:edac9d449caa
Transforming x and y to angles for the motor, can't return the arguments yet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Duif | 0:30f920202a5c | 1 | #include "mbed.h" |
Duif | 0:30f920202a5c | 2 | #include "MODSERIAL.h" |
Duif | 0:30f920202a5c | 3 | #include "math.h" |
Duif | 0:30f920202a5c | 4 | |
Duif | 0:30f920202a5c | 5 | const double pi = 3.14159265358979323846; //definition of pi |
Duif | 0:30f920202a5c | 6 | int sq = 2; //to square numbers |
Duif | 0:30f920202a5c | 7 | int ox = 0; //origin |
Duif | 0:30f920202a5c | 8 | int oy = 0; //origin |
Duif | 0:30f920202a5c | 9 | const double x0 = 80.0; //zero x position after homing |
Duif | 0:30f920202a5c | 10 | const double y0 = 141.0; //zero y position after homing |
Duif | 0:30f920202a5c | 11 | const double L1 = 250.0; //length of the first link |
Duif | 0:30f920202a5c | 12 | const double L3 = 350.0; //length of the second link |
Duif | 0:30f920202a5c | 13 | |
Duif | 0:30f920202a5c | 14 | double x = 500.0; //x variable that will be set; input |
Duif | 0:30f920202a5c | 15 | double y = 141.0; //y variable that will be set; input |
Duif | 0:30f920202a5c | 16 | |
Duif | 0:30f920202a5c | 17 | //reference angles of the starting position |
Duif | 0:30f920202a5c | 18 | double q2_0 = -acos((pow(x0,sq)+pow(y0,sq)-pow(L1,sq)-pow(L3,sq))/(2*L1*L3)); |
Duif | 0:30f920202a5c | 19 | double q1_0 = atan(y0/x0)+acos((-pow(L3,sq)+pow(L1,sq)+pow(x0,sq)+pow(y0,2))/(2*L1*sqrt(pow(x0,2)+pow(y0,2)))); |
Duif | 0:30f920202a5c | 20 | |
Duif | 0:30f920202a5c | 21 | //function to calculate the angle from |
Duif | 0:30f920202a5c | 22 | void makeAngle(double x, double y){ |
Duif | 0:30f920202a5c | 23 | |
Duif | 0:30f920202a5c | 24 | double q2 = -acos((pow(x,sq)+pow(y,sq)-pow(L1,sq)-pow(L3,sq))/(2*L1*L3)); //angle of the second joint in setpoint configuration |
Duif | 0:30f920202a5c | 25 | |
Duif | 0:30f920202a5c | 26 | double q1 = atan(y/x)+acos((-pow(L3,sq)+pow(L1,sq)+pow(x,sq)+pow(y,sq))/(2*L1*sqrt(pow(x,sq)+pow(y,sq)))); //angle of the first joint in the setpoint configuration |
Duif | 0:30f920202a5c | 27 | |
Duif | 0:30f920202a5c | 28 | double q2_motor = pi - q2; //because q2 represents the angle at joint two and not at the motor a calculation has to be done |
Duif | 0:30f920202a5c | 29 | |
Duif | 0:30f920202a5c | 30 | double q1_diff = 2*(q1-q1_0); //the actual amount of radians that the motor has to turn in total to reach the setpoint |
Duif | 0:30f920202a5c | 31 | double q2_diff = 2*(q2_motor - q2_0); //the actual amount of radians that the motor has to turn in total to reach the setpoint |
Duif | 0:30f920202a5c | 32 | |
Duif | 0:30f920202a5c | 33 | double q_diff[2] = (q1_diff,q2_diff); //make a vector so the answer can be returned |
Duif | 0:30f920202a5c | 34 | |
Duif | 0:30f920202a5c | 35 | return q_diff; |
Duif | 0:30f920202a5c | 36 | } |
Duif | 0:30f920202a5c | 37 | |
Duif | 0:30f920202a5c | 38 | int main() |
Duif | 0:30f920202a5c | 39 | { |
Duif | 0:30f920202a5c | 40 | while (true) { |
Duif | 0:30f920202a5c | 41 | |
Duif | 0:30f920202a5c | 42 | } |
Duif | 0:30f920202a5c | 43 | } |