FireAccelerometer

From EQUIS Lab Wiki

Jump to: navigation, search

Contents

Introduction

FireAccelerometer is a simple program designed to use the Accelerometer (and Gyrometer) of a Windows Surface to determine if the device is moving. This program has applications in Exercise Game Development involving a Stationary Bike. The reason for this is that cadence sensors have a very high latency, while FireAccelerometer does not. When the Surface is mounted to the stationary bike, the movement of the bike caused by pedaling is enough to trigger the Accelerometer of the Surface.

Getting Started

To run FireAccelerometer, simply run FireAccelerometer.exe.

Understanding the Program

FireAccelerometer is a very simple program, containing only one C# file: Program.cs. Program.cs has only 5 methods:

  • void Main() - The bulk of the program exists in the Main() method, which calls upon utility methods. Main does the following:
    • Creates a queue named history, which will be used to smooth the movement values for accuracy.
    • Begins the main loop. In each iteration of the main loop, the program does the following:
      • Takes x, y, and z values from both the accelerometer and gyrometer, and stores them in a double[] of Length 6 called currentReading.
      • Compares currentReading to lastReading (the reading from the most recent iteration) using CompareReadings(currentReading, LastReading), storing the returned value in a bool called moving.
      • Enqueues moving into the history queue.
      • Passes history through a smoothing algorithm. The program has two methods for smoothing but only uses one: SmoothQueueByConsensus(). The returned value is stored in a bool called state, which is outputted to the console as either a 0 or 1.
  • double[] GetCurrentReading(Accelerometer acc, Gyrometer gyro) - Queries the acc and gyro sensors for their x, y, and z values, stores them in a double[], and returns it.
  • bool CompareReadings(double[] r1, double[] r2) - In essence, returns (r1 != r2); note however that this method can also support comparison given a certain threshold, i.e. if the values are off by more than a given threshold.
  • bool SmoothQueueByConsensus(Queue<bool> queue) - Compares the values in queue.
    • If all of the values in queue are equal, returns the value. Otherwise, returns the value of state from the last iteration of the Main() loop.
  • bool SmoothQueueByAverage(Queue<bool> queue) - DEPRECATED. Compares the values in queue.
    • Returns the value of the majority of the queue. i.e. If the number of true values is greater than or equal to (queue.Length / 2), returns true. Else returns false.

Understanding The Output

When FireAccelerometer is run, it will output a stream of 1's and 0's. These values correspond to the boolean truth values:

  • 0 -- The device is not moving enough to trigger a change in the accelerometer or gyrometer.
  • 1 -- The device is moving enough to trigger a change in the accelerometer or gyrometer.
Personal tools