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

Revision:
7:9068fc754a0b
Parent:
6:414e1fea64f3
Child:
8:5c0833506d67
Child:
11:ccb8653e285f
--- a/main.cpp	Thu Dec 03 16:36:20 2015 +0000
+++ b/main.cpp	Thu Dec 03 17:53:04 2015 +0000
@@ -1,92 +1,14 @@
-
+#include "mbed.h"
+#include "rtos.h"
 #include "M3Dpi.h"
-#include "MbedJSONValue.h"
-
-M3Dpi robot;
-Serial pc(USBTX,USBRX);
-
-Ticker tick;
+#include "jsonReporter.h"
 
 const char M3DPI_ID[] = "1234567890";
 
-MbedJSONValue* jsonFactory()
-{
-    MbedJSONValue* json = new MbedJSONValue();
-    (*json)["id"] = M3DPI_ID;
-    return json;
-}
-
-void printJson(MbedJSONValue* json)
-{
-    pc.printf("%s\n", json->serialize().c_str());
-}
-
-void printTime(time_t seconds)
-{
-    char buffer[32];
-    std::strftime(buffer, 32, "%d-%m-%Y %T", localtime(&seconds));
-    MbedJSONValue* jsonTime = jsonFactory();
-    (*jsonTime)["time"] = buffer;
-    printJson(jsonTime);
-    delete jsonTime;   
-}
-
-void printDistance(m3dpi::Distance distance)
-{
-    MbedJSONValue* json = jsonFactory();
-    const char property[] = "distance";
-
-    (*json)[property][0] = distance.front;
-    (*json)[property][1] = distance.front_right;
-    (*json)[property][2] = distance.right;
-    (*json)[property][3] = distance.back_right;
-    (*json)[property][4] = distance.back;
-    (*json)[property][5] = distance.back_left;
-    (*json)[property][6] = distance.left;
-    (*json)[property][7] = distance.front_left;
+M3Dpi robot;
+Serial pc(USBTX,USBRX);
+JsonReporter report(&pc, M3DPI_ID);
 
-    printJson(json);
-    delete json;
-}
-
-void printAcceleration(m3dpi::Acceleration acc)
-{
-    MbedJSONValue* json = jsonFactory();
-    const char property[] = "acceleration";
-    
-    (*json)[property]["x"] = acc.x;
-    (*json)[property]["y"] = acc.y;
-    (*json)[property]["z"] = acc.z;
-    
-    printJson(json);
-    delete json;
-}
-
-void printDirection(m3dpi::Direction direction)
-{
-    MbedJSONValue* json = jsonFactory();
-    const char property[] = "direction";
-    
-    (*json)[property]["x"] = direction.x;
-    (*json)[property]["y"] = direction.y;
-    (*json)[property]["z"] = direction.z;
-    
-    printJson(json);
-    delete json;
-}
-
-void printRotation(m3dpi::Rotation rotation)
-{
-    MbedJSONValue* json = jsonFactory();
-    const char property[] = "rotation";
-    
-    (*json)[property]["x"] = rotation.x;
-    (*json)[property]["y"] = rotation.y;
-    (*json)[property]["z"] = rotation.z;
-    
-    printJson(json);
-    delete json;
-}
 
 void setLeds(m3dpi::Distance distance)
 {
@@ -98,30 +20,31 @@
     robot.setLeds(colors);
 }
 
-void readSensors()
+void read_sensors_thread(void const * args)
 {
+    robot.setStatus(Color::GREEN);
     m3dpi::Distance distance = robot.getDistance();
+    setLeds(distance);
     
-    printDistance(distance);
-    printDirection(robot.getDirection());
-    printRotation(robot.getRotation());
-    printAcceleration(robot.getAcceleration());
+    report.distance(distance);
+    report.direction(robot.getDirection());
+    report.rotation(robot.getRotation());
+    report.acceleration(robot.getAcceleration());
     
-    printTime(robot.getTime());
-    setLeds(distance);
+    report.time(robot.getTime());
+    robot.setStatus(Color::OFF);
 }
 
 int main()
 {            
     pc.baud(115200);
-    robot.setStatus(Color::GREEN);
-    
+       
+    // for testing...
     pc.printf("Device ID is: 0x%02x\n", robot.accelerometer.getDevId());
     pc.printf("Gyro id: 0x%02x\n", robot.gyro.getWhoAmI());
     
-    tick.attach(&readSensors, 0.25);
+    RtosTimer periodicThread(read_sensors_thread, osTimerPeriodic);
+    periodicThread.start(50);
     
-    while(1) {
-
-    }
+    Thread::wait(osWaitForever);
 }
\ No newline at end of file