I tried to find the accelerometer update frequency on my N95 using the following code:
Code:import appuifw, sys, e32 import time, math import axyz # accelerometer class Accelerometer: p_text_colour = (200, 20, 20) p_buffer_time = 3.0 # [s] p_acc_frequency = 10.0 # [Hz] def __init__(self): self.acc_x = int(self.p_buffer_time * self.p_acc_frequency) * [0.0] self.acc_y = int(self.p_buffer_time * self.p_acc_frequency) * [0.0] self.acc_z = int(self.p_buffer_time * self.p_acc_frequency) * [0.0] self.acc_time = int(self.p_buffer_time * self.p_acc_frequency) * [0.0] self.acc_time2 = int(self.p_buffer_time * self.p_acc_frequency) * [0.0] self.exitflag = 0 self.canvas = appuifw.Canvas(event_callback=self.event) appuifw.app.body = self.canvas def frame_rate(self, timestamps): try: frequency = len(timestamps) / (timestamps[-1] - timestamps[0]) except: frequency = -1 return frequency def update(self): appuifw.app.body.clear() appuifw.app.body.text((10, 10), u"ACC: %3.1f Hz (%4.1f, %4.1f, %4.1f)" % (self.frame_rate(self.acc_time), self.acc_x[-1], self.acc_y[-1], self.acc_z[-1]), self.p_text_colour) appuifw.app.body.text((10, 20), u"ACC: %3.1f Hz" % self.frame_rate(self.acc_time2), self.p_text_colour) def event(self, ev): pass def set_exit(self): self.exitflag = True def start_data(self): axyz.connect(self.accelerometer_data) def stop_data(self): axyz.disconnect() def run(self): appuifw.app.exit_key_handler = self.set_exit self.start_data() while not self.exitflag: self.update() e32.ao_sleep(0.00000001) self.stop_data() def accelerometer_data(self, x, y, z): timestamp = time.time() + math.modf(time.clock())[0] self.acc_time2 = self.acc_time2[1:] self.acc_time2.append(timestamp) if (timestamp - self.acc_time[-1]) >= 1 / self.p_acc_frequency: self.acc_time = self.acc_time[1:] self.acc_time.append(timestamp) self.acc_x = self.acc_x[1:] self.acc_x.append(x) self.acc_y = self.acc_y[1:] self.acc_y.append(y) self.acc_z = self.acc_y[1:] self.acc_z.append(y) app = Accelerometer() app.run()
I would also like to be able to set the update frequency using p_acc_frequency.
Some questions:
1) Why are the frequency measurements so unstable and why does (timestamp - self.acc_time[-1]) sometimes return negative values?
2) How can I improve the frequency measurements, so that I can get the accelerometer data in the buffer at a specified rate, e.g., 10 Hz?
3) Why is the e32.ao_sleep command necessary?
4) Any other comments/improvements on my very first accelerometer code are welcome.![]()

Reply With Quote


