Hardware code replace in the correct folder and update hardware readme.txt

This commit is contained in:
Leyao Li 2021-04-08 12:10:00 -04:00
parent bfcd8177d6
commit cd4859af83
8 changed files with 317 additions and 243 deletions

View file

@ -1,98 +0,0 @@
import RPi.GPIO as GPIO
from time import sleep
class Motor:
def __init__(self):
print("Starting of the program")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# preset GPIO ports for 2 motors
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
# preset the port for buttons and alarm
GPIO.setup(5, GPIO.IN) # start motor button, initially True
GPIO.setup(13, GPIO.IN) # stop motor button, initially True
GPIO.setup(16, GPIO.IN) # start alarm button, initially True
GPIO.setup(26, GPIO.OUT) # alarm output
for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT)
# utilize PWM function, enable motors and frequency is 100Hz
self.EN1 = GPIO.PWM(self.Motor1['EN'], 100)
self.EN2 = GPIO.PWM(self.Motor2['EN'], 100)
self.EN1.start(0)
self.EN2.start(0)
# stop signals for motors and alarm
self.motorStop = False
self.alarmStop = False
def start_motor(self):
while (not self.motorStop) or (not GPIO.input(5)): # break the loop when motor stop signal is detected
print("FORWARD MOTION")
self.motorStop = self.stop_motor()
self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
GPIO.output(self.Motor1['input1'], GPIO.HIGH)
GPIO.output(self.Motor1['input2'], GPIO.LOW)
GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
GPIO.cleanup()
def stop_motor(self):
userStop = input("Stop the motor? choose between Y/N")
if (userStop == "Y") or (not GPIO.input(13)):
print("stopping motor...")
self.EN1.ChangeDutyCycle(0)
self.EN2.ChangeDutyCycle(0)
print("motor stops")
return True
elif userStop == "N":
return False
else:
self.stop_motor(self)
def start_alarm(self):
while (not self.alarmStop) or (not GPIO.input(16)):
self.alarmStop = self.stop_alarm()
GPIO.output(26, True)
GPIO.cleanup()
def stop_alarm(self):
stopRequest = input("Turn off the alarm? choose between Y/N")
if stopRequest == "Y":
print("Alarm turning off...")
GPIO.output(26, False)
print("Alarm is off")
return True
elif stopRequest == "N":
return False
else:
self.stop_alarm()
if __name__ == "__main__":
# print("Execute function...")
motor1 = Motor()
# motor1.start_motor()
motor1.start_alarm()

View file

@ -1,8 +1,11 @@
Motor class has the following functions: Car class has the following functions:
-------waiting to be verified by LeYao Lee---------- -------waiting to be verified by Leyao Lee----------
- start motor by physical button or app call - start motor when motor signal on firebase is "on"
- stop motor by physical button or app call - stop motor when motor signal on firebase is "off"
- turn on the alarm by physical button or app call - start alarm when alarm signal on firebase is "on"
- turn off the alarm by physical button or app call - stop alarm when alarm signal on firebase is "off"
- start remote camera when camera signal on firebase is "on" and the power signal on firebase is "off"
- stop remote camera when camera on firebase is "off" or the power signal on firebase is "on"
- take a theif picture when item in front of the distance sensor for more than 5 seconds

View file

@ -1,127 +0,0 @@
import RPi.GPIO as GPIO
import pyrebase # u need to install Pyrebase module firstly
from time import sleep
class Motor:
print("Starting of the program")
def __init__(self):
config = {
"apiKey": "AIzaSyAdL0W5HscjEDFPK4BDi6Cnc7FLa30GPYY",
"authDomain": "vehicleantitheftrecognition.firebaseapp.com",
"databaseURL": "https://vehicleantitheftrecognition.firebaseio.com/",
"storageBucket": "vehicleantitheftrecognition.firebaseapp.com"
}
self.firebase = pyrebase.initialize_app(config)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#preset GPIO ports for 2 motors
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
# preset the port for buttons and alarm
GPIO.setup(5,GPIO.IN) # start motor button, initially True
GPIO.setup(13,GPIO.IN) # stop motor button, initially True
GPIO.setup(16,GPIO.IN) # start alarm button, initially True
GPIO.setup(26,GPIO.OUT) # alarm output
for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT)
#utilize PWM function, enable motors and frequency is 100Hz
self.EN1 = GPIO.PWM(self.Motor1['EN'], 100)
self.EN2 = GPIO.PWM(self.Motor2['EN'], 100)
self.EN1.start(0)
self.EN2.start(0)
#stop signals for motors and alarm
self.motorStop=False
self.alarmStop=False
# new update motor and alarm functions, are able to connect embedded system throught firebase
def start_motor(self):
self.motorStop=self.stop_motor()
while (not self.motorStop) or (not GPIO.input(5)): #break the loop when motor stop signal is detected
self.motorStop=self.stop_motor()
def stop_motor(self):
database = self.firebase.database() # get alarm on/off signal from firebase
signals = database.child("signal")
motorSignal = signals.child("motor").get().val()
if (motorSignal=="off") or (not GPIO.input(13)):
print("stopping motor...")
self.EN1.ChangeDutyCycle(0)
self.EN2.ChangeDutyCycle(0)
print("motor stops")
return True
elif motorSignal=="on":
self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
GPIO.output(self.Motor1['input1'], GPIO.HIGH)
GPIO.output(self.Motor1['input2'], GPIO.LOW)
GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
print("motor is turned on")
return False
def start_alarm(self):
self.alarmStop=self.stop_alarm()
while (not self.alarmStop) or (not GPIO.input(16)): # if alarmStop is False or button is pressed
# # enter the loop
self.alarmStop=self.stop_alarm() # infinitely check if alarmStop True
# break the loop if alarm is turned off
def stop_alarm(self):
database = self.firebase.database() # get alarm on/off signal from firebase
signals = database.child("signal")
alarmSignal = signals.child("alarm").get().val()
if alarmSignal=="off":
print("Alarm turning off...")
self.alarmStop=True
GPIO.output(26,False)
print("Alarm is off")
return True
elif alarmSignal=="on":
GPIO.output(26,True)
print("Alarm is turned on")
return False
if __name__=="__main__":
#print("Execute function...")
motor1=Motor()
while True: # turn on the system forever
motor1.start_alarm() # alarm on/off test
motor1.start_motor() # motor on/off test

