]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
Updates to WindVaneReadC
authorsmckown <smckown@4bc1554a-c7f2-4f65-a403-e0be01f0239c>
Mon, 9 Nov 2009 19:30:54 +0000 (19:30 +0000)
committerR. Steve McKown <rsmckown@gmail.com>
Tue, 1 Dec 2009 03:01:50 +0000 (20:01 -0700)
* Use AverageAngleC to average readings
* Adapt to latest changes to MultiSampleC

tos/platforms/tmirws/sensors/WindVaneReadC.nc
tos/platforms/tmirws/sensors/WindVaneReadP.nc

index 3b467349d722b85afad20ea5b8d3091e76b98bc0..732db2640d909bfe931c1d43de9971ff93c7f77f 100644 (file)
@@ -28,7 +28,8 @@
  */
  
 /**
- * Take an instantaneous reading of the wind vane position, in degrees.
+ * Take an instantaneous reading of the wind vane position as a 10 bit unsigned
+ * value.
  * 
  * @author R. Steve McKown <smckown@gmail.com>
  */
@@ -37,12 +38,13 @@ configuration WindVaneReadC {
   provides interface Read<uint16_t>;
 }
 implementation {
-  components WindVaneReadP;
+  enum { SAMPLES_PER_READ = 4, };
+
+  components new WindVaneReadP(SAMPLES_PER_READ);
   Read = WindVaneReadP;
 
-  components new MultiSampleC(uint16_t, 4);
-  WindVaneReadP.Count -> MultiSampleC;
-  WindVaneReadP.SubRead -> MultiSampleC;
+  components new MultiSampleC(SAMPLES_PER_READ);
+  WindVaneReadP.ReadRef -> MultiSampleC;
 
   components WindVaneAdcP;
   MultiSampleC.AdcConfigure -> WindVaneAdcP;
@@ -51,6 +53,9 @@ implementation {
   WindVaneReadP.WPower -> HalWindVaneC.WPower;
   WindVaneReadP.WDead -> HalWindVaneC.WDead;
 
+  components new AverageAngleC(SAMPLES_PER_READ);
+  WindVaneReadP.Average -> AverageAngleC;
+
   components new StateC();
   WindVaneReadP.State -> StateC;
 }
index 3b10041ca4437a862b6108acdb1b22776733fd16..4bbb1e383e81899697ca9d5d36708d8e22dbdc71 100644 (file)
  */
  
 /**
- * Take an instantaneous reading of the wind vane position.
+ * Take an instantaneous reading of the wind vane position as a 10 bit unsigned
+ * value.
  * 
  * @author R. Steve McKown <smckown@gmail.com>
  */
  
-module WindVaneReadP @safe()
+generic module WindVaneReadP(uint16_t count) @safe()
 {
   provides interface Read<uint16_t>;
   uses {
-    interface Get<uint16_t> as Count;
-    interface Read<uint16_t> as SubRead;
+    interface ReadRef<uint16_t>;
     interface GeneralIO as WPower;
     interface GeneralIO as WDead;
+    interface Average<uint16_t>;
     interface State;
   }
 }
@@ -64,12 +65,13 @@ implementation
      * within the dead zone, of course, but would be even if we did implement
      * the extra logic.
      */
-    DEAD_BEGIN = 6000,         /* 4 12-bit ADC readings; about 120 degrees */
-    DEAD_END = 10000,          /* About 220 degrees */
+    DEAD_BEGIN = 1500,         /* 10-bit degree value; about 120 degrees */
+    DEAD_END = 2500,           /* About 220 degrees */
     DEAD_THRESH = 14472,       /* > when checking means in dead zone */
   };
 
-  char m_msg[80];
+  uint16_t m_value;
+  uint16_t m_samples[count];
 
   command error_t Read.read()
   {
@@ -77,7 +79,7 @@ implementation
       return EBUSY;
 
     call WPower.set();
-    if (call SubRead.read() == SUCCESS) {
+    if (call ReadRef.read(m_samples) == SUCCESS) {
       call State.forceState(S_READ);
       return SUCCESS;
     } else {
@@ -91,28 +93,34 @@ implementation
     call WDead.makeInput();
     call WPower.clr();
 
-    /* Value is the sum of multiple 12-bit ADC acquisitions, per the attached
-     * MultiSampleC component.  Convert the value to degrees.  Note, the values
-     * 360 and 0 (zero) represent the same angular position.  We don't do a
-     * modulus operation here because we fully expect the client will do
-     * additional processing that will likely involve a modulus function anyway.
-     */
-    value = (uint16_t)(((uint32_t)(value / call Count.get()) * 360 + 2047) /
-       4096);
-
     call State.toIdle();
     signal Read.readDone(error, value);
   }
 
-  event void SubRead.readDone(error_t error, uint16_t result)
-  {
-    static uint16_t m_value;
+  void averageAngle();
 
-    if (error != SUCCESS) {
+  event void ReadRef.readDone(error_t error, uint16_t* result)
+  {
+    if (error != SUCCESS)
       signalReadDone(error, 0);
-      return;
-    }
+    else
+      averageAngle();
+  }
 
+  void averageAngle()
+  {
+    unsigned i;
+
+    /* Submit a full set of values to be averaged, which will cause
+     * Average.average() to be signalled.
+     */
+    call Average.reset();
+    for (i = 0; i < count; i++)
+      call Average.submit(m_samples[i]);
+  }
+
+  event void Average.average(uint16_t result)
+  {
     switch (call State.getState()) {
       case S_READ:
        if (result < DEAD_BEGIN || result > DEAD_END)
@@ -120,7 +128,7 @@ implementation
        else {
          m_value = result;
          call WDead.makeOutput();
-         if (call SubRead.read() == SUCCESS)
+         if (call ReadRef.read(m_samples) == SUCCESS)
            call State.forceState(S_CHECK);
          else
            signalReadDone(FAIL, 0);