From 05524ca6ed45d4591c69e1adcd59194b471f91df Mon Sep 17 00:00:00 2001 From: VIP Raspberry Pi Date: Wed, 29 Mar 2017 14:37:06 -0400 Subject: [PATCH 1/3] Corrected device settings --- Dockerfile | 8 +------- README.md | 5 +++-- install_git.sh | 2 +- install_rtimulib.sh | 2 +- main.py | 2 +- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index cb78009..d38876f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,14 @@ -FROM python:2.7 +FROM armhf/python:2.7 MAINTAINER Cem Gokmen COPY . /verne WORKDIR /verne -RUN chmod +x install_git.sh -RUN ./install_git.sh - RUN chmod +x install_rtimulib.sh RUN ./install_rtimulib.sh RUN pip install -r requirements.txt -VOLUME /dev/uart -VOLUME /dev/i2c-0 -VOLUME /dev/i2c-1 VOLUME /data CMD python main.py diff --git a/README.md b/README.md index dd05bd5..6281d8c 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,9 @@ To run the project, a container must be created with this image and the correct ``` docker create \ --name=verne - -v /dev/ttyAMA0:/dev/uart \ - -v /dev/i2c-1:/dev/i2c-1 \ + --device /dev/ttyAMA0 \ + --device /dev/i2c-0 \ + --device /dev/i2c-1 \ -v /home/pi/vernedata:/data \ --restart=unless-stopped \ gtviples/verne diff --git a/install_git.sh b/install_git.sh index b8053db..5898918 100755 --- a/install_git.sh +++ b/install_git.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh set -ex #Fail if any line fails, print everything apt-get update diff --git a/install_rtimulib.sh b/install_rtimulib.sh index d247bae..2395914 100755 --- a/install_rtimulib.sh +++ b/install_rtimulib.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh set -ex #Fail if any line fails, print everything apt-get update diff --git a/main.py b/main.py index 0a6f119..20116cb 100644 --- a/main.py +++ b/main.py @@ -24,7 +24,7 @@ def exit_gracefully(self, signum, frame): logger = logging.getLogger("verne") modules['imu'] = IMUModule(logger.getChild("imu")) - modules['geiger'] = GeigerCounterModule(logger.getChild("geiger"), "/dev/uart", 9600) + modules['geiger'] = GeigerCounterModule(logger.getChild("geiger"), "/dev/ttyAMA0", 9600) missionTime = datetime.now() From 51d9dc7b43f0afa52d2ab29aa7e8e77cf57b2e28 Mon Sep 17 00:00:00 2001 From: VIP Raspberry Pi Date: Wed, 29 Mar 2017 15:02:51 -0400 Subject: [PATCH 2/3] Minor fixes --- README.md | 1 - main.py | 9 +++++---- sensormodules/GeigerCounterModule.py | 10 +++++----- sensormodules/IMUModule.py | 6 ++++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6281d8c..59ee77f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,6 @@ To run the project, a container must be created with this image and the correct docker create \ --name=verne --device /dev/ttyAMA0 \ - --device /dev/i2c-0 \ --device /dev/i2c-1 \ -v /home/pi/vernedata:/data \ --restart=unless-stopped \ diff --git a/main.py b/main.py index 20116cb..fe790da 100644 --- a/main.py +++ b/main.py @@ -32,7 +32,8 @@ def exit_gracefully(self, signum, frame): f = open('/data/%s.csv' % m, 'wb') writer = csv.writer(f, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) - writer.writerow([missionTime]) + printableMissionTime = missionTime - datetime.fromtimestamp(0) + writer.writerow([int(printableMissionTime.total_seconds()*1000)]) csvs[m] = (f, writer) @@ -42,8 +43,8 @@ def exit_gracefully(self, signum, frame): for m in modules.keys(): data = modules[m].poll(missionElapsedTime) - if len(data) > 0: - writer = csvs[m][0] + if data is not None and len(data) > 0: + writer = csvs[m][1] for datum in data: writer.writerow([missionElapsedTime] + list(datum)) @@ -52,6 +53,6 @@ def exit_gracefully(self, signum, frame): break for c in csvs.values(): - c[1].close() + c[0].close() print("The eagle has landed.") diff --git a/sensormodules/GeigerCounterModule.py b/sensormodules/GeigerCounterModule.py index 96c985e..4226e00 100644 --- a/sensormodules/GeigerCounterModule.py +++ b/sensormodules/GeigerCounterModule.py @@ -13,9 +13,9 @@ def poll(self, dt): data = self.sm.poll(dt) - retval = [] + if data is not None: + retval = [] + for c in data: + retval.append(c) - for c in data: - retval.append(c) - - return retval \ No newline at end of file + return retval diff --git a/sensormodules/IMUModule.py b/sensormodules/IMUModule.py index a30a72b..0ffee43 100644 --- a/sensormodules/IMUModule.py +++ b/sensormodules/IMUModule.py @@ -32,8 +32,10 @@ def __init__(self, logger): imu.setCompassEnable(True) self.imu = imu - self.pollInterval = imu.IMUGetPollInterval() + self.pollInterval = IMUModule.MILLISECOND_POLLING_INTERVAL self.logger = logger + self.data = None + self.lastPoll = None def poll(self, dt): if self.imu.IMURead(): @@ -43,4 +45,4 @@ def poll(self, dt): 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 + return self.data From 50f1817d60a3d76f42b18f4dbad0f0338e2758d5 Mon Sep 17 00:00:00 2001 From: Cem Gokmen Date: Thu, 30 Mar 2017 18:29:56 -0400 Subject: [PATCH 3/3] Added file and script cutoff time settings. --- main.py | 81 ++++++++++++++++++++++++++++++++------ sampleConfig.yml | 5 +++ sensormodules/IMUModule.py | 5 ++- 3 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 sampleConfig.yml diff --git a/main.py b/main.py index 0a6f119..528009e 100644 --- a/main.py +++ b/main.py @@ -2,8 +2,14 @@ import logging import signal import csv +import yaml +import os import time -from datetime import datetime +from datetime import datetime, timedelta +from shutil import copyfile + +fileDir = os.path.dirname(os.path.realpath(__file__)) +CONFIG_PATH = "/data/config.yml" class GracefulKiller: kill_now = False @@ -15,30 +21,84 @@ def __init__(self): def exit_gracefully(self, signum, frame): self.kill_now = True +def getCSVFilesFromModules(modules, missionTime, i): + csvs = {} + for m in modules.keys(): + f = open('/data/%s-%d.csv' % (m, i), 'wb') + writer = csv.writer(f, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) + + writer.writerow([missionTime]) + + csvs[m] = (f, writer) + + return csvs + +def closeCSVFiles(csvs): + for c in csvs.values(): + c[1].close() if __name__ == '__main__': killer = GracefulKiller() - modules = {} - csvs = {} + if not os.path.isfile(CONFIG_PATH): + copyfile(os.path.join(fileDir, "sampleConfig.yml"), CONFIG_PATH) + + # Start by reading the config + cutFileAfterHours = None + killScriptAfterHours = None + with open(CONFIG_PATH, 'r') as ymlfile: + cfg = yaml.load(ymlfile) + + cutFileAfterHours = float(cfg["cutFileInterval"]) + killScriptAfterHours = float(cfg["killScriptInterval"]) + + if cutFileAfterHours is None or killScriptAfterHours is None: + raise ValueError("Could not read config file. Delete file if you want it reset.") + + # Initialize the logger logger = logging.getLogger("verne") + # Load the modules + modules = {} modules['imu'] = IMUModule(logger.getChild("imu")) modules['geiger'] = GeigerCounterModule(logger.getChild("geiger"), "/dev/uart", 9600) missionTime = datetime.now() + timeToKill = missionTime + timedelta(hours=killScriptAfterHours) + timeToRenewFile = missionTime + timedelta(hours=cutFileAfterHours) - for m in modules.keys(): - f = open('/data/%s.csv' % m, 'wb') - writer = csv.writer(f, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) + currentFile = 0 - writer.writerow([missionTime]) + # If files exist, we want to find an int at which file-i does not exist, and increment it + # once more to make it clear that this is from a new recording. + while True: + filesExist = [os.path.isfile("/data/%s-%d.csv" % (m, currentFile)) for m in modules.keys()].count(True) - csvs[m] = (f, writer) + if filesExist > 0: + currentFile += 1 + else: + break + + currentFile += 1 + + csvs = getCSVFilesFromModules(modules, missionTime, currentFile) + currentFile += 1 while True: missionElapsedTime = int((datetime.now() - missionTime).total_seconds() * 1000) + if missionElapsedTime > timeToKill: + # It's time to end the recording! Goodbye! + break + + if missionElapsedTime > timeToRenewFile: + closeCSVFiles(csvs) + + csvs = getCSVFilesFromModules(modules, missionTime, currentFile) + currentFile += 1 + + timeToRenewFile = missionElapsedTime + timedelta(hours=cutFileAfterHours) + for m in modules.keys(): data = modules[m].poll(missionElapsedTime) @@ -51,7 +111,4 @@ def exit_gracefully(self, signum, frame): if killer.kill_now: break - for c in csvs.values(): - c[1].close() - - print("The eagle has landed.") + logger.info("The eagle has landed: stopping recording. Goodbye!") diff --git a/sampleConfig.yml b/sampleConfig.yml new file mode 100644 index 0000000..ab49865 --- /dev/null +++ b/sampleConfig.yml @@ -0,0 +1,5 @@ +# Create new CSV file after how many hours: +cutFileInterval: 1 + +# Stop the main script after how many hours: +killScriptInterval: 6 \ No newline at end of file diff --git a/sensormodules/IMUModule.py b/sensormodules/IMUModule.py index a30a72b..c9ddc43 100644 --- a/sensormodules/IMUModule.py +++ b/sensormodules/IMUModule.py @@ -39,8 +39,9 @@ def poll(self, dt): if self.imu.IMURead(): data = self.imu.getIMUData() fusionPose = data["fusionPose"] - self.data = [(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]))] + self.data = tuple([math.degrees(v) for v in fusionPose]) 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 + return [self.data] \ No newline at end of file