![](/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@29:e4f3455aaa0b, 2015-10-06 (annotated)
- Committer:
- annesteenbeek
- Date:
- Tue Oct 06 17:55:11 2015 +0000
- Revision:
- 29:e4f3455aaa0b
- Parent:
- 28:40a931dfe840
- Child:
- 30:a20f16bf8dda
Timer to calculate encoder speed
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 | 0:525558a26464 | 7 | // functions for controlling the motors |
annesteenbeek | 26:0a9e4147a31a | 8 | bool motorEnable = true; |
annesteenbeek | 6:b957d8809e7c | 9 | |
annesteenbeek | 6:b957d8809e7c | 10 | bool direction1 = false; // CCW is false(positive rotation), CW is true (neg rotation) |
annesteenbeek | 6:b957d8809e7c | 11 | bool direction2 = false; |
annesteenbeek | 6:b957d8809e7c | 12 | |
annesteenbeek | 14:0c0d1bfd94ea | 13 | float motor1Pos = 0; |
annesteenbeek | 14:0c0d1bfd94ea | 14 | float motor2Pos = 0; |
annesteenbeek | 6:b957d8809e7c | 15 | |
annesteenbeek | 14:0c0d1bfd94ea | 16 | float motorSpeed1 = 0; |
annesteenbeek | 14:0c0d1bfd94ea | 17 | float motorSpeed2 = 0; |
annesteenbeek | 6:b957d8809e7c | 18 | |
annesteenbeek | 14:0c0d1bfd94ea | 19 | float motorSetSpeed1 = 0; |
annesteenbeek | 14:0c0d1bfd94ea | 20 | float motorSetSpeed2 = 0; |
annesteenbeek | 6:b957d8809e7c | 21 | |
annesteenbeek | 6:b957d8809e7c | 22 | |
annesteenbeek | 14:0c0d1bfd94ea | 23 | float motorPWM1 = 0; |
annesteenbeek | 14:0c0d1bfd94ea | 24 | float motorPWM2 = 0; |
annesteenbeek | 6:b957d8809e7c | 25 | |
annesteenbeek | 6:b957d8809e7c | 26 | // Set PID values |
annesteenbeek | 14:0c0d1bfd94ea | 27 | float Kp1 = 1; |
annesteenbeek | 14:0c0d1bfd94ea | 28 | float Ki1 = 1; |
annesteenbeek | 14:0c0d1bfd94ea | 29 | float Kd1 = 1; |
annesteenbeek | 6:b957d8809e7c | 30 | |
annesteenbeek | 14:0c0d1bfd94ea | 31 | float Kp2 = 1; |
annesteenbeek | 14:0c0d1bfd94ea | 32 | float Ki2 = 1; |
annesteenbeek | 14:0c0d1bfd94ea | 33 | float Kd2 = 1; |
annesteenbeek | 0:525558a26464 | 34 | |
annesteenbeek | 20:3cba803cd771 | 35 | float PIDinterval = 0.2; |
annesteenbeek | 20:3cba803cd771 | 36 | |
annesteenbeek | 29:e4f3455aaa0b | 37 | float prevMotor2Pos = 0; |
annesteenbeek | 29:e4f3455aaa0b | 38 | float prevMotor1Pos = 0; |
annesteenbeek | 29:e4f3455aaa0b | 39 | float prevTime = 0; |
annesteenbeek | 29:e4f3455aaa0b | 40 | float curTime = 0; |
annesteenbeek | 25:874675516927 | 41 | |
annesteenbeek | 26:0a9e4147a31a | 42 | // Create object instances |
annesteenbeek | 26:0a9e4147a31a | 43 | // Initialze motors |
annesteenbeek | 26:0a9e4147a31a | 44 | PwmOut motor1(motor1PWMPin); |
annesteenbeek | 26:0a9e4147a31a | 45 | PwmOut motor2(motor2PWMPin); |
annesteenbeek | 26:0a9e4147a31a | 46 | |
annesteenbeek | 26:0a9e4147a31a | 47 | // Initialize encoders |
annesteenbeek | 29:e4f3455aaa0b | 48 | Encoder encoder1(enc1A, enc1B); |
annesteenbeek | 29:e4f3455aaa0b | 49 | Encoder encoder2(enc2A, enc2B); |
annesteenbeek | 25:874675516927 | 50 | |
annesteenbeek | 26:0a9e4147a31a | 51 | // Set direction pins |
annesteenbeek | 26:0a9e4147a31a | 52 | DigitalOut motor1Dir(motor1DirPin); |
annesteenbeek | 26:0a9e4147a31a | 53 | DigitalOut motor2Dir(motor2DirPin); |
annesteenbeek | 26:0a9e4147a31a | 54 | |
annesteenbeek | 26:0a9e4147a31a | 55 | // create PID instances |
annesteenbeek | 26:0a9e4147a31a | 56 | PID PIDmotor1(Kp1, Ki1, Kd1, PIDinterval); |
annesteenbeek | 26:0a9e4147a31a | 57 | PID PIDmotor2(Kp2, Ki2, Kd2, PIDinterval); |
annesteenbeek | 29:e4f3455aaa0b | 58 | Timer t; |
annesteenbeek | 25:874675516927 | 59 | |
annesteenbeek | 25:874675516927 | 60 | void motorInit(){ |
annesteenbeek | 25:874675516927 | 61 | |
annesteenbeek | 3:47c76be6d402 | 62 | motor1Dir.write(direction1); |
annesteenbeek | 3:47c76be6d402 | 63 | motor2Dir.write(direction2); |
annesteenbeek | 3:47c76be6d402 | 64 | |
annesteenbeek | 3:47c76be6d402 | 65 | // Set motor PWM period |
annesteenbeek | 3:47c76be6d402 | 66 | motor1.period(1/pwm_frequency); |
annesteenbeek | 3:47c76be6d402 | 67 | motor2.period(1/pwm_frequency); |
annesteenbeek | 3:47c76be6d402 | 68 | |
annesteenbeek | 14:0c0d1bfd94ea | 69 | PIDmotor1.setSetPoint(motorSetSpeed1); |
annesteenbeek | 14:0c0d1bfd94ea | 70 | PIDmotor2.setSetPoint(motorSetSpeed2); |
annesteenbeek | 1:80f098c05d4b | 71 | |
annesteenbeek | 20:3cba803cd771 | 72 | PIDmotor1.setProcessValue(motorSpeed1); |
annesteenbeek | 20:3cba803cd771 | 73 | PIDmotor2.setProcessValue(motorSpeed2); |
annesteenbeek | 1:80f098c05d4b | 74 | // set PID mode |
annesteenbeek | 22:c562b9a4176d | 75 | PIDmotor1.setMode(1); |
annesteenbeek | 22:c562b9a4176d | 76 | PIDmotor2.setMode(1); |
annesteenbeek | 1:80f098c05d4b | 77 | |
annesteenbeek | 2:95ba9f6f0128 | 78 | // set limits for PID output to avoid integrator build up. |
annesteenbeek | 21:3e6ebec86438 | 79 | PIDmotor1.setOutputLimits(-1.0, 1.0); |
annesteenbeek | 21:3e6ebec86438 | 80 | PIDmotor2.setOutputLimits(-1.0, 1.0); |
annesteenbeek | 29:e4f3455aaa0b | 81 | t.start(); |
annesteenbeek | 4:80e2280058ed | 82 | } |
annesteenbeek | 0:525558a26464 | 83 | |
annesteenbeek | 26:0a9e4147a31a | 84 | |
annesteenbeek | 0:525558a26464 | 85 | void motorControl(){ |
annesteenbeek | 1:80f098c05d4b | 86 | if(motorEnable){ // only run motors if switch is enabled |
annesteenbeek | 1:80f098c05d4b | 87 | |
annesteenbeek | 0:525558a26464 | 88 | // get encoder positions |
annesteenbeek | 3:47c76be6d402 | 89 | motor1Pos = encoder1.getPosition(); |
annesteenbeek | 3:47c76be6d402 | 90 | motor2Pos = encoder2.getPosition(); |
annesteenbeek | 3:47c76be6d402 | 91 | |
annesteenbeek | 0:525558a26464 | 92 | // check if motor's are within rotational boundarys |
annesteenbeek | 4:80e2280058ed | 93 | // get encoder speeds |
annesteenbeek | 29:e4f3455aaa0b | 94 | curTime = t.read() |
annesteenbeek | 29:e4f3455aaa0b | 95 | motorSpeed1 = (motor1Pos - prevMotor1Pos)/(curTime-prevTime); |
annesteenbeek | 29:e4f3455aaa0b | 96 | motorSpeed2 = (motor2Pos - prevMotor2Pos)/(curTime-prevTime); |
annesteenbeek | 29:e4f3455aaa0b | 97 | prevTime = curTime; |
annesteenbeek | 29:e4f3455aaa0b | 98 | prevMotor1Pos = motor1Pos; |
annesteenbeek | 29:e4f3455aaa0b | 99 | prevMotor2Pos = motor2Pos; |
annesteenbeek | 29:e4f3455aaa0b | 100 | |
annesteenbeek | 29:e4f3455aaa0b | 101 | |
annesteenbeek | 0:525558a26464 | 102 | // translate to x/y speed |
annesteenbeek | 0:525558a26464 | 103 | // compute new PID parameters using setpoint speeds and x/y speeds |
annesteenbeek | 14:0c0d1bfd94ea | 104 | motorPWM1 = PIDmotor1.compute(); |
annesteenbeek | 14:0c0d1bfd94ea | 105 | motorPWM2 = PIDmotor2.compute(); |
annesteenbeek | 2:95ba9f6f0128 | 106 | // translate to motor rotation speed |
annesteenbeek | 0:525558a26464 | 107 | // write new values to motor's |
annesteenbeek | 5:73bfad06b775 | 108 | if (motorPWM1 > 0 ){ // CCW rotation (unitcircle convetion) |
annesteenbeek | 5:73bfad06b775 | 109 | direction1 = false; |
annesteenbeek | 5:73bfad06b775 | 110 | }else{ |
annesteenbeek | 5:73bfad06b775 | 111 | direction1 = true; // CW rotation |
annesteenbeek | 5:73bfad06b775 | 112 | } |
annesteenbeek | 5:73bfad06b775 | 113 | if (motorPWM2 > 0 ){ // CCW rotation (unitcircle convetion) |
annesteenbeek | 5:73bfad06b775 | 114 | direction2 = false; |
annesteenbeek | 5:73bfad06b775 | 115 | }else{ |
annesteenbeek | 5:73bfad06b775 | 116 | direction2 = true; // CW rotation |
annesteenbeek | 5:73bfad06b775 | 117 | } |
annesteenbeek | 5:73bfad06b775 | 118 | motor1.write(abs(motorPWM1)); |
annesteenbeek | 28:40a931dfe840 | 119 | // motor2.write(abs(motorPWM2)); |
annesteenbeek | 28:40a931dfe840 | 120 | motor2.write(motorSetSpeed2); |
annesteenbeek | 2:95ba9f6f0128 | 121 | |
annesteenbeek | 0:525558a26464 | 122 | }else{ |
annesteenbeek | 0:525558a26464 | 123 | // write 0 to motors |
annesteenbeek | 2:95ba9f6f0128 | 124 | motor1.write(0); |
annesteenbeek | 2:95ba9f6f0128 | 125 | motor2.write(0); |
annesteenbeek | 0:525558a26464 | 126 | } |
annesteenbeek | 0:525558a26464 | 127 | } |
annesteenbeek | 0:525558a26464 | 128 | |
annesteenbeek | 0:525558a26464 | 129 | void servoControl(){ |
annesteenbeek | 0:525558a26464 | 130 | // use potMeter Value to set servo angle |
annesteenbeek | 0:525558a26464 | 131 | // (optionaly calculate xy position to keep balloon in position) |
annesteenbeek | 0:525558a26464 | 132 | // calculate z position using angle |
annesteenbeek | 0:525558a26464 | 133 | // calculate x y translation of endpoint |
annesteenbeek | 0:525558a26464 | 134 | // find new x and y speed. |
annesteenbeek | 0:525558a26464 | 135 | |
annesteenbeek | 25:874675516927 | 136 | } |
annesteenbeek | 25:874675516927 | 137 |