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@6:414e1fea64f3, 2015-12-03 (annotated)
- Committer:
- sillevl
- Date:
- Thu Dec 03 16:36:20 2015 +0000
- Revision:
- 6:414e1fea64f3
- Parent:
- 1:6f620263c9f8
- Child:
- 7:9068fc754a0b
add json library and ticker for sensor reading
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sillevl | 0:ab0ce9ab81bf | 1 | |
sillevl | 1:6f620263c9f8 | 2 | #include "M3Dpi.h" |
sillevl | 6:414e1fea64f3 | 3 | #include "MbedJSONValue.h" |
sillevl | 1:6f620263c9f8 | 4 | |
sillevl | 6:414e1fea64f3 | 5 | M3Dpi robot; |
sillevl | 1:6f620263c9f8 | 6 | Serial pc(USBTX,USBRX); |
sillevl | 1:6f620263c9f8 | 7 | |
sillevl | 6:414e1fea64f3 | 8 | Ticker tick; |
sillevl | 6:414e1fea64f3 | 9 | |
sillevl | 6:414e1fea64f3 | 10 | const char M3DPI_ID[] = "1234567890"; |
sillevl | 6:414e1fea64f3 | 11 | |
sillevl | 6:414e1fea64f3 | 12 | MbedJSONValue* jsonFactory() |
sillevl | 6:414e1fea64f3 | 13 | { |
sillevl | 6:414e1fea64f3 | 14 | MbedJSONValue* json = new MbedJSONValue(); |
sillevl | 6:414e1fea64f3 | 15 | (*json)["id"] = M3DPI_ID; |
sillevl | 6:414e1fea64f3 | 16 | return json; |
sillevl | 6:414e1fea64f3 | 17 | } |
sillevl | 6:414e1fea64f3 | 18 | |
sillevl | 6:414e1fea64f3 | 19 | void printJson(MbedJSONValue* json) |
sillevl | 6:414e1fea64f3 | 20 | { |
sillevl | 6:414e1fea64f3 | 21 | pc.printf("%s\n", json->serialize().c_str()); |
sillevl | 6:414e1fea64f3 | 22 | } |
sillevl | 6:414e1fea64f3 | 23 | |
sillevl | 6:414e1fea64f3 | 24 | void printTime(time_t seconds) |
sillevl | 6:414e1fea64f3 | 25 | { |
sillevl | 6:414e1fea64f3 | 26 | char buffer[32]; |
sillevl | 6:414e1fea64f3 | 27 | std::strftime(buffer, 32, "%d-%m-%Y %T", localtime(&seconds)); |
sillevl | 6:414e1fea64f3 | 28 | MbedJSONValue* jsonTime = jsonFactory(); |
sillevl | 6:414e1fea64f3 | 29 | (*jsonTime)["time"] = buffer; |
sillevl | 6:414e1fea64f3 | 30 | printJson(jsonTime); |
sillevl | 6:414e1fea64f3 | 31 | delete jsonTime; |
sillevl | 6:414e1fea64f3 | 32 | } |
sillevl | 6:414e1fea64f3 | 33 | |
sillevl | 6:414e1fea64f3 | 34 | void printDistance(m3dpi::Distance distance) |
sillevl | 6:414e1fea64f3 | 35 | { |
sillevl | 6:414e1fea64f3 | 36 | MbedJSONValue* json = jsonFactory(); |
sillevl | 6:414e1fea64f3 | 37 | const char property[] = "distance"; |
sillevl | 6:414e1fea64f3 | 38 | |
sillevl | 6:414e1fea64f3 | 39 | (*json)[property][0] = distance.front; |
sillevl | 6:414e1fea64f3 | 40 | (*json)[property][1] = distance.front_right; |
sillevl | 6:414e1fea64f3 | 41 | (*json)[property][2] = distance.right; |
sillevl | 6:414e1fea64f3 | 42 | (*json)[property][3] = distance.back_right; |
sillevl | 6:414e1fea64f3 | 43 | (*json)[property][4] = distance.back; |
sillevl | 6:414e1fea64f3 | 44 | (*json)[property][5] = distance.back_left; |
sillevl | 6:414e1fea64f3 | 45 | (*json)[property][6] = distance.left; |
sillevl | 6:414e1fea64f3 | 46 | (*json)[property][7] = distance.front_left; |
sillevl | 6:414e1fea64f3 | 47 | |
sillevl | 6:414e1fea64f3 | 48 | printJson(json); |
sillevl | 6:414e1fea64f3 | 49 | delete json; |
sillevl | 6:414e1fea64f3 | 50 | } |
sillevl | 6:414e1fea64f3 | 51 | |
sillevl | 6:414e1fea64f3 | 52 | void printAcceleration(m3dpi::Acceleration acc) |
sillevl | 6:414e1fea64f3 | 53 | { |
sillevl | 6:414e1fea64f3 | 54 | MbedJSONValue* json = jsonFactory(); |
sillevl | 6:414e1fea64f3 | 55 | const char property[] = "acceleration"; |
sillevl | 6:414e1fea64f3 | 56 | |
sillevl | 6:414e1fea64f3 | 57 | (*json)[property]["x"] = acc.x; |
sillevl | 6:414e1fea64f3 | 58 | (*json)[property]["y"] = acc.y; |
sillevl | 6:414e1fea64f3 | 59 | (*json)[property]["z"] = acc.z; |
sillevl | 6:414e1fea64f3 | 60 | |
sillevl | 6:414e1fea64f3 | 61 | printJson(json); |
sillevl | 6:414e1fea64f3 | 62 | delete json; |
sillevl | 6:414e1fea64f3 | 63 | } |
sillevl | 6:414e1fea64f3 | 64 | |
sillevl | 6:414e1fea64f3 | 65 | void printDirection(m3dpi::Direction direction) |
sillevl | 6:414e1fea64f3 | 66 | { |
sillevl | 6:414e1fea64f3 | 67 | MbedJSONValue* json = jsonFactory(); |
sillevl | 6:414e1fea64f3 | 68 | const char property[] = "direction"; |
sillevl | 6:414e1fea64f3 | 69 | |
sillevl | 6:414e1fea64f3 | 70 | (*json)[property]["x"] = direction.x; |
sillevl | 6:414e1fea64f3 | 71 | (*json)[property]["y"] = direction.y; |
sillevl | 6:414e1fea64f3 | 72 | (*json)[property]["z"] = direction.z; |
sillevl | 6:414e1fea64f3 | 73 | |
sillevl | 6:414e1fea64f3 | 74 | printJson(json); |
sillevl | 6:414e1fea64f3 | 75 | delete json; |
sillevl | 6:414e1fea64f3 | 76 | } |
sillevl | 6:414e1fea64f3 | 77 | |
sillevl | 6:414e1fea64f3 | 78 | void printRotation(m3dpi::Rotation rotation) |
sillevl | 6:414e1fea64f3 | 79 | { |
sillevl | 6:414e1fea64f3 | 80 | MbedJSONValue* json = jsonFactory(); |
sillevl | 6:414e1fea64f3 | 81 | const char property[] = "rotation"; |
sillevl | 6:414e1fea64f3 | 82 | |
sillevl | 6:414e1fea64f3 | 83 | (*json)[property]["x"] = rotation.x; |
sillevl | 6:414e1fea64f3 | 84 | (*json)[property]["y"] = rotation.y; |
sillevl | 6:414e1fea64f3 | 85 | (*json)[property]["z"] = rotation.z; |
sillevl | 6:414e1fea64f3 | 86 | |
sillevl | 6:414e1fea64f3 | 87 | printJson(json); |
sillevl | 6:414e1fea64f3 | 88 | delete json; |
sillevl | 6:414e1fea64f3 | 89 | } |
sillevl | 6:414e1fea64f3 | 90 | |
sillevl | 6:414e1fea64f3 | 91 | void setLeds(m3dpi::Distance distance) |
sillevl | 6:414e1fea64f3 | 92 | { |
sillevl | 6:414e1fea64f3 | 93 | 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 | 94 | // shift distance value to get red colors |
sillevl | 6:414e1fea64f3 | 95 | for(int i = 0; i < 8; i++){ |
sillevl | 6:414e1fea64f3 | 96 | colors[i] = colors[i] << 16; |
sillevl | 6:414e1fea64f3 | 97 | } |
sillevl | 6:414e1fea64f3 | 98 | robot.setLeds(colors); |
sillevl | 6:414e1fea64f3 | 99 | } |
sillevl | 6:414e1fea64f3 | 100 | |
sillevl | 6:414e1fea64f3 | 101 | void readSensors() |
sillevl | 6:414e1fea64f3 | 102 | { |
sillevl | 6:414e1fea64f3 | 103 | m3dpi::Distance distance = robot.getDistance(); |
sillevl | 6:414e1fea64f3 | 104 | |
sillevl | 6:414e1fea64f3 | 105 | printDistance(distance); |
sillevl | 6:414e1fea64f3 | 106 | printDirection(robot.getDirection()); |
sillevl | 6:414e1fea64f3 | 107 | printRotation(robot.getRotation()); |
sillevl | 6:414e1fea64f3 | 108 | printAcceleration(robot.getAcceleration()); |
sillevl | 6:414e1fea64f3 | 109 | |
sillevl | 6:414e1fea64f3 | 110 | printTime(robot.getTime()); |
sillevl | 6:414e1fea64f3 | 111 | setLeds(distance); |
sillevl | 6:414e1fea64f3 | 112 | } |
sillevl | 0:ab0ce9ab81bf | 113 | |
sillevl | 1:6f620263c9f8 | 114 | int main() |
sillevl | 6:414e1fea64f3 | 115 | { |
sillevl | 1:6f620263c9f8 | 116 | pc.baud(115200); |
sillevl | 6:414e1fea64f3 | 117 | robot.setStatus(Color::GREEN); |
sillevl | 1:6f620263c9f8 | 118 | |
sillevl | 6:414e1fea64f3 | 119 | pc.printf("Device ID is: 0x%02x\n", robot.accelerometer.getDevId()); |
sillevl | 6:414e1fea64f3 | 120 | pc.printf("Gyro id: 0x%02x\n", robot.gyro.getWhoAmI()); |
sillevl | 6:414e1fea64f3 | 121 | |
sillevl | 6:414e1fea64f3 | 122 | tick.attach(&readSensors, 0.25); |
sillevl | 0:ab0ce9ab81bf | 123 | |
sillevl | 1:6f620263c9f8 | 124 | while(1) { |
sillevl | 1:6f620263c9f8 | 125 | |
sillevl | 1:6f620263c9f8 | 126 | } |
sillevl | 0:ab0ce9ab81bf | 127 | } |