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

Committer:
kudrom
Date:
Fri Dec 11 10:40:40 2015 +0000
Revision:
8:5c0833506d67
Parent:
7:9068fc754a0b
Added battery to the reporter and the commands interface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 7:9068fc754a0b 1 #include "jsonReporter.h"
sillevl 7:9068fc754a0b 2
sillevl 7:9068fc754a0b 3 JsonReporter::JsonReporter(mbed::Stream* _out, const char _id[]) : out(_out), id(_id)
sillevl 7:9068fc754a0b 4 {
sillevl 7:9068fc754a0b 5
sillevl 7:9068fc754a0b 6 }
sillevl 7:9068fc754a0b 7
sillevl 7:9068fc754a0b 8 MbedJSONValue* JsonReporter::jsonFactory()
sillevl 7:9068fc754a0b 9 {
sillevl 7:9068fc754a0b 10 MbedJSONValue* json = new MbedJSONValue();
sillevl 7:9068fc754a0b 11 (*json)["id"] = id;
sillevl 7:9068fc754a0b 12 return json;
sillevl 7:9068fc754a0b 13 }
sillevl 7:9068fc754a0b 14
sillevl 7:9068fc754a0b 15 void JsonReporter::print(MbedJSONValue* json)
sillevl 7:9068fc754a0b 16 {
sillevl 7:9068fc754a0b 17 out->printf("%s\n", json->serialize().c_str());
sillevl 7:9068fc754a0b 18 }
sillevl 7:9068fc754a0b 19
sillevl 7:9068fc754a0b 20 void JsonReporter::time(time_t seconds)
sillevl 7:9068fc754a0b 21 {
sillevl 7:9068fc754a0b 22 MbedJSONValue* jsonTime = jsonFactory();
kudrom 8:5c0833506d67 23 int timestamp = static_cast<int>(seconds);
kudrom 8:5c0833506d67 24 (*jsonTime)["time"] = timestamp;
sillevl 7:9068fc754a0b 25 print(jsonTime);
sillevl 7:9068fc754a0b 26 delete jsonTime;
sillevl 7:9068fc754a0b 27 }
sillevl 7:9068fc754a0b 28
sillevl 7:9068fc754a0b 29 void JsonReporter::distance(m3dpi::Distance distance)
sillevl 7:9068fc754a0b 30 {
sillevl 7:9068fc754a0b 31 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 32 const char property[] = "distance";
sillevl 7:9068fc754a0b 33
sillevl 7:9068fc754a0b 34 (*json)[property][0] = distance.front;
sillevl 7:9068fc754a0b 35 (*json)[property][1] = distance.front_right;
sillevl 7:9068fc754a0b 36 (*json)[property][2] = distance.right;
sillevl 7:9068fc754a0b 37 (*json)[property][3] = distance.back_right;
sillevl 7:9068fc754a0b 38 (*json)[property][4] = distance.back;
sillevl 7:9068fc754a0b 39 (*json)[property][5] = distance.back_left;
sillevl 7:9068fc754a0b 40 (*json)[property][6] = distance.left;
sillevl 7:9068fc754a0b 41 (*json)[property][7] = distance.front_left;
sillevl 7:9068fc754a0b 42
sillevl 7:9068fc754a0b 43 print(json);
sillevl 7:9068fc754a0b 44 delete json;
sillevl 7:9068fc754a0b 45 }
sillevl 7:9068fc754a0b 46
sillevl 7:9068fc754a0b 47 void JsonReporter::acceleration(m3dpi::Acceleration acc)
sillevl 7:9068fc754a0b 48 {
sillevl 7:9068fc754a0b 49 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 50 const char property[] = "acceleration";
sillevl 7:9068fc754a0b 51
sillevl 7:9068fc754a0b 52 (*json)[property]["x"] = acc.x;
sillevl 7:9068fc754a0b 53 (*json)[property]["y"] = acc.y;
sillevl 7:9068fc754a0b 54 (*json)[property]["z"] = acc.z;
sillevl 7:9068fc754a0b 55
sillevl 7:9068fc754a0b 56 print(json);
sillevl 7:9068fc754a0b 57 delete json;
sillevl 7:9068fc754a0b 58 }
sillevl 7:9068fc754a0b 59
sillevl 7:9068fc754a0b 60 void JsonReporter::direction(m3dpi::Direction direction)
sillevl 7:9068fc754a0b 61 {
sillevl 7:9068fc754a0b 62 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 63 const char property[] = "direction";
sillevl 7:9068fc754a0b 64
sillevl 7:9068fc754a0b 65 (*json)[property]["x"] = direction.x;
sillevl 7:9068fc754a0b 66 (*json)[property]["y"] = direction.y;
sillevl 7:9068fc754a0b 67 (*json)[property]["z"] = direction.z;
sillevl 7:9068fc754a0b 68
sillevl 7:9068fc754a0b 69 print(json);
sillevl 7:9068fc754a0b 70 delete json;
sillevl 7:9068fc754a0b 71 }
sillevl 7:9068fc754a0b 72
sillevl 7:9068fc754a0b 73 void JsonReporter::rotation(m3dpi::Rotation rotation)
sillevl 7:9068fc754a0b 74 {
sillevl 7:9068fc754a0b 75 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 76 const char property[] = "rotation";
sillevl 7:9068fc754a0b 77
sillevl 7:9068fc754a0b 78 (*json)[property]["x"] = rotation.x;
sillevl 7:9068fc754a0b 79 (*json)[property]["y"] = rotation.y;
sillevl 7:9068fc754a0b 80 (*json)[property]["z"] = rotation.z;
sillevl 7:9068fc754a0b 81
sillevl 7:9068fc754a0b 82 print(json);
sillevl 7:9068fc754a0b 83 delete json;
sillevl 7:9068fc754a0b 84 }
kudrom 8:5c0833506d67 85
kudrom 8:5c0833506d67 86 void JsonReporter::battery(float battery_value)
kudrom 8:5c0833506d67 87 {
kudrom 8:5c0833506d67 88 MbedJSONValue* json = jsonFactory();
kudrom 8:5c0833506d67 89 const char property[] = "battery";
kudrom 8:5c0833506d67 90
kudrom 8:5c0833506d67 91 (*json)[property] = battery_value;
kudrom 8:5c0833506d67 92
kudrom 8:5c0833506d67 93 print(json);
kudrom 8:5c0833506d67 94 delete json;
kudrom 8:5c0833506d67 95 }