diff --git a/final_demo.py b/final_demo.py index a89005700..ef8235b49 100644 --- a/final_demo.py +++ b/final_demo.py @@ -19,102 +19,95 @@ class Motor: 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 + self.motorStop=True + self.alarmStop=True + self.cameraOff=True - - # 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): #break the loop when motor stop signal is detected - - self.motorStop=self.stop_motor() + self.motorStop=False + 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") - def stop_motor(self): - - database = self.firebase.database() # get alarm on/off signal from firebase - signals = database.child("signal") - motorSignal = signals.child(1).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") or (not GPIO.input(5)): - - 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 - + print("stopping motor...") + self.motorStop=True + self.EN1.ChangeDutyCycle(0) + self.EN2.ChangeDutyCycle(0) + print("motor stops") def start_alarm(self): - - self.alarmStop=self.stop_alarm() - - while (not self.alarmStop): # 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 + print("Alarm is turned on") + self.alarmStop=False + GPIO.output(26,True) + return False + def stop_alarm(self): - - database = self.firebase.database() # get alarm on/off signal from firebase - signals = database.child("signal") - alarmSignal = signals.child(1).child("alarm").get().val() + print("Alarm turning off...") + self.alarmStop=True + GPIO.output(26,False) + print("Alarm is off") + return True - if (alarmSignal=="on" or (not GPIO.input(16))): - print("Alarm is turned on") - GPIO.output(26,True) - return False - elif alarmSignal=="off": - 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 if __name__=="__main__": - #print("Execute function...") - - motor1=Motor() + car=Motor() while True: # turn on the system forever - - motor1.start_alarm() # alarm on/off test - motor1.start_motor() # motor on/off test + database = car.firebase.database() # get camera on/off signal from firebase + signals = database.child("signal") + 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() + if (motorSignal=="on" and car.motorStop): + car.start_motor() + elif (motorSignal=="off" and not car.motorStop): + car.stop_motor() + if (alarmSignal=="on" and car.alarmStop): + car.start_alarm() + elif (alarmSignal=="off" and not car.alarmStop): + car.stop_alarm() + if (cameraSignal=="on" and car.cameraOff): + car.start_camera() + elif (cameraSignal=="off" and not car.cameraOff): + car.stop_camera() \ No newline at end of file diff --git a/remote_camera.py b/remote_camera.py index 0a17a33ab..4474eebad 100644 --- a/remote_camera.py +++ b/remote_camera.py @@ -91,4 +91,4 @@ with picamera.PiCamera(resolution='1080x720', framerate=24) as camera: server = StreamingServer(address, StreamingHandler) server.serve_forever() finally: - camera.stop_recording() + camera.stop_recording() \ No newline at end of file