* Anemometer is read immediately after the Timer fires to reduce jitter.
* Count interface is renamed Revolutions to be more meaningful.
* m_count is now uint8_t to reflect a more reasonable real life range.
* Direction data is returned no as degrees but as a 10-bit unsigned value.
* m_sum accumulates the correct, updated angle values.
* Rounding is done properly when m_sum is negative.
components AnemometerReadC;
AeroVaneReadP.AnemometerControl -> AnemometerReadC.StdControl;
components AnemometerReadC;
AeroVaneReadP.AnemometerControl -> AnemometerReadC.StdControl;
- AeroVaneReadP.Count -> AnemometerReadC;
+ AeroVaneReadP.Revolutions -> AnemometerReadC;
components new StateC();
AeroVaneReadP.State -> StateC;
components new StateC();
AeroVaneReadP.State -> StateC;
interface Timer<TMilli>;
interface Read<uint16_t>;
interface StdControl as AnemometerControl;
interface Timer<TMilli>;
interface Read<uint16_t>;
interface StdControl as AnemometerControl;
- interface AsyncGet<uint16_t> as Count;
+ interface Get<uint16_t> as Revolutions;
PHYS_PER_READ = 4, /* # of physical reads per reading */
};
PHYS_PER_READ = 4, /* # of physical reads per reading */
};
+ uint8_t m_count;
+ uint16_t m_speed;
event void Timer.fired()
{
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();
}
if (!(call State.isIdle()))
call Read.read();
}
{
int d = angle2 - angle1;
{
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;
newVane = m_vane + distance(m_vane, result);
return;
newVane = m_vane + distance(m_vane, result);
- 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 */
init();
/* Inform the consumer of the vector */