2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
Revision 25:b8176ebb96c6, committed 2018-12-21
- Comitter:
- shimniok
- Date:
- Fri Dec 21 20:38:55 2018 +0000
- Parent:
- 24:a7f92dfc5310
- Child:
- 26:2dc31a801cc8
- Commit message:
- Extracted eventqueue from Updater, implemented callback
Changed in this revision
--- a/Updater.cpp Fri Dec 21 20:04:09 2018 +0000 +++ b/Updater.cpp Fri Dec 21 20:38:55 2018 +0000 @@ -2,6 +2,14 @@ #include "pinouts.h" #include "IncrementalEncoder.h" +Updater::Updater() { + thisTime = 0; + lastTime = 0; + t = new Timer; + t->start(); + _callback = 0; +} + Updater *Updater::instance() { static Updater instance; @@ -10,34 +18,15 @@ } -void Updater::setInterval(int interval_ms) -{ - _interval = interval_ms; - return; -} - +void Updater::attach(Callback<void()> cb) { + _callback = cb; -void Updater::start() -{ - Timer timer; - - thisTime = 0; - lastTime = 0; - t = &timer; - t->start(); - - EventQueue *queue = mbed_highprio_event_queue(); - Event<void()> event(queue, callback(this, &Updater::update)); - event.period(_interval); - event.post(); - queue->dispatch_forever(); return; } void Updater::update() { - static DigitalOut led2(LED2); static L3G4200D gyro(I2CSDA, I2CSCL); // TODO parameterize static IncrementalEncoder enc(ALEFT); @@ -64,7 +53,9 @@ //history[now].y = history[prev].y + history[now].dist * cos(r); // Convert this into some kind of status message/event thingy - led2 = !led2; + + if (_callback) + _callback(); return; }
--- a/Updater.h Fri Dec 21 20:04:09 2018 +0000 +++ b/Updater.h Fri Dec 21 20:38:55 2018 +0000 @@ -5,27 +5,29 @@ #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 + * This class reads and updates sensor data. Intended to be called at a fixed + * interval. + * @code + * Updater *u = Updater::instance(); // beware of lifetime of this pointer + * Thread updaterThread(osPriorityRealtime, 512, 0, "updater"); + * EventQueue *updaterQueue = mbed_highprio_event_queue(); + * Event<void()> event(updaterQueue, callback(u, &Updater::update)); + * event.period(20); + * event.post(); // if lifetime of u not correct, this will hard fault + * updaterThread.start(callback(updaterQueue, &EventQueue::dispatch_forever)); + * */ class Updater: private mbed::NonCopyable<Updater> { public: /// 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(); - + /// Attach a callback handler run each time updater() is run + void attach(Callback<void()> cb); + + /// Update all sensors + void update(); + /** Get gyro values * @return g array of x, y, and z gyro values * @return dt time since data last updated @@ -39,11 +41,9 @@ private: /// Basic constructor (singleton) - Updater() {} + Updater(); - /// Update all sensors - void update(); - + Callback<void()> _callback; // notification callback Timer *t; // timer used to measure dt int _gyro[3]; // gyro raw int _ecount; // encoder count
--- a/main.cpp Fri Dec 21 20:04:09 2018 +0000 +++ b/main.cpp Fri Dec 21 20:38:55 2018 +0000 @@ -28,7 +28,7 @@ FATFileSystem ffs("log", &bd); Serial pc(USBTX, USBRX); DigitalOut led1(LED1); -DigitalOut led3(LED3); +DigitalOut led2(LED2); RawSerial s(UART1TX, UART1RX, 38400); /////////////////////////////////////////////////////////////////////////////// @@ -45,6 +45,15 @@ EventQueue logQueue(8 * EVENTS_EVENT_SIZE); Logger logger("/etc/test.log"); + +/////////////////////////////////////////////////////////////////////////////// +// Updater +Updater *u = Updater::instance(); + +void updater_callback() { + led1 = !led1; +} + /////////////////////////////////////////////////////////////////////////////// // GPS Ublox6 ublox; @@ -54,7 +63,7 @@ void gps_callback() { GpsData d; - led3 = !led3; + led2 = !led2; ublox.read(d.latitude, d.longitude, d.course, d.speed, d.hdop, d.svcount); //logQueue.call(&logger, &Logger::log_gps, d); } @@ -136,7 +145,7 @@ { //bridge_uart1(); - Kernel::attach_idle_hook(idler); + //Kernel::attach_idle_hook(idler); printf("Bootup...\n"); fflush(stdout); @@ -169,10 +178,13 @@ } printf("Starting updater...\n"); - Updater *u = Updater::instance(); - u->setInterval(20); + u->attach(updater_callback); Thread updaterThread(osPriorityRealtime, 512, 0, "updater"); - updaterThread.start(callback(u, &Updater::start)); + EventQueue *updaterQueue = mbed_highprio_event_queue(); + Event<void()> event(updaterQueue, callback(u, &Updater::update)); + event.period(20); + event.post(); + updaterThread.start(callback(updaterQueue, &EventQueue::dispatch_forever)); printf("Starting gps...\n"); Thread gpsThread(osPriorityHigh, 256, 0, "gps");