Remote camera function finish

This commit is contained in:
LEYAO LI 2021-04-03 23:08:57 -04:00
parent 14dd6f7e98
commit 76da8a15d5
2 changed files with 66 additions and 73 deletions

View file

@ -19,102 +19,95 @@ class Motor:
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23} self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22} self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
# preset the port for buttons and alarm # 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 GPIO.setup(26,GPIO.OUT) # alarm output
for x in self.Motor1: for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT) GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT) GPIO.setup(self.Motor2[x], GPIO.OUT)
#utilize PWM function, enable motors and frequency is 100Hz #utilize PWM function, enable motors and frequency is 100Hz
self.EN1 = GPIO.PWM(self.Motor1['EN'], 100) self.EN1 = GPIO.PWM(self.Motor1['EN'], 100)
self.EN2 = GPIO.PWM(self.Motor2['EN'], 100) self.EN2 = GPIO.PWM(self.Motor2['EN'], 100)
self.EN1.start(0) self.EN1.start(0)
self.EN2.start(0) self.EN2.start(0)
#stop signals for motors and alarm #stop signals for motors and alarm
self.motorStop=False self.motorStop=True
self.alarmStop=False self.alarmStop=True
self.cameraOff=True
# new update motor and alarm functions, are able to connect embedded system throught firebase # new update motor and alarm functions, are able to connect embedded system throught firebase
def start_motor(self): def start_motor(self):
self.motorStop=False
self.motorStop=self.stop_motor() self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
while (not self.motorStop): #break the loop when motor stop signal is detected GPIO.output(self.Motor1['input1'], GPIO.HIGH)
GPIO.output(self.Motor1['input2'], GPIO.LOW)
self.motorStop=self.stop_motor() GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
print("motor is turned on")
def stop_motor(self): def stop_motor(self):
print("stopping motor...")
database = self.firebase.database() # get alarm on/off signal from firebase self.motorStop=True
signals = database.child("signal") self.EN1.ChangeDutyCycle(0)
motorSignal = signals.child(1).child("motor").get().val() self.EN2.ChangeDutyCycle(0)
print("motor stops")
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
def start_alarm(self): def start_alarm(self):
print("Alarm is turned on")
self.alarmStop=self.stop_alarm() self.alarmStop=False
GPIO.output(26,True)
while (not self.alarmStop): # if alarmStop is False or button is pressed return False
# # 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): def stop_alarm(self):
print("Alarm turning off...")
database = self.firebase.database() # get alarm on/off signal from firebase self.alarmStop=True
signals = database.child("signal") GPIO.output(26,False)
alarmSignal = signals.child(1).child("alarm").get().val() print("Alarm is off")
return True
if (alarmSignal=="on" or (not GPIO.input(16))): def kill_target(self, target):
print("Alarm is turned on") cmd_run="ps aux | grep {}".format(target)
GPIO.output(26,True) out=os.popen(cmd_run).read()
return False for line in os.popen("ps ax | grep "+target+" | grep -v grep"):
elif alarmSignal=="off": fields = line.split()
print("Alarm turning off...") #print(fields)
self.alarmStop=True pid = fields[0]
GPIO.output(26,False) a = os.kill(int(pid),signal.SIGKILL)
print("Alarm is off") print('Killed PID %s, return value:%s' % (pid, a))
return True
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__": if __name__=="__main__":
#print("Execute function...") #print("Execute function...")
motor1=Motor() car=Motor()
while True: # turn on the system forever while True: # turn on the system forever
database = car.firebase.database() # get camera on/off signal from firebase
motor1.start_alarm() # alarm on/off test signals = database.child("signal")
motor1.start_motor() # motor on/off test 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()

View file

@ -91,4 +91,4 @@ with picamera.PiCamera(resolution='1080x720', framerate=24) as camera:
server = StreamingServer(address, StreamingHandler) server = StreamingServer(address, StreamingHandler)
server.serve_forever() server.serve_forever()
finally: finally:
camera.stop_recording() camera.stop_recording()