-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrash_Detect.py
More file actions
57 lines (48 loc) · 1.69 KB
/
Copy pathCrash_Detect.py
File metadata and controls
57 lines (48 loc) · 1.69 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
#Detect Crash
import paho.mqtt.publish as publish
#Notice: Wiring CLK = 11 MISO = 9 MOSI = 10 CS = 8
import time
import board
import busio
import adafruit_mpu6050
import RPi.GPIO as GPIO
import Adafruit_MCP3008
import numpy as np
#Use MPU6050 to detect crash
#A crash is defined as a sudden change in acceleration
#The MPU6050 is a 6-axis accelerometer and gyroscope
#The accelerometer measures acceleration in 3 axes
#The gyroscope measures angular velocity in 3 axes
#Read the accelerometer data, Write it as a function
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_mpu6050.MPU6050(i2c)
def read_accel():
#Read the accelerometer data, translate it to G value
#Return the acceleration in 3 axes
#The accelerometer measures acceleration in m/s^2
#The accelerometer measures acceleration in 3 axes
#The accelerometer measures acceleration in 3 axes
accel_x, accel_y, accel_z = sensor.acceleration
accel_x = accel_x / 9.8
accel_y = accel_y / 9.8
accel_z = accel_z / 9.8
return accel_x, accel_y, accel_z
def read_sound():
CLK = 11
MISO = 9
MOSI = 10
CS = 8
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
analog_value = mcp.read_adc(0)
return analog_value
def detect_crash(accel_thres, sound_thres):
accel_x,accel_y,accel_z = read_accel()
sound = read_sound()
abs_accel = np.sqrt(accel_x**2 + accel_y**2 + accel_z**2)
if abs_accel > accel_thres and sound > sound_thres:
crash_flag = 1
else:
crash_flag = 0
# Publish the crash flag value to the MQTT broker
##publish.single("crash_detect/crash_flag", crash_flag, hostname="test.mosquitto.org")
return crash_flag, abs_accel, sound