![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
control for robotic arm that can play chess using a granular gripper
Dependencies: Encoder mbed HIDScope Servo MODSERIAL
Fork of chessRobot by
actuators.cpp@118:49605b5bd802, 2015-10-28 (annotated)
- Committer:
- annesteenbeek
- Date:
- Wed Oct 28 12:16:02 2015 +0100
- Revision:
- 118:49605b5bd802
- Parent:
- 117:b1667291748d
- Child:
- 120:440f1516101b
entered kinematics values
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
annesteenbeek | 13:4837b36b9a68 | 1 | #include "actuators.h" |
annesteenbeek | 13:4837b36b9a68 | 2 | #include "PID.h" |
annesteenbeek | 13:4837b36b9a68 | 3 | #include "mbed.h" |
annesteenbeek | 13:4837b36b9a68 | 4 | #include "config.h" |
annesteenbeek | 13:4837b36b9a68 | 5 | #include "encoder.h" |
annesteenbeek | 25:874675516927 | 6 | #include "HIDScope.h" |
annesteenbeek | 107:de47331612d9 | 7 | #include "buttons.h" |
annesteenbeek | 107:de47331612d9 | 8 | #include "Servo.h" |
annesteenbeek | 98:25528494287d | 9 | |
annesteenbeek | 109:026abd708dce | 10 | // Motor control constants |
annesteenbeek | 109:026abd708dce | 11 | #define pwm_frequency 50000 // still High, could be lowered |
annesteenbeek | 109:026abd708dce | 12 | #define PI 3.14159265 |
annesteenbeek | 106:1773bf7b95c5 | 13 | |
annesteenbeek | 109:026abd708dce | 14 | // functions for controlling the motors |
annesteenbeek | 109:026abd708dce | 15 | bool motorsEnable = false; |
annesteenbeek | 109:026abd708dce | 16 | bool safetyOn = true; |
annesteenbeek | 76:0aa90e728e4a | 17 | |
annesteenbeek | 6:b957d8809e7c | 18 | |
annesteenbeek | 109:026abd708dce | 19 | double encoder1Counts = 0; |
annesteenbeek | 109:026abd708dce | 20 | double encoder2Counts = 0; |
annesteenbeek | 68:21cb054f1399 | 21 | |
annesteenbeek | 109:026abd708dce | 22 | bool direction1 = false; // CCW is false(positive rotation), CW is true (neg rotation) |
annesteenbeek | 109:026abd708dce | 23 | bool direction2 = false; |
annesteenbeek | 6:b957d8809e7c | 24 | |
annesteenbeek | 109:026abd708dce | 25 | double motor1Pos = 0; |
annesteenbeek | 109:026abd708dce | 26 | double motor2Pos = 0; |
annesteenbeek | 6:b957d8809e7c | 27 | |
annesteenbeek | 109:026abd708dce | 28 | double motor1Speed = 0; |
annesteenbeek | 109:026abd708dce | 29 | double motor2Speed = 0; |
annesteenbeek | 6:b957d8809e7c | 30 | |
annesteenbeek | 109:026abd708dce | 31 | double motor1SetSpeed = 0; |
annesteenbeek | 109:026abd708dce | 32 | double motor2SetSpeed = 0; |
annesteenbeek | 6:b957d8809e7c | 33 | |
annesteenbeek | 118:49605b5bd802 | 34 | double setXSpeed = 0; |
annesteenbeek | 118:49605b5bd802 | 35 | double setYSpeed = 0; |
annesteenbeek | 118:49605b5bd802 | 36 | |
annesteenbeek | 109:026abd708dce | 37 | double motor1PWM = 0; |
annesteenbeek | 109:026abd708dce | 38 | double motor2PWM = 0; |
annesteenbeek | 6:b957d8809e7c | 39 | |
annesteenbeek | 109:026abd708dce | 40 | // Set PID values |
annesteenbeek | 109:026abd708dce | 41 | double Kp1 = 1; |
annesteenbeek | 109:026abd708dce | 42 | double Ki1 = 0; |
annesteenbeek | 109:026abd708dce | 43 | double Kd1 = 0; |
annesteenbeek | 0:525558a26464 | 44 | |
annesteenbeek | 109:026abd708dce | 45 | double Kp2 = 0.008; |
annesteenbeek | 109:026abd708dce | 46 | double Ki2 = 0.08; |
annesteenbeek | 109:026abd708dce | 47 | double Kd2 = 0; |
annesteenbeek | 20:3cba803cd771 | 48 | |
annesteenbeek | 109:026abd708dce | 49 | double motor1PrevCounts = 0; |
annesteenbeek | 109:026abd708dce | 50 | double motor2PrevCounts = 0; |
annesteenbeek | 109:026abd708dce | 51 | double prevTime = 0; |
annesteenbeek | 109:026abd708dce | 52 | double now = 0; |
annesteenbeek | 109:026abd708dce | 53 | double timechange; |
annesteenbeek | 109:026abd708dce | 54 | |
annesteenbeek | 109:026abd708dce | 55 | // Set servo values |
annesteenbeek | 110:a6439e13be8b | 56 | double servoSpeed = 0; |
annesteenbeek | 109:026abd708dce | 57 | const int servoStartPos = 1300; // Servo ranges from 600ms(-90) to 2000ms(90), 1300 is 0 deg |
annesteenbeek | 26:0a9e4147a31a | 58 | |
annesteenbeek | 109:026abd708dce | 59 | // Set calibration values |
annesteenbeek | 111:43c0881fe7e7 | 60 | double motor1CalSpeed = -5; // deg/sec |
annesteenbeek | 111:43c0881fe7e7 | 61 | double motor2CalSpeed = 5; |
annesteenbeek | 109:026abd708dce | 62 | bool springHit = false; |
annesteenbeek | 109:026abd708dce | 63 | float lastCall = 0; |
annesteenbeek | 109:026abd708dce | 64 | bool calibrating1 = true; |
annesteenbeek | 109:026abd708dce | 65 | bool calibrating2 = false; |
annesteenbeek | 111:43c0881fe7e7 | 66 | double looseTime = 0; |
annesteenbeek | 98:25528494287d | 67 | |
annesteenbeek | 112:7b964afb97b4 | 68 | bool calReady = false; |
annesteenbeek | 112:7b964afb97b4 | 69 | |
annesteenbeek | 109:026abd708dce | 70 | // Create object instances |
annesteenbeek | 109:026abd708dce | 71 | // Safety Pin |
annesteenbeek | 109:026abd708dce | 72 | DigitalIn safetyIn(safetyPin); |
annesteenbeek |
32:2006977785f5 | 73 | |
annesteenbeek | 109:026abd708dce | 74 | // Initialze motors |
annesteenbeek | 109:026abd708dce | 75 | PwmOut motor1(motor1PWMPin); |
annesteenbeek | 109:026abd708dce | 76 | PwmOut motor2(motor2PWMPin); |
annesteenbeek | 109:026abd708dce | 77 | |
annesteenbeek | 109:026abd708dce | 78 | // initialize Servo |
annesteenbeek | 109:026abd708dce | 79 | Servo servo(servoPin); |
annesteenbeek | 98:25528494287d | 80 | |
annesteenbeek | 98:25528494287d | 81 | |
annesteenbeek | 109:026abd708dce | 82 | // Initialize encoders |
annesteenbeek | 109:026abd708dce | 83 | Encoder encoder1(enc1A, enc1B); |
annesteenbeek | 109:026abd708dce | 84 | Encoder encoder2(enc2A, enc2B); |
annesteenbeek | 25:874675516927 | 85 | |
annesteenbeek | 109:026abd708dce | 86 | // Set direction pins |
annesteenbeek | 109:026abd708dce | 87 | DigitalOut motor1Dir(motor1DirPin); |
annesteenbeek | 109:026abd708dce | 88 | DigitalOut motor2Dir(motor2DirPin); |
annesteenbeek | 26:0a9e4147a31a | 89 | |
annesteenbeek | 109:026abd708dce | 90 | // create PID instances |
annesteenbeek | 109:026abd708dce | 91 | PID motor1PID(&motor1Speed, &motor1PWM, &motor1SetSpeed, Kp1, Ki1, Kd1); |
annesteenbeek | 109:026abd708dce | 92 | PID motor2PID(&motor2Speed, &motor2PWM, &motor2SetSpeed, Kp2, Ki2, Kd2); |
annesteenbeek | 31:8fbee6c92753 | 93 | |
annesteenbeek | 109:026abd708dce | 94 | Timer t; |
annesteenbeek | 25:874675516927 | 95 | |
annesteenbeek | 25:874675516927 | 96 | void motorInit(){ |
annesteenbeek | 25:874675516927 | 97 | |
annesteenbeek | 3:47c76be6d402 | 98 | motor1Dir.write(direction1); |
annesteenbeek | 3:47c76be6d402 | 99 | motor2Dir.write(direction2); |
annesteenbeek | 3:47c76be6d402 | 100 | |
annesteenbeek | 3:47c76be6d402 | 101 | // Set motor PWM period |
annesteenbeek | 3:47c76be6d402 | 102 | motor1.period(1/pwm_frequency); |
annesteenbeek | 3:47c76be6d402 | 103 | motor2.period(1/pwm_frequency); |
annesteenbeek | 55:ee5257fb73df | 104 | |
annesteenbeek | 55:ee5257fb73df | 105 | motor1PID.SetSampleTime(motorCall); |
annesteenbeek | 55:ee5257fb73df | 106 | motor2PID.SetSampleTime(motorCall); |
annesteenbeek | 55:ee5257fb73df | 107 | |
annesteenbeek | 69:37f75a7d36d8 | 108 | motor1PID.SetOutputLimits(0,1); |
annesteenbeek | 69:37f75a7d36d8 | 109 | motor2PID.SetOutputLimits(0,1); |
annesteenbeek | 69:37f75a7d36d8 | 110 | |
annesteenbeek | 30:a20f16bf8dda | 111 | // Turn PID on |
annesteenbeek | 55:ee5257fb73df | 112 | motor1PID.SetMode(AUTOMATIC); |
annesteenbeek | 55:ee5257fb73df | 113 | motor2PID.SetMode(AUTOMATIC); |
annesteenbeek | 98:25528494287d | 114 | |
annesteenbeek | 98:25528494287d | 115 | // set servo period |
annesteenbeek | 107:de47331612d9 | 116 | servo.Enable(servoStartPos, 20000); |
annesteenbeek | 98:25528494287d | 117 | |
annesteenbeek | 30:a20f16bf8dda | 118 | // start the timer |
annesteenbeek | 29:e4f3455aaa0b | 119 | t.start(); |
annesteenbeek | 4:80e2280058ed | 120 | } |
annesteenbeek | 0:525558a26464 | 121 | |
annesteenbeek | 26:0a9e4147a31a | 122 | |
annesteenbeek | 0:525558a26464 | 123 | void motorControl(){ |
annesteenbeek | 107:de47331612d9 | 124 | // EMG signals to motor speeds |
annesteenbeek | 112:7b964afb97b4 | 125 | // const double scaleVel = 20; |
annesteenbeek | 112:7b964afb97b4 | 126 | // motor1SetSpeed = x_velocity*scaleVel; |
annesteenbeek | 112:7b964afb97b4 | 127 | // motor2SetSpeed = y_velocity*scaleVel; |
annesteenbeek | 112:7b964afb97b4 | 128 | // servoSpeed = z_velocity*scaleVel; |
annesteenbeek | 30:a20f16bf8dda | 129 | // get encoder positions in degrees |
annesteenbeek | 30:a20f16bf8dda | 130 | // 131.25:1 gear ratio |
annesteenbeek | 30:a20f16bf8dda | 131 | // getPosition uses X2 configuration, so 32 counts per revolution |
annesteenbeek | 30:a20f16bf8dda | 132 | // encoder reads CCW negative, and CW positive, so multiply by -1 to make CCW positive |
annesteenbeek | 68:21cb054f1399 | 133 | |
annesteenbeek | 68:21cb054f1399 | 134 | encoder1Counts = encoder1.getPosition(); |
annesteenbeek | 68:21cb054f1399 | 135 | encoder2Counts = encoder2.getPosition(); |
annesteenbeek | 68:21cb054f1399 | 136 | |
annesteenbeek | 68:21cb054f1399 | 137 | |
annesteenbeek | 111:43c0881fe7e7 | 138 | motor1Pos = ((encoder1Counts/32)/131.25)*360; |
annesteenbeek | 111:43c0881fe7e7 | 139 | motor2Pos = ((encoder2Counts/32)/131.25)*360; |
annesteenbeek | 3:47c76be6d402 | 140 | |
annesteenbeek | 0:525558a26464 | 141 | // check if motor's are within rotational boundarys |
annesteenbeek | 30:a20f16bf8dda | 142 | // get encoder speeds in deg/sec |
annesteenbeek | 66:d1ab5904f8e5 | 143 | now = t.read(); |
annesteenbeek | 50:b0cf07ca53cf | 144 | timechange = (now - prevTime); |
annesteenbeek | 111:43c0881fe7e7 | 145 | motor1Speed = ((((encoder1Counts - motor1PrevCounts)/32)/131.25)*360)/timechange; |
annesteenbeek | 111:43c0881fe7e7 | 146 | motor2Speed = ((((encoder2Counts - motor2PrevCounts)/32)/131.25)*360)/timechange; |
annesteenbeek | 50:b0cf07ca53cf | 147 | prevTime = now; |
annesteenbeek | 68:21cb054f1399 | 148 | motor1PrevCounts = encoder1Counts; |
annesteenbeek | 68:21cb054f1399 | 149 | motor2PrevCounts = encoder2Counts; |
annesteenbeek | 29:e4f3455aaa0b | 150 | |
annesteenbeek | 31:8fbee6c92753 | 151 | // calculate motor setpoint speed in deg/sec from setpoint x/y speed |
annesteenbeek | 30:a20f16bf8dda | 152 | |
annesteenbeek | 112:7b964afb97b4 | 153 | // exclude kinematics when still calibrating |
annesteenbeek | 112:7b964afb97b4 | 154 | if (calReady){ |
annesteenbeek | 112:7b964afb97b4 | 155 | kinematics(); |
annesteenbeek | 112:7b964afb97b4 | 156 | } |
annesteenbeek | 112:7b964afb97b4 | 157 | |
annesteenbeek | 48:3cf1eaf34926 | 158 | if(motorsEnable){ // only run motors if switch is enabled |
annesteenbeek |
32:2006977785f5 | 159 | // compute new PID parameters using setpoint angle speeds and encoder speed |
annesteenbeek | 31:8fbee6c92753 | 160 | writeMotors(); |
annesteenbeek | 98:25528494287d | 161 | servoControl(); |
annesteenbeek | 0:525558a26464 | 162 | }else{ |
annesteenbeek | 0:525558a26464 | 163 | // write 0 to motors |
annesteenbeek | 2:95ba9f6f0128 | 164 | motor1.write(0); |
annesteenbeek | 2:95ba9f6f0128 | 165 | motor2.write(0); |
annesteenbeek | 0:525558a26464 | 166 | } |
annesteenbeek | 0:525558a26464 | 167 | } |
annesteenbeek | 0:525558a26464 | 168 | |
annesteenbeek | 31:8fbee6c92753 | 169 | void writeMotors(){ |
annesteenbeek | 54:c14c3bc48b8a | 170 | motor1PID.Compute(); // calculate PID outputs, output changes automatically |
annesteenbeek | 76:0aa90e728e4a | 171 | motor2PID.Compute(); |
annesteenbeek | 31:8fbee6c92753 | 172 | // write new values to motor's |
annesteenbeek | 82:4cc8f9ad3fec | 173 | if (motor1SetSpeed > 0 ){ // CCW rotation |
annesteenbeek | 111:43c0881fe7e7 | 174 | direction1 = true; |
annesteenbeek | 84:df770ec4df61 | 175 | motor1PID.SetOutputLimits(0,1); // change pid output direction |
annesteenbeek | 31:8fbee6c92753 | 176 | }else{ |
annesteenbeek | 111:43c0881fe7e7 | 177 | direction1 = false; // CW rotation |
annesteenbeek | 83:8fa05f53fc73 | 178 | motor1PID.SetOutputLimits(-1,0); |
annesteenbeek | 31:8fbee6c92753 | 179 | } |
annesteenbeek | 82:4cc8f9ad3fec | 180 | if (motor2SetSpeed > 0 ){ // CCW rotation |
annesteenbeek | 111:43c0881fe7e7 | 181 | direction2 = true; |
annesteenbeek | 83:8fa05f53fc73 | 182 | motor2PID.SetOutputLimits(0,1); |
annesteenbeek | 68:21cb054f1399 | 183 | }else{ |
annesteenbeek | 111:43c0881fe7e7 | 184 | direction2 = false; // CW rotation |
annesteenbeek | 83:8fa05f53fc73 | 185 | motor2PID.SetOutputLimits(-1,0); |
annesteenbeek | 68:21cb054f1399 | 186 | } |
annesteenbeek | 31:8fbee6c92753 | 187 | motor1Dir.write(direction1); |
annesteenbeek | 31:8fbee6c92753 | 188 | motor2Dir.write(direction2); |
annesteenbeek | 69:37f75a7d36d8 | 189 | |
annesteenbeek | 52:2ac9dee099ce | 190 | motor1.write(abs(motor1PWM)); |
annesteenbeek | 76:0aa90e728e4a | 191 | motor2.write(abs(motor2PWM)); |
annesteenbeek | 31:8fbee6c92753 | 192 | } |
annesteenbeek | 31:8fbee6c92753 | 193 | |
annesteenbeek | 0:525558a26464 | 194 | void servoControl(){ |
annesteenbeek | 107:de47331612d9 | 195 | servo.SetPosition(1300 + 700*servoSpeed); |
annesteenbeek | 25:874675516927 | 196 | } |
annesteenbeek | 25:874675516927 | 197 | |
annesteenbeek | 112:7b964afb97b4 | 198 | const double motor1StartPos = (-10*32*131.25)/360; // angle to encoder counts |
annesteenbeek | 112:7b964afb97b4 | 199 | const double motor2StartPos = (114*32*131.25)/360; |
annesteenbeek | 112:7b964afb97b4 | 200 | |
annesteenbeek | 106:1773bf7b95c5 | 201 | bool calibrateMotors(){ |
annesteenbeek | 98:25528494287d | 202 | safetyOn = false; // safety springs off |
annesteenbeek | 98:25528494287d | 203 | motorsEnable = true; // motors on |
annesteenbeek | 98:25528494287d | 204 | while (calibrating1 || calibrating2){ |
annesteenbeek | 98:25528494287d | 205 | if (calibrating1){ |
annesteenbeek | 111:43c0881fe7e7 | 206 | motor1SetSpeed = motor1CalSpeed; |
annesteenbeek | 98:25528494287d | 207 | redLed.write(1); greenLed.write(0); blueLed.write(1); |
annesteenbeek | 111:43c0881fe7e7 | 208 | if(safetyIn.read() !=1 && !springHit){ // check if arm reached safety position |
annesteenbeek | 112:7b964afb97b4 | 209 | encoder1.setPosition(motor1StartPos); // set motor 1 cal angle |
annesteenbeek | 112:7b964afb97b4 | 210 | motor1PrevCounts = motor1StartPos; // set previous count to prevent speed spike |
annesteenbeek | 111:43c0881fe7e7 | 211 | motor1CalSpeed = -motor1CalSpeed; // move away |
annesteenbeek | 98:25528494287d | 212 | springHit = true; |
annesteenbeek | 111:43c0881fe7e7 | 213 | looseTime = t.read(); // timer to compensate spring movement |
annesteenbeek | 98:25528494287d | 214 | }else{ |
annesteenbeek | 111:43c0881fe7e7 | 215 | // if hit and after is no longer touching spring and 0.5seconds passed |
annesteenbeek | 111:43c0881fe7e7 | 216 | if(springHit && ((t.read() - looseTime) > 2)){ |
annesteenbeek | 98:25528494287d | 217 | motor1SetSpeed = 0; |
annesteenbeek | 98:25528494287d | 218 | springHit = false; |
annesteenbeek | 111:43c0881fe7e7 | 219 | calibrating2 = true; // start calibrating 2 |
annesteenbeek | 98:25528494287d | 220 | calibrating1 = false; |
annesteenbeek | 98:25528494287d | 221 | } |
annesteenbeek | 98:25528494287d | 222 | } |
annesteenbeek | 98:25528494287d | 223 | } |
annesteenbeek | 98:25528494287d | 224 | if (calibrating2){ |
annesteenbeek | 111:43c0881fe7e7 | 225 | motor2SetSpeed = motor2CalSpeed; |
annesteenbeek | 98:25528494287d | 226 | redLed.write(1); greenLed.write(1); blueLed.write(0); |
annesteenbeek | 111:43c0881fe7e7 | 227 | if(safetyIn.read() !=1 && !springHit){ // check if arm reached safety position |
annesteenbeek | 112:7b964afb97b4 | 228 | encoder2.setPosition(motor2StartPos); // set motor 2 cal angle |
annesteenbeek | 112:7b964afb97b4 | 229 | motor2PrevCounts = motor2StartPos; // set previous cunt to prevent speed spike |
annesteenbeek | 111:43c0881fe7e7 | 230 | motor2CalSpeed = -motor2CalSpeed; // move away |
annesteenbeek | 98:25528494287d | 231 | springHit = true; |
annesteenbeek | 111:43c0881fe7e7 | 232 | looseTime = t.read(); |
annesteenbeek | 98:25528494287d | 233 | }else{ |
annesteenbeek | 111:43c0881fe7e7 | 234 | if(springHit && ((t.read() - looseTime) > 2)){ |
annesteenbeek | 98:25528494287d | 235 | motor2SetSpeed = 0; |
annesteenbeek | 98:25528494287d | 236 | springHit = false; |
annesteenbeek | 98:25528494287d | 237 | calibrating2 = false; // stop calibrating 2 |
annesteenbeek | 98:25528494287d | 238 | } |
annesteenbeek | 98:25528494287d | 239 | } |
annesteenbeek | 98:25528494287d | 240 | } |
annesteenbeek | 111:43c0881fe7e7 | 241 | // now = t.read(); // call motor using timer instead of wait |
annesteenbeek | 111:43c0881fe7e7 | 242 | // if(now - lastCall > motorCall){ |
annesteenbeek | 98:25528494287d | 243 | motorControl(); |
annesteenbeek | 111:43c0881fe7e7 | 244 | wait(motorCall); // keep calling PID's with motorCall frequency |
annesteenbeek | 111:43c0881fe7e7 | 245 | // lastCall = now; |
annesteenbeek | 111:43c0881fe7e7 | 246 | // } |
annesteenbeek | 98:25528494287d | 247 | |
annesteenbeek | 98:25528494287d | 248 | } |
annesteenbeek | 111:43c0881fe7e7 | 249 | redLed.write(0); greenLed.write(1); blueLed.write(1); |
annesteenbeek | 99:7030e9790b1d | 250 | motorsEnable = false; // turn motor's off again |
annesteenbeek | 76:0aa90e728e4a | 251 | safetyOn = true; // turn safety on after callibration |
annesteenbeek | 110:a6439e13be8b | 252 | return true; // return true when finished |
annesteenbeek | 74:75be98779124 | 253 | } |
annesteenbeek | 74:75be98779124 | 254 | |
annesteenbeek | 74:75be98779124 | 255 | |
annesteenbeek | 74:75be98779124 | 256 | void safety(){ |
annesteenbeek | 74:75be98779124 | 257 | if (safetyOn){ |
annesteenbeek | 81:71e7e98deb2c | 258 | if (safetyIn.read() != 1){ |
annesteenbeek | 79:cf500b63f349 | 259 | motorsEnable = false; |
annesteenbeek | 79:cf500b63f349 | 260 | } |
annesteenbeek | 74:75be98779124 | 261 | } |
annesteenbeek | 79:cf500b63f349 | 262 | } |
annesteenbeek | 106:1773bf7b95c5 | 263 | |
annesteenbeek | 112:7b964afb97b4 | 264 | const double L1 = 0.436; // first arm in m |
annesteenbeek | 112:7b964afb97b4 | 265 | const double L2 = 0.120; // first arm in m |
annesteenbeek | 112:7b964afb97b4 | 266 | const double L3 = 0.255; // servo arm in m |
annesteenbeek | 112:7b964afb97b4 | 267 | const double Xmax = 0.3; |
annesteenbeek | 112:7b964afb97b4 | 268 | const double Xmin; = -0.3; |
annesteenbeek | 112:7b964afb97b4 | 269 | const double Ymax = 0.645; |
annesteenbeek | 112:7b964afb97b4 | 270 | const double Ymin = 0.33; |
annesteenbeek | 106:1773bf7b95c5 | 271 | |
annesteenbeek | 112:7b964afb97b4 | 272 | |
annesteenbeek | 106:1773bf7b95c5 | 273 | |
annesteenbeek | 112:7b964afb97b4 | 274 | bool kinematics(){ |
annesteenbeek | 112:7b964afb97b4 | 275 | // calculate current x and Y |
annesteenbeek | 112:7b964afb97b4 | 276 | double X = (L2+L3)*cos((motor1Pos + motor2Pos)*PI/180) + L1*cos(motor1Pos*PI/180); |
annesteenbeek | 112:7b964afb97b4 | 277 | double Y = (L2+L3)*sin((motor1Pos + motor2Pos)*PI/180) + L1*sin(motor1Pos*PI/180); |
annesteenbeek | 112:7b964afb97b4 | 278 | // check if x and y are within limits |
annesteenbeek | 112:7b964afb97b4 | 279 | // else Store the constraint line |
annesteenbeek | 112:7b964afb97b4 | 280 | // check if movement is in direction of constraint |
annesteenbeek | 112:7b964afb97b4 | 281 | // else return false no movement (anglespeed = 0) |
annesteenbeek | 112:7b964afb97b4 | 282 | // calculate required angle speeds |
annesteenbeek | 112:7b964afb97b4 | 283 | if( (X>Xmax && setXSpeed > 0 )|| \ |
annesteenbeek | 112:7b964afb97b4 | 284 | (X<Xmin && setXSpeed < 0 )|| \ |
annesteenbeek | 112:7b964afb97b4 | 285 | (Y>Ymax && setYSpeed > 0 )|| \ |
annesteenbeek | 112:7b964afb97b4 | 286 | (Y<Ymin && setYSpeed < 0 ) \ |
annesteenbeek | 112:7b964afb97b4 | 287 | ){ |
annesteenbeek | 112:7b964afb97b4 | 288 | motor1SetSpeed = 0; |
annesteenbeek | 112:7b964afb97b4 | 289 | motor2SetSpeed = 0; |
annesteenbeek | 112:7b964afb97b4 | 290 | return false; |
annesteenbeek | 112:7b964afb97b4 | 291 | break; |
annesteenbeek | 112:7b964afb97b4 | 292 | } |
annesteenbeek | 112:7b964afb97b4 | 293 | motor1SetSpeed = (setXSpeed*cos((motor1Pos + motor2Pos)*PI/180) + \ |
annesteenbeek | 112:7b964afb97b4 | 294 | setYSpeed*sin((motor1Pos + motor2Pos)*PI/180))/(L1*sin(motor2Pos*PI/180)); |
annesteenbeek | 112:7b964afb97b4 | 295 | motor2SetSpeed = -(setXSpeed*(L2+L3)*cos((motor1Pos + motor2Pos)*PI/180) + \ |
annesteenbeek | 112:7b964afb97b4 | 296 | setYSpeed*(L2+L3)*sin((motor1Pos + motor2Pos)*PI/180) + \ |
annesteenbeek | 112:7b964afb97b4 | 297 | setXSpeed*L1*cos(motor1Pos*PI/180) + \ |
annesteenbeek | 112:7b964afb97b4 | 298 | setYSpeed*L1*sin(motor1Pos*PI/180))/(L1*(L2+L3)*sin(motor2Pos*PI/180)); |
annesteenbeek | 112:7b964afb97b4 | 299 | |
annesteenbeek | 112:7b964afb97b4 | 300 | } |