]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/platforms/tmirws/sensors/AeroVaneReadP.nc
Enhancements to AeroVaneReadC.
[tinyos-2.x.git] / tos / platforms / tmirws / sensors / AeroVaneReadP.nc
index c620006e693ccc1dbd0f114e44c6879f8c1c60f9..68be7c67c700e80ba84e06eb34554ef4cf5bfd94 100644 (file)
@@ -42,7 +42,7 @@ generic module AeroVaneReadP(uint16_t period) {
     interface Timer<TMilli>;
     interface Read<uint16_t>;
     interface StdControl as AnemometerControl;
-    interface AsyncGet<uint16_t> as Count;
+    interface Get<uint16_t> as Revolutions;
     interface State;
   }
 }
@@ -54,7 +54,8 @@ implementation {
     PHYS_PER_READ = 4,         /* # of physical reads per reading */
   };
 
-  uint16_t m_count;
+  uint8_t m_count;
+  uint16_t m_speed;
   int m_vane;
   int m_sum;
 
@@ -87,6 +88,11 @@ implementation {
 
   event void Timer.fired()
   {
+    if (++m_count == PHYS_PER_READ) {
+      /* Get anemometer revolutions for the period */
+      m_speed = call Revolutions.get();
+    }
+
     if (!(call State.isIdle()))
       call Read.read();
   }
@@ -95,10 +101,10 @@ implementation {
   {
     int d = angle2 - angle1;
 
-    if (d <= -180)
-      d += 360;
-    else if (d > 180)
-      d -= 360;
+    if (d <= -512)
+      d += 1024;
+    else if (d > 512)
+      d -= 1024;
     return d;
   }
 
@@ -110,20 +116,17 @@ implementation {
       return;
 
     newVane = m_vane + distance(m_vane, result);
-    m_sum += result;
+    m_sum += m_vane;
     m_vane = newVane;
-    if (++m_count == PHYS_PER_READ) {
-      norace aerovector_t vector;
-
-      /* Get speed average */
-      atomic vector.speed = (call Count.get() * 2.25) / (period * 4 / 1024) +
-       0.5;
-
-      /* Complete direction average */
-      vector.dir = m_sum / PHYS_PER_READ;
-      while (vector.dir < 0)
-       vector.dir += 360;
-      vector.dir %= 360;
+    if (m_count == PHYS_PER_READ) {
+      aerovector_t vector;
+
+      vector.speed = m_speed;
+      if (m_sum > 0)
+       vector.dir = (m_sum + (PHYS_PER_READ / 2)) / PHYS_PER_READ;
+      else
+       vector.dir = (m_sum - (PHYS_PER_READ / 2)) / PHYS_PER_READ + 1024;
+      vector.dir &= 0x03ff; /* %= 1024 */
       init();
 
       /* Inform the consumer of the vector */