2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
Updater.cpp@15:35c40765f7c3, 2018-12-13 (annotated)
- Committer:
- shimniok
- Date:
- Thu Dec 13 00:30:59 2018 +0000
- Revision:
- 15:35c40765f7c3
- Parent:
- 14:1dd83e626153
- Child:
- 25:b8176ebb96c6
start updater in its own thread
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 11:8ec858b7c6d1 | 1 | #include "Updater.h" |
shimniok | 14:1dd83e626153 | 2 | #include "pinouts.h" |
shimniok | 14:1dd83e626153 | 3 | #include "IncrementalEncoder.h" |
shimniok | 11:8ec858b7c6d1 | 4 | |
shimniok | 12:3cd91e150d9c | 5 | Updater *Updater::instance() |
shimniok | 12:3cd91e150d9c | 6 | { |
shimniok | 11:8ec858b7c6d1 | 7 | static Updater instance; |
shimniok | 11:8ec858b7c6d1 | 8 | |
shimniok | 11:8ec858b7c6d1 | 9 | return &instance; |
shimniok | 11:8ec858b7c6d1 | 10 | } |
shimniok | 11:8ec858b7c6d1 | 11 | |
shimniok | 11:8ec858b7c6d1 | 12 | |
shimniok | 15:35c40765f7c3 | 13 | void Updater::setInterval(int interval_ms) |
shimniok | 15:35c40765f7c3 | 14 | { |
shimniok | 15:35c40765f7c3 | 15 | _interval = interval_ms; |
shimniok | 15:35c40765f7c3 | 16 | return; |
shimniok | 15:35c40765f7c3 | 17 | } |
shimniok | 15:35c40765f7c3 | 18 | |
shimniok | 15:35c40765f7c3 | 19 | |
shimniok | 15:35c40765f7c3 | 20 | void Updater::start() |
shimniok | 11:8ec858b7c6d1 | 21 | { |
shimniok | 11:8ec858b7c6d1 | 22 | Timer timer; |
shimniok | 11:8ec858b7c6d1 | 23 | |
shimniok | 11:8ec858b7c6d1 | 24 | thisTime = 0; |
shimniok | 11:8ec858b7c6d1 | 25 | lastTime = 0; |
shimniok | 11:8ec858b7c6d1 | 26 | t = &timer; |
shimniok | 11:8ec858b7c6d1 | 27 | t->start(); |
shimniok | 15:35c40765f7c3 | 28 | |
shimniok | 11:8ec858b7c6d1 | 29 | EventQueue *queue = mbed_highprio_event_queue(); |
shimniok | 11:8ec858b7c6d1 | 30 | Event<void()> event(queue, callback(this, &Updater::update)); |
shimniok | 15:35c40765f7c3 | 31 | event.period(_interval); |
shimniok | 11:8ec858b7c6d1 | 32 | event.post(); |
shimniok | 11:8ec858b7c6d1 | 33 | queue->dispatch_forever(); |
shimniok | 15:35c40765f7c3 | 34 | return; |
shimniok | 11:8ec858b7c6d1 | 35 | } |
shimniok | 11:8ec858b7c6d1 | 36 | |
shimniok | 11:8ec858b7c6d1 | 37 | |
shimniok | 11:8ec858b7c6d1 | 38 | void Updater::update() |
shimniok | 11:8ec858b7c6d1 | 39 | { |
shimniok | 11:8ec858b7c6d1 | 40 | static DigitalOut led2(LED2); |
shimniok | 14:1dd83e626153 | 41 | static L3G4200D gyro(I2CSDA, I2CSCL); // TODO parameterize |
shimniok | 14:1dd83e626153 | 42 | static IncrementalEncoder enc(ALEFT); |
shimniok | 11:8ec858b7c6d1 | 43 | |
shimniok | 11:8ec858b7c6d1 | 44 | // Compute dt |
shimniok | 11:8ec858b7c6d1 | 45 | thisTime = t->read_us(); |
shimniok | 13:5566df1250f1 | 46 | _dt = (lastTime < 0) ? 0 : ((float) thisTime - (float) lastTime) / 1000.0; // first pass let dt=0 |
shimniok | 11:8ec858b7c6d1 | 47 | lastTime = thisTime; |
shimniok | 11:8ec858b7c6d1 | 48 | |
shimniok | 11:8ec858b7c6d1 | 49 | // Read encoders |
shimniok | 14:1dd83e626153 | 50 | _ecount += enc.read(); |
shimniok | 14:1dd83e626153 | 51 | |
shimniok | 11:8ec858b7c6d1 | 52 | // Read gyro |
shimniok | 12:3cd91e150d9c | 53 | gyro.read(_gyro); |
shimniok | 11:8ec858b7c6d1 | 54 | |
shimniok | 11:8ec858b7c6d1 | 55 | //gyro[_z_] = (g_sign[_z_] * (g[_z_] - g_offset[_z_])) / g_scale[_z_]; |
shimniok | 11:8ec858b7c6d1 | 56 | |
shimniok | 11:8ec858b7c6d1 | 57 | // Save current data into history fifo to use 1 second from now |
shimniok | 11:8ec858b7c6d1 | 58 | //history[now].dist = (sensors.lrEncDistance + sensors.rrEncDistance) / 2.0; // current distance traveled |
shimniok | 11:8ec858b7c6d1 | 59 | //history[now].gyro = sensors.gyro[_z_]; // current raw gyro data |
shimniok | 11:8ec858b7c6d1 | 60 | //history[now].dt = dt; // current dt, to address jitter |
shimniok | 11:8ec858b7c6d1 | 61 | //history[now].hdg = clamp360( history[prev].hdg + dt*(history[now].gyro) ); // compute current heading from current gyro |
shimniok | 11:8ec858b7c6d1 | 62 | //float r = PI/180 * history[now].hdg; |
shimniok | 11:8ec858b7c6d1 | 63 | //history[now].x = history[prev].x + history[now].dist * sin(r); // update current position |
shimniok | 11:8ec858b7c6d1 | 64 | //history[now].y = history[prev].y + history[now].dist * cos(r); |
shimniok | 11:8ec858b7c6d1 | 65 | |
shimniok | 11:8ec858b7c6d1 | 66 | // Convert this into some kind of status message/event thingy |
shimniok | 11:8ec858b7c6d1 | 67 | led2 = !led2; |
shimniok | 11:8ec858b7c6d1 | 68 | |
shimniok | 11:8ec858b7c6d1 | 69 | return; |
shimniok | 15:35c40765f7c3 | 70 | } |
shimniok | 15:35c40765f7c3 | 71 | |
shimniok | 15:35c40765f7c3 | 72 | |
shimniok | 15:35c40765f7c3 | 73 | void Updater::gyro(int g[3], float& dt) |
shimniok | 15:35c40765f7c3 | 74 | { |
shimniok | 15:35c40765f7c3 | 75 | for (int i=0; i < 3; i++) { |
shimniok | 15:35c40765f7c3 | 76 | g[i] = _gyro[i]; |
shimniok | 15:35c40765f7c3 | 77 | } |
shimniok | 15:35c40765f7c3 | 78 | dt = _dt; |
shimniok | 15:35c40765f7c3 | 79 | |
shimniok | 15:35c40765f7c3 | 80 | return; |
shimniok | 15:35c40765f7c3 | 81 | } |
shimniok | 15:35c40765f7c3 | 82 | |
shimniok | 15:35c40765f7c3 | 83 | |
shimniok | 15:35c40765f7c3 | 84 | int Updater::encoder() |
shimniok | 15:35c40765f7c3 | 85 | { |
shimniok | 15:35c40765f7c3 | 86 | int result=_ecount; |
shimniok | 15:35c40765f7c3 | 87 | _ecount = 0; |
shimniok | 15:35c40765f7c3 | 88 | |
shimniok | 15:35c40765f7c3 | 89 | return result; |
shimniok | 15:35c40765f7c3 | 90 | } |
shimniok | 15:35c40765f7c3 | 91 |