Script voor aansturen motoren
Dependencies: Encoder HIDScope MODSERIAL mbed-dsp mbed
main.cpp@6:f260176f704b, 2014-10-23 (annotated)
- Committer:
- jessekaiser
- Date:
- Thu Oct 23 10:26:25 2014 +0000
- Revision:
- 6:f260176f704b
- Parent:
- 5:e4b9cc904928
- Child:
- 7:697293226e5e
Motor aansturen met toetsenbord.;
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 | 0:d5d3b731340c | 4 | |
jessekaiser | 6:f260176f704b | 5 | #define TSAMP 0.005 |
jessekaiser | 0:d5d3b731340c | 6 | #define K_P (0.1) |
jessekaiser | 0:d5d3b731340c | 7 | #define K_I (0.03 *TSAMP) |
jessekaiser | 0:d5d3b731340c | 8 | #define K_D (0.001 /TSAMP) |
jessekaiser | 0:d5d3b731340c | 9 | #define I_LIMIT 1. |
jessekaiser | 0:d5d3b731340c | 10 | |
jessekaiser | 0:d5d3b731340c | 11 | #define M1_PWM PTC8 //blauw |
jessekaiser | 0:d5d3b731340c | 12 | #define M1_DIR PTC9 //groen |
jessekaiser | 2:11076f69e0a7 | 13 | #define M2_PWM PTA5 //blauw |
jessekaiser | 2:11076f69e0a7 | 14 | #define M2_DIR PTA4 //groen |
jessekaiser | 0:d5d3b731340c | 15 | |
jessekaiser | 3:dcc4cebba0d7 | 16 | Serial pc(USBTX, USBRX); |
jessekaiser | 4:db3dad7e53e3 | 17 | DigitalOut led1(LED_RED); |
jessekaiser | 3:dcc4cebba0d7 | 18 | |
jessekaiser | 3:dcc4cebba0d7 | 19 | |
jessekaiser | 0:d5d3b731340c | 20 | void clamp(float * in, float min, float max); |
jessekaiser | 0:d5d3b731340c | 21 | float pid(float setpoint, float measurement); |
jessekaiser | 0:d5d3b731340c | 22 | volatile bool looptimerflag; |
jessekaiser | 5:e4b9cc904928 | 23 | HIDScope scope(2); |
jessekaiser | 0:d5d3b731340c | 24 | |
jessekaiser | 3:dcc4cebba0d7 | 25 | |
jessekaiser | 0:d5d3b731340c | 26 | void setlooptimerflag(void) |
jessekaiser | 0:d5d3b731340c | 27 | { |
jessekaiser | 0:d5d3b731340c | 28 | looptimerflag = true; |
jessekaiser | 0:d5d3b731340c | 29 | } |
jessekaiser | 0:d5d3b731340c | 30 | |
jessekaiser | 3:dcc4cebba0d7 | 31 | |
jessekaiser | 3:dcc4cebba0d7 | 32 | |
jessekaiser | 0:d5d3b731340c | 33 | int main() |
jessekaiser | 3:dcc4cebba0d7 | 34 | { |
jessekaiser | 3:dcc4cebba0d7 | 35 | //Let op dat de jumpers goed staan als het motortje niet wilt draaien. De E1 jumper moet onder de nummer 7 pin. De locatie van de M1 pin |
jessekaiser | 3:dcc4cebba0d7 | 36 | //bepaalt of de motor CW of CCW draait. |
jessekaiser | 0:d5d3b731340c | 37 | //start Encoder-> first pin should be PTDx or PTAx, second pin doesn't matter |
jessekaiser | 3:dcc4cebba0d7 | 38 | |
jessekaiser | 3:dcc4cebba0d7 | 39 | //motor 25D |
jessekaiser | 0:d5d3b731340c | 40 | Encoder motor1(PTD3,PTD5); //wit, geel |
jessekaiser | 6:f260176f704b | 41 | PwmOut pwm_motor1(M2_PWM); // |
jessekaiser | 2:11076f69e0a7 | 42 | pwm_motor1.period_us(75); //10kHz PWM frequency |
jessekaiser | 6:f260176f704b | 43 | DigitalOut motordir1(M2_DIR); |
jessekaiser | 3:dcc4cebba0d7 | 44 | |
jessekaiser | 3:dcc4cebba0d7 | 45 | //motor2 37D |
jessekaiser | 2:11076f69e0a7 | 46 | Encoder motor2(PTD2, PTD0); //wit, geel |
jessekaiser | 6:f260176f704b | 47 | PwmOut pwm_motor2(M1_PWM); |
jessekaiser | 2:11076f69e0a7 | 48 | pwm_motor2.period_us(75); |
jessekaiser | 6:f260176f704b | 49 | DigitalOut motordir2(M1_DIR); |
jessekaiser | 3:dcc4cebba0d7 | 50 | |
jessekaiser | 6:f260176f704b | 51 | //char c ='0'; |
jessekaiser | 0:d5d3b731340c | 52 | while(1) { |
jessekaiser | 6:f260176f704b | 53 | wait(13); |
jessekaiser | 4:db3dad7e53e3 | 54 | pwm_motor1.write(1); |
jessekaiser | 6:f260176f704b | 55 | |
jessekaiser | 6:f260176f704b | 56 | |
jessekaiser | 6:f260176f704b | 57 | |
jessekaiser | 5:e4b9cc904928 | 58 | |
jessekaiser | 5:e4b9cc904928 | 59 | wait(0.01); |
jessekaiser | 6:f260176f704b | 60 | scope.set(0, motor1.getPosition()); |
jessekaiser | 5:e4b9cc904928 | 61 | scope.set(1, motor2.getPosition()); |
jessekaiser | 5:e4b9cc904928 | 62 | scope.send(); |
jessekaiser | 5:e4b9cc904928 | 63 | /*do { |
jessekaiser | 4:db3dad7e53e3 | 64 | |
jessekaiser | 4:db3dad7e53e3 | 65 | if(pc.readable()) { |
jessekaiser | 4:db3dad7e53e3 | 66 | c = pc.getc(); |
jessekaiser | 4:db3dad7e53e3 | 67 | } |
jessekaiser | 5:e4b9cc904928 | 68 | } while((c !='1')); |
jessekaiser | 0:d5d3b731340c | 69 | |
jessekaiser | 5:e4b9cc904928 | 70 | c = '0'; |
jessekaiser | 5:e4b9cc904928 | 71 | pwm_motor1.write(0.5); |
jessekaiser | 5:e4b9cc904928 | 72 | motordir1.write(0); |
jessekaiser | 5:e4b9cc904928 | 73 | wait(3); |
jessekaiser | 5:e4b9cc904928 | 74 | |
jessekaiser | 6:f260176f704b | 75 | }*/ |
jessekaiser | 6:f260176f704b | 76 | } |
jessekaiser | 0:d5d3b731340c | 77 | } |