Code for the Poolinator
Dependencies: mbed QEI HIDScope Pulse biquadFilter MODSERIAL FastPWM
main.cpp@9:913a59894698, 2019-10-31 (annotated)
- Committer:
- sjoerd1999
- Date:
- Thu Oct 31 14:11:14 2019 +0000
- Revision:
- 9:913a59894698
- Parent:
- 8:1733338758d3
- Child:
- 10:6711ee2f1fa8
- Child:
- 12:c3fd0712f43d
save before mayhem
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjoerd1999 | 4:8113394bed1b | 1 | /* |
sjoerd1999 | 4:8113394bed1b | 2 | The Poolinator - A pool playing robot for people with DMD |
sjoerd1999 | 4:8113394bed1b | 3 | |
sjoerd1999 | 4:8113394bed1b | 4 | GROUP 10 |
sjoerd1999 | 4:8113394bed1b | 5 | Sjoerd de Jong - s1949950 |
sjoerd1999 | 4:8113394bed1b | 6 | Joost Loohuis - s1969633 |
sjoerd1999 | 4:8113394bed1b | 7 | Viktor Edlund - s2430878 |
sjoerd1999 | 4:8113394bed1b | 8 | Giuseppina Pinky Diatmiko - s1898841 |
sjoerd1999 | 4:8113394bed1b | 9 | Daan v.d Veen - s2003171 |
sjoerd1999 | 4:8113394bed1b | 10 | */ |
sjoerd1999 | 4:8113394bed1b | 11 | |
RobertoO | 0:67c50348f842 | 12 | #include "mbed.h" |
sjoerd1999 | 3:d319bc2b19f1 | 13 | #include "HIDScope.h" |
sjoerd1999 | 2:f8e0a7b5c90a | 14 | #include "QEI.h" |
RobertoO | 1:b862262a9d14 | 15 | #include "MODSERIAL.h" |
sjoerd1999 | 2:f8e0a7b5c90a | 16 | #include "BiQuad.h" |
sjoerd1999 | 2:f8e0a7b5c90a | 17 | #include "FastPWM.h" |
sjoerd1999 | 9:913a59894698 | 18 | #include <Pulse.h> |
sjoerd1999 | 3:d319bc2b19f1 | 19 | |
sjoerd1999 | 8:1733338758d3 | 20 | #define PI 3.14159265358979323846 |
sjoerd1999 | 8:1733338758d3 | 21 | |
sjoerd1999 | 9:913a59894698 | 22 | // COMMUNICATION |
RobertoO | 1:b862262a9d14 | 23 | MODSERIAL pc(USBTX, USBRX); |
sjoerd1999 | 9:913a59894698 | 24 | PulseInOut pulsePin(D2); |
RobertoO | 0:67c50348f842 | 25 | |
sjoerd1999 | 9:913a59894698 | 26 | void moveWithEMG() |
sjoerd1999 | 9:913a59894698 | 27 | { |
sjoerd1999 | 9:913a59894698 | 28 | int i = int(pulsePin.read_high_us(1000) / 100.00 + 0.5); |
sjoerd1999 | 9:913a59894698 | 29 | if(i > 0 && i < 10) { |
sjoerd1999 | 9:913a59894698 | 30 | int EMG_A = (i > 6) ? 2 : (i > 3) ? 1 : 0; |
sjoerd1999 | 9:913a59894698 | 31 | int EMG_B = (i == 1 || i == 4 || i == 7) ? 0 : (i == 2 || i == 5 || i == 8) ? 1 : 2; |
sjoerd1999 | 9:913a59894698 | 32 | // Do something here |
sjoerd1999 | 9:913a59894698 | 33 | } |
sjoerd1999 | 9:913a59894698 | 34 | } |
sjoerd1999 | 8:1733338758d3 | 35 | |
sjoerd1999 | 8:1733338758d3 | 36 | struct vec { |
sjoerd1999 | 8:1733338758d3 | 37 | double x,y,z; |
sjoerd1999 | 8:1733338758d3 | 38 | }; |
sjoerd1999 | 8:1733338758d3 | 39 | |
sjoerd1999 | 9:913a59894698 | 40 | vec endPos{0,60,10}; |
sjoerd1999 | 9:913a59894698 | 41 | vec ballPos{10,50,10}; |
sjoerd1999 | 9:913a59894698 | 42 | |
sjoerd1999 | 9:913a59894698 | 43 | bool demo = true; |
sjoerd1999 | 9:913a59894698 | 44 | enum State {CALIBRATE, INIT_0, IDLE, INIT_1, PHASE_1, INIT_2, PHASE_2, INIT_3, PHASE_3}; |
sjoerd1999 | 9:913a59894698 | 45 | State currentState = CALIBRATE; |
sjoerd1999 | 9:913a59894698 | 46 | |
sjoerd1999 | 9:913a59894698 | 47 | // JOYSTICK CONTROL // |
sjoerd1999 | 9:913a59894698 | 48 | AnalogIn joyX(A2), joyY(A1), slider(A0); |
sjoerd1999 | 9:913a59894698 | 49 | DigitalIn joyButton(PTB20); |
sjoerd1999 | 9:913a59894698 | 50 | void moveWithJoystick() |
sjoerd1999 | 9:913a59894698 | 51 | { |
sjoerd1999 | 9:913a59894698 | 52 | float delta = 0.04; |
sjoerd1999 | 9:913a59894698 | 53 | if(joyX.read() < 0.2) endPos.x += delta; |
sjoerd1999 | 9:913a59894698 | 54 | else if(joyX.read() > 0.8) endPos.x -= delta; |
sjoerd1999 | 9:913a59894698 | 55 | if(joyY.read() < 0.2) endPos.z += delta; |
sjoerd1999 | 9:913a59894698 | 56 | else if(joyY.read() > 0.8) endPos.z -= delta; |
sjoerd1999 | 9:913a59894698 | 57 | if(slider.read() < 0.2) endPos.y += delta; |
sjoerd1999 | 9:913a59894698 | 58 | else if(slider.read() > 0.9) endPos.y -= delta; |
sjoerd1999 | 9:913a59894698 | 59 | } |
sjoerd1999 | 3:d319bc2b19f1 | 60 | |
sjoerd1999 | 8:1733338758d3 | 61 | // DC MOTORS // |
sjoerd1999 | 9:913a59894698 | 62 | QEI encoder1(D10,D11,NC,32), encoder2(PTC5,PTC7, NC, 32), encoder3(D12,D13,NC,32); |
sjoerd1999 | 8:1733338758d3 | 63 | PwmOut motor1_pwm(D5), motor3_pwm(D6); |
sjoerd1999 | 8:1733338758d3 | 64 | DigitalOut motor1_dir(D4), motor3_dir(D7), motor2_A(D2), motor2_B(D3); |
sjoerd1999 | 7:bfcb74384f46 | 65 | |
sjoerd1999 | 8:1733338758d3 | 66 | float motor1_cur = 0, motor2_cur = 0, motor3_cur = 0; |
sjoerd1999 | 8:1733338758d3 | 67 | float motor1_tar = 0, motor2_tar = 0, motor3_tar = 0; |
sjoerd1999 | 8:1733338758d3 | 68 | float motor1_zero = 37.5, motor2_zero = 14.3, motor3_zero = 57.8; // in degrees(M1) / cm(M2/M3) |
sjoerd1999 | 2:f8e0a7b5c90a | 69 | |
sjoerd1999 | 8:1733338758d3 | 70 | void setMotor(int motor, float motor_spd) // Set the motor speed (between -1 and 1) |
sjoerd1999 | 2:f8e0a7b5c90a | 71 | { |
sjoerd1999 | 2:f8e0a7b5c90a | 72 | int motor_dir = (motor_spd >= 0) ? 0 : 1; |
sjoerd1999 | 2:f8e0a7b5c90a | 73 | motor_spd = fabs(motor_spd); |
sjoerd1999 | 2:f8e0a7b5c90a | 74 | |
sjoerd1999 | 2:f8e0a7b5c90a | 75 | if(motor == 1) { |
sjoerd1999 | 9:913a59894698 | 76 | motor1_dir.write(1 - motor_dir); |
sjoerd1999 | 2:f8e0a7b5c90a | 77 | motor1_pwm.write(motor_spd); |
sjoerd1999 | 8:1733338758d3 | 78 | } else if(motor == 3) { |
sjoerd1999 | 8:1733338758d3 | 79 | motor3_dir.write(motor_dir); |
sjoerd1999 | 8:1733338758d3 | 80 | motor3_pwm.write(motor_spd); |
sjoerd1999 | 2:f8e0a7b5c90a | 81 | } else if(motor == 2) { |
sjoerd1999 | 8:1733338758d3 | 82 | motor2_A.write((motor_dir == 0) ? 0 : int(motor_spd)); // Motor 2 has digital pins so no PWM |
sjoerd1999 | 8:1733338758d3 | 83 | motor2_B.write((motor_dir == 0) ? int(motor_spd) : 0); |
sjoerd1999 | 2:f8e0a7b5c90a | 84 | } |
sjoerd1999 | 2:f8e0a7b5c90a | 85 | } |
sjoerd1999 | 2:f8e0a7b5c90a | 86 | |
sjoerd1999 | 8:1733338758d3 | 87 | void getMotorPositions() // Calculate current motor positions (M1:angle, M2/M3:length) based on encoder pulses |
sjoerd1999 | 8:1733338758d3 | 88 | { |
sjoerd1999 | 9:913a59894698 | 89 | // 18530 pulses per rotation |
sjoerd1999 | 9:913a59894698 | 90 | motor1_cur = 244 - float(encoder1.getPulses()) / 18530.00 * 360; |
sjoerd1999 | 9:913a59894698 | 91 | if(motor1_cur > 360) motor1_cur -= 360; |
sjoerd1999 | 9:913a59894698 | 92 | if(motor1_cur < 0) motor1_cur += 360; |
sjoerd1999 | 9:913a59894698 | 93 | motor2_cur = float(encoder2.getPulses()) / 41920.00 + 14.3; |
sjoerd1999 | 9:913a59894698 | 94 | motor3_cur = float(encoder3.getPulses()) / 41920.00 + 57.8; |
sjoerd1999 | 8:1733338758d3 | 95 | } |
sjoerd1999 | 8:1733338758d3 | 96 | |
sjoerd1999 | 8:1733338758d3 | 97 | void moveToTargets() // Move to the target positions. No PID. |
sjoerd1999 | 8:1733338758d3 | 98 | { |
sjoerd1999 | 9:913a59894698 | 99 | setMotor(1, (fabs(motor1_cur - motor1_tar) < 1) ? 0 : (motor1_cur < motor1_tar) ? 0.1 : -0.1); |
sjoerd1999 | 9:913a59894698 | 100 | setMotor(2, (fabs(motor2_cur - motor2_tar) < 0.03) ? 0 : (motor2_cur < motor2_tar) ? 1 : -1); |
sjoerd1999 | 9:913a59894698 | 101 | setMotor(3, (fabs(motor3_cur - motor3_tar) < 0.03) ? 0 : (motor3_cur < motor3_tar) ? 1: -1); |
sjoerd1999 | 8:1733338758d3 | 102 | } |
sjoerd1999 | 8:1733338758d3 | 103 | |
sjoerd1999 | 8:1733338758d3 | 104 | // KINEMATICS // |
sjoerd1999 | 8:1733338758d3 | 105 | void calculateKinematics(float x, float y, float z) // y is up |
sjoerd1999 | 8:1733338758d3 | 106 | { |
sjoerd1999 | 9:913a59894698 | 107 | float angle1 = fmod(2*PI - atan2(z, x), 2*PI) * (360 / (2*PI)); |
sjoerd1999 | 8:1733338758d3 | 108 | float angle2 = acos(sqrt(x*x + y*y + z*z) / 100.00) + atan2(sqrt(x*x + z*z), y); |
sjoerd1999 | 8:1733338758d3 | 109 | float angle3 = 2 * asin(sqrt(x*x + y*y + z*z) / 100.00); |
sjoerd1999 | 8:1733338758d3 | 110 | |
sjoerd1999 | 9:913a59894698 | 111 | motor1_tar = angle1; |
sjoerd1999 | 9:913a59894698 | 112 | motor2_tar = sqrt(200 - 200 * cos(angle2)); // a^2 = b^2 + c^2 - 2bc * cos(angle) (b = c = 10cm) |
sjoerd1999 | 9:913a59894698 | 113 | motor3_tar = sqrt(2600 - 1000 * cos(PI - angle3)); // a^2 = b^2 + c^2 - 2bc * cos(angle) (b = 10cm, c = 50cm) |
sjoerd1999 | 9:913a59894698 | 114 | } |
sjoerd1999 | 9:913a59894698 | 115 | |
sjoerd1999 | 9:913a59894698 | 116 | void calculateKinematicsHand(float x, float y, float z) // y is up |
sjoerd1999 | 9:913a59894698 | 117 | { |
sjoerd1999 | 9:913a59894698 | 118 | float angle1 = fmod(2*PI - atan2(z, x), 2*PI) * (360 / (2*PI)); |
sjoerd1999 | 9:913a59894698 | 119 | float xz = sqrt(x*x + z*z) - 15; |
sjoerd1999 | 9:913a59894698 | 120 | y -= 9; |
sjoerd1999 | 9:913a59894698 | 121 | float angle2 = acos(sqrt(xz*xz + y*y) / 100.00) + atan2(xz, y); |
sjoerd1999 | 9:913a59894698 | 122 | float angle3 = 2 * asin(sqrt(xz*xz +y*y) / 100.00); |
sjoerd1999 | 9:913a59894698 | 123 | |
sjoerd1999 | 9:913a59894698 | 124 | motor1_tar = angle1; |
sjoerd1999 | 8:1733338758d3 | 125 | motor2_tar = sqrt(200 - 200 * cos(angle2)); // a^2 = b^2 + c^2 - 2bc * cos(angle) (b = c = 10cm) |
sjoerd1999 | 8:1733338758d3 | 126 | motor3_tar = sqrt(2600 - 1000 * cos(PI - angle3)); // a^2 = b^2 + c^2 - 2bc * cos(angle) (b = 10cm, c = 50cm) |
sjoerd1999 | 8:1733338758d3 | 127 | } |
sjoerd1999 | 8:1733338758d3 | 128 | |
sjoerd1999 | 8:1733338758d3 | 129 | // STEPPER MOTOR // |
sjoerd1999 | 2:f8e0a7b5c90a | 130 | DigitalOut STEPPER_IN1(PTB18), STEPPER_IN2(PTB19), STEPPER_IN3(PTC1), STEPPER_IN4(PTC8); |
sjoerd1999 | 2:f8e0a7b5c90a | 131 | |
sjoerd1999 | 2:f8e0a7b5c90a | 132 | int stepper_steps = 0; |
sjoerd1999 | 8:1733338758d3 | 133 | float stepper_angle = 0, stepper_target = 0; |
sjoerd1999 | 2:f8e0a7b5c90a | 134 | |
sjoerd1999 | 8:1733338758d3 | 135 | void stepper_step(int direction_) // Requires ~1.5ms wait time between each step, 4096 steps is one rotation |
sjoerd1999 | 2:f8e0a7b5c90a | 136 | { |
sjoerd1999 | 3:d319bc2b19f1 | 137 | STEPPER_IN1 = (stepper_steps == 5 || stepper_steps == 6 || stepper_steps == 7); |
sjoerd1999 | 2:f8e0a7b5c90a | 138 | STEPPER_IN2 = (stepper_steps == 3 || stepper_steps == 4 || stepper_steps == 5); |
sjoerd1999 | 2:f8e0a7b5c90a | 139 | STEPPER_IN3 = (stepper_steps == 1 || stepper_steps == 2 || stepper_steps == 3); |
sjoerd1999 | 2:f8e0a7b5c90a | 140 | STEPPER_IN4 = (stepper_steps == 7 || stepper_steps == 0 || stepper_steps == 1); |
sjoerd1999 | 2:f8e0a7b5c90a | 141 | stepper_steps += (direction_ == 0) ? - 1 : 1; |
sjoerd1999 | 2:f8e0a7b5c90a | 142 | stepper_steps = (stepper_steps + 8) % 8; |
sjoerd1999 | 9:913a59894698 | 143 | stepper_angle -= ((direction_ == 0) ? -1 : 1) * (360.00 / 4096.00); |
sjoerd1999 | 2:f8e0a7b5c90a | 144 | } |
sjoerd1999 | 2:f8e0a7b5c90a | 145 | |
sjoerd1999 | 8:1733338758d3 | 146 | Ticker stepper_moveToAngle; |
sjoerd1999 | 8:1733338758d3 | 147 | void stepper_move() // Move toward desired angle with threshold. In Ticker function because requires wait otherwise |
sjoerd1999 | 8:1733338758d3 | 148 | { |
sjoerd1999 | 9:913a59894698 | 149 | if(fabs(stepper_angle - stepper_target) > 1) stepper_step((stepper_angle < stepper_target) ? 0 : 1); |
sjoerd1999 | 8:1733338758d3 | 150 | } |
sjoerd1999 | 8:1733338758d3 | 151 | |
sjoerd1999 | 8:1733338758d3 | 152 | // SERVO // |
sjoerd1999 | 9:913a59894698 | 153 | AnalogOut servo(DAC0_OUT); // Write analog value to the Arduino |
sjoerd1999 | 9:913a59894698 | 154 | void setServo(float i) // Set servo to specified angle(0-180 degrees) PWM signal measured by Arduino and decoded there. |
sjoerd1999 | 5:33133ebe37fd | 155 | { |
sjoerd1999 | 9:913a59894698 | 156 | servo = i / 180.00; |
sjoerd1999 | 5:33133ebe37fd | 157 | } |
sjoerd1999 | 3:d319bc2b19f1 | 158 | |
sjoerd1999 | 8:1733338758d3 | 159 | // AIMING // |
sjoerd1999 | 9:913a59894698 | 160 | float aimAngle, aimTilt; |
sjoerd1999 | 9:913a59894698 | 161 | float cueLength = 18; |
sjoerd1999 | 9:913a59894698 | 162 | void aim(float angle, float tilt) // Moves both stepper and servo so the end affector points towards desired angle |
sjoerd1999 | 8:1733338758d3 | 163 | { |
sjoerd1999 | 9:913a59894698 | 164 | setServo(tilt + 90); |
sjoerd1999 | 9:913a59894698 | 165 | |
sjoerd1999 | 9:913a59894698 | 166 | // SPHERICAL TO CARTESIAN: |
sjoerd1999 | 9:913a59894698 | 167 | float handX = ballPos.x + cueLength * sin(tilt / 180 * PI) * cos(angle / 180 * PI); |
sjoerd1999 | 9:913a59894698 | 168 | float handY = ballPos.y - cueLength * cos(tilt / 180 * PI); |
sjoerd1999 | 9:913a59894698 | 169 | float handZ = ballPos.z - cueLength * sin(tilt / 180 * PI) * sin(angle / 180 * PI); |
sjoerd1999 | 9:913a59894698 | 170 | endPos.x = handX; |
sjoerd1999 | 9:913a59894698 | 171 | endPos.y = handY; |
sjoerd1999 | 9:913a59894698 | 172 | endPos.z = handZ; |
sjoerd1999 | 9:913a59894698 | 173 | |
sjoerd1999 | 9:913a59894698 | 174 | float stepperAngle = angle - fmod(2*PI - atan2(handZ, handX), 2*PI) * (180 / PI); |
sjoerd1999 | 9:913a59894698 | 175 | if(stepperAngle < 0) stepperAngle += 360; |
sjoerd1999 | 9:913a59894698 | 176 | stepper_target = stepperAngle; |
sjoerd1999 | 8:1733338758d3 | 177 | } |
sjoerd1999 | 8:1733338758d3 | 178 | |
sjoerd1999 | 8:1733338758d3 | 179 | // SOLENOID // |
sjoerd1999 | 5:33133ebe37fd | 180 | DigitalOut solenoidA(PTC0), solenoidB(PTC9); |
sjoerd1999 | 8:1733338758d3 | 181 | void setSolenoid(int dir) // 1 is out, 0 is in |
sjoerd1999 | 3:d319bc2b19f1 | 182 | { |
sjoerd1999 | 9:913a59894698 | 183 | solenoidA = (dir == 0) ? 0 : 1; |
sjoerd1999 | 9:913a59894698 | 184 | solenoidB = (dir == 0) ? 1 : 0; |
sjoerd1999 | 7:bfcb74384f46 | 185 | } |
sjoerd1999 | 7:bfcb74384f46 | 186 | |
sjoerd1999 | 8:1733338758d3 | 187 | // LASER // |
sjoerd1999 | 7:bfcb74384f46 | 188 | DigitalOut laserPin(D8); |
sjoerd1999 | 7:bfcb74384f46 | 189 | void setLaser(bool on) |
sjoerd1999 | 7:bfcb74384f46 | 190 | { |
sjoerd1999 | 7:bfcb74384f46 | 191 | if(on) laserPin.write(1); |
sjoerd1999 | 7:bfcb74384f46 | 192 | else laserPin.write(0); |
sjoerd1999 | 7:bfcb74384f46 | 193 | } |
sjoerd1999 | 7:bfcb74384f46 | 194 | |
sjoerd1999 | 9:913a59894698 | 195 | // CALIBRATION // a3 a4 A5 |
sjoerd1999 | 9:913a59894698 | 196 | AnalogIn switch1(A3), switch2(A4), switch3(A5); |
sjoerd1999 | 8:1733338758d3 | 197 | void calibrate() // Calibrates all 3 motors simultaniously |
sjoerd1999 | 7:bfcb74384f46 | 198 | { |
sjoerd1999 | 8:1733338758d3 | 199 | setMotor(1,0.2); // Set all motors to move towards the switches |
sjoerd1999 | 7:bfcb74384f46 | 200 | setMotor(2,1); |
sjoerd1999 | 7:bfcb74384f46 | 201 | setMotor(3,1); |
sjoerd1999 | 7:bfcb74384f46 | 202 | |
sjoerd1999 | 9:913a59894698 | 203 | while(!(switch1.read() < 0.5 && switch2.read() > 0.5 && switch3.read() > 0.5)) { // When all switches have been pushed in, stop |
sjoerd1999 | 9:913a59894698 | 204 | if(switch1.read() < 0.5) setMotor(1,0); |
sjoerd1999 | 7:bfcb74384f46 | 205 | if(switch2.read() > 0.5) setMotor(2,0); |
sjoerd1999 | 7:bfcb74384f46 | 206 | if(switch3.read() > 0.5) setMotor(3,0); |
sjoerd1999 | 9:913a59894698 | 207 | wait_ms(30); |
sjoerd1999 | 7:bfcb74384f46 | 208 | } |
sjoerd1999 | 8:1733338758d3 | 209 | for(int i = 1; i <= 3; i++) setMotor(i,0); // Make sure they've all stopped |
sjoerd1999 | 8:1733338758d3 | 210 | |
sjoerd1999 | 8:1733338758d3 | 211 | encoder1.reset(); // Reset encoder positions |
sjoerd1999 | 7:bfcb74384f46 | 212 | encoder2.reset(); |
sjoerd1999 | 7:bfcb74384f46 | 213 | encoder3.reset(); |
sjoerd1999 | 8:1733338758d3 | 214 | |
sjoerd1999 | 8:1733338758d3 | 215 | //setServo(0); |
sjoerd1999 | 9:913a59894698 | 216 | setSolenoid(0); |
sjoerd1999 | 9:913a59894698 | 217 | //setLaser(0); |
sjoerd1999 | 3:d319bc2b19f1 | 218 | } |
sjoerd1999 | 3:d319bc2b19f1 | 219 | |
sjoerd1999 | 3:d319bc2b19f1 | 220 | |
RobertoO | 0:67c50348f842 | 221 | int main() |
RobertoO | 0:67c50348f842 | 222 | { |
RobertoO | 0:67c50348f842 | 223 | pc.baud(115200); |
RobertoO | 1:b862262a9d14 | 224 | pc.printf("\r\nStarting...\r\n\r\n"); |
sjoerd1999 | 8:1733338758d3 | 225 | motor1_pwm.period(0.0001); |
sjoerd1999 | 8:1733338758d3 | 226 | motor3_pwm.period(0.0001); |
sjoerd1999 | 9:913a59894698 | 227 | stepper_moveToAngle.attach(&stepper_move, 0.0015); |
sjoerd1999 | 7:bfcb74384f46 | 228 | |
RobertoO | 0:67c50348f842 | 229 | while (true) { |
sjoerd1999 | 9:913a59894698 | 230 | switch(currentState) { |
sjoerd1999 | 9:913a59894698 | 231 | case CALIBRATE: |
sjoerd1999 | 9:913a59894698 | 232 | calibrate(); |
sjoerd1999 | 9:913a59894698 | 233 | currentState = PHASE_1; |
sjoerd1999 | 9:913a59894698 | 234 | setServo(90); |
sjoerd1999 | 9:913a59894698 | 235 | break; |
sjoerd1999 | 9:913a59894698 | 236 | case PHASE_1: |
sjoerd1999 | 9:913a59894698 | 237 | if(demo) moveWithJoystick(); |
sjoerd1999 | 9:913a59894698 | 238 | else moveWithEMG(); |
sjoerd1999 | 9:913a59894698 | 239 | calculateKinematicsHand(endPos.x, endPos.y, endPos.z); // Calculate target positions |
sjoerd1999 | 9:913a59894698 | 240 | getMotorPositions(); // Calculate current positions |
sjoerd1999 | 9:913a59894698 | 241 | moveToTargets(); // Set the motors speeds accordingly |
sjoerd1999 | 9:913a59894698 | 242 | pc.printf("x: %f\r\n",endPos.x); |
sjoerd1999 | 9:913a59894698 | 243 | pc.printf("y: %f\r\n",endPos.y); |
sjoerd1999 | 9:913a59894698 | 244 | pc.printf("z: %f\r\n",endPos.z); |
sjoerd1999 | 9:913a59894698 | 245 | if(joyButton.read() == 0) { |
sjoerd1999 | 9:913a59894698 | 246 | currentState = PHASE_2; |
sjoerd1999 | 9:913a59894698 | 247 | wait_ms(1000); |
sjoerd1999 | 9:913a59894698 | 248 | ballPos.x = endPos.x; |
sjoerd1999 | 9:913a59894698 | 249 | ballPos.y = 65; |
sjoerd1999 | 9:913a59894698 | 250 | ballPos.z = endPos.z; |
sjoerd1999 | 9:913a59894698 | 251 | } |
sjoerd1999 | 9:913a59894698 | 252 | wait_ms(40); |
sjoerd1999 | 9:913a59894698 | 253 | break; |
sjoerd1999 | 9:913a59894698 | 254 | |
sjoerd1999 | 9:913a59894698 | 255 | case PHASE_2: |
sjoerd1999 | 9:913a59894698 | 256 | if(slider.read() > 0.9) aimTilt += 0.5; |
sjoerd1999 | 9:913a59894698 | 257 | else if(slider.read() < 0.1) aimTilt -= 0.5; |
sjoerd1999 | 9:913a59894698 | 258 | if(aimTilt < 0) aimTilt = 0; |
sjoerd1999 | 9:913a59894698 | 259 | else if(aimTilt > 90) aimTilt = 90; |
sjoerd1999 | 9:913a59894698 | 260 | |
sjoerd1999 | 9:913a59894698 | 261 | if(joyX.read() < 0.2) aimAngle -= 1; |
sjoerd1999 | 9:913a59894698 | 262 | else if(joyX.read() > 0.8) aimAngle += 1; |
sjoerd1999 | 9:913a59894698 | 263 | if(aimAngle < 0) aimAngle = 360; |
sjoerd1999 | 9:913a59894698 | 264 | else if(aimAngle > 360) aimAngle = 0; |
sjoerd1999 | 9:913a59894698 | 265 | |
sjoerd1999 | 9:913a59894698 | 266 | aim(aimAngle, aimTilt); |
sjoerd1999 | 9:913a59894698 | 267 | pc.printf("x: %f\r\n",endPos.x); |
sjoerd1999 | 9:913a59894698 | 268 | pc.printf("y: %f\r\n",endPos.y); |
sjoerd1999 | 9:913a59894698 | 269 | pc.printf("z: %f\r\n",endPos.z); |
sjoerd1999 | 9:913a59894698 | 270 | pc.printf("angle: %f\r\n",aimAngle); |
sjoerd1999 | 9:913a59894698 | 271 | pc.printf("tilt: %f\r\n",aimTilt); |
sjoerd1999 | 9:913a59894698 | 272 | |
sjoerd1999 | 9:913a59894698 | 273 | calculateKinematicsHand(endPos.x, endPos.y, endPos.z); // Calculate target positions |
sjoerd1999 | 9:913a59894698 | 274 | getMotorPositions(); // Calculate current positions |
sjoerd1999 | 9:913a59894698 | 275 | moveToTargets(); // Set the motors speeds accordingly |
sjoerd1999 | 9:913a59894698 | 276 | wait_ms(40); |
sjoerd1999 | 9:913a59894698 | 277 | break; |
sjoerd1999 | 9:913a59894698 | 278 | } |
sjoerd1999 | 4:8113394bed1b | 279 | /* |
sjoerd1999 | 4:8113394bed1b | 280 | SOME EXAPLE CODE |
sjoerd1999 | 5:33133ebe37fd | 281 | |
sjoerd1999 | 4:8113394bed1b | 282 | * MOTOR |
sjoerd1999 | 8:1733338758d3 | 283 | setMotor(..., ...) // which motor (1,2,3), and speed (-1.0, +1.0) |
sjoerd1999 | 5:33133ebe37fd | 284 | |
sjoerd1999 | 8:1733338758d3 | 285 | * KINEMATICS (this should be done every ~30 ms) |
sjoerd1999 | 8:1733338758d3 | 286 | calculateKinematics(x, y, z); // Calculate target positions |
sjoerd1999 | 8:1733338758d3 | 287 | getMotorPositions(); // Calculate current positions |
sjoerd1999 | 8:1733338758d3 | 288 | moveToTargets(); // Set the motors speeds accordingly |
sjoerd1999 | 3:d319bc2b19f1 | 289 | |
sjoerd1999 | 4:8113394bed1b | 290 | * STEPPER |
sjoerd1999 | 8:1733338758d3 | 291 | stepper_target = ...; // angle (between 0.0 and 180.0) |
sjoerd1999 | 4:8113394bed1b | 292 | |
sjoerd1999 | 4:8113394bed1b | 293 | * SERVO |
sjoerd1999 | 7:bfcb74384f46 | 294 | setServo(...) // value between 0.0 and 180.0 (= 0 and 180 degrees) |
sjoerd1999 | 5:33133ebe37fd | 295 | |
sjoerd1999 | 8:1733338758d3 | 296 | * AIMING |
sjoerd1999 | 8:1733338758d3 | 297 | aim(...) // value between 0 and 360 |
sjoerd1999 | 8:1733338758d3 | 298 | |
sjoerd1999 | 5:33133ebe37fd | 299 | * SOLENOID |
sjoerd1999 | 8:1733338758d3 | 300 | setSolenoid(...); // position, 0(in) or 1(out) |
sjoerd1999 | 8:1733338758d3 | 301 | |
sjoerd1999 | 8:1733338758d3 | 302 | * LASER |
sjoerd1999 | 8:1733338758d3 | 303 | setLaser(...) // 0(off) or 1(on) |
sjoerd1999 | 5:33133ebe37fd | 304 | |
sjoerd1999 | 4:8113394bed1b | 305 | */ |
sjoerd1999 | 7:bfcb74384f46 | 306 | |
sjoerd1999 | 8:1733338758d3 | 307 | // The EMG MBED sends one of these values: 11 12 13 21 22 23 31 32 33 (first digit is GOLEFT(1),STOP(2),GORIGHT(3), same for second digit but z-axis. |
sjoerd1999 | 8:1733338758d3 | 308 | // endPos.z += (n > 30) ? 1 : (n > 20) ? 0 : -1; |
sjoerd1999 | 8:1733338758d3 | 309 | // endPos.x += (n % 10 == 1) ? 1 : (n % 10 == 2) ? 0 : -1; |
sjoerd1999 | 9:913a59894698 | 310 | // |
sjoerd1999 | 9:913a59894698 | 311 | // if(pot.read() < 0.2) setMotor(1,-0.2); |
sjoerd1999 | 9:913a59894698 | 312 | // else if(pot.read() > 0.8) setMotor(1,0.2); |
sjoerd1999 | 9:913a59894698 | 313 | // else setMotor(1,0); |
sjoerd1999 | 9:913a59894698 | 314 | // wait_ms(30); |
sjoerd1999 | 9:913a59894698 | 315 | |
sjoerd1999 | 9:913a59894698 | 316 | // if(pot.read() > 0.8) setSolenoid(1); |
sjoerd1999 | 9:913a59894698 | 317 | // else setSolenoid(0); |
sjoerd1999 | 9:913a59894698 | 318 | |
sjoerd1999 | 9:913a59894698 | 319 | // moveWithJoystick(); |
sjoerd1999 | 9:913a59894698 | 320 | // calculateKinematics(endPos.x, endPos.y, endPos.z); // Calculate target positions |
sjoerd1999 | 9:913a59894698 | 321 | // getMotorPositions(); // Calculate current positions |
sjoerd1999 | 9:913a59894698 | 322 | // moveToTargets(); // Set the motors speeds accordingly |
sjoerd1999 | 9:913a59894698 | 323 | // pc.printf("x: %f\r\n",endPos.x); |
sjoerd1999 | 9:913a59894698 | 324 | // pc.printf("y: %f\r\n",endPos.y); |
sjoerd1999 | 9:913a59894698 | 325 | // pc.printf("z: %f\r\n",endPos.z); |
sjoerd1999 | 9:913a59894698 | 326 | // wait_ms(40); |
sjoerd1999 | 9:913a59894698 | 327 | |
sjoerd1999 | 8:1733338758d3 | 328 | |
sjoerd1999 | 7:bfcb74384f46 | 329 | |
sjoerd1999 | 9:913a59894698 | 330 | // |
sjoerd1999 | 9:913a59894698 | 331 | // for(int i = 0; i < 360; i++) { |
sjoerd1999 | 9:913a59894698 | 332 | // aim(i); |
sjoerd1999 | 9:913a59894698 | 333 | // wait_ms(100); |
sjoerd1999 | 9:913a59894698 | 334 | // pc.printf("angle: %d\r\n",i); |
sjoerd1999 | 9:913a59894698 | 335 | // } |
sjoerd1999 | 7:bfcb74384f46 | 336 | |
sjoerd1999 | 9:913a59894698 | 337 | // aimTilt = slider.read() * 180; |
sjoerd1999 | 9:913a59894698 | 338 | // if(joyX.read() < 0.2) aimAngle -= 1; |
sjoerd1999 | 9:913a59894698 | 339 | // else if(joyX.read() > 0.8) aimAngle += 1; |
sjoerd1999 | 9:913a59894698 | 340 | // aim(aimAngle, aimTilt); |
sjoerd1999 | 9:913a59894698 | 341 | // wait_ms(50); |
sjoerd1999 | 9:913a59894698 | 342 | // getMotorPositions(); |
sjoerd1999 | 9:913a59894698 | 343 | // pc.printf("jx: %f\r\n",joyX.read()); |
sjoerd1999 | 9:913a59894698 | 344 | // pc.printf("jy: %f\r\n",joyY.read()); |
sjoerd1999 | 9:913a59894698 | 345 | // pc.printf("sli: %d\r\n", joyButton.read()); |
sjoerd1999 | 9:913a59894698 | 346 | // wait_ms(100); |
RobertoO | 0:67c50348f842 | 347 | } |
RobertoO | 0:67c50348f842 | 348 | } |