-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pimyride_Logger.py
180 lines (150 loc) · 6.4 KB
/
Pimyride_Logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
###########################################################################
# PiMyRide_Logger.py
#
# For use without LCD screen can be used on any linux distro
#
# Copyright 2013 Alan Kehoe, David O'Regan (www.pimyride.com)
#
# This file is part of PiMyRide.
#
# PiMyRide is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# PiMyRide ips distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PiMyRide; if not, visit http://www.gnu.org/licenses/gpl.html
###########################################################################
from datetime import datetime
# from time import sleep
import sys
import os
import RPi.GPIO as GPIO
from obd_utils import scanSerial
import obd_io
import obd_sensors
from gopro_rasp import gopro
def setup_GPIO():
global STATUS_PIN
STATUS_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(STATUS_PIN, GPIO.OUT)
def status_led(mode, state):
STATUS_PIN = 18
if mode == "Normal":
GPIO.setup(STATUS_PIN, GPIO.OUT)
if state:
GPIO.output(STATUS_PIN, True)
elif not state:
GPIO.output(STATUS_PIN, False)
elif mode == "Loading":
p = GPIO.PWM(STATUS_PIN, 2)
if state:
p.start(1)
elif not state:
p.stop()
elif mode == "Error":
p = GPIO.PWM(STATUS_PIN, 4)
if state:
p.start(1)
elif not state:
p.stop()
def EXIT():
status_led("Normal", False)
status_led("Blink", False)
GPIO.cleanup()
sys.exit()
# os.shutdown()
class PiMyRide_Logger():
def __init__(self, path, log_sensors):
status_led("Loading", True)
self.port = None
self.sensor_list = []
destination_file = path + (datetime.now().strftime('%d%b-%H:%M:%S')) + ".csv"
self.log_csv = open(destination_file, "w", 128)
self.log_csv.write(
"Time,RPM,MPH,Throttle-Position,Calculated-Load,"
"Coolant-Temp,Air-Temp,Intake-Manifold-Pressure,MAF,Timing Advance,Engine Time\n")
for sensor in log_sensors:
self.add_log_sensor(sensor)
status_led("Loading", False)
def connect(self):
status_led("Loading", True)
port_names = scanSerial() # Check all serial ports.
print(port_names) # print available ports
for port in port_names:
self.port = obd_io.OBDPort(port, None, 2, 2)
if self.port.State == 0:
self.port.close()
self.port = None # no open ports close
else:
break # break with connection
if self.port:
print("Connected ")
status_led("Loading", False)
def is_connected(self): # check if connected
return self.port
# add the sensors to read from from the list below. this sensors are in obd_sensors.py
def add_log_sensor(self, sensor):
for index, e in enumerate(obd_sensors.SENSORS):
if sensor == e.shortname:
self.sensor_list.append(index)
print("Logging Sensor: " + e.name) # logging this sensor
break
def get_mpg(self, MPH, MAF):
# Instant_MPG = (14.7 * 8.637571 * 4.54 * MPH) / (3600 * (MAF * 7.5599) / 100) # Diesel Inaccurate formula
Instant_MPG = (14.7 * 7.273744 * 4.54 * MPH) / (3600 * MAF / 100) # Petrol Should accurate
return Instant_MPG
def Start_Logging(self): # logging starts
if self.port is None:
return None # leave if there is no connection
print("Logging started")
while 1:
status_led("Normal", True)
log_time = datetime.now().strftime('%d%b-%H:%M:%S.%f') # today's date and time
log_data = log_time # start of the logging string
result_set = {}
for index in self.sensor_list: # log all of our sensors data from sensor_list
(name, value, unit) = self.port.sensor(index)
# print(self.port.sensor(index)) # print the data provides feedback to user
log_data = log_data + "," + str(value) # add to log string
result_set[
obd_sensors.SENSORS[index].shortname] = value # add data to a result set for more manipulation
# we don't want to log "NODATA" if the car drops the OBDII connection rather exit the program
if (result_set["rpm"] == "NODATA") or (result_set["speed"] == "NODATA") or (
result_set["throttle_pos"] == "NODATA") or (result_set["load"] == "NODATA") or (
result_set["temp"] == "NODATA") or (result_set["intake_air_temp"] == "NODATA") or (
result_set["manifold_pressure"] == "NODATA") or (result_set["maf"] == "NODATA"):
# print("Connection Error Disconnecting")
# sleep(1) # show the message
EXIT()
Instant_MPG = self.get_mpg(result_set["speed"], result_set["maf"]) # calculate mpg
log_data = log_data + "," + str(Instant_MPG) # add mpg to result string
self.log_csv.write(log_data + "\n") # write to csv
print '\n'
def ensure_dir(f):
# Make a new directory for each day will make managing the logs easier
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
setup_GPIO()
if gopro.is_connected():
gopro.turn_on()
else:
print("GoPro is not connected")
dir_date = datetime.now()
path = datetime.now().strftime('%d-%b-%Y')
ensure_dir("/home/pi/logs/" + path + "/") # ensure the dir is available
log_sensors = ["rpm", "speed", "throttle_pos", "load", "temp", "intake_air_temp", "manifold_pressure", "maf", "timing_advance", "engine time"]
logger = PiMyRide_Logger("/home/pi/logs/" + path + "/", log_sensors)
logger.connect()
if not logger.is_connected():
print "Not connected"
logger.Start_Logging()
EXIT()