m3Dpi robot, based on the Pololu 3pi and m3pi. m3Dpi has multiple distance sensors, gyroscope, compass and accelerometer sensor to be fully aware of its environment. With the addition of xbee or nrf24n01 module it has wireless communication capabilities.
Dependencies: m3Dpi mbed-rtos mbed MbedJSONValue
main.cpp@7:9068fc754a0b, 2015-12-03 (annotated)
- Committer:
- sillevl
- Date:
- Thu Dec 03 17:53:04 2015 +0000
- Revision:
- 7:9068fc754a0b
- Parent:
- 6:414e1fea64f3
- Child:
- 8:5c0833506d67
- Child:
- 11:ccb8653e285f
add json reporter class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sillevl | 7:9068fc754a0b | 1 | #include "mbed.h" |
sillevl | 7:9068fc754a0b | 2 | #include "rtos.h" |
sillevl | 1:6f620263c9f8 | 3 | #include "M3Dpi.h" |
sillevl | 7:9068fc754a0b | 4 | #include "jsonReporter.h" |
sillevl | 6:414e1fea64f3 | 5 | |
sillevl | 6:414e1fea64f3 | 6 | const char M3DPI_ID[] = "1234567890"; |
sillevl | 6:414e1fea64f3 | 7 | |
sillevl | 7:9068fc754a0b | 8 | M3Dpi robot; |
sillevl | 7:9068fc754a0b | 9 | Serial pc(USBTX,USBRX); |
sillevl | 7:9068fc754a0b | 10 | JsonReporter report(&pc, M3DPI_ID); |
sillevl | 6:414e1fea64f3 | 11 | |
sillevl | 6:414e1fea64f3 | 12 | |
sillevl | 6:414e1fea64f3 | 13 | void setLeds(m3dpi::Distance distance) |
sillevl | 6:414e1fea64f3 | 14 | { |
sillevl | 6:414e1fea64f3 | 15 | 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 | 16 | // shift distance value to get red colors |
sillevl | 6:414e1fea64f3 | 17 | for(int i = 0; i < 8; i++){ |
sillevl | 6:414e1fea64f3 | 18 | colors[i] = colors[i] << 16; |
sillevl | 6:414e1fea64f3 | 19 | } |
sillevl | 6:414e1fea64f3 | 20 | robot.setLeds(colors); |
sillevl | 6:414e1fea64f3 | 21 | } |
sillevl | 6:414e1fea64f3 | 22 | |
sillevl | 7:9068fc754a0b | 23 | void read_sensors_thread(void const * args) |
sillevl | 6:414e1fea64f3 | 24 | { |
sillevl | 7:9068fc754a0b | 25 | robot.setStatus(Color::GREEN); |
sillevl | 6:414e1fea64f3 | 26 | m3dpi::Distance distance = robot.getDistance(); |
sillevl | 7:9068fc754a0b | 27 | setLeds(distance); |
sillevl | 6:414e1fea64f3 | 28 | |
sillevl | 7:9068fc754a0b | 29 | report.distance(distance); |
sillevl | 7:9068fc754a0b | 30 | report.direction(robot.getDirection()); |
sillevl | 7:9068fc754a0b | 31 | report.rotation(robot.getRotation()); |
sillevl | 7:9068fc754a0b | 32 | report.acceleration(robot.getAcceleration()); |
sillevl | 6:414e1fea64f3 | 33 | |
sillevl | 7:9068fc754a0b | 34 | report.time(robot.getTime()); |
sillevl | 7:9068fc754a0b | 35 | robot.setStatus(Color::OFF); |
sillevl | 6:414e1fea64f3 | 36 | } |
sillevl | 0:ab0ce9ab81bf | 37 | |
sillevl | 1:6f620263c9f8 | 38 | int main() |
sillevl | 6:414e1fea64f3 | 39 | { |
sillevl | 1:6f620263c9f8 | 40 | pc.baud(115200); |
sillevl | 7:9068fc754a0b | 41 | |
sillevl | 7:9068fc754a0b | 42 | // for testing... |
sillevl | 6:414e1fea64f3 | 43 | pc.printf("Device ID is: 0x%02x\n", robot.accelerometer.getDevId()); |
sillevl | 6:414e1fea64f3 | 44 | pc.printf("Gyro id: 0x%02x\n", robot.gyro.getWhoAmI()); |
sillevl | 6:414e1fea64f3 | 45 | |
sillevl | 7:9068fc754a0b | 46 | RtosTimer periodicThread(read_sensors_thread, osTimerPeriodic); |
sillevl | 7:9068fc754a0b | 47 | periodicThread.start(50); |
sillevl | 0:ab0ce9ab81bf | 48 | |
sillevl | 7:9068fc754a0b | 49 | Thread::wait(osWaitForever); |
sillevl | 0:ab0ce9ab81bf | 50 | } |