Script voor aansturen motoren
Dependencies: Encoder HIDScope MODSERIAL mbed-dsp mbed
main.cpp@10:a2a2e7bfdc86, 2014-10-28 (annotated)
- Committer:
- jessekaiser
- Date:
- Tue Oct 28 00:11:24 2014 +0000
- Revision:
- 10:a2a2e7bfdc86
- Parent:
- 9:18e7a0273106
- Child:
- 11:ba0a33e6ff0d
motor1 kan 90 graden draaien en weer terug. Nog geen regelaar erop
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 | 9:18e7a0273106 | 4 | #include "MODSERIAL.h" |
jessekaiser | 0:d5d3b731340c | 5 | |
jessekaiser | 9:18e7a0273106 | 6 | |
jessekaiser | 9:18e7a0273106 | 7 | #define TSAMP 0.01 |
jessekaiser | 0:d5d3b731340c | 8 | #define K_P (0.1) |
jessekaiser | 0:d5d3b731340c | 9 | #define K_I (0.03 *TSAMP) |
jessekaiser | 0:d5d3b731340c | 10 | #define K_D (0.001 /TSAMP) |
jessekaiser | 0:d5d3b731340c | 11 | #define I_LIMIT 1. |
jessekaiser | 0:d5d3b731340c | 12 | |
jessekaiser | 7:697293226e5e | 13 | #define M1_PWM PTC8 |
jessekaiser | 7:697293226e5e | 14 | #define M1_DIR PTC9 |
jessekaiser | 7:697293226e5e | 15 | #define M2_PWM PTA5 |
jessekaiser | 7:697293226e5e | 16 | #define M2_DIR PTA4 |
jessekaiser | 7:697293226e5e | 17 | |
jessekaiser | 7:697293226e5e | 18 | //Groene kabel moet op de GROUND en blauw op de 3.3v aansluiting |
jessekaiser | 0:d5d3b731340c | 19 | |
jessekaiser | 3:dcc4cebba0d7 | 20 | Serial pc(USBTX, USBRX); |
jessekaiser | 4:db3dad7e53e3 | 21 | DigitalOut led1(LED_RED); |
jessekaiser | 9:18e7a0273106 | 22 | HIDScope scope(4); |
jessekaiser | 0:d5d3b731340c | 23 | |
jessekaiser | 7:697293226e5e | 24 | //motor 25D |
jessekaiser | 9:18e7a0273106 | 25 | Encoder motor1(PTD3,PTD5,true); //wit, geel |
jessekaiser | 7:697293226e5e | 26 | PwmOut pwm_motor1(M2_PWM); |
jessekaiser | 7:697293226e5e | 27 | DigitalOut motordir1(M2_DIR); |
jessekaiser | 3:dcc4cebba0d7 | 28 | |
jessekaiser | 7:697293226e5e | 29 | //motor2 37D |
jessekaiser | 9:18e7a0273106 | 30 | Encoder motor2(PTD2, PTD0,true); //wit, geel |
jessekaiser | 7:697293226e5e | 31 | PwmOut pwm_motor2(M1_PWM); |
jessekaiser | 7:697293226e5e | 32 | DigitalOut motordir2(M1_DIR); |
jessekaiser | 7:697293226e5e | 33 | |
jessekaiser | 7:697293226e5e | 34 | bool flip=false; |
jessekaiser | 7:697293226e5e | 35 | |
jessekaiser | 7:697293226e5e | 36 | void attime() |
jessekaiser | 0:d5d3b731340c | 37 | { |
jessekaiser | 7:697293226e5e | 38 | flip = !flip; |
jessekaiser | 0:d5d3b731340c | 39 | } |
jessekaiser | 0:d5d3b731340c | 40 | |
jessekaiser | 7:697293226e5e | 41 | void looper() |
jessekaiser | 7:697293226e5e | 42 | { |
jessekaiser | 10:a2a2e7bfdc86 | 43 | /*motordir1=0; |
jessekaiser | 10:a2a2e7bfdc86 | 44 | pwm_motor1.write(0.06); |
jessekaiser | 7:697293226e5e | 45 | scope.set(0, motor1.getPosition()); |
jessekaiser | 8:0bbbe93bd1c4 | 46 | scope.set(1, motor1.getSpeed()); |
jessekaiser | 8:0bbbe93bd1c4 | 47 | scope.set(2, motor2.getPosition()); |
jessekaiser | 10:a2a2e7bfdc86 | 48 | scope.set(3, motor2.getSpeed()); |
jessekaiser | 10:a2a2e7bfdc86 | 49 | scope.send();*/ |
jessekaiser | 7:697293226e5e | 50 | } |
jessekaiser | 3:dcc4cebba0d7 | 51 | |
jessekaiser | 8:0bbbe93bd1c4 | 52 | /*void clamp(float * in, float min, float max) |
jessekaiser | 8:0bbbe93bd1c4 | 53 | { |
jessekaiser | 8:0bbbe93bd1c4 | 54 | *in > min ? *in < max? : *in = max: *in = min; |
jessekaiser | 8:0bbbe93bd1c4 | 55 | } |
jessekaiser | 8:0bbbe93bd1c4 | 56 | |
jessekaiser | 8:0bbbe93bd1c4 | 57 | float pi(float setpoint, float measurement) |
jessekaiser | 8:0bbbe93bd1c4 | 58 | { |
jessekaiser | 8:0bbbe93bd1c4 | 59 | float error; |
jessekaiser | 8:0bbbe93bd1c4 | 60 | float out_p = 0; |
jessekaiser | 8:0bbbe93bd1c4 | 61 | static float out_i = 0; |
jessekaiser | 8:0bbbe93bd1c4 | 62 | error = setpoint - motor1.getPosition(); |
jessekaiser | 8:0bbbe93bd1c4 | 63 | out_p = error*K_P; |
jessekaiser | 8:0bbbe93bd1c4 | 64 | out_i += error*K_I; |
jessekaiser | 8:0bbbe93bd1c4 | 65 | clamp(&out_i,-I_LIMIT,I_LIMIT); |
jessekaiser | 8:0bbbe93bd1c4 | 66 | |
jessekaiser | 8:0bbbe93bd1c4 | 67 | return out_p + out_i; |
jessekaiser | 8:0bbbe93bd1c4 | 68 | }*/ |
jessekaiser | 3:dcc4cebba0d7 | 69 | |
jessekaiser | 0:d5d3b731340c | 70 | int main() |
jessekaiser | 10:a2a2e7bfdc86 | 71 | { |
jessekaiser | 10:a2a2e7bfdc86 | 72 | wait(1); |
jessekaiser | 8:0bbbe93bd1c4 | 73 | pwm_motor1.period_us(100); |
jessekaiser | 10:a2a2e7bfdc86 | 74 | motordir1=0; |
jessekaiser | 10:a2a2e7bfdc86 | 75 | pwm_motor1.write(0.7); |
jessekaiser | 10:a2a2e7bfdc86 | 76 | wait(0.78); |
jessekaiser | 8:0bbbe93bd1c4 | 77 | pwm_motor1.write(0); |
jessekaiser | 8:0bbbe93bd1c4 | 78 | wait(1); |
jessekaiser | 8:0bbbe93bd1c4 | 79 | motordir1=1; |
jessekaiser | 10:a2a2e7bfdc86 | 80 | pwm_motor1.write(0.7); |
jessekaiser | 10:a2a2e7bfdc86 | 81 | wait(0.78); |
jessekaiser | 10:a2a2e7bfdc86 | 82 | pwm_motor1.write(0); |
jessekaiser | 10:a2a2e7bfdc86 | 83 | wait(1); |
jessekaiser | 10:a2a2e7bfdc86 | 84 | motordir1=1; |
jessekaiser | 10:a2a2e7bfdc86 | 85 | |
jessekaiser | 10:a2a2e7bfdc86 | 86 | |
jessekaiser | 9:18e7a0273106 | 87 | |
jessekaiser | 7:697293226e5e | 88 | Ticker log_timer; |
jessekaiser | 7:697293226e5e | 89 | Ticker timer; |
jessekaiser | 7:697293226e5e | 90 | log_timer.attach(looper, TSAMP); |
jessekaiser | 7:697293226e5e | 91 | timer.attach(&attime, 7); |
jessekaiser | 8:0bbbe93bd1c4 | 92 | |
jessekaiser | 9:18e7a0273106 | 93 | pc.baud(115200); |
jessekaiser | 0:d5d3b731340c | 94 | while(1) { |
jessekaiser | 9:18e7a0273106 | 95 | wait(0.2); |
jessekaiser | 10:a2a2e7bfdc86 | 96 | pc.printf("%d, %f \r\n", motor1.getPosition(), motor1.getSpeed()); |
jessekaiser | 6:f260176f704b | 97 | } |
jessekaiser | 0:d5d3b731340c | 98 | } |