Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MODSERIAL mbed-rtos mbed
Fork of Master by
control.cpp@2:c610e1a7fbcd, 2014-09-11 (annotated)
- Committer:
- 9uS7
- Date:
- Thu Sep 11 15:09:05 2014 +0000
- Revision:
- 2:c610e1a7fbcd
- Parent:
- 1:e1cfb5850088
- Child:
- 3:12e1f116ea42
made motorSetup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
9uS7 | 0:4f07ba929908 | 1 | #include "mbed.h" |
9uS7 | 0:4f07ba929908 | 2 | #include "control.h" |
9uS7 | 0:4f07ba929908 | 3 | |
9uS7 | 1:e1cfb5850088 | 4 | //sensor |
9uS7 | 1:e1cfb5850088 | 5 | AnalogIn IR(p17); |
9uS7 | 1:e1cfb5850088 | 6 | AnalogIn FSR(p18); |
9uS7 | 1:e1cfb5850088 | 7 | |
9uS7 | 1:e1cfb5850088 | 8 | //motor |
9uS7 | 1:e1cfb5850088 | 9 | DigitalOut Sig1(p21); |
9uS7 | 1:e1cfb5850088 | 10 | DigitalOut Sig2(p23); |
9uS7 | 1:e1cfb5850088 | 11 | PwmOut Pwm(p22); |
9uS7 | 1:e1cfb5850088 | 12 | |
9uS7 | 2:c610e1a7fbcd | 13 | void motorSetup(void){ |
9uS7 | 2:c610e1a7fbcd | 14 | resetPos(); |
9uS7 | 2:c610e1a7fbcd | 15 | } |
9uS7 | 2:c610e1a7fbcd | 16 | |
9uS7 | 0:4f07ba929908 | 17 | void pull(float buf){ |
9uS7 | 0:4f07ba929908 | 18 | Sig1 = 0; |
9uS7 | 0:4f07ba929908 | 19 | Sig2 = 1; |
9uS7 | 0:4f07ba929908 | 20 | Pwm = zeroPWM+buf/3; |
9uS7 | 0:4f07ba929908 | 21 | } |
9uS7 | 0:4f07ba929908 | 22 | |
9uS7 | 0:4f07ba929908 | 23 | void loose(float buf){ |
9uS7 | 0:4f07ba929908 | 24 | Sig1 = 1; |
9uS7 | 0:4f07ba929908 | 25 | Sig2 = 0; |
9uS7 | 0:4f07ba929908 | 26 | Pwm = zeroPWM+buf/3; |
9uS7 | 0:4f07ba929908 | 27 | } |
9uS7 | 0:4f07ba929908 | 28 | |
9uS7 | 0:4f07ba929908 | 29 | void brake(void){ |
9uS7 | 0:4f07ba929908 | 30 | Sig1 = 0; |
9uS7 | 0:4f07ba929908 | 31 | Sig2 = 0; |
9uS7 | 0:4f07ba929908 | 32 | Pwm = 1; |
9uS7 | 0:4f07ba929908 | 33 | } |
9uS7 | 0:4f07ba929908 | 34 | |
9uS7 | 0:4f07ba929908 | 35 | void open(void){ |
9uS7 | 0:4f07ba929908 | 36 | Sig1 = 0; |
9uS7 | 0:4f07ba929908 | 37 | Sig2 = 0; |
9uS7 | 0:4f07ba929908 | 38 | Pwm = 0; |
9uS7 | 0:4f07ba929908 | 39 | } |
9uS7 | 0:4f07ba929908 | 40 | |
9uS7 | 2:c610e1a7fbcd | 41 | void resetPos(void){ |
9uS7 | 0:4f07ba929908 | 42 | int pos_flag=0; |
9uS7 | 1:e1cfb5850088 | 43 | |
9uS7 | 0:4f07ba929908 | 44 | while(1){ |
9uS7 | 1:e1cfb5850088 | 45 | if(IR>center_pos){ |
9uS7 | 0:4f07ba929908 | 46 | pull(0.3); |
9uS7 | 1:e1cfb5850088 | 47 | }else if(IR<center_pos){ |
9uS7 | 0:4f07ba929908 | 48 | loose(0.3); |
9uS7 | 0:4f07ba929908 | 49 | } |
9uS7 | 1:e1cfb5850088 | 50 | if(abs(IR-center_pos)<0.1){ |
9uS7 | 1:e1cfb5850088 | 51 | open(); |
9uS7 | 1:e1cfb5850088 | 52 | pos_flag=-1; |
9uS7 | 1:e1cfb5850088 | 53 | } |
9uS7 | 1:e1cfb5850088 | 54 | if(pos_flag) break; |
9uS7 | 0:4f07ba929908 | 55 | } |
9uS7 | 0:4f07ba929908 | 56 | } |