voor thomas
Dependencies: BMT-K9-Regelaar MODSERIAL mbed
Fork of BMT-K9-Regelaar by
main.cpp@0:7bc93f851767, 2013-10-08 (annotated)
- Committer:
- vsluiter
- Date:
- Tue Oct 08 21:57:18 2013 +0000
- Revision:
- 0:7bc93f851767
- Child:
- 1:9d05c0236c7e
start
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:7bc93f851767 | 1 | #include "mbed.h" |
vsluiter | 0:7bc93f851767 | 2 | #include "encoder.h" |
vsluiter | 0:7bc93f851767 | 3 | #include "MODSERIAL.h" |
vsluiter | 0:7bc93f851767 | 4 | |
vsluiter | 0:7bc93f851767 | 5 | /** Coerce -> float in, and coerce if less than min, or larger than max **/ |
vsluiter | 0:7bc93f851767 | 6 | void coerce(float * in, float min, float max); |
vsluiter | 0:7bc93f851767 | 7 | AnalogIn potmeter(PTC2); |
vsluiter | 0:7bc93f851767 | 8 | volatile bool looptimerflag; |
vsluiter | 0:7bc93f851767 | 9 | |
vsluiter | 0:7bc93f851767 | 10 | |
vsluiter | 0:7bc93f851767 | 11 | void setlooptimerflag(void) |
vsluiter | 0:7bc93f851767 | 12 | { |
vsluiter | 0:7bc93f851767 | 13 | looptimerflag = true; |
vsluiter | 0:7bc93f851767 | 14 | } |
vsluiter | 0:7bc93f851767 | 15 | |
vsluiter | 0:7bc93f851767 | 16 | |
vsluiter | 0:7bc93f851767 | 17 | int main() { |
vsluiter | 0:7bc93f851767 | 18 | Encoder motor1(PTD0,PTC9); |
vsluiter | 0:7bc93f851767 | 19 | MODSERIAL pc(USBTX,USBRX); |
vsluiter | 0:7bc93f851767 | 20 | PwmOut pwm_motor(PTA12); |
vsluiter | 0:7bc93f851767 | 21 | DigitalOut motordir(PTD3); |
vsluiter | 0:7bc93f851767 | 22 | float setpoint; |
vsluiter | 0:7bc93f851767 | 23 | float new_pwm; |
vsluiter | 0:7bc93f851767 | 24 | pc.baud(115200); |
vsluiter | 0:7bc93f851767 | 25 | Ticker looptimer; |
vsluiter | 0:7bc93f851767 | 26 | looptimer.attach(setlooptimerflag,0.01); |
vsluiter | 0:7bc93f851767 | 27 | pc.printf("bla"); |
vsluiter | 0:7bc93f851767 | 28 | while(1) { |
vsluiter | 0:7bc93f851767 | 29 | while(!looptimerflag); |
vsluiter | 0:7bc93f851767 | 30 | looptimerflag = false; |
vsluiter | 0:7bc93f851767 | 31 | setpoint = (potmeter.read())*2000; |
vsluiter | 0:7bc93f851767 | 32 | pc.printf("s: %f, %d \n\r", setpoint, motor1.getPosition()); |
vsluiter | 0:7bc93f851767 | 33 | if(motor1.getPosition() > setpoint) |
vsluiter | 0:7bc93f851767 | 34 | new_pwm = 0.5; |
vsluiter | 0:7bc93f851767 | 35 | else |
vsluiter | 0:7bc93f851767 | 36 | new_pwm = -0.5; |
vsluiter | 0:7bc93f851767 | 37 | |
vsluiter | 0:7bc93f851767 | 38 | if(new_pwm > 0) |
vsluiter | 0:7bc93f851767 | 39 | motordir = 0; |
vsluiter | 0:7bc93f851767 | 40 | else |
vsluiter | 0:7bc93f851767 | 41 | motordir = 1; |
vsluiter | 0:7bc93f851767 | 42 | pwm_motor.write(abs(new_pwm)); |
vsluiter | 0:7bc93f851767 | 43 | } |
vsluiter | 0:7bc93f851767 | 44 | } |
vsluiter | 0:7bc93f851767 | 45 | |
vsluiter | 0:7bc93f851767 | 46 | |
vsluiter | 0:7bc93f851767 | 47 | //coerces value 'in' to min or max when exceeding those values |
vsluiter | 0:7bc93f851767 | 48 | //if you'd like to understand the statement below take a google for |
vsluiter | 0:7bc93f851767 | 49 | //'ternary operators'. |
vsluiter | 0:7bc93f851767 | 50 | void coerce(float * in, float min, float max) |
vsluiter | 0:7bc93f851767 | 51 | { |
vsluiter | 0:7bc93f851767 | 52 | *in > min ? *in < max? : *in = max: *in = min; |
vsluiter | 0:7bc93f851767 | 53 | } |
vsluiter | 0:7bc93f851767 | 54 | |
vsluiter | 0:7bc93f851767 | 55 | |
vsluiter | 0:7bc93f851767 | 56 |