From 7621fee2f44164cc7339ad7def7d59112faf192f Mon Sep 17 00:00:00 2001 From: Cem Gokmen Date: Wed, 29 Mar 2017 11:35:03 -0400 Subject: [PATCH] Fixed time recording method and added polling rate for IMU --- main.py | 18 ++++++++++-------- sensormodules/GeigerCounterModule.py | 8 ++++---- sensormodules/IMUModule.py | 13 +++++++------ sensormodules/SensorModule.py | 2 +- sensormodules/SerialModule.py | 2 +- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index d0e5da7..8d34799 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,7 @@ import signal import csv import time - - -# TODO: Improve the time function! -millis = lambda: int(round(time.time() * 1000)) +from datetime import datetime class GracefulKiller: kill_now = False @@ -29,22 +26,27 @@ def exit_gracefully(self, signum, frame): modules['imu'] = IMUModule(logger.getChild("imu")) modules['geiger'] = GeigerCounterModule(logger.getChild("geiger"), "/dev/uart", 9600) + missionTime = datetime.now() + for m in modules.keys(): f = open('%s.csv' % m, 'wb') writer = csv.writer(f, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) + writer.writerow([missionTime]) + csvs[m] = (f, writer) while True: + missionElapsedTime = int((datetime.now() - missionTime).total_seconds() * 1000) + for m in modules.keys(): - data = modules[m].poll() + data = modules[m].poll(missionElapsedTime) if len(data) > 0: - currentTime = millis() writer = csvs[m][0] for datum in data: - writer.writerow([currentTime] + list(datum)) + writer.writerow([missionElapsedTime] + list(datum)) if killer.kill_now: break @@ -52,4 +54,4 @@ def exit_gracefully(self, signum, frame): for c in csvs.values(): c[1].close() - print("Goodbye!") + print("The eagle has landed.") diff --git a/sensormodules/GeigerCounterModule.py b/sensormodules/GeigerCounterModule.py index be99db9..96c985e 100644 --- a/sensormodules/GeigerCounterModule.py +++ b/sensormodules/GeigerCounterModule.py @@ -7,15 +7,15 @@ def __init__(self, logger, device, baudRate): self.sm = SerialModule(logger, device, baudRate) self.logger = logger - def poll(self): + def poll(self, dt): # The geiger counter's number of ticks is equal to the number of # characters on the serial output. - data = self.sm.poll() + data = self.sm.poll(dt) retval = [] - for _ in xrange(data): - retval.append(True) + for c in data: + retval.append(c) return retval \ No newline at end of file diff --git a/sensormodules/IMUModule.py b/sensormodules/IMUModule.py index 659e9e1..a30a72b 100644 --- a/sensormodules/IMUModule.py +++ b/sensormodules/IMUModule.py @@ -9,6 +9,7 @@ class IMUModule(SensorModule): SETTINGS_FILE = "RTIMULib" + MILLISECOND_POLLING_INTERVAL = 500 def __init__(self, logger): sys.path.append('.') @@ -31,15 +32,15 @@ def __init__(self, logger): imu.setCompassEnable(True) self.imu = imu - self.poll_interval = imu.IMUGetPollInterval() + self.pollInterval = imu.IMUGetPollInterval() self.logger = logger - def poll(self): + def poll(self, dt): if self.imu.IMURead(): - # x, y, z = imu.getFusionData() - # print("%f %f %f" % (x,y,z)) data = self.imu.getIMUData() fusionPose = data["fusionPose"] - processedData = [(math.degrees(fusionPose[0]), math.degrees(fusionPose[1]), math.degrees(fusionPose[2]))] + self.data = [(math.degrees(fusionPose[0]), math.degrees(fusionPose[1]), math.degrees(fusionPose[2]))] - return processedData \ No newline at end of file + if (self.data is not None) and (self.lastPoll is None or ((dt - self.lastPoll).total_seconds() * 1000 >= self.pollInterval)): + self.lastPoll = dt + return self.data \ No newline at end of file diff --git a/sensormodules/SensorModule.py b/sensormodules/SensorModule.py index ec3225e..79dccfc 100644 --- a/sensormodules/SensorModule.py +++ b/sensormodules/SensorModule.py @@ -4,7 +4,7 @@ class SensorModule: __metaclass__ = abc.ABCMeta @abc.abstractmethod - def poll(self): + def poll(self, dt): """ This is our module's polling method which will be called from the main event loop. This method should not block under any circumstances. diff --git a/sensormodules/SerialModule.py b/sensormodules/SerialModule.py index 33673ba..97ed4f6 100644 --- a/sensormodules/SerialModule.py +++ b/sensormodules/SerialModule.py @@ -7,7 +7,7 @@ def __init__(self, logger, device, baudRate): self.logger = logger - def poll(self): + def poll(self, dt): # It's important to realize that this method may produce incomplete data if called # in the middle of a line and it's up to the main loop to handle that. if self.sd.in_waiting > 0: