Remote camera function finish
This commit is contained in:
parent
14dd6f7e98
commit
76da8a15d5
2 changed files with 66 additions and 73 deletions
133
final_demo.py
133
final_demo.py
|
@ -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=False
|
||||||
|
GPIO.output(26,True)
|
||||||
|
return False
|
||||||
|
|
||||||
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
|
|
||||||
def stop_alarm(self):
|
def stop_alarm(self):
|
||||||
|
print("Alarm turning off...")
|
||||||
|
self.alarmStop=True
|
||||||
|
GPIO.output(26,False)
|
||||||
|
print("Alarm is off")
|
||||||
|
return True
|
||||||
|
|
||||||
database = self.firebase.database() # get alarm on/off signal from firebase
|
def kill_target(self, target):
|
||||||
signals = database.child("signal")
|
cmd_run="ps aux | grep {}".format(target)
|
||||||
alarmSignal = signals.child(1).child("alarm").get().val()
|
out=os.popen(cmd_run).read()
|
||||||
|
for line in os.popen("ps ax | grep "+target+" | grep -v grep"):
|
||||||
if (alarmSignal=="on" or (not GPIO.input(16))):
|
fields = line.split()
|
||||||
print("Alarm is turned on")
|
#print(fields)
|
||||||
GPIO.output(26,True)
|
pid = fields[0]
|
||||||
return False
|
a = os.kill(int(pid),signal.SIGKILL)
|
||||||
elif alarmSignal=="off":
|
print('Killed PID %s, return value:%s' % (pid, a))
|
||||||
print("Alarm turning off...")
|
|
||||||
self.alarmStop=True
|
|
||||||
GPIO.output(26,False)
|
|
||||||
print("Alarm is off")
|
|
||||||
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...")
|
||||||
|
|
||||||
|
car=Motor()
|
||||||
motor1=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()
|
Loading…
Reference in a new issue