-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9dof.py
More file actions
68 lines (63 loc) · 2.43 KB
/
9dof.py
File metadata and controls
68 lines (63 loc) · 2.43 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
#!/usr/bin/python3
# data capture support for 9-DOF ACCEL/MAG/GYRO+TEMP BREAKOUT BOARD - LSM9DS1 from https://www.adafruit.com/product/3387
# install Adafruit libraries: sudo pip3 install adafruit-circuitpython-lsm9ds1
# this file adapted from Adafruit example at https://github.com/adafruit/Adafruit_CircuitPython_LSM9DS1/blob/master/examples/lsm9ds1_simpletest.py
# Will print the acceleration, magnetometer, and gyroscope values every 0.25 second.
import time
import board
import busio
import adafruit_lsm9ds1
import json
import math
import obd
import time
sessionId = math.floor(time.time())
vehicleId = "subaru"
sourceFile = open('/home/pi/9dof-'+str(sessionId)+'.json', 'a')
# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
#SPI connection:
# from digitalio import DigitalInOut, Direction
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# csag = DigitalInOut(board.D5)
# csag.direction = Direction.OUTPUT
# csag.value = True
# csm = DigitalInOut(board.D6)
# csm.direction = Direction.OUTPUT
# csm.value = True
# sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)
# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
# values every second and print them out.
while True:
ts = math.floor(time.time())
obdDict = {}
obdDict["vehicle"] = vehicleId
obdDict["sessionId"] = str(sessionId)
obdDict["timestamp"] = str(ts)
# Read acceleration, magnetometer, gyroscope, temperature.
accel_x, accel_y, accel_z = sensor.acceleration
mag_x, mag_y, mag_z = sensor.magnetic
gyro_x, gyro_y, gyro_z = sensor.gyro
temp = sensor.temperature
# Print values.
# print('Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(accel_x, accel_y, accel_z))
# print('Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(mag_x, mag_y, mag_z))
# print('Gyroscope (degrees/sec): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(gyro_x, gyro_y, gyro_z))
# print('Temperature: {0:0.3f}C'.format(temp))
# print JSON
obdDict["accel_x"] = accel_x
obdDict["accel_y"] = accel_y
obdDict["accel_z"] = accel_z
obdDict["mag_x"] = mag_x
obdDict["mag_y"] = mag_y
obdDict["mag_z"] = mag_z
obdDict["gyro_x"] = gyro_x
obdDict["gyro_y"] = gyro_y
obdDict["gyro_z"] = gyro_z
obdDict["temp_C"] = temp
obdJson = json.dumps(obdDict)
print(obdJson, file = sourceFile)
sourceFile.flush()
# Delay for a second.
time.sleep(0.25)