2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
Updater.cpp@12:3cd91e150d9c, 2018-12-12 (annotated)
- Committer:
- shimniok
- Date:
- Wed Dec 12 17:32:24 2018 +0000
- Revision:
- 12:3cd91e150d9c
- Parent:
- 11:8ec858b7c6d1
- Child:
- 13:5566df1250f1
pass back 3 gyro axis data; implement reset shell cmd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 11:8ec858b7c6d1 | 1 | #include "Updater.h" |
shimniok | 11:8ec858b7c6d1 | 2 | |
shimniok | 11:8ec858b7c6d1 | 3 | |
shimniok | 12:3cd91e150d9c | 4 | void Updater::gyro(int g[3]) |
shimniok | 12:3cd91e150d9c | 5 | { |
shimniok | 12:3cd91e150d9c | 6 | for (int i=0; i < 3; i++) { |
shimniok | 12:3cd91e150d9c | 7 | g[i] = _gyro[i]; |
shimniok | 12:3cd91e150d9c | 8 | } |
shimniok | 12:3cd91e150d9c | 9 | |
shimniok | 12:3cd91e150d9c | 10 | return; |
shimniok | 12:3cd91e150d9c | 11 | } |
shimniok | 12:3cd91e150d9c | 12 | |
shimniok | 12:3cd91e150d9c | 13 | |
shimniok | 12:3cd91e150d9c | 14 | Updater *Updater::instance() |
shimniok | 12:3cd91e150d9c | 15 | { |
shimniok | 11:8ec858b7c6d1 | 16 | static Updater instance; |
shimniok | 11:8ec858b7c6d1 | 17 | |
shimniok | 11:8ec858b7c6d1 | 18 | return &instance; |
shimniok | 11:8ec858b7c6d1 | 19 | } |
shimniok | 11:8ec858b7c6d1 | 20 | |
shimniok | 11:8ec858b7c6d1 | 21 | |
shimniok | 11:8ec858b7c6d1 | 22 | void Updater::start(int interval_ms) |
shimniok | 11:8ec858b7c6d1 | 23 | { |
shimniok | 11:8ec858b7c6d1 | 24 | Timer timer; |
shimniok | 11:8ec858b7c6d1 | 25 | |
shimniok | 11:8ec858b7c6d1 | 26 | thisTime = 0; |
shimniok | 11:8ec858b7c6d1 | 27 | lastTime = 0; |
shimniok | 11:8ec858b7c6d1 | 28 | t = &timer; |
shimniok | 11:8ec858b7c6d1 | 29 | t->start(); |
shimniok | 11:8ec858b7c6d1 | 30 | |
shimniok | 11:8ec858b7c6d1 | 31 | EventQueue *queue = mbed_highprio_event_queue(); |
shimniok | 11:8ec858b7c6d1 | 32 | Event<void()> event(queue, callback(this, &Updater::update)); |
shimniok | 11:8ec858b7c6d1 | 33 | event.period(interval_ms); |
shimniok | 11:8ec858b7c6d1 | 34 | event.post(); |
shimniok | 11:8ec858b7c6d1 | 35 | queue->dispatch_forever(); |
shimniok | 11:8ec858b7c6d1 | 36 | } |
shimniok | 11:8ec858b7c6d1 | 37 | |
shimniok | 11:8ec858b7c6d1 | 38 | |
shimniok | 11:8ec858b7c6d1 | 39 | void Updater::update() |
shimniok | 11:8ec858b7c6d1 | 40 | { |
shimniok | 11:8ec858b7c6d1 | 41 | static DigitalOut led2(LED2); |
shimniok | 11:8ec858b7c6d1 | 42 | static L3G4200D gyro(p9, p10); // TODO parameterize |
shimniok | 11:8ec858b7c6d1 | 43 | |
shimniok | 11:8ec858b7c6d1 | 44 | // Compute dt |
shimniok | 11:8ec858b7c6d1 | 45 | thisTime = t->read_us(); |
shimniok | 11:8ec858b7c6d1 | 46 | //data.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 | 11:8ec858b7c6d1 | 50 | // Read gyro |
shimniok | 12:3cd91e150d9c | 51 | gyro.read(_gyro); |
shimniok | 11:8ec858b7c6d1 | 52 | |
shimniok | 11:8ec858b7c6d1 | 53 | //gyro[_z_] = (g_sign[_z_] * (g[_z_] - g_offset[_z_])) / g_scale[_z_]; |
shimniok | 11:8ec858b7c6d1 | 54 | |
shimniok | 11:8ec858b7c6d1 | 55 | // Save current data into history fifo to use 1 second from now |
shimniok | 11:8ec858b7c6d1 | 56 | //history[now].dist = (sensors.lrEncDistance + sensors.rrEncDistance) / 2.0; // current distance traveled |
shimniok | 11:8ec858b7c6d1 | 57 | //history[now].gyro = sensors.gyro[_z_]; // current raw gyro data |
shimniok | 11:8ec858b7c6d1 | 58 | //history[now].dt = dt; // current dt, to address jitter |
shimniok | 11:8ec858b7c6d1 | 59 | //history[now].hdg = clamp360( history[prev].hdg + dt*(history[now].gyro) ); // compute current heading from current gyro |
shimniok | 11:8ec858b7c6d1 | 60 | //float r = PI/180 * history[now].hdg; |
shimniok | 11:8ec858b7c6d1 | 61 | //history[now].x = history[prev].x + history[now].dist * sin(r); // update current position |
shimniok | 11:8ec858b7c6d1 | 62 | //history[now].y = history[prev].y + history[now].dist * cos(r); |
shimniok | 11:8ec858b7c6d1 | 63 | |
shimniok | 11:8ec858b7c6d1 | 64 | // Convert this into some kind of status message/event thingy |
shimniok | 11:8ec858b7c6d1 | 65 | led2 = !led2; |
shimniok | 11:8ec858b7c6d1 | 66 | |
shimniok | 11:8ec858b7c6d1 | 67 | return; |
shimniok | 11:8ec858b7c6d1 | 68 | } |