demo project

Dependencies:   AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL

Files at this revision

API Documentation at this revision

Comitter:
henryrawas
Date:
Fri Jan 22 01:35:07 2016 +0000
Parent:
16:c3c0a3a56015
Child:
18:224289104fc0
Commit message:
add all measures to circular buffer at once. Handle alert command

Changed in this revision

ArmController.cpp Show annotated file Show diff for this revision Revisions of this file
ControllerUtil.cpp Show annotated file Show diff for this revision Revisions of this file
IothubSerial.cpp Show annotated file Show diff for this revision Revisions of this file
IothubSerial.h Show annotated file Show diff for this revision Revisions of this file
MeasureBuf.cpp Show annotated file Show diff for this revision Revisions of this file
MeasureBuf.h Show annotated file Show diff for this revision Revisions of this file
--- a/ArmController.cpp	Tue Jan 19 20:18:23 2016 +0000
+++ b/ArmController.cpp	Fri Jan 22 01:35:07 2016 +0000
@@ -26,8 +26,7 @@
 
 // try to send some status this many ms
 #define SEND_STATUS_TO          500
-// use slower status send rate if paused
-#define SEND_STATUS_PAUSED_TO   20000
+
 
 // controller polling timer
 Timer IdleTimer;
@@ -59,6 +58,11 @@
         ShowLedBlue();
         MainState = MS_Paused;
     }
