From cd4859af8356aa1dff10884c9468c56fc672eba4 Mon Sep 17 00:00:00 2001 From: Leyao Li Date: Thu, 8 Apr 2021 12:10:00 -0400 Subject: [PATCH] Hardware code replace in the correct folder and update hardware readme.txt --- Hardware/Motor.py | 98 -------- Hardware/README.txt | 15 +- Hardware/Updated_HW_codes/NewMotorFunc.py | 127 ----------- Hardware/Updated_HW_codes/README.md | 12 - .../__pycache__/NewMotorFunc.cpython-36.pyc | Bin 2828 -> 0 bytes Hardware/__pycache__/Motor.cpython-36.pyc | Bin 2402 -> 0 bytes Hardware/final_demo.py | 214 ++++++++++++++++++ Hardware/remote_camera.py | 94 ++++++++ 8 files changed, 317 insertions(+), 243 deletions(-) delete mode 100644 Hardware/Motor.py delete mode 100644 Hardware/Updated_HW_codes/NewMotorFunc.py delete mode 100644 Hardware/Updated_HW_codes/README.md delete mode 100644 Hardware/Updated_HW_codes/__pycache__/NewMotorFunc.cpython-36.pyc delete mode 100644 Hardware/__pycache__/Motor.cpython-36.pyc create mode 100644 Hardware/final_demo.py create mode 100644 Hardware/remote_camera.py 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 1fe7c4e900ec1709a5430c09f7eca5d0297a1409..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2828 zcmb_eZEqVz5Wd@c@!38*FExDwB=aUPZJj1fTUx5BY2MY;jp~G`C&TIRu9I`@J7@3Q zCRV}++;8v;Q2zkm`8E5MFGS)OAR#fcXFCo81QND(c6MiWXJ+@A*}Ij?WxoC4C-?UW zMfq77`)R;Gf);H7p$b)7N>g=I$q|P@Pg+`AcXU-*Rj5Yw*9z79#4(sjjeWv5nbB9A z)M0_8npR&?73kC0A3WHB-Kt8-JcXT=N=X$))MBht$KmitfO!NhdH{qeXxJf4qiSDO z!8FxgD~?WeYCvyLlcu0IsYTP!r)Y*|p|_|FW^%%K8pa{-U8=?&kNse`7&eOW9xHZu zxXZnET?G^P!;lSVi_jt)W~qvnpy_Y)6E%hdI0pO2ic4axsY5f+8|Wcq=@xwG|d6c^od-PrulQDiF2b#7-doO@XoQ)Of4y2UOz#rX&fDsD`-{92Xof= z+`;!dD7R6UPF+#xbf17nXdUOQvAH~qbIl2Q0sRL)Kjf(aD<&nKq6(a2p&~wBSnqk& zqXoKgbLaM2RBtXXt!zG-f4t=1Sq$oTS2ny`H&-`byy#tf$@ctui+Mrp!$~({&g$WA z;KzO#lp8*0UwaYrI-PPoZ1-;M#c?OPKQr@h7WpBrnJMYQ^g8|%b|f-hH{M$c+nyhY zEcId!_uhWKA#xGiy0^<7ckBBsUOBx2*Lr#x$t55qO_1e^F#Vv@jb{h_-02j`-$rr~ z3Butthh!4TRUq6(jv|M^DNPHjbCg6R@;=7swfr7)Av|FXV-@=9=K3>{idftZDRZ>P zi%*>_j9z&>07yo{lpqC1WIqQv!QyU5koAhAJ=@-Lw9TETLR+rPB0({NS)|(t?P?r$ zMA~b4yp1e`rbn#R5N16L8vd?O4}VnnC*bUF-!0v*Ers=Nn+0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a>!m^!+67qc6pvOMKDy4+qU| zYEd_FoGKF`G^#s9DA?2mToS3p6=j~jR8bzpF`OIDC1=)gw4S2%n(e^%%Z}%4Z?!A* xnMx;7CT{nnqS|R!RRQPOx?dL4kS^K&A$p)V_tQH+AQ15) 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="""\ + + +Raspberry Pi - Remote Camera + + +

Raspberry Pi - Remote Camera

+
+ + +""" + +class StreamingOutput(object): + def __init__(self): + self.frame = None + self.buffer = io.BytesIO() + self.condition = Condition() + + def write(self, buf): + if buf.startswith(b'\xff\xd8'): + # New frame, copy the existing buffer's content and notify all + # clients it's available + self.buffer.truncate() + with self.condition: + self.frame = self.buffer.getvalue() + self.condition.notify_all() + self.buffer.seek(0) + return self.buffer.write(buf) + +class StreamingHandler(server.BaseHTTPRequestHandler): + def do_GET(self): + if self.path == '/': + self.send_response(301) + self.send_header('Location', '/index.html') + self.end_headers() + elif self.path == '/index.html': + content = PAGE.encode('utf-8') + self.send_response(200) + self.send_header('Content-Type', 'text/html') + self.send_header('Content-Length', len(content)) + self.end_headers() + self.wfile.write(content) + elif self.path == '/stream.mjpg': + self.send_response(200) + self.send_header('Age', 0) + self.send_header('Cache-Control', 'no-cache, private') + self.send_header('Pragma', 'no-cache') + self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') + self.end_headers() + try: + while True: + with output.condition: + output.condition.wait() + frame = output.frame + self.wfile.write(b'--FRAME\r\n') + self.send_header('Content-Type', 'image/jpeg') + self.send_header('Content-Length', len(frame)) + self.end_headers() + self.wfile.write(frame) + self.wfile.write(b'\r\n') + except Exception as e: + logging.warning( + 'Removed streaming client %s: %s', + self.client_address, str(e)) + else: + self.send_error(404) + self.end_headers() + +class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): + allow_reuse_address = True + daemon_threads = True + +with picamera.PiCamera(resolution='1080x720', framerate=24) as camera: + output = StreamingOutput() + #Uncomment the next line to change your Pi's Camera rotation (in degrees) + camera.rotation = 90 + camera.start_recording(output, format='mjpeg') + try: + address = ('', 8000) + server = StreamingServer(address, StreamingHandler) + server.serve_forever() + finally: + camera.stop_recording() \ No newline at end of file