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.
main.cpp@0:f4862c7fd394, 2017-02-28 (annotated)
- Committer:
- ZHAW_Prometheus
- Date:
- Tue Feb 28 15:28:07 2017 +0000
- Revision:
- 0:f4862c7fd394
0.1;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ZHAW_Prometheus | 0:f4862c7fd394 | 1 | #include "mbed.h" |
| ZHAW_Prometheus | 0:f4862c7fd394 | 2 | #include "IRSensor.h" |
| ZHAW_Prometheus | 0:f4862c7fd394 | 3 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 4 | DigitalOut led(LED1); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 5 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 6 | //Perophery for distance sensors |
| ZHAW_Prometheus | 0:f4862c7fd394 | 7 | AnalogIn distance(PB_1); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 8 | DigitalOut enable(PC_1); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 9 | DigitalOut bit0(PH_1); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 10 | DigitalOut bit1(PC_2); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 11 | DigitalOut bit2(PC_3); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 12 | IRSensor sensors[6]; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 13 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 14 | //indicator leds arround robot |
| ZHAW_Prometheus | 0:f4862c7fd394 | 15 | DigitalOut leds[] = { PC_8, PC_6, PB_12, PA_7, PC_0, PC_9 }; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 16 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 17 | //timer object for ledshow and distance sensor |
| ZHAW_Prometheus | 0:f4862c7fd394 | 18 | Ticker t1; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 19 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 20 | //motor stuff |
| ZHAW_Prometheus | 0:f4862c7fd394 | 21 | DigitalOut enableMotorDriver(PB_2); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 22 | PwmOut pwmL(PA_8); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 23 | PwmOut pwmR(PA_9); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 24 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 25 | //lights up the led according to the sensor signal |
| ZHAW_Prometheus | 0:f4862c7fd394 | 26 | void ledDistance() |
| ZHAW_Prometheus | 0:f4862c7fd394 | 27 | { |
| ZHAW_Prometheus | 0:f4862c7fd394 | 28 | for( int ii = 0; ii<6; ++ii) |
| ZHAW_Prometheus | 0:f4862c7fd394 | 29 | sensors[ii]< 0.1f ? leds[ii] = 1 : leds[ii] = 0; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 30 | } |
| ZHAW_Prometheus | 0:f4862c7fd394 | 31 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 32 | //blinks at startup and starts ledDistance task |
| ZHAW_Prometheus | 0:f4862c7fd394 | 33 | void ledShow() |
| ZHAW_Prometheus | 0:f4862c7fd394 | 34 | { |
| ZHAW_Prometheus | 0:f4862c7fd394 | 35 | static int timer = 0; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 36 | for( int ii = 0; ii<6; ++ii) |
| ZHAW_Prometheus | 0:f4862c7fd394 | 37 | leds[ii] = !leds[ii]; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 38 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 39 | //quit ticker and start led distance show |
| ZHAW_Prometheus | 0:f4862c7fd394 | 40 | if( ++timer > 10) { |
| ZHAW_Prometheus | 0:f4862c7fd394 | 41 | t1.detach(); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 42 | t1.attach( &ledDistance, 0.01f ); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 43 | } |
| ZHAW_Prometheus | 0:f4862c7fd394 | 44 | } |
| ZHAW_Prometheus | 0:f4862c7fd394 | 45 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 46 | int main() |
| ZHAW_Prometheus | 0:f4862c7fd394 | 47 | { |
| ZHAW_Prometheus | 0:f4862c7fd394 | 48 | pwmL.period(0.00005f); // Setzt die Periode auf 50 μs |
| ZHAW_Prometheus | 0:f4862c7fd394 | 49 | pwmR.period(0.00005f); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 50 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 51 | pwmL = 0.5f; // Setzt die Duty-Cycle auf 50% |
| ZHAW_Prometheus | 0:f4862c7fd394 | 52 | pwmR = 0.5f; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 53 | enableMotorDriver = 0; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 54 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 55 | t1.attach( &ledShow, 0.05f ); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 56 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 57 | //init distance sensors |
| ZHAW_Prometheus | 0:f4862c7fd394 | 58 | for( int ii = 0; ii<6; ++ii) |
| ZHAW_Prometheus | 0:f4862c7fd394 | 59 | sensors[ii].init(&distance, &bit0, &bit1, &bit2, ii); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 60 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 61 | enable = 1; |
| ZHAW_Prometheus | 0:f4862c7fd394 | 62 | |
| ZHAW_Prometheus | 0:f4862c7fd394 | 63 | while(1) { |
| ZHAW_Prometheus | 0:f4862c7fd394 | 64 | wait( 0.2f ); |
| ZHAW_Prometheus | 0:f4862c7fd394 | 65 | } |
| ZHAW_Prometheus | 0:f4862c7fd394 | 66 | } |
| ZHAW_Prometheus | 0:f4862c7fd394 | 67 |