Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: m3Dpi mbed-rtos mbed MbedJSONValue
main.cpp@14:06576ab99415, 2016-01-20 (annotated)
- Committer:
- sillevl
- Date:
- Wed Jan 20 17:21:31 2016 +0000
- Revision:
- 14:06576ab99415
- Parent:
- 13:d6374484b953
- Child:
- 16:0043244f8a61
adding RTOS to manage tasks
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sillevl | 7:9068fc754a0b | 1 | #include "mbed.h" |
| sillevl | 14:06576ab99415 | 2 | #include "rtos.h" |
| sillevl | 1:6f620263c9f8 | 3 | #include "M3Dpi.h" |
| sillevl | 7:9068fc754a0b | 4 | #include "jsonReporter.h" |
| sillevl | 13:d6374484b953 | 5 | #include "binaryReporter.h" |
| sillevl | 6:414e1fea64f3 | 6 | |
| sillevl | 14:06576ab99415 | 7 | const char M3DPI_ID[] = "1234567890"; |
| sillevl | 6:414e1fea64f3 | 8 | |
| sillevl | 7:9068fc754a0b | 9 | M3Dpi robot; |
| sillevl | 7:9068fc754a0b | 10 | Serial pc(USBTX,USBRX); |
| sillevl | 14:06576ab99415 | 11 | Reporter* report = new JsonReporter(&pc, M3DPI_ID); |
| sillevl | 11:ccb8653e285f | 12 | |
| sillevl | 11:ccb8653e285f | 13 | m3dpi::Distance distance; |
| sillevl | 11:ccb8653e285f | 14 | m3dpi::Direction direction; |
| sillevl | 11:ccb8653e285f | 15 | m3dpi::Rotation rotation; |
| sillevl | 11:ccb8653e285f | 16 | m3dpi::Acceleration acceleration; |
| sillevl | 6:414e1fea64f3 | 17 | |
| sillevl | 6:414e1fea64f3 | 18 | |
| sillevl | 6:414e1fea64f3 | 19 | void setLeds(m3dpi::Distance distance) |
| sillevl | 6:414e1fea64f3 | 20 | { |
| sillevl | 6:414e1fea64f3 | 21 | int colors[8] = {distance.front, distance.front_right, distance.right, distance.back_right, distance.back, distance.back_left, distance.left, distance.front_left}; |
| sillevl | 6:414e1fea64f3 | 22 | // shift distance value to get red colors |
| sillevl | 6:414e1fea64f3 | 23 | for(int i = 0; i < 8; i++){ |
| sillevl | 14:06576ab99415 | 24 | colors[i] = ((~(colors[i]*2)) & 0xFF) << 16; |
| sillevl | 6:414e1fea64f3 | 25 | } |
| sillevl | 6:414e1fea64f3 | 26 | robot.setLeds(colors); |
| sillevl | 6:414e1fea64f3 | 27 | } |
| sillevl | 6:414e1fea64f3 | 28 | |
| sillevl | 14:06576ab99415 | 29 | void read_sensors_task(void const *name) |
| sillevl | 6:414e1fea64f3 | 30 | { |
| sillevl | 14:06576ab99415 | 31 | //Timer t; t.start(); |
| sillevl | 7:9068fc754a0b | 32 | robot.setStatus(Color::GREEN); |
| sillevl | 11:ccb8653e285f | 33 | ::distance = robot.getDistance(); |
| sillevl | 11:ccb8653e285f | 34 | direction = robot.getDirection(); |
| sillevl | 11:ccb8653e285f | 35 | rotation = robot.getRotation(); |
| sillevl | 11:ccb8653e285f | 36 | acceleration = robot.getAcceleration(); |
| sillevl | 11:ccb8653e285f | 37 | setLeds(::distance); |
| sillevl | 11:ccb8653e285f | 38 | robot.setStatus(Color::BLACK); |
| sillevl | 14:06576ab99415 | 39 | //t.stop();pc.printf("*** Sensor acquisition time: %d us\r\n", t.read_us()); |
| sillevl | 14:06576ab99415 | 40 | } |
| sillevl | 14:06576ab99415 | 41 | |
| sillevl | 14:06576ab99415 | 42 | void report_task(void const *name) |
| sillevl | 14:06576ab99415 | 43 | { |
| sillevl | 14:06576ab99415 | 44 | //Timer t; t.start(); |
| sillevl | 14:06576ab99415 | 45 | report->distance(::distance); |
| sillevl | 14:06576ab99415 | 46 | report->direction(direction); |
| sillevl | 14:06576ab99415 | 47 | report->rotation(rotation); |
| sillevl | 14:06576ab99415 | 48 | report->acceleration(acceleration); |
| sillevl | 14:06576ab99415 | 49 | //t.stop();pc.printf("*** Report time: %d us\r\n", t.read_us()); |
| sillevl | 14:06576ab99415 | 50 | } |
| sillevl | 14:06576ab99415 | 51 | |
| sillevl | 14:06576ab99415 | 52 | void receive_task(void const *name) |
| sillevl | 14:06576ab99415 | 53 | { |
| sillevl | 14:06576ab99415 | 54 | |
| sillevl | 14:06576ab99415 | 55 | } |
| sillevl | 14:06576ab99415 | 56 | |
| sillevl | 14:06576ab99415 | 57 | void idle_task(void const *name) |
| sillevl | 14:06576ab99415 | 58 | { |
| sillevl | 14:06576ab99415 | 59 | |
| sillevl | 6:414e1fea64f3 | 60 | } |
| sillevl | 0:ab0ce9ab81bf | 61 | |
| sillevl | 1:6f620263c9f8 | 62 | int main() |
| sillevl | 6:414e1fea64f3 | 63 | { |
| sillevl | 1:6f620263c9f8 | 64 | pc.baud(115200); |
| sillevl | 14:06576ab99415 | 65 | |
| sillevl | 14:06576ab99415 | 66 | RtosTimer read_sensors_timer(read_sensors_task, osTimerPeriodic); |
| sillevl | 14:06576ab99415 | 67 | read_sensors_timer.start(50); |
| sillevl | 14:06576ab99415 | 68 | |
| sillevl | 14:06576ab99415 | 69 | RtosTimer report_timer(report_task, osTimerPeriodic); |
| sillevl | 14:06576ab99415 | 70 | report_timer.start(500); |
| sillevl | 14:06576ab99415 | 71 | |
| sillevl | 14:06576ab99415 | 72 | Thread receive_thread(receive_task); |
| sillevl | 14:06576ab99415 | 73 | Thread idle_thread(idle_task, NULL, osPriorityLow); |
| sillevl | 14:06576ab99415 | 74 | |
| sillevl | 14:06576ab99415 | 75 | Thread::wait(osWaitForever); |
| sillevl | 0:ab0ce9ab81bf | 76 | } |
