forked from SjulsonLab/RPi4_behavior_boxes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_treadmill.py
More file actions
71 lines (55 loc) · 1.95 KB
/
record_treadmill.py
File metadata and controls
71 lines (55 loc) · 1.95 KB
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
import signal
import sys
import datetime as dt
import io
import smbus
import time
import struct
# this function is called when the program receives a SIGINT
def signal_handler(signum, frame):
print("SIGINT detected")
print("Saving the log file for the treadmill activity...")
flush(threadmill_filename, treadmill_log)
print("Existing the system!")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# base_path = sys.argv[1]
base_path = "/mnt/hd/"
threadmill_filename = base_path + "treadmill" + "_output" + str(dt.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) + ".csv"
bus_i2c = smbus.SMBus(1) # "On all recent (since 2014) raspberries the GPIO pin's I2C device is /dev/i2c-1"
# This is the address we setup in the Arduino Program
address_i2c = 0x08
def dacval(bus, address):
time.sleep(0.3)
block = bus.read_i2c_block_data(address, 1)
running_speed = struct.unpack("<f", bytes(block[:4]))[0]
# velocity = struct.unpack("<l", bytes(block[:4]))[0]
# distance = struct.unpack("<l", bytes(block[4:]))[0]
# data = (velocity, distance)
# print(str(data))
# return data
# print(str(block) + '/n')
# print(str(bytes(block)) + '/n')
print("Running speed: " + str(running_speed)) # for debug purpose
return running_speed
# save the element list
def flush(filename, list):
with io.open(filename, 'w') as f:
f.write('time.time(), running_speed\n')
for entry in list:
f.write('%f, %f\n' % entry)
treadmill_log = []
delay = 0.3
while True:
time.sleep(delay)
running_speed = dacval(bus_i2c, address_i2c)
treadmill_log.append(
(time.time(),
running_speed)
)
"""
for each element in the element_list, calculate the consecutive differences
for the consecutive differences, we can yield a velocity of the displacment
And from the velocity of the displacement we can get the direction and the acceleration
The problem that need to take into consideration
"""