Proof of concept for distance and temperature monitoring

Dependencies:   mbed mbedConnectorInterface mbedEndpointNetwork

Files at this revision

API Documentation at this revision

Comitter:
coyotebush
Date:
Mon May 18 00:44:59 2015 +0000
Parent:
9:16b63cd4aba8
Commit message:
max distance

Changed in this revision

hcsr04.cpp Show annotated file Show diff for this revision Revisions of this file
hcsr04.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
nsp_configuration.h Show annotated file Show diff for this revision Revisions of this file
resource/MaxDistanceResource.h Show annotated file Show diff for this revision Revisions of this file
diff -r 16b63cd4aba8 -r 338191178cbf hcsr04.cpp
--- a/hcsr04.cpp	Thu May 07 04:14:57 2015 +0000
+++ b/hcsr04.cpp	Mon May 18 00:44:59 2015 +0000
@@ -22,7 +22,7 @@
 
 
 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
-    trigger(TrigPin), echo(EchoPin)
+    trigger(TrigPin), echo(EchoPin), maxpulse(-1)
 {
     pulsetime.stop();
     pulsetime.reset();
@@ -42,7 +42,7 @@
 }
 void HCSR04::start(void)
 {
-    pulsedur = 0;
+    pulsedur = -1;
     
     trigger=1;
     wait_us(10);
@@ -53,6 +53,8 @@
 {
     pulsetime.stop();
     pulsedur = pulsetime.read_us();
+    if (pulsedur > maxpulse && pulsedur < kBogusPulse)
+        maxpulse = pulsedur;
 }
 
 void HCSR04::rise (void (*fptr)(void))
@@ -66,11 +68,13 @@
 
 double HCSR04::get_dist_cm()
 {
-    if (pulsedur == 0)
-        return -1;
     return pulsedur * kMicrosecondsToCentimeters;
 }
-unsigned int HCSR04::get_pulse_us()
+double HCSR04::get_max_dist_cm()
+{
+    return maxpulse * kMicrosecondsToCentimeters;
+}
+int HCSR04::get_pulse_us()
 {
     return pulsedur;
 }
diff -r 16b63cd4aba8 -r 338191178cbf hcsr04.h
--- a/hcsr04.h	Thu May 07 04:14:57 2015 +0000
+++ b/hcsr04.h	Mon May 18 00:44:59 2015 +0000
@@ -35,6 +35,7 @@
 {
 public:
     static const double kMicrosecondsToCentimeters = 343.0 / 20000;
+    static const double kBogusPulse = 500 / kMicrosecondsToCentimeters;
     
     /** Create a HCSR04 object connected to the specified pin
     * @param pin i/o pin to connect to
@@ -46,10 +47,11 @@
     * @param distance in cms and returns -1, in case of failure
     */
     double get_dist_cm(void);
+    double get_max_dist_cm(void);
     /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver.
     * @param pulse duration in microseconds.
     */
-    unsigned int get_pulse_us(void);
+    int get_pulse_us(void);
     /** Generates the trigger pulse of 10us on the trigger PIN.
     */
     void start(void );
@@ -65,7 +67,8 @@
     Timer pulsetime;
     DigitalOut  trigger;
     InterruptIn echo;
-    unsigned int pulsedur;
+    int pulsedur;
+    int maxpulse;
 };
 
 #endif
\ No newline at end of file
diff -r 16b63cd4aba8 -r 338191178cbf main.cpp
--- a/main.cpp	Thu May 07 04:14:57 2015 +0000
+++ b/main.cpp	Mon May 18 00:44:59 2015 +0000
@@ -7,9 +7,10 @@
 #include "GroveTemp.h"
 
 #include "StaticResource.h"
-#include "OnBoardLED.h"
+//#include "OnBoardLED.h"
 #include "TemperatureResource.h"
 #include "DistanceResource.h"
+#include "MaxDistanceResource.h"
 
 /* Terminal connection */
 RawSerial term(USBTX, USBRX);
@@ -28,9 +29,14 @@
 StaticResource latitude(&logger, "6/0/0", "35.296");
 StaticResource longitude(&logger, "6/0/1", "-120.677");
 
-DistanceResource distR(&logger, "3302/0/5600", &distS, true);
+DistanceResource distR(&logger, "3302/0/5700", &distS, true);
+MaxDistanceResource maxDistR(&logger, "3302/0/5602", &distS, true);
+StaticResource unitsDistR(&logger, "3302/0/5701", "cm");
+
 TemperatureResource tempR(&logger, "3303/0/5700", &tempS, true);
-LEDResource led(&logger, "3311/1/5706");
+StaticResource unitsTempR(&logger, "3302/0/5701", "Cel");
+
+//LEDResource led(&logger, "3311/1/5706");
 
 /* invoked through linker magic via Endpoint::start() */
 Connector::Options *configure_endpoint(Connector::OptionsBuilder &config)
@@ -44,8 +50,11 @@
                  .addResource(&latitude)
                  .addResource(&longitude)
                  .addResource(&distR)
+                 .addResource(&maxDistR)
+                 .addResource(&unitsDistR)
                  .addResource(&tempR)
-                 .addResource(&led)
+                 .addResource(&unitsTempR)
+                 //.addResource(&led)
                  .build();
 }
 
diff -r 16b63cd4aba8 -r 338191178cbf nsp_configuration.h
--- a/nsp_configuration.h	Thu May 07 04:14:57 2015 +0000
+++ b/nsp_configuration.h	Mon May 18 00:44:59 2015 +0000
@@ -25,7 +25,7 @@
  
  // NSP node name
  #define NODE_NAME_LENGTH         24
- #define NODE_NAME                "trashcan"
+ #define NODE_NAME                "Throop"
  
  // NSP Address (4 bytes for IPV4, 16 bytes for IPV6)
  #define NSP_IP_ADDRESS_LENGTH    4
diff -r 16b63cd4aba8 -r 338191178cbf resource/MaxDistanceResource.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resource/MaxDistanceResource.h	Mon May 18 00:44:59 2015 +0000
@@ -0,0 +1,23 @@
+#ifndef MAX_DISTANCE_RESOURCE_H
+#define MAX_DISTANCE_RESOURCE_H
+
+#include "DynamicResource.h"
+#include "hcsr04.h"
+
+class MaxDistanceResource : public DynamicResource {
+public:
+    MaxDistanceResource(const Logger *logger, const char *name, HCSR04 *sensor, const bool observable = false)
+     : DynamicResource(logger, name, "MaxDistanceResource", SN_GRS_GET_ALLOWED, observable), sensor(sensor)
+    {}
+    
+    virtual string get()
+    {
+        char buf[10];
+        snprintf(buf, 10, "%0.2f", sensor->get_max_dist_cm());
+        return string(buf);
+    }
+private:
+    HCSR04 *sensor;
+};
+
+#endif
\ No newline at end of file