+    else if (strncmp(cmd, "alert", 6) == 0)
+    {
+        ShowLedRed();
+        MainState = MS_Error;
+    }
     else if (strncmp(cmd, "resume", 6) == 0)
     {
         ShowLedGreen();
@@ -344,22 +348,17 @@
         // check if time to send status
         if (now >= NextSendMs)
         {
-            // if paused, use longer time out for sending data
-            if (MainState != MS_Paused ||
-                now > NextSendMs + SEND_STATUS_PAUSED_TO)
+            NextSendMs = now + SEND_STATUS_TO;
+            if (!PushMeasurements(partSize, robotArm))
             {
-                NextSendMs = now + SEND_STATUS_TO;
-                if (!PushMeasurements(partSize, robotArm))
+                if (sendAlert)
                 {
-                    if (sendAlert)
-                    {
-                        int partix = robotArm.GetLastErrorPart();
-                        int errCode = robotArm.GetLastError();
-                        printf("Hardware error detected joint %d, code %d \r\n", partix, errCode); 
-                        PushHardwareAlert(partix, errCode);
-                        NeedHwReset = true;
-                        MainState = MS_Error;
-                    }
+                    int partix = robotArm.GetLastErrorPart();
+                    int errCode = robotArm.GetLastError();
+                    printf("Hardware error detected joint %d, code %d \r\n", partix, errCode); 
+                    PushHardwareAlert(partix, errCode);
+                    NeedHwReset = true;
+                    MainState = MS_Error;
                 }
             }
         }
--- a/ControllerUtil.cpp	Tue Jan 19 20:18:23 2016 +0000
+++ b/ControllerUtil.cpp	Fri Jan 22 01:35:07 2016 +0000
@@ -129,40 +129,41 @@
 bool PushMeasurements(int partSize, RobotArm& robotArm)
 {
     // space for getting measurements from arm
-    MeasureGroup measureGroup;
+    MeasureSnapshot measureSnap;
     
     float lastVals[NUMJOINTS];
-    time_t seconds = time(NULL);
+    measureSnap.Created = time(NULL);
    
     bool ok = true;
 
     ok = robotArm.GetArmLastMeasure(NM_Temperature, lastVals);
     DispMeasure("Temperatures", partSize, lastVals);
-    measureGroup.SetMeasure(NM_Temperature, seconds, partSize, lastVals);
-    MeasureBuf.push(measureGroup);
+    measureSnap.Temps.SetMeasure(NM_Temperature, partSize, lastVals);
     
     if (ok)
     {
         ok = robotArm.GetArmLastMeasure(NM_Voltage, lastVals);
         DispMeasure("Voltage", partSize, lastVals);
-        measureGroup.SetMeasure(NM_Voltage, seconds, partSize, lastVals);
-        MeasureBuf.push(measureGroup);
+        measureSnap.Volts.SetMeasure(NM_Voltage, partSize, lastVals);
     }
     
     if (ok)
     {
         ok = robotArm.GetArmLastMeasure(NM_Degrees, lastVals);
         DispMeasure("Rotation", partSize, lastVals);
-        measureGroup.SetMeasure(NM_Degrees, seconds, partSize, lastVals);
-        MeasureBuf.push(measureGroup);
+        measureSnap.Positions.SetMeasure(NM_Degrees, partSize, lastVals);
     }
     
     if (ok)
     {
-        robotArm.GetArmLastMeasure(NM_Load, lastVals);
+        ok = robotArm.GetArmLastMeasure(NM_Load, lastVals);
         DispMeasure("Load", partSize, lastVals);
-        measureGroup.SetMeasure(NM_Load, seconds, partSize, lastVals);
-        MeasureBuf.push(measureGroup);
+        measureSnap.Loads.SetMeasure(NM_Load, partSize, lastVals);
+    }
+    
+    if (ok)
+    {
+        MeasureBuf.push(measureSnap);
     }
     
     return ok;
--- a/IothubSerial.cpp	Tue Jan 19 20:18:23 2016 +0000
+++ b/IothubSerial.cpp	Fri Jan 22 01:35:07 2016 +0000
@@ -96,7 +96,77 @@
         return -1;
 }
 
-// try to serialize one or more measurements into the buffer
+
+// try to serialize a snapshot into the buffer
+// return bytes used / -1 if buffer too small / 0 if no data
+// current serialization is a json object with time and array per measure
+// eg: { "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }
+int IothubSerial::MeasureSnapshotToString(MeasureSnapshot& msnap, char* buf, int bufsize)
+{
+    int slen;
+    int startlen = bufsize;
+
+    slen = sprintf_s(buf, bufsize, "{");
+    if (slen > 0)
+    {
+        bufsize -= slen;
+        buf += slen;
+    }
+    else
+        return -1;
+
+    slen = AddTime(msnap.Created, buf, bufsize);
+    if (slen > 0)
+    {
+        bufsize -= slen;
+        buf += slen;
+    }
+    else
+        return -1;
+
+    slen = MeasureGroupToString(msnap.Temps, buf, bufsize);
+    if (slen > 0)
+    {
+        bufsize -= slen;
+        buf += slen;
+    }
+    else
+        return -1;
+
+    slen = MeasureGroupToString(msnap.Positions, buf, bufsize);
+    if (slen > 0)
+    {
+        bufsize -= slen;
+        buf += slen;
+    }
+    else
+        return -1;
+
+    slen = MeasureGroupToString(msnap.Loads, buf, bufsize);
+    if (slen > 0)
+    {
+        bufsize -= slen;
+        buf += slen;
+    }
+    else
+        return -1;
+
+    slen = MeasureGroupToString(msnap.Volts, buf, bufsize);
+    if (slen > 0)
+    {
+        bufsize -= slen;
+        buf += slen;
+    }
+    else
+        return -1;
+
+    // replace final ',' with '}'
+    *(buf - 1) = '}';
+    
+    return startlen - bufsize;
+}
+
+// try to serialize one or more measurement snapshots into the buffer
 // return bytes used / -1 if buffer too small / 0 if no data
 // current serialization is a json array of objects with time and array per measure
 // eg: [{ "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }]
@@ -106,14 +176,9 @@
     bool hasdata = false;
     bool copydata = false;
     char* startbuf = buf;
-    bool settime = false;
     char* lastcomma = NULL;
     
-    time_t secs = 0;
-    // reserve 1 space for end
-    bufsize--;
-    
-    slen = sprintf_s(buf, bufsize, "[{");
+    slen = sprintf_s(buf, bufsize, "[");
     if (slen > 0)
     {
         bufsize -= slen;
@@ -125,28 +190,26 @@
     if (_hasPending)
     {
         hasdata = true;
-        secs = _pending.Created;
-        slen = AddTime(secs, buf, bufsize);
+        slen = MeasureSnapshotToString(_pending, buf, bufsize);
         if (slen > 0)
         {
             bufsize -= slen;
             buf += slen;
-            settime = true;
         }
         else
             return -1;          // no room for pending record
-
-        slen = MeasureGroupToString(_pending, buf, bufsize);
+        // add comma
+        slen = sprintf_s(buf, bufsize, ",");
         if (slen > 0)
         {
             bufsize -= slen;
             buf += slen;
-            lastcomma = buf;
-            _hasPending = false;
-            copydata = true;
         }
         else
-            return -1;          // no room for pending record
+            return -1;
+        lastcomma = buf;
+        _hasPending = false;
+        copydata = true;
     }
     
     while (!MeasureBuf.empty())
@@ -158,41 +221,26 @@
         hasdata = true;
         _hasPending = true;
         
-        if (secs != _pending.Created)
-        {
-            if (settime && lastcomma != NULL)
-            {
-                *(lastcomma - 1) = '}';
-                if (bufsize > 2)
-                {
-                    strcpy(buf, ",{");
-                    buf += 2;
-                    bufsize -= 2;
-                }
-            }
-            secs = _pending.Created;
-            slen = AddTime(secs, buf, bufsize);
-            if (slen > 0)
-            {
-                bufsize -= slen;
-                buf += slen;
-                settime = true;
-            }
-            else
-                break;
-        }
-        
-        slen = MeasureGroupToString(_pending, buf, bufsize);
+        slen = MeasureSnapshotToString(_pending, buf, bufsize);
         if (slen > 0)
         {
             bufsize -= slen;
             buf += slen;
-            lastcomma = buf;
-            _hasPending = false;
-            copydata = true;
         }
         else
             break;              // no room to serialize, leave pending for next message
+        // add comma
+        slen = sprintf_s(buf, bufsize, ",");
+        if (slen > 0)
+        {
+            bufsize -= slen;
+            buf += slen;
+        }
+        else
+            break;
+        _hasPending = false;
+        lastcomma = buf;
+        copydata = true;
     }
     
     if (!hasdata)
@@ -201,11 +249,8 @@
     if (!copydata)
         return -1;              // have data but buffer too small
 
-    // replace final ',' with '}'
-    *(lastcomma - 1) = '}';
-    // this is the extra space we reserved at the start.
-    *(lastcomma) = ']';
-    lastcomma++;
+    // replace final ',' with ']'
+    *(lastcomma - 1) = ']';
     
     return lastcomma - startbuf;
 }
