Vehicle-Anti-Theft-Face-Rec.../Hardware/Updated_HW_codes/NewMotorFunc.py

128 lines
4.1 KiB
Python
Raw Normal View History

2020-11-19 16:07:08 +00:00
mport RPi.GPIO as GPIO
import pyrebase # u need to install Pyrebase module firstly
2020-11-18 02:56:47 +00:00
from time import sleep
class Motor:
2020-11-19 16:07:08 +00:00
2020-11-18 02:56:47 +00:00
print("Starting of the program")
2020-11-19 16:07:08 +00:00
2020-11-18 02:56:47 +00:00
def __init__(self):
2020-11-19 16:07:08 +00:00
config = {
"apiKey": "AIzaSyAdL0W5HscjEDFPK4BDi6Cnc7FLa30GPYY",
"authDomain": "vehicleantitheftrecognition.firebaseapp.com",
"databaseURL": "https://vehicleantitheftrecognition.firebaseio.com/",
"storageBucket": "vehicleantitheftrecognition.firebaseapp.com"
2020-11-18 02:56:47 +00:00
}
self.firebase = pyrebase.initialize_app(config)
2020-11-19 16:07:08 +00:00
2020-11-18 02:56:47 +00:00
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
2020-11-19 16:07:08 +00:00
#preset GPIO ports for 2 motors
2020-11-18 02:56:47 +00:00
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
2020-11-19 16:07:08 +00:00
2020-11-18 02:56:47 +00:00
# preset the port for buttons and alarm
2020-11-19 16:07:08 +00:00
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
2020-11-18 02:56:47 +00:00
for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT)
2020-11-19 16:07:08 +00:00
#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)
2020-11-18 02:56:47 +00:00
2020-11-19 16:07:08 +00:00
self.EN1.start(0)
2020-11-18 02:56:47 +00:00
self.EN2.start(0)
2020-11-19 16:07:08 +00:00
#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
2020-11-18 02:56:47 +00:00
def start_motor(self):
2020-11-19 16:07:08 +00:00
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()
2020-11-18 02:56:47 +00:00
def stop_motor(self):
2020-11-19 16:07:08 +00:00
database = self.firebase.database() # get alarm on/off signal from firebase
2020-11-18 02:56:47 +00:00
signals = database.child("signal")
2020-11-19 16:52:08 +00:00
motorSignal = signals.child("motor").get().val()
2020-11-19 16:07:08 +00:00
if (motorSignal=="off") or (not GPIO.input(13)):
2020-11-18 02:56:47 +00:00
print("stopping motor...")
self.EN1.ChangeDutyCycle(0)
self.EN2.ChangeDutyCycle(0)
print("motor stops")
return True
2020-11-19 16:07:08 +00:00
elif motorSignal=="on":
2020-11-18 02:56:47 +00:00
self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
GPIO.output(self.Motor1['input1'], GPIO.HIGH)
GPIO.output(self.Motor1['input2'], GPIO.LOW)
2020-11-19 16:07:08 +00:00
2020-11-18 02:56:47 +00:00
GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
2020-11-19 16:07:08 +00:00
2020-11-18 02:56:47 +00:00
print("motor is turned on")
return False
2020-11-19 16:07:08 +00:00
2020-11-18 02:56:47 +00:00
def start_alarm(self):
2020-11-19 16:07:08 +00:00
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
2020-11-18 02:56:47 +00:00
def stop_alarm(self):
2020-11-19 16:07:08 +00:00
database = self.firebase.database() # get alarm on/off signal from firebase
2020-11-18 02:56:47 +00:00
signals = database.child("signal")
2020-11-19 16:07:08 +00:00
alarmSignal = signals.child("alarm").get().val()
2020-11-18 02:56:47 +00:00
2020-11-19 16:07:08 +00:00
if alarmSignal=="off":
2020-11-18 02:56:47 +00:00
print("Alarm turning off...")
2020-11-19 16:07:08 +00:00
self.alarmStop=True
GPIO.output(26,False)
2020-11-18 02:56:47 +00:00
print("Alarm is off")
return True
2020-11-19 16:07:08 +00:00
elif alarmSignal=="on":
GPIO.output(26,True)
2020-11-18 02:56:47 +00:00
print("Alarm is turned on")
return False
2020-11-19 16:07:08 +00:00
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