View file

@ -1,12 +0,0 @@
*******************************************************************************************************************
Date: 11.17 2020 Tuesday, Qian Ma
motor, alarm tests are completed,
functionalities work perfectly without errors
expected objectives achieved: by changing real-time signals on Firebase, the embedded system can be controlled properly
;turn on/off motor/alarm
physical button tests are planned to be completed soon......

214
Hardware/final_demo.py Normal file
View file

@ -0,0 +1,214 @@
import os, signal
import RPi.GPIO as GPIO
import pyrebase # u need to install Pyrebase module firstly
import time
from picamera import PiCamera
from time import sleep
class Car:
print("Starting of the program")
def __init__(self):
config = {
"apiKey": "AIzaSyAdL0W5HscjEDFPK4BDi6Cnc7FLa30GPYY",
"authDomain": "vehicleantitheftrecognition.firebaseapp.com",
"databaseURL": "https://vehicleantitheftrecognition.firebaseio.com",
"projectId": "vehicleantitheftrecognition",
"storageBucket": "vehicleantitheftrecognition.appspot.com",
"messagingSenderId": "163692530359",
"appId": "1:163692530359:web:b6dc7ccfc56a79afb11b32",
"measurementId": "G-EPWP2LK89Q"
}
self.firebase = pyrebase.initialize_app(config)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# preset GPIO ports for 2 motors
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
self.Motor3 = {'EN': 6, 'input1': 19, 'input2': 13}
self.Motor4 = {'EN': 16, 'input1': 20, 'input2': 21}
# preset the port for buttons and alarm
GPIO.setup(26,GPIO.OUT) # alarm output
# preset the port for the distance sensor
self.GPIO_TRIGGER = 18
self.GPIO_ECHO = 4
# set GPIO direction (IN / OUT)
GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(self.GPIO_ECHO, GPIO.IN)
for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT)
GPIO.setup(self.Motor3[x], GPIO.OUT)
GPIO.setup(self.Motor4[x], GPIO.OUT)
# utilize PWM function, enable motors and frequency is 100Hz
self.EN1 = GPIO.PWM(self.Motor1['EN'], 100)
self.EN2 = GPIO.PWM(self.Motor2['EN'], 100)
self.EN3 = GPIO.PWM(self.Motor3['EN'], 100)
self.EN4 = GPIO.PWM(self.Motor4['EN'], 100)
self.EN1.start(0)
self.EN2.start(0)
self.EN3.start(0)
self.EN4.start(0)
# stop signals for motors and alarm
self.motorStop=True
self.alarmStop=True
self.cameraOff=True
# countor for theaf picture been taken
self.counter = self.firebase.database().child("signal").child(1).child("counter").get().val()
#print(str(self.counter))
# new update motor and alarm functions, are able to connect embedded system throught firebase
def start_motor(self):
self.motorStop=False
self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
self.EN3.ChangeDutyCycle(50)
self.EN4.ChangeDutyCycle(50)
GPIO.output(self.Motor1['input1'], GPIO.LOW)
GPIO.output(self.Motor1['input2'], GPIO.HIGH)
GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
GPIO.output(self.Motor3['input1'], GPIO.LOW)
GPIO.output(self.Motor3['input2'], GPIO.HIGH)
GPIO.output(self.Motor4['input1'], GPIO.HIGH)
GPIO.output(self.Motor4['input2'], GPIO.LOW)
print("motor is turned on")
def stop_motor(self):
print("stopping motor...")
self.motorStop=True
self.EN1.ChangeDutyCycle(0)
self.EN2.ChangeDutyCycle(0)
self.EN3.ChangeDutyCycle(0)
self.EN4.ChangeDutyCycle(0)
print("motor stops")
def start_alarm(self):
print("Alarm is turned on")
self.alarmStop=False
GPIO.output(26,True)
return False
def stop_alarm(self):
print("Alarm turning off...")
self.alarmStop=True
GPIO.output(26,False)
print("Alarm is off")
return True
def kill_target(self, target):
cmd_run="ps aux | grep {}".format(target)
out=os.popen(cmd_run).read()
for line in os.popen("ps ax | grep "+target+" | grep -v grep"):
fields = line.split()
#print(fields)
pid = fields[0]
a = os.kill(int(pid),signal.SIGKILL)
print('Killed PID %s, return value:%s' % (pid, a))
def start_camera(self):
self.cameraOff=False
print("Remote camera is turned on")
os.system('python3 remote_camera.py &')
def stop_camera(self):
self.cameraOff=True
self.kill_target("remote_camera.py")
print("Remote camera is off")
#return False
def distance(self):
# set Trigger to HIGH
GPIO.output(self.GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(self.GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(self.GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(self.GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
if __name__=="__main__":
#print("Execute function...")
car=Car()
while True: # turn on the system forever
# get connection to firebase
database = car.firebase.database()
signals = database.child("signal")
auth = car.firebase.auth()
storage = car.firebase.storage()
# get signal from firebase
motorSignal = signals.child(1).child("motor").get().val()
signals = database.child("signal")
alarmSignal = signals.child(1).child("alarm").get().val()
signals = database.child("signal")
cameraSignal = signals.child(1).child("camera").get().val()
signals = database.child("signal")
powerSignal = signals.child(1).child("power").get().val()
# get distance data from distance sensor
dist = car.distance()
#print ("Measured Distance = %.1f cm" % dist)
# Turn on motor if get sensor signal
if ((motorSignal=="on" and dist>15) and car.motorStop):
car.start_motor()
elif ((motorSignal=="off" or dist<=10) and not car.motorStop):
# Stop the motor if the vehicle is too close to the item at front as well
car.stop_motor()
# Turn on alarm if get sensor signal
if (alarmSignal=="on" and car.alarmStop):
car.start_alarm()
elif (alarmSignal=="off" and not car.alarmStop):
car.stop_alarm()
# Turn on remote camera if get sensor signal
if (cameraSignal=="on" and car.cameraOff and powerSignal=="off"):
car.start_camera()
elif ((cameraSignal=="off" and not car.cameraOff) or powerSignal =="on"):
car.stop_camera()
# Take a picture of someone or some thing try to get close to the vehicle
if (dist<=15 and car.cameraOff and car.motorStop and car.alarmStop):
time.sleep(5)
dist = car.distance()
if (dist<=15):
print('Take a theaf picture due to distance at ' + str(int(dist)) + 'cm')
camera = PiCamera()
camera.start_preview()
# Camera warm-up time
time.sleep(1)
camera.capture('/home/pi/Vehicle-Anti-Theft-Face-Recognition-System/sensor/picture'+str(car.counter)+'.jpg')
camera.close()
storage.child('Photos_of_Thieves/Thief_Sensor/picture'+str(car.counter)+'.jpg').put('/home/pi/Vehicle-Anti-Theft-Face-Recognition-System/sensor/picture'+str(car.counter)+'.jpg')
car.counter+=1
car.firebase.database().child("signal").child(1).child("counter").set(car.counter)
time.sleep(1)

94
Hardware/remote_camera.py Normal file
View file

@ -0,0 +1,94 @@
# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server
PAGE="""\
<html>
<head>
<title>Raspberry Pi - Remote Camera</title>
</head>
<body>
<center><h1>Raspberry Pi - Remote Camera</h1></center>
<center><img src="stream.mjpg" width="1080" height="720"></center>
</body>
</html>
"""
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# New frame, copy the existing buffer's content and notify all
# clients it's available
self.buffer.truncate()
with self.condition:
self.frame = self.buffer.getvalue()
self.condition.notify_all()
self.buffer.seek(0)
return self.buffer.write(buf)
class StreamingHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = PAGE.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', len(content))
self.end_headers()
self.wfile.write(content)
elif self.path == '/stream.mjpg':
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
try:
while True:
with output.condition:
output.condition.wait()
frame = output.frame
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
with picamera.PiCamera(resolution='1080x720', framerate=24) as camera:
output = StreamingOutput()
#Uncomment the next line to change your Pi's Camera rotation (in degrees)
camera.rotation = 90
camera.start_recording(output, format='mjpeg')
try:
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
finally:
camera.stop_recording()