Werkt

Dependencies:   Encoder HIDScope MODSERIAL QEI mbed

Fork of Inverse_kinematics_PIDController by Casper Kroon

Committer:
CasperK
Date:
Fri Oct 19 12:45:17 2018 +0000
Revision:
4:f36406c9e42f
Parent:
3:56cbed6caacc
Child:
5:14a68d0ee71a
With way to test the math

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CasperK 0:dc2c63f663f8 1 #include "mbed.h"
CasperK 1:fc216448bb57 2 #include "math.h"
CasperK 0:dc2c63f663f8 3 #include "MODSERIAL.h"
CasperK 0:dc2c63f663f8 4
CasperK 0:dc2c63f663f8 5 DigitalIn button(SW2);
CasperK 0:dc2c63f663f8 6 DigitalOut directionpin1(D7);
CasperK 0:dc2c63f663f8 7 DigitalOut directionpin2(D8);
CasperK 0:dc2c63f663f8 8 MODSERIAL pc(USBTX, USBRX);
CasperK 0:dc2c63f663f8 9
CasperK 4:f36406c9e42f 10 Ticker ticker;
CasperK 4:f36406c9e42f 11
CasperK 3:56cbed6caacc 12 volatile bool emg0Bool = false;
CasperK 3:56cbed6caacc 13 volatile bool emg1Bool = false;
CasperK 3:56cbed6caacc 14 volatile bool emg2Bool = false;
CasperK 3:56cbed6caacc 15 volatile bool y_direction = true;
CasperK 4:f36406c9e42f 16 volatile bool a;
CasperK 1:fc216448bb57 17
CasperK 4:f36406c9e42f 18 volatile float x_position = 0.0;
CasperK 4:f36406c9e42f 19 volatile float y_position = 0.0;
CasperK 4:f36406c9e42f 20 volatile float old_y;
CasperK 4:f36406c9e42f 21 volatile float old_x;
CasperK 4:f36406c9e42f 22 volatile int motor1_angle = 0;
CasperK 4:f36406c9e42f 23 volatile int motor2_angle = 0;
CasperK 3:56cbed6caacc 24 volatile int direction;
CasperK 4:f36406c9e42f 25 volatile char c;
CasperK 1:fc216448bb57 26
CasperK 1:fc216448bb57 27 const float length = 0.300; //length in m (placeholder)
CasperK 1:fc216448bb57 28 const int C1 = 3; //motor 1 gear ratio (placeholder)
CasperK 1:fc216448bb57 29 const int C2 = 3; //motor 2 gear ratio
CasperK 4:f36406c9e42f 30
CasperK 3:56cbed6caacc 31 void xDirection() {
CasperK 3:56cbed6caacc 32 //direction of the motion
CasperK 4:f36406c9e42f 33 if (emg0Bool && !emg1Bool) { //if a is pressed and not d, move to the left
CasperK 3:56cbed6caacc 34 directionpin1 = true;
CasperK 3:56cbed6caacc 35 directionpin2 = true;
CasperK 4:f36406c9e42f 36 direction = -1;
CasperK 3:56cbed6caacc 37 }
CasperK 4:f36406c9e42f 38 else if (!emg0Bool && emg1Bool) { //if d is pressed and not a, move to the right
CasperK 3:56cbed6caacc 39 directionpin1 = false;
CasperK 3:56cbed6caacc 40 directionpin2 = false;
CasperK 4:f36406c9e42f 41 direction = 1;
CasperK 3:56cbed6caacc 42 }
CasperK 1:fc216448bb57 43
CasperK 3:56cbed6caacc 44 if (emg0Bool || emg1Bool){
CasperK 1:fc216448bb57 45 //calculating the motion
CasperK 4:f36406c9e42f 46 old_x = x_position;
CasperK 4:f36406c9e42f 47 x_position = old_x + direction;
CasperK 4:f36406c9e42f 48 motor1_angle = asin( x_position ) / length; //theta = arcsin(x) / L
CasperK 4:f36406c9e42f 49
CasperK 4:f36406c9e42f 50 old_y = y_position;
CasperK 4:f36406c9e42f 51 y_position = old_y + direction;
CasperK 4:f36406c9e42f 52 motor2_angle = acos( x_position ) / length; //theta = arcsin(x) / L
CasperK 1:fc216448bb57 53 }
CasperK 4:f36406c9e42f 54
CasperK 4:f36406c9e42f 55 //reset the booleans
CasperK 4:f36406c9e42f 56 emg0Bool = false;
CasperK 4:f36406c9e42f 57 emg1Bool = false;
CasperK 1:fc216448bb57 58 }
CasperK 1:fc216448bb57 59
CasperK 3:56cbed6caacc 60 void yDirection () {
CasperK 4:f36406c9e42f 61 if (emg2Bool) { //if w is pressed, move up/down
CasperK 1:fc216448bb57 62 //direction of the motion
CasperK 4:f36406c9e42f 63 if (y_direction) {
CasperK 1:fc216448bb57 64 directionpin2 = true;
CasperK 2:ffd0553701d3 65 direction = 1;
CasperK 1:fc216448bb57 66 }
CasperK 1:fc216448bb57 67 else if (!y_direction) {
CasperK 1:fc216448bb57 68 directionpin2 = false;
CasperK 2:ffd0553701d3 69 direction = -1;
CasperK 1:fc216448bb57 70 }
CasperK 2:ffd0553701d3 71
CasperK 2:ffd0553701d3 72 //calculating the motion
CasperK 4:f36406c9e42f 73 old_y = y_position;
CasperK 4:f36406c9e42f 74 y_position = old_y + direction;
CasperK 2:ffd0553701d3 75 motor2_angle = C2 * y_position;
CasperK 4:f36406c9e42f 76
CasperK 4:f36406c9e42f 77 //reset the boolean
CasperK 4:f36406c9e42f 78 emg2Bool = false;
CasperK 1:fc216448bb57 79 }
CasperK 1:fc216448bb57 80 }
CasperK 1:fc216448bb57 81
CasperK 0:dc2c63f663f8 82 int main() {
CasperK 4:f36406c9e42f 83 pc.baud(115200);
CasperK 0:dc2c63f663f8 84 pc.printf(" ** program reset **\n\r");
CasperK 4:f36406c9e42f 85
CasperK 0:dc2c63f663f8 86 while (true) {
CasperK 4:f36406c9e42f 87 //if the button is pressed, reverse the y direction
CasperK 4:f36406c9e42f 88 if (!button) {
CasperK 3:56cbed6caacc 89 y_direction = !y_direction;
CasperK 4:f36406c9e42f 90 wait(0.5);
CasperK 3:56cbed6caacc 91 }
CasperK 3:56cbed6caacc 92
CasperK 4:f36406c9e42f 93 //testing the code from keyboard imputs: a-d: left to right, w: forward/backwards
CasperK 4:f36406c9e42f 94 a = pc.readable();
CasperK 4:f36406c9e42f 95 if (a) {
CasperK 4:f36406c9e42f 96 c = pc.getc();
CasperK 4:f36406c9e42f 97 switch (c){
CasperK 4:f36406c9e42f 98 case 'a': //move to the left
CasperK 4:f36406c9e42f 99 emg0Bool = true;
CasperK 4:f36406c9e42f 100 break;
CasperK 4:f36406c9e42f 101 case 'd': //move to the right
CasperK 4:f36406c9e42f 102 emg1Bool = true;
CasperK 4:f36406c9e42f 103 break;
CasperK 4:f36406c9e42f 104 case 'w': //move up/down
CasperK 4:f36406c9e42f 105 emg2Bool = true;
CasperK 4:f36406c9e42f 106 break;
CasperK 4:f36406c9e42f 107 }
CasperK 4:f36406c9e42f 108 }
CasperK 4:f36406c9e42f 109 xDirection(); //call the function to move in the x direction
CasperK 4:f36406c9e42f 110 yDirection(); //call the function to move in the y direction
CasperK 0:dc2c63f663f8 111
CasperK 4:f36406c9e42f 112 // print the motor angles
CasperK 0:dc2c63f663f8 113 pc.printf("motor1 angle: %i\n\r", motor1_angle);
CasperK 0:dc2c63f663f8 114 pc.printf("motor2 angle: %i\n\r\n", motor2_angle);
CasperK 4:f36406c9e42f 115
CasperK 4:f36406c9e42f 116 wait(0.25f); //can also be done with ticker, to be sure that it happens exactly every 0.5 seconds
CasperK 0:dc2c63f663f8 117 }
CasperK 0:dc2c63f663f8 118 }