Script voor aansturen motoren
Dependencies: Encoder HIDScope MODSERIAL mbed-dsp mbed
main.cpp@30:6ace856e0ad7, 2014-10-31 (annotated)
- Committer:
- jessekaiser
- Date:
- Fri Oct 31 13:34:40 2014 +0000
- Revision:
- 30:6ace856e0ad7
- Parent:
- 29:4b7f6f15cedf
- Child:
- 31:0309fbf7b71c
En door!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jessekaiser | 0:d5d3b731340c | 1 | #include "mbed.h" |
jessekaiser | 0:d5d3b731340c | 2 | #include "encoder.h" |
jessekaiser | 0:d5d3b731340c | 3 | #include "HIDScope.h" |
jessekaiser | 19:3e46c457091c | 4 | #include "PwmOut.h" |
jessekaiser | 0:d5d3b731340c | 5 | |
jessekaiser | 6:f260176f704b | 6 | #define TSAMP 0.005 |
jessekaiser | 25:f45d61cc6dc6 | 7 | #define K_P1 (3.5) //voor motor1 is het 3.5 |
jessekaiser | 25:f45d61cc6dc6 | 8 | #define K_I1 (0.01 *TSAMP) //voor motor1 is het 0.01 |
jessekaiser | 25:f45d61cc6dc6 | 9 | #define K_P2 (1.0) |
jessekaiser | 25:f45d61cc6dc6 | 10 | #define K_I2 (0.01 *TSAMP) |
jessekaiser | 0:d5d3b731340c | 11 | #define I_LIMIT 1. |
jessekaiser | 18:fb85c58a4106 | 12 | #define PI 3.14159265 |
jessekaiser | 18:fb85c58a4106 | 13 | #define l_arm 0.5 |
jessekaiser | 0:d5d3b731340c | 14 | |
jessekaiser | 7:697293226e5e | 15 | #define M1_PWM PTC8 |
jessekaiser | 7:697293226e5e | 16 | #define M1_DIR PTC9 |
jessekaiser | 7:697293226e5e | 17 | #define M2_PWM PTA5 |
jessekaiser | 7:697293226e5e | 18 | #define M2_DIR PTA4 |
jessekaiser | 7:697293226e5e | 19 | |
jessekaiser | 7:697293226e5e | 20 | //Groene kabel moet op de GROUND en blauw op de 3.3v aansluiting |
jessekaiser | 0:d5d3b731340c | 21 | |
jessekaiser | 3:dcc4cebba0d7 | 22 | Serial pc(USBTX, USBRX); |
jessekaiser | 4:db3dad7e53e3 | 23 | DigitalOut led1(LED_RED); |
jessekaiser | 5:e4b9cc904928 | 24 | HIDScope scope(2); |
jessekaiser | 0:d5d3b731340c | 25 | |
jessekaiser | 7:697293226e5e | 26 | //motor 25D |
jessekaiser | 7:697293226e5e | 27 | Encoder motor1(PTD3,PTD5); //wit, geel |
jessekaiser | 7:697293226e5e | 28 | PwmOut pwm_motor1(M2_PWM); |
jessekaiser | 7:697293226e5e | 29 | DigitalOut motordir1(M2_DIR); |
jessekaiser | 3:dcc4cebba0d7 | 30 | |
jessekaiser | 7:697293226e5e | 31 | //motor2 37D |
jessekaiser | 7:697293226e5e | 32 | Encoder motor2(PTD2, PTD0); //wit, geel |
jessekaiser | 7:697293226e5e | 33 | PwmOut pwm_motor2(M1_PWM); |
jessekaiser | 7:697293226e5e | 34 | DigitalOut motordir2(M1_DIR); |
jessekaiser | 28:4f2e15118f13 | 35 | |
jessekaiser | 18:fb85c58a4106 | 36 | float pwm1_percentage = 0; |
jessekaiser | 18:fb85c58a4106 | 37 | float pwm2_percentage = 0; |
jessekaiser | 18:fb85c58a4106 | 38 | int cur_pos_motor1; |
jessekaiser | 18:fb85c58a4106 | 39 | int prev_pos_motor1 = 0; |
jessekaiser | 18:fb85c58a4106 | 40 | int cur_pos_motor2; |
jessekaiser | 18:fb85c58a4106 | 41 | int prev_pos_motor2 = 0; |
jessekaiser | 18:fb85c58a4106 | 42 | float speed1_rad; |
jessekaiser | 19:3e46c457091c | 43 | float speed2_rad; |
jessekaiser | 19:3e46c457091c | 44 | float pos_motor1_rad; |
jessekaiser | 19:3e46c457091c | 45 | float pos_motor2_rad; |
jessekaiser | 30:6ace856e0ad7 | 46 | int staat1 = 0; |
jessekaiser | 18:fb85c58a4106 | 47 | |
jessekaiser | 21:c856b7752d7d | 48 | void clamp(float* in, float min, float max) // "*" is een pointer (verwijst naar het adres waar een variabele instaat). Dus je slaat niet de variabele op |
jessekaiser | 21:c856b7752d7d | 49 | // maar de locatie van de variabele. |
jessekaiser | 21:c856b7752d7d | 50 | { |
jessekaiser | 21:c856b7752d7d | 51 | *in > min ? /*(*/*in < max? /*niets doen*/ : *in = max/*)*/: *in = min; // a ? b : c --> als a waar is, dan doe je b, en anders c |
jessekaiser | 21:c856b7752d7d | 52 | // *in = het getal dat staat op locatie van in --> waarde van new_pwm |
jessekaiser | 7:697293226e5e | 53 | } |
jessekaiser | 3:dcc4cebba0d7 | 54 | |
jessekaiser | 25:f45d61cc6dc6 | 55 | float pid1(float setpoint1, float measurement1) |
jessekaiser | 21:c856b7752d7d | 56 | { |
jessekaiser | 25:f45d61cc6dc6 | 57 | float error1; |
jessekaiser | 25:f45d61cc6dc6 | 58 | float out_p1 = 0; |
jessekaiser | 25:f45d61cc6dc6 | 59 | static float out_i1 = 0; |
jessekaiser | 25:f45d61cc6dc6 | 60 | error1 = (setpoint1 - measurement1); |
jessekaiser | 25:f45d61cc6dc6 | 61 | out_p1 = error1*K_P1; |
jessekaiser | 25:f45d61cc6dc6 | 62 | out_i1 += error1*K_I1; |
jessekaiser | 25:f45d61cc6dc6 | 63 | clamp(&out_i1,-I_LIMIT,I_LIMIT); |
jessekaiser | 25:f45d61cc6dc6 | 64 | return out_p1 + out_i1; |
jessekaiser | 24:7fbd904d191c | 65 | } |
jessekaiser | 24:7fbd904d191c | 66 | |
jessekaiser | 28:4f2e15118f13 | 67 | float pid2(float setpoint2, float measurement2) |
jessekaiser | 28:4f2e15118f13 | 68 | { |
jessekaiser | 28:4f2e15118f13 | 69 | float error2; |
jessekaiser | 28:4f2e15118f13 | 70 | float out_p2 = 0; |
jessekaiser | 28:4f2e15118f13 | 71 | static float out_i2 = 0; |
jessekaiser | 28:4f2e15118f13 | 72 | error2 = (setpoint2 - measurement2); |
jessekaiser | 28:4f2e15118f13 | 73 | out_p2 = error2*K_P2; |
jessekaiser | 28:4f2e15118f13 | 74 | out_i2 += error2*K_I2; |
jessekaiser | 28:4f2e15118f13 | 75 | clamp(&out_i2,-I_LIMIT,I_LIMIT); |
jessekaiser | 28:4f2e15118f13 | 76 | return out_p2 + out_i2; |
jessekaiser | 28:4f2e15118f13 | 77 | } |
jessekaiser | 25:f45d61cc6dc6 | 78 | float prev_setpoint1 = 0; |
jessekaiser | 25:f45d61cc6dc6 | 79 | float setpoint1 = 0; |
jessekaiser | 25:f45d61cc6dc6 | 80 | float prev_setpoint2 = 0; |
jessekaiser | 25:f45d61cc6dc6 | 81 | float setpoint2 = 0; |
jessekaiser | 25:f45d61cc6dc6 | 82 | |
jessekaiser | 25:f45d61cc6dc6 | 83 | void batje_links () |
jessekaiser | 24:7fbd904d191c | 84 | { |
jessekaiser | 25:f45d61cc6dc6 | 85 | speed1_rad = -1.0; //positief is CCW, negatief CW (boven aanzicht) |
jessekaiser | 25:f45d61cc6dc6 | 86 | cur_pos_motor1 = motor1.getPosition(); |
jessekaiser | 25:f45d61cc6dc6 | 87 | pos_motor1_rad = (float)cur_pos_motor1/(3600.0/(2.0*PI)); |
jessekaiser | 25:f45d61cc6dc6 | 88 | setpoint1 = prev_setpoint1 + TSAMP * speed1_rad; |
jessekaiser | 30:6ace856e0ad7 | 89 | if(setpoint1 > (180*2.3*2.0*PI/360)) { //setpoint in graden |
jessekaiser | 30:6ace856e0ad7 | 90 | setpoint1 = (180*2.3*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 91 | } |
jessekaiser | 30:6ace856e0ad7 | 92 | if(setpoint1 < -(180*2.3*2.0*PI/360)) { |
jessekaiser | 30:6ace856e0ad7 | 93 | setpoint1 = -(180*2.3*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 94 | } |
jessekaiser | 25:f45d61cc6dc6 | 95 | |
jessekaiser | 25:f45d61cc6dc6 | 96 | pwm_motor1.write(abs(pwm1_percentage)); |
jessekaiser | 25:f45d61cc6dc6 | 97 | prev_setpoint1 = setpoint1; |
jessekaiser | 25:f45d61cc6dc6 | 98 | wait(TSAMP); |
jessekaiser | 25:f45d61cc6dc6 | 99 | |
jessekaiser | 24:7fbd904d191c | 100 | } |
jessekaiser | 24:7fbd904d191c | 101 | |
jessekaiser | 24:7fbd904d191c | 102 | void batje_rechts () |
jessekaiser | 24:7fbd904d191c | 103 | { |
jessekaiser | 25:f45d61cc6dc6 | 104 | speed1_rad = 2.0; //positief is CCW, negatief CW (boven aanzicht) |
jessekaiser | 25:f45d61cc6dc6 | 105 | setpoint1 = prev_setpoint1 + TSAMP * speed1_rad; |
jessekaiser | 30:6ace856e0ad7 | 106 | if(setpoint1 > (360*2.3*2.0*PI/360)) { //setpoint in graden |
jessekaiser | 30:6ace856e0ad7 | 107 | setpoint1 = (360*2.3*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 108 | } |
jessekaiser | 30:6ace856e0ad7 | 109 | if(setpoint1 < -(360*2.3*2.0*PI/360)) { |
jessekaiser | 30:6ace856e0ad7 | 110 | setpoint1 = -(360*2.3*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 111 | } |
jessekaiser | 25:f45d61cc6dc6 | 112 | pwm_motor1.write(abs(pwm1_percentage)); |
jessekaiser | 25:f45d61cc6dc6 | 113 | prev_setpoint1 = setpoint1; |
jessekaiser | 30:6ace856e0ad7 | 114 | if(setpoint1 > (360*2.3*2.0*PI/360)) { |
jessekaiser | 30:6ace856e0ad7 | 115 | staat1 = 1; |
jessekaiser | 25:f45d61cc6dc6 | 116 | } |
jessekaiser | 30:6ace856e0ad7 | 117 | |
jessekaiser | 25:f45d61cc6dc6 | 118 | |
jessekaiser | 25:f45d61cc6dc6 | 119 | |
jessekaiser | 25:f45d61cc6dc6 | 120 | } |
jessekaiser | 25:f45d61cc6dc6 | 121 | |
jessekaiser | 25:f45d61cc6dc6 | 122 | void arm_hoog () //LET OP, PAS VARIABELE NOG AAN DIT IS VOOR TESTEN |
jessekaiser | 25:f45d61cc6dc6 | 123 | { |
jessekaiser | 25:f45d61cc6dc6 | 124 | speed1_rad = 6.0; //positief is CCW, negatief CW (boven aanzicht) |
jessekaiser | 25:f45d61cc6dc6 | 125 | setpoint1 = prev_setpoint1 + TSAMP * speed1_rad; |
jessekaiser | 25:f45d61cc6dc6 | 126 | if(setpoint1 > (155.0*2.0*PI/360)) { //setpoint in graden |
jessekaiser | 25:f45d61cc6dc6 | 127 | setpoint1 = (155.0*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 128 | } |
jessekaiser | 25:f45d61cc6dc6 | 129 | if(setpoint1 < -(155.0*2.0*PI/360)) { |
jessekaiser | 25:f45d61cc6dc6 | 130 | setpoint1 = -(155.0*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 131 | } |
jessekaiser | 25:f45d61cc6dc6 | 132 | pwm_motor1.write(abs(pwm1_percentage)); |
jessekaiser | 25:f45d61cc6dc6 | 133 | prev_setpoint1 = setpoint1; |
jessekaiser | 26:5981d4b1144e | 134 | if(setpoint1 >= (155.0*2.0*PI/360)-0.1) { |
jessekaiser | 30:6ace856e0ad7 | 135 | staat1 = 1; |
jessekaiser | 26:5981d4b1144e | 136 | } |
jessekaiser | 26:5981d4b1144e | 137 | |
jessekaiser | 25:f45d61cc6dc6 | 138 | |
jessekaiser | 25:f45d61cc6dc6 | 139 | } |
jessekaiser | 25:f45d61cc6dc6 | 140 | |
jessekaiser | 25:f45d61cc6dc6 | 141 | void arm_mid () |
jessekaiser | 25:f45d61cc6dc6 | 142 | { |
jessekaiser | 28:4f2e15118f13 | 143 | speed1_rad = -2.0; //positief is CCW, negatief CW (boven aanzicht) |
jessekaiser | 26:5981d4b1144e | 144 | setpoint1 = prev_setpoint1 + (TSAMP * speed1_rad); |
jessekaiser | 25:f45d61cc6dc6 | 145 | if(setpoint1 > (155*2.0*PI/360)) { //setpoint in graden |
jessekaiser | 25:f45d61cc6dc6 | 146 | setpoint1 = (155*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 147 | } |
jessekaiser | 28:4f2e15118f13 | 148 | if(setpoint1 < -(0.0*2.0*PI/360)) { |
jessekaiser | 28:4f2e15118f13 | 149 | setpoint1 = -(0.0*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 150 | } |
jessekaiser | 25:f45d61cc6dc6 | 151 | prev_setpoint1 = setpoint1; |
jessekaiser | 25:f45d61cc6dc6 | 152 | |
jessekaiser | 26:5981d4b1144e | 153 | //wait(TSAMP); |
jessekaiser | 25:f45d61cc6dc6 | 154 | } |
jessekaiser | 25:f45d61cc6dc6 | 155 | |
jessekaiser | 25:f45d61cc6dc6 | 156 | void arm_laag () |
jessekaiser | 25:f45d61cc6dc6 | 157 | { |
jessekaiser | 28:4f2e15118f13 | 158 | speed1_rad = -2.0; //positief is CCW, negatief CW (boven aanzicht) |
jessekaiser | 28:4f2e15118f13 | 159 | setpoint1 = prev_setpoint1 + (TSAMP * speed1_rad); |
jessekaiser | 28:4f2e15118f13 | 160 | if(setpoint1 > (155*2.0*PI/360)) { //setpoint in graden |
jessekaiser | 28:4f2e15118f13 | 161 | setpoint1 = (155*2.0*PI/360); |
jessekaiser | 28:4f2e15118f13 | 162 | } |
jessekaiser | 28:4f2e15118f13 | 163 | if(setpoint1 < -(0.0*2.0*PI/360)) { |
jessekaiser | 28:4f2e15118f13 | 164 | setpoint1 = -(0.0*2.0*PI/360); |
jessekaiser | 25:f45d61cc6dc6 | 165 | } |
jessekaiser | 28:4f2e15118f13 | 166 | prev_setpoint1 = setpoint1; |
jessekaiser | 28:4f2e15118f13 | 167 | } |
jessekaiser | 25:f45d61cc6dc6 | 168 | |
jessekaiser | 28:4f2e15118f13 | 169 | void arm_begin () |
jessekaiser | 28:4f2e15118f13 | 170 | { |
jessekaiser | 28:4f2e15118f13 | 171 | speed1_rad = -2.0; //positief is CCW, negatief CW (boven aanzicht) |
jessekaiser | 28:4f2e15118f13 | 172 | setpoint1 = prev_setpoint1 + (TSAMP * speed1_rad); |
jessekaiser | 28:4f2e15118f13 | 173 | if(setpoint1 > (155*2.0*PI/360)) { //setpoint in graden |
jessekaiser | 28:4f2e15118f13 | 174 | setpoint1 = (155*2.0*PI/360); |
jessekaiser | 28:4f2e15118f13 | 175 | } |
jessekaiser | 28:4f2e15118f13 | 176 | if(setpoint1 < -(0.0*2.0*PI/360)) { |
jessekaiser | 28:4f2e15118f13 | 177 | setpoint1 = -(0.0*2.0*PI/360); |
jessekaiser | 28:4f2e15118f13 | 178 | } |
jessekaiser | 28:4f2e15118f13 | 179 | prev_setpoint1 = setpoint1; |
jessekaiser | 25:f45d61cc6dc6 | 180 | } |
jessekaiser | 25:f45d61cc6dc6 | 181 | |
jessekaiser | 0:d5d3b731340c | 182 | int main() |
jessekaiser | 3:dcc4cebba0d7 | 183 | { |
jessekaiser | 26:5981d4b1144e | 184 | int wait_iterator = 0; |
jessekaiser | 25:f45d61cc6dc6 | 185 | pwm_motor1.period_us(100); |
jessekaiser | 25:f45d61cc6dc6 | 186 | motor1.setPosition(0); |
jessekaiser | 25:f45d61cc6dc6 | 187 | pwm_motor2.period_us(100); |
jessekaiser | 25:f45d61cc6dc6 | 188 | motor2.setPosition(0); |
jessekaiser | 25:f45d61cc6dc6 | 189 | pc.baud(115200); |
jessekaiser | 25:f45d61cc6dc6 | 190 | |
jessekaiser | 0:d5d3b731340c | 191 | while(1) { |
jessekaiser | 26:5981d4b1144e | 192 | //MOTOR1 |
jessekaiser | 25:f45d61cc6dc6 | 193 | pc.printf("%d \r\n", motor1.getPosition()); |
jessekaiser | 26:5981d4b1144e | 194 | cur_pos_motor1 = motor1.getPosition(); |
jessekaiser | 26:5981d4b1144e | 195 | pos_motor1_rad = (float)cur_pos_motor1/(3200.0/(2.0*PI)); //moet 4128 |
jessekaiser | 25:f45d61cc6dc6 | 196 | pwm1_percentage = pid1(setpoint1, pos_motor1_rad); |
jessekaiser | 22:22cb158bcea4 | 197 | if (pwm1_percentage < -1.0) { |
jessekaiser | 22:22cb158bcea4 | 198 | pwm1_percentage = -1.0; |
jessekaiser | 24:7fbd904d191c | 199 | } |
jessekaiser | 24:7fbd904d191c | 200 | if (pwm1_percentage > 1.0) { |
jessekaiser | 24:7fbd904d191c | 201 | pwm1_percentage = 1.0; |
jessekaiser | 22:22cb158bcea4 | 202 | } |
jessekaiser | 26:5981d4b1144e | 203 | pwm_motor1.write(abs(pwm1_percentage)); |
jessekaiser | 24:7fbd904d191c | 204 | if(pwm1_percentage > 0) { |
jessekaiser | 24:7fbd904d191c | 205 | motordir1 = 0; |
jessekaiser | 24:7fbd904d191c | 206 | } else { |
jessekaiser | 22:22cb158bcea4 | 207 | motordir1 = 1; |
jessekaiser | 24:7fbd904d191c | 208 | } |
jessekaiser | 29:4b7f6f15cedf | 209 | |
jessekaiser | 29:4b7f6f15cedf | 210 | //MOTOR2 |
jessekaiser | 29:4b7f6f15cedf | 211 | cur_pos_motor2 = motor2.getPosition(); |
jessekaiser | 29:4b7f6f15cedf | 212 | pos_motor2_rad = (float)cur_pos_motor2/(3200.0/(2.0*PI)); //moet 4128 |
jessekaiser | 29:4b7f6f15cedf | 213 | pwm2_percentage = pid1(setpoint2, pos_motor2_rad); |
jessekaiser | 29:4b7f6f15cedf | 214 | if (pwm2_percentage < -1.0) { |
jessekaiser | 29:4b7f6f15cedf | 215 | pwm2_percentage = -1.0; |
jessekaiser | 29:4b7f6f15cedf | 216 | } |
jessekaiser | 29:4b7f6f15cedf | 217 | if (pwm2_percentage > 1.0) { |
jessekaiser | 29:4b7f6f15cedf | 218 | pwm2_percentage = 1.0; |
jessekaiser | 29:4b7f6f15cedf | 219 | } |
jessekaiser | 29:4b7f6f15cedf | 220 | pwm_motor2.write(abs(pwm2_percentage)); |
jessekaiser | 29:4b7f6f15cedf | 221 | if(pwm2_percentage > 0) { |
jessekaiser | 29:4b7f6f15cedf | 222 | motordir2 = 0; |
jessekaiser | 29:4b7f6f15cedf | 223 | } else { |
jessekaiser | 29:4b7f6f15cedf | 224 | motordir2 = 1; |
jessekaiser | 29:4b7f6f15cedf | 225 | } |
jessekaiser | 26:5981d4b1144e | 226 | wait(TSAMP); |
jessekaiser | 26:5981d4b1144e | 227 | //STATES |
jessekaiser | 30:6ace856e0ad7 | 228 | if(staat1 == 0) { |
jessekaiser | 25:f45d61cc6dc6 | 229 | arm_hoog(); |
jessekaiser | 26:5981d4b1144e | 230 | wait_iterator = 0; |
jessekaiser | 25:f45d61cc6dc6 | 231 | } |
jessekaiser | 30:6ace856e0ad7 | 232 | else if(staat1 ==1) |
jessekaiser | 26:5981d4b1144e | 233 | { |
jessekaiser | 26:5981d4b1144e | 234 | wait_iterator++; |
jessekaiser | 26:5981d4b1144e | 235 | if(wait_iterator > 200) |
jessekaiser | 30:6ace856e0ad7 | 236 | staat1 = 2; |
jessekaiser | 26:5981d4b1144e | 237 | } |
jessekaiser | 26:5981d4b1144e | 238 | else |
jessekaiser | 25:f45d61cc6dc6 | 239 | { |
jessekaiser | 28:4f2e15118f13 | 240 | arm_begin(); |
jessekaiser | 24:7fbd904d191c | 241 | } |
jessekaiser | 25:f45d61cc6dc6 | 242 | } |
jessekaiser | 0:d5d3b731340c | 243 | } |
jessekaiser | 18:fb85c58a4106 | 244 | |
jessekaiser | 18:fb85c58a4106 | 245 |