I'm trying to write code for a raspberry pi. The code is supposed to set up a connection to obd on my car. What the code is supposed to do is wait for me to press a GPIO pin and the recording will stop. This pin (27) acts as a toggle, so I can start / stop by pressing it.
I wrote the code originally outside a class just to get it to work, but I rewrote it from scratch to adapt it into a class, but the GPIO events are not behaving.
Here is the code:
import RPi.GPIO as GPIO
import time
import obd
import numpy as np
class obdScan:
rec_loop = 0
key_listen = 1
rec_freq = 2 # per second
my_data = []
def __init__(self):
try:
self.conn = obd.OBD()
except:
print("connection failed")
def stop_loop(self):
self.rec_loop = (self.rec_loop +1)%2
def closeall(self):
self.rec_loop, self.key_listen = 0,0
def measurement(self,val):
device = obd.commands[1][val]
value = self.conn.query(device).value.magnitude
return value
def record(self,devices):
value=[]
for i in range(len(devices)):
value.append(self.measurement(devices[i]))
self.my_data.append(value)
time.sleep(1/self.rec_freq)
print(["recording:", self.my_data[-1]])
if __name__ == "__main__":
dat_file = "data.csv"
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
my_data = np.genfromtxt(dat_file, delimiter=',')
except:
my_data = [[], []]
obd_connection = obdScan()
GPIO.add_event_detect(27, GPIO.FALLING, callback=obd_connection.stop_loop)
GPIO.add_event_detect(22, GPIO.FALLING, callback=obd_connection.closeall)
print("ready")
while obd_connection.key_listen:
while obd_connection.rec_loop:
obd_connection.record([13,7])
else:
print("done")
try:
data = np.append(my_data,obd_connection.my_data,axis=0)
except:
data = obd_connection.my_data
np.savetxt(dat_file, data, delimiter=",")
The Error I'm getting:
"TypeError: stop_loop() takes 1 positional argument but 2 were given"