diff --git a/Hardware/Motor.py b/Hardware/Motor.py deleted file mode 100644 index b73efeb3b..000000000 --- a/Hardware/Motor.py +++ /dev/null @@ -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() diff --git a/Hardware/README.txt b/Hardware/README.txt index c147decfc..8ab66469d 100644 --- a/Hardware/README.txt +++ b/Hardware/README.txt @@ -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 - - stop motor by physical button or app call - - turn on the alarm by physical button or app call - - turn off the alarm by physical button or app call + - start motor when motor signal on firebase is "on" + - stop motor when motor signal on firebase is "off" + - start alarm when alarm signal on firebase is "on" + - 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 diff --git a/Hardware/Updated_HW_codes/NewMotorFunc.py b/Hardware/Updated_HW_codes/NewMotorFunc.py deleted file mode 100644 index b5d142294..000000000 --- a/Hardware/Updated_HW_codes/NewMotorFunc.py +++ /dev/null @@ -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 diff --git a/Hardware/Updated_HW_codes/README.md b/Hardware/Updated_HW_codes/README.md deleted file mode 100644 index d91e1e537..000000000 --- a/Hardware/Updated_HW_codes/README.md +++ /dev/null @@ -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...... - diff --git a/Hardware/Updated_HW_codes/__pycache__/NewMotorFunc.cpython-36.pyc b/Hardware/Updated_HW_codes/__pycache__/NewMotorFunc.cpython-36.pyc deleted file mode 100644 index 1fe7c4e90..000000000 Binary files a/Hardware/Updated_HW_codes/__pycache__/NewMotorFunc.cpython-36.pyc and /dev/null differ diff --git a/Hardware/__pycache__/Motor.cpython-36.pyc b/Hardware/__pycache__/Motor.cpython-36.pyc deleted file mode 100644 index 33ca6a5e8..000000000 Binary files a/Hardware/__pycache__/Motor.cpython-36.pyc and /dev/null differ diff --git a/Hardware/final_demo.py b/Hardware/final_demo.py new file mode 100644 index 000000000..9eb78625f --- /dev/null +++ b/Hardware/final_demo.py @@ -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) diff --git a/Hardware/remote_camera.py b/Hardware/remote_camera.py new file mode 100644 index 000000000..4474eebad --- /dev/null +++ b/Hardware/remote_camera.py @@ -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="""\ + +
+