Start van regelaar
Dependencies: Encoder MODSERIAL mbed
Dependents: K9motoraansturing_copy lichtpoortjes
main.cpp@3:c9c1c0ebf89d, 2013-10-09 (annotated)
- Committer:
- vsluiter
- Date:
- Wed Oct 09 09:43:49 2013 +0000
- Revision:
- 3:c9c1c0ebf89d
- Parent:
- 2:3d39bce54dfe
- Child:
- 4:2ad0cc818166
Added comment with link
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 | 3:c9c1c0ebf89d | 5 | /******************************************************************************* |
vsluiter | 3:c9c1c0ebf89d | 6 | * * |
vsluiter | 3:c9c1c0ebf89d | 7 | * Code can be found at http://mbed.org/users/vsluiter/code/BMT-K9-Regelaar/ * |
vsluiter | 3:c9c1c0ebf89d | 8 | * * |
vsluiter | 3:c9c1c0ebf89d | 9 | ********************************************************************************/ |
vsluiter | 3:c9c1c0ebf89d | 10 | |
vsluiter | 1:9d05c0236c7e | 11 | /** keep_in_range -> float in, and keep_in_range if less than min, or larger than max **/ |
vsluiter | 1:9d05c0236c7e | 12 | void keep_in_range(float * in, float min, float max); |
vsluiter | 1:9d05c0236c7e | 13 | |
vsluiter | 0:7bc93f851767 | 14 | volatile bool looptimerflag; |
vsluiter | 0:7bc93f851767 | 15 | |
vsluiter | 0:7bc93f851767 | 16 | void setlooptimerflag(void) |
vsluiter | 0:7bc93f851767 | 17 | { |
vsluiter | 0:7bc93f851767 | 18 | looptimerflag = true; |
vsluiter | 0:7bc93f851767 | 19 | } |
vsluiter | 0:7bc93f851767 | 20 | |
vsluiter | 0:7bc93f851767 | 21 | |
vsluiter | 0:7bc93f851767 | 22 | int main() { |
vsluiter | 1:9d05c0236c7e | 23 | //LOCAL VARIABLES |
vsluiter | 1:9d05c0236c7e | 24 | AnalogIn potmeter(PTC2); |
vsluiter | 1:9d05c0236c7e | 25 | Encoder motor1(PTD0,PTC9);//first pin on PTAx or PTDx |
vsluiter | 0:7bc93f851767 | 26 | MODSERIAL pc(USBTX,USBRX); |
vsluiter | 0:7bc93f851767 | 27 | PwmOut pwm_motor(PTA12); |
vsluiter | 0:7bc93f851767 | 28 | DigitalOut motordir(PTD3); |
vsluiter | 0:7bc93f851767 | 29 | float setpoint; |
vsluiter | 1:9d05c0236c7e | 30 | float pwm_to_motor; |
vsluiter | 1:9d05c0236c7e | 31 | //START OF CODE |
vsluiter | 1:9d05c0236c7e | 32 | pc.baud(230400); |
vsluiter | 0:7bc93f851767 | 33 | Ticker looptimer; |
vsluiter | 0:7bc93f851767 | 34 | looptimer.attach(setlooptimerflag,0.01); |
vsluiter | 1:9d05c0236c7e | 35 | pc.printf("bla"); |
vsluiter | 1:9d05c0236c7e | 36 | //INFINITE LOOP |
vsluiter | 0:7bc93f851767 | 37 | while(1) { |
vsluiter | 1:9d05c0236c7e | 38 | while(looptimerflag != true); |
vsluiter | 0:7bc93f851767 | 39 | looptimerflag = false; |
vsluiter | 1:9d05c0236c7e | 40 | setpoint = (potmeter.read()-0.5)*2000; |
vsluiter | 0:7bc93f851767 | 41 | pc.printf("s: %f, %d \n\r", setpoint, motor1.getPosition()); |
vsluiter | 1:9d05c0236c7e | 42 | pwm_to_motor = (setpoint - motor1.getPosition())*.001; |
vsluiter | 1:9d05c0236c7e | 43 | keep_in_range(&pwm_to_motor, -1,1); |
vsluiter | 1:9d05c0236c7e | 44 | if(pwm_to_motor > 0) |
vsluiter | 2:3d39bce54dfe | 45 | motordir = 1; |
vsluiter | 0:7bc93f851767 | 46 | else |
vsluiter | 2:3d39bce54dfe | 47 | motordir = 0; |
vsluiter | 1:9d05c0236c7e | 48 | //WRITE VALUE TO MOTOR |
vsluiter | 1:9d05c0236c7e | 49 | pwm_motor.write(abs(pwm_to_motor)); |
vsluiter | 0:7bc93f851767 | 50 | } |
vsluiter | 0:7bc93f851767 | 51 | } |
vsluiter | 0:7bc93f851767 | 52 | |
vsluiter | 0:7bc93f851767 | 53 | |
vsluiter | 0:7bc93f851767 | 54 | //coerces value 'in' to min or max when exceeding those values |
vsluiter | 0:7bc93f851767 | 55 | //if you'd like to understand the statement below take a google for |
vsluiter | 0:7bc93f851767 | 56 | //'ternary operators'. |
vsluiter | 1:9d05c0236c7e | 57 | void keep_in_range(float * in, float min, float max) |
vsluiter | 0:7bc93f851767 | 58 | { |
vsluiter | 0:7bc93f851767 | 59 | *in > min ? *in < max? : *in = max: *in = min; |
vsluiter | 0:7bc93f851767 | 60 | } |
vsluiter | 0:7bc93f851767 | 61 | |
vsluiter | 0:7bc93f851767 | 62 | |
vsluiter | 0:7bc93f851767 | 63 |