2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
Revision 15:35c40765f7c3, committed 2018-12-13
- Comitter:
- shimniok
- Date:
- Thu Dec 13 00:30:59 2018 +0000
- Parent:
- 14:1dd83e626153
- Child:
- 16:eb28d0f64a9b
- Commit message:
- start updater in its own thread
Changed in this revision
--- a/Updater.cpp Wed Dec 12 18:21:57 2018 +0000 +++ b/Updater.cpp Thu Dec 13 00:30:59 2018 +0000 @@ -2,24 +2,6 @@ #include "pinouts.h" #include "IncrementalEncoder.h" -void Updater::gyro(int g[3], float& dt) -{ - for (int i=0; i < 3; i++) { - g[i] = _gyro[i]; - } - dt = _dt; - - return; -} - -int Updater::encoder() -{ - int result=_ecount; - _ecount = 0; - - return result; -} - Updater *Updater::instance() { static Updater instance; @@ -28,7 +10,14 @@ } -void Updater::start(int interval_ms) +void Updater::setInterval(int interval_ms) +{ + _interval = interval_ms; + return; +} + + +void Updater::start() { Timer timer; @@ -36,12 +25,13 @@ lastTime = 0; t = &timer; t->start(); - + EventQueue *queue = mbed_highprio_event_queue(); Event<void()> event(queue, callback(this, &Updater::update)); - event.period(interval_ms); + event.period(_interval); event.post(); queue->dispatch_forever(); + return; } @@ -77,4 +67,25 @@ led2 = !led2; return; -} \ No newline at end of file +} + + +void Updater::gyro(int g[3], float& dt) +{ + for (int i=0; i < 3; i++) { + g[i] = _gyro[i]; + } + dt = _dt; + + return; +} + + +int Updater::encoder() +{ + int result=_ecount; + _ecount = 0; + + return result; +} +
--- a/Updater.h Wed Dec 12 18:21:57 2018 +0000 +++ b/Updater.h Thu Dec 13 00:30:59 2018 +0000 @@ -4,17 +4,28 @@ #include "mbed.h" #include "L3G4200D.h" +/** Periodically reads sensor data + * This class executes an update function at a configurable interval to read + * and update sensor data. The class makes use of EventQueue and Event for + * task scheduling and runs events on the mbed_highprio_event_queue + */ class Updater: private mbed::NonCopyable<Updater> { public: - /** Start the updater running - cannot be exited! - * @param interval_ms is the interval in ms between calls to update() - * @note Makes use of RTOS EventQueue and Event. - */ - void start(int interval_ms); - /// Return singleton instance static Updater *instance(); + /** Sets the interval for running updater() + * @param interval_ms is the interval in milliseconds + */ + void setInterval(int interval_ms); + + /** Start the updater running + * @note Makes use of RTOS EventQueue and Event. The function runs in an + * infinite loop so best to start in a separate thread or from main thread + * when done with everything else. + */ + void start(); + /** Get gyro values * @return g array of x, y, and z gyro values * @return dt time since data last updated @@ -37,6 +48,7 @@ int _gyro[3]; // gyro raw int _ecount; // encoder count float _dt; + int _interval; int thisTime; int lastTime; };
--- a/main.cpp Wed Dec 12 18:21:57 2018 +0000 +++ b/main.cpp Thu Dec 13 00:30:59 2018 +0000 @@ -21,7 +21,6 @@ SimpleShell sh; DigitalOut led1(LED1); -Thread thread; /******** SHELL COMMANDS ********/ @@ -93,11 +92,15 @@ sh.attach(read_gyro, "gyro"); sh.attach(read_enc, "enc"); sh.attach(reset, "reset"); - thread.start(callback(&sh, &SimpleShell::run)); + Thread shellThread; + shellThread.start(callback(&sh, &SimpleShell::run)); printf("Starting updater...\n"); Updater *u = Updater::instance(); - u->start(50); + u->setInterval(50); + Thread updaterThread; + updaterThread.set_priority(osPriorityRealtime); + updaterThread.start(callback(u, &Updater::start)); /* FILE *fp;