Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5 from VIP-LES/develop
Browse files Browse the repository at this point in the history
Completed file and script cutting implementations, etc.
  • Loading branch information
Skyman authored Mar 30, 2017
2 parents 01b497e + 96ca82d commit 925a08f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 35 deletions.
8 changes: 1 addition & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
FROM python:2.7
FROM armhf/python:2.7
MAINTAINER Cem Gokmen <[email protected]>

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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ 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-1 \
-v /home/pi/vernedata:/data \
--restart=unless-stopped \
--restart=on-failure \
gtviples/verne
```

Expand Down
2 changes: 1 addition & 1 deletion install_git.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
set -ex #Fail if any line fails, print everything

apt-get update
Expand Down
2 changes: 1 addition & 1 deletion install_rtimulib.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
set -ex #Fail if any line fails, print everything

apt-get update
Expand Down
88 changes: 74 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,43 +21,97 @@ 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)

printableMissionTime = missionTime - datetime.fromtimestamp(0)
writer.writerow([int(printableMissionTime.total_seconds() * 1000)])

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)
modules['geiger'] = GeigerCounterModule(logger.getChild("geiger"), "/dev/ttyAMA0", 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)

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))

if killer.kill_now:
break

for c in csvs.values():
c[1].close()
closeCSVFiles(csvs)

print("The eagle has landed.")
logger.info("The eagle has landed: stopping recording. Goodbye!")
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pyserial==3.3
pyserial==3.3
PyYAML==3.12
5 changes: 5 additions & 0 deletions sampleConfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Create new CSV file after how many hours:
cutFileInterval: 1

# Stop the main script after how many hours:
killScriptInterval: 6
10 changes: 5 additions & 5 deletions sensormodules/GeigerCounterModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return retval
9 changes: 6 additions & 3 deletions sensormodules/IMUModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ 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():
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
return [self.data]

0 comments on commit 925a08f

Please sign in to comment.