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