@@ -247,7 +292,3 @@
     return startlen - bufsize;
 }
 
-bool IothubSerial::HasMeasureGroup()
-{
-    return _hasPending;
-}
--- a/IothubSerial.h	Tue Jan 19 20:18:23 2016 +0000
+++ b/IothubSerial.h	Fri Jan 22 01:35:07 2016 +0000
@@ -15,16 +15,16 @@
 public:
     IothubSerial();
     
-    int MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize);
-    
     int MeasureBufToString(char* buf, int bufsize);
 
     int AlertBufToString(char* buf, int bufsize);
-
-    bool HasMeasureGroup();
     
 private:
-    MeasureGroup _pending;
+    int MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize);
+    
+    int MeasureSnapshotToString(MeasureSnapshot& msnap, char* buf, int bufsize);
+    
+    MeasureSnapshot _pending;
     
     bool _hasPending;
     
--- a/MeasureBuf.cpp	Tue Jan 19 20:18:23 2016 +0000
+++ b/MeasureBuf.cpp	Fri Jan 22 01:35:07 2016 +0000
@@ -4,12 +4,11 @@
 
 
 
-SafeCircBuf<MeasureGroup, MeasureBufSize, uint32_t> MeasureBuf;
+SafeCircBuf<MeasureSnapshot, MeasureBufSize, uint32_t> MeasureBuf;
 
-void MeasureGroup::SetMeasure(NodeMeasure mId, time_t created, int numParts, float vals[])
+void MeasureGroup::SetMeasure(NodeMeasure mId, int numParts, float vals[])
 {
     MeasId = mId;
-    Created = created;
     
     NumVals = NUMJOINTS < numParts ? NUMJOINTS : numParts;
     
@@ -22,7 +21,6 @@
 MeasureGroup& MeasureGroup::operator=(const MeasureGroup& rhs)
 {
     MeasId = rhs.MeasId;
-    Created = rhs.Created;
     NumVals = rhs.NumVals;
     for (int ix = 0; ix < NUMJOINTS; ix++)
     {
--- a/MeasureBuf.h	Tue Jan 19 20:18:23 2016 +0000
+++ b/MeasureBuf.h	Fri Jan 22 01:35:07 2016 +0000
@@ -20,17 +20,31 @@
     
     MeasureGroup& operator=(const MeasureGroup& rhs);
     
-    void SetMeasure(NodeMeasure mId, time_t created, int numParts, float vals[]);
+    void SetMeasure(NodeMeasure mId, int numParts, float vals[]);
     
     NodeMeasure MeasId;
     
     int NumVals;
     
     float MeasVals[NUMJOINTS];
+};
+
+class MeasureSnapshot
+{
+public:
+    MeasureSnapshot() {};
+    
+    MeasureGroup Temps;
+    
+    MeasureGroup Volts;
+    
+    MeasureGroup Loads;
+    
+    MeasureGroup Positions;
     
     time_t Created;
 };
 
-extern SafeCircBuf<MeasureGroup, MeasureBufSize, uint32_t> MeasureBuf;
+extern SafeCircBuf<MeasureSnapshot, MeasureBufSize, uint32_t> MeasureBuf;
 
 #endif
\ No newline at end of file