Accelerometer sample

From Digi Developer

Jump to: navigation, search

This page contains a simple demonstration of the accelerometer device on python enabled Digi devices. It is meant to be instructive on how to use the functionality.

This sample assumes a basic understanding of accelerometer principles and vector math. For information on accelerometers and how they work, go here: accelerometer primer.

## From the digihw embedded library, import the accelerometer
from digihw import accelerometer
## Import the Queue library to act as a thread safe object between callback and
## the main thread.
from Queue import Queue
## Create a empty Queue object
sample_queue = Queue()
## Get a handle from the accelerometer
accel = accelerometer()
## print a sample.
print accel.sample()
## Define a callback function when the threshold is crossed.  
## Have it append the sample to the queue
def callback(sample, sample_queue):
  print "Callback function was called"
  print "Sample: ", sample
  print "Argument: ", arg
threshold_in_g = 3.0
print "Registering %.2f g threshold for accelerometer" %threshold_in_g
accel.register_threshold(threshold_in_g, callback, sample_queue)
print "Please lightly shake the accelerometer"
sample = sample_queue.get()
## The sum of the Gs should be equal to one, however sometimes the total value of Gs will have a +/-1% error
print "The sample that triggered the accelerometer was:\n", sample

Product Compatibility

Known to work on:

  • Digi ConnectPort X5 variants
  • Digi X-Trak 3

Known to NOT work on:

  • Most Digi gateways lack the required hardware to support this code.
Personal tools
Wiki Editing