Controller firmware for a mobile robot, having a K64F MCU on board. Please read README.md for details.
demo.cpp@0:260ca1f1cba7, 2018-05-30 (annotated)
- Committer:
- dralisz82
- Date:
- Wed May 30 15:10:20 2018 +0000
- Revision:
- 0:260ca1f1cba7
Controller firmware for a mobile robot, having a K64F MCU on board.; ; See README.md for details;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dralisz82 | 0:260ca1f1cba7 | 1 | #include "demo.h" |
dralisz82 | 0:260ca1f1cba7 | 2 | |
dralisz82 | 0:260ca1f1cba7 | 3 | Demo::Demo() {} |
dralisz82 | 0:260ca1f1cba7 | 4 | Demo::Demo(Drive* drive, Lights* lights) { |
dralisz82 | 0:260ca1f1cba7 | 5 | this->drive = drive; |
dralisz82 | 0:260ca1f1cba7 | 6 | this->lights = lights; |
dralisz82 | 0:260ca1f1cba7 | 7 | |
dralisz82 | 0:260ca1f1cba7 | 8 | demoThread = 0; |
dralisz82 | 0:260ca1f1cba7 | 9 | // initialize flags |
dralisz82 | 0:260ca1f1cba7 | 10 | f_run = false; |
dralisz82 | 0:260ca1f1cba7 | 11 | printf("Demo created\n"); |
dralisz82 | 0:260ca1f1cba7 | 12 | } |
dralisz82 | 0:260ca1f1cba7 | 13 | |
dralisz82 | 0:260ca1f1cba7 | 14 | Demo::~Demo() { |
dralisz82 | 0:260ca1f1cba7 | 15 | delete demoThread; |
dralisz82 | 0:260ca1f1cba7 | 16 | } |
dralisz82 | 0:260ca1f1cba7 | 17 | |
dralisz82 | 0:260ca1f1cba7 | 18 | void Demo::demoThread_main(void const *argument) { |
dralisz82 | 0:260ca1f1cba7 | 19 | Demo* self = (Demo*)argument; |
dralisz82 | 0:260ca1f1cba7 | 20 | printf("Demo thread started\n"); |
dralisz82 | 0:260ca1f1cba7 | 21 | |
dralisz82 | 0:260ca1f1cba7 | 22 | Thread::wait(10000); |
dralisz82 | 0:260ca1f1cba7 | 23 | self->lights->headLightOn(); |
dralisz82 | 0:260ca1f1cba7 | 24 | Thread::wait(1000); |
dralisz82 | 0:260ca1f1cba7 | 25 | self->drive->forward(); |
dralisz82 | 0:260ca1f1cba7 | 26 | Thread::wait(500); |
dralisz82 | 0:260ca1f1cba7 | 27 | while(true) { |
dralisz82 | 0:260ca1f1cba7 | 28 | while(self->f_run) { |
dralisz82 | 0:260ca1f1cba7 | 29 | self->drive->forward(); |
dralisz82 | 0:260ca1f1cba7 | 30 | self->drive->steerLeft(); |
dralisz82 | 0:260ca1f1cba7 | 31 | Thread::wait(1000); |
dralisz82 | 0:260ca1f1cba7 | 32 | self->drive->stop(); |
dralisz82 | 0:260ca1f1cba7 | 33 | Thread::wait(500); |
dralisz82 | 0:260ca1f1cba7 | 34 | self->drive->steerRight(); |
dralisz82 | 0:260ca1f1cba7 | 35 | self->drive->backward(); |
dralisz82 | 0:260ca1f1cba7 | 36 | Thread::wait(1500); |
dralisz82 | 0:260ca1f1cba7 | 37 | self->drive->stop(); |
dralisz82 | 0:260ca1f1cba7 | 38 | Thread::wait(500); |
dralisz82 | 0:260ca1f1cba7 | 39 | } |
dralisz82 | 0:260ca1f1cba7 | 40 | printf("Demo thread paused\n"); |
dralisz82 | 0:260ca1f1cba7 | 41 | Thread::wait(1000); |
dralisz82 | 0:260ca1f1cba7 | 42 | } |
dralisz82 | 0:260ca1f1cba7 | 43 | } |
dralisz82 | 0:260ca1f1cba7 | 44 | |
dralisz82 | 0:260ca1f1cba7 | 45 | void Demo::playPause() { |
dralisz82 | 0:260ca1f1cba7 | 46 | f_run = !f_run; |
dralisz82 | 0:260ca1f1cba7 | 47 | if(demoThread == 0) { |
dralisz82 | 0:260ca1f1cba7 | 48 | printf("Launch Demo thread\n"); |
dralisz82 | 0:260ca1f1cba7 | 49 | demoThread = new Thread(demoThread_main, this); |
dralisz82 | 0:260ca1f1cba7 | 50 | } |
dralisz82 | 0:260ca1f1cba7 | 51 | } |
dralisz82 | 0:260ca1f1cba7 | 52 | |
dralisz82 | 0:260ca1f1cba7 | 53 | void Demo::reset() { |
dralisz82 | 0:260ca1f1cba7 | 54 | delete demoThread; |
dralisz82 | 0:260ca1f1cba7 | 55 | f_run = false; |
dralisz82 | 0:260ca1f1cba7 | 56 | printf("Demo thread terminated\n"); |
dralisz82 | 0:260ca1f1cba7 | 57 | lights->headLightOff(); |
dralisz82 | 0:260ca1f1cba7 | 58 | } |