├── .gitignore ├── HCSR04 ├── HSCR04Driver.py └── HSCR04Test.py ├── L298NMotor ├── L298NHBridge.py └── L298NHBridgeTest.py ├── LIRC ├── LIRCDemo.py ├── Lirc.md └── config │ ├── hardware.conf │ ├── lircd.conf │ └── lircrc ├── OpenCV ├── Face │ └── face.py └── Motion │ ├── Motion.py │ └── conf.json ├── README.md └── SmartCar ├── L298NHBridge.py └── SmartCar.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /HCSR04/HSCR04Driver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: latin-1 3 | 4 | # Autor: kevin 5 | # Date: 20160331 6 | # Version: 1.0 7 | 8 | # This module is designed to control Hcsr04 sensor get distance 9 | 10 | import RPi.GPIO as io 11 | import time 12 | 13 | class HCSR04(object): 14 | 15 | def __init__(self, Trig, Echo): 16 | io.setmode(io.BCM) 17 | # Here we configure the GPIO settings for sensor. 18 | self.trig_pin = Trig 19 | self.echo_pin = Echo 20 | self.SetupGPIO() 21 | # Disable warning from GPIO 22 | io.setwarnings(False) 23 | 24 | def SetupGPIO(self): 25 | io.setup(self.trig_pin, io.OUT, initial = False) 26 | io.setup(self.echo_pin, io.IN) 27 | 28 | def GetDistance(self): 29 | io.output(self.trig_pin, True) 30 | time.sleep(0.00015) 31 | io.output(self.trig_pin, False) 32 | while not io.input(self.echo_pin): 33 | pass 34 | t1 = time.time() 35 | while io.input(self.echo_pin): 36 | pass 37 | t2 = time.time() 38 | 39 | return (t2-t1) *340/2 40 | 41 | # Program will clean up all GPIO settings and terminates 42 | def exit(self): 43 | io.cleanup() 44 | 45 | -------------------------------------------------------------------------------- /HCSR04/HSCR04Test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from HSCR04Driver import HCSR04 4 | import time 5 | 6 | 7 | if __name__ == "__main__": 8 | Sensor = HCSR04(17, 18) 9 | try: 10 | while True: 11 | print 'Distance:%0.2f m' % Sensor.GetDistance() 12 | time.sleep(2) 13 | except KeyboardInterrupt: 14 | Sensor.exit() 15 | 16 | 17 | -------------------------------------------------------------------------------- /L298NMotor/L298NHBridge.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: latin-1 3 | 4 | # Autor: kevin 5 | # Date: 20160331 6 | # Version: 1.0 7 | #Thanks for origin Autor's Ingmar Stape 8 | 9 | # This module is designed to control two motors with a L298N H-Bridge 10 | 11 | # Use this module by creating an instance of the class. To do so call the Init function, then command as desired, e.g. 12 | # import L298NHBridge 13 | # HBridge = L298NHBridge.L298NHBridge() 14 | # HBridge.Init() 15 | 16 | # Import the libraries the class needs 17 | import RPi.GPIO as io 18 | import time 19 | 20 | class HBridge(object): 21 | 22 | def __init__(self, left_pin1, left_pin2, right_pin1, right_pin2, leftpwm_pin, rightpwm_pin): 23 | io.setmode(io.BCM) 24 | # Constant values 25 | self.PWM_MAX = 100 26 | # Here we configure the GPIO settings for the left and right motors spinning direction. 27 | # It defines the four GPIO pins used as input on the L298 H-Bridge to set the motor mode (forward, reverse and stopp). 28 | self.leftmotor_in1_pin = left_pin1 29 | self.leftmotor_in2_pin = left_pin2 30 | self.rightmotor_in1_pin = right_pin1 31 | self.rightmotor_in2_pin = right_pin2 32 | self.leftmotorpwm_pin = leftpwm_pin 33 | self.rightmotorpwm_pin = rightpwm_pin 34 | self.SetupGPIO() 35 | self.leftmotorpwm = io.PWM(self.leftmotorpwm_pin,100) 36 | self.rightmotorpwm = io.PWM(self.rightmotorpwm_pin,100) 37 | self.InitPWM() 38 | # Disable warning from GPIO 39 | io.setwarnings(False) 40 | 41 | def SetupGPIO(self): 42 | io.setup(self.rightmotor_in1_pin, io.OUT) 43 | io.setup(self.rightmotor_in2_pin, io.OUT) 44 | io.setup(self.leftmotor_in1_pin, io.OUT) 45 | io.setup(self.leftmotor_in2_pin, io.OUT) 46 | io.setup(self.leftmotorpwm_pin, io.OUT) 47 | io.setup(self.rightmotorpwm_pin, io.OUT) 48 | 49 | def InitPWM(self): 50 | # Here we configure the GPIO settings for the left and right motors spinning speed. 51 | # It defines the two GPIO pins used as input on the L298 H-Bridge to set the motor speed with a PWM signal. 52 | self.leftmotorpwm.start(0) 53 | self.leftmotorpwm.ChangeDutyCycle(0) 54 | self.rightmotorpwm.start(0) 55 | self.rightmotorpwm.ChangeDutyCycle(0) 56 | 57 | def resetMotorGPIO(self): 58 | io.output(self.leftmotor_in1_pin, False) 59 | io.output(self.leftmotor_in2_pin, False) 60 | io.output(self.rightmotor_in1_pin, False) 61 | io.output(self.rightmotor_in2_pin, False) 62 | 63 | # setMotorMode() 64 | 65 | # Sets the mode for the L298 H-Bridge which motor is in which mode. 66 | 67 | # This is a short explanation for a better understanding: 68 | # motor -> which motor is selected left motor or right motor 69 | # mode -> mode explains what action should be performed by the H-Bridge 70 | 71 | # setMotorMode(leftmotor, reverse) -> The left motor is called by a function and set into reverse mode 72 | # setMotorMode(rightmotor, stopp) -> The right motor is called by a function and set into stopp mode 73 | def setMotorMode(self, motor, mode): 74 | 75 | if motor == "leftmotor": 76 | if mode == "reverse": 77 | io.output(self.leftmotor_in1_pin, True) 78 | io.output(self.leftmotor_in2_pin, False) 79 | elif mode == "forward": 80 | io.output(self.leftmotor_in1_pin, False) 81 | io.output(self.leftmotor_in2_pin, True) 82 | else: 83 | io.output(self.leftmotor_in1_pin, False) 84 | io.output(self.leftmotor_in2_pin, False) 85 | 86 | elif motor == "rightmotor": 87 | if mode == "reverse": 88 | io.output(self.rightmotor_in1_pin, False) 89 | io.output(self.rightmotor_in2_pin, True) 90 | elif mode == "forward": 91 | io.output(self.rightmotor_in1_pin, True) 92 | io.output(self.rightmotor_in2_pin, False) 93 | else: 94 | io.output(self.rightmotor_in1_pin, False) 95 | io.output(self.rightmotor_in2_pin, False) 96 | else: 97 | self.resetMotorGPIO() 98 | 99 | # SetMotorLeft(power) 100 | 101 | # Sets the drive level for the left motor, from +1 (max) to -1 (min). 102 | 103 | # This is a short explanation for a better understanding: 104 | # SetMotorLeft(0) -> left motor is stopped 105 | # SetMotorLeft(0.75) -> left motor moving forward at 75% power 106 | # SetMotorLeft(-0.5) -> left motor moving reverse at 50% power 107 | # SetMotorLeft(1) -> left motor moving forward at 100% power 108 | def setMotorLeft(self, power): 109 | if power < 0: 110 | # Reverse mode for the left motor 111 | self.setMotorMode("leftmotor", "reverse") 112 | pwm = -int(self.PWM_MAX * power) 113 | if pwm > self.PWM_MAX: 114 | pwm = self.PWM_MAX 115 | elif power > 0: 116 | # Forward mode for the left motor 117 | self.setMotorMode("leftmotor", "forward") 118 | pwm = int(self.PWM_MAX * power) 119 | if pwm > self.PWM_MAX: 120 | pwm = self.PWM_MAX 121 | else: 122 | # Stopp mode for the left motor 123 | self.setMotorMode("leftmotor", "stopp") 124 | pwm = 0 125 | # print "SetMotorLeft", pwm 126 | self.leftmotorpwm.ChangeDutyCycle(pwm) 127 | 128 | # SetMotorRight(power) 129 | 130 | # Sets the drive level for the right motor, from +1 (max) to -1 (min). 131 | 132 | # This is a short explanation for a better understanding: 133 | # SetMotorRight(0) -> right motor is stopped 134 | # SetMotorRight(0.75) -> right motor moving forward at 75% power 135 | # SetMotorRight(-0.5) -> right motor moving reverse at 50% power 136 | # SetMotorRight(1) -> right motor moving forward at 100% power 137 | def setMotorRight(self, power): 138 | if power < 0: 139 | # Reverse mode for the right motor 140 | self.setMotorMode("rightmotor", "reverse") 141 | pwm = -int(self.PWM_MAX * power) 142 | if pwm > self.PWM_MAX: 143 | pwm = self.PWM_MAX 144 | elif power > 0: 145 | # Forward mode for the right motor 146 | self.setMotorMode("rightmotor", "forward") 147 | pwm = int(self.PWM_MAX * power) 148 | if pwm > self.PWM_MAX: 149 | pwm = self.PWM_MAX 150 | else: 151 | # Stopp mode for the right motor 152 | self.setMotorMode("rightmotor", "stopp") 153 | pwm = 0 154 | #print "SetMotorRight", pwm 155 | self.rightmotorpwm.ChangeDutyCycle(pwm) 156 | 157 | # Program will clean up all GPIO settings and terminates 158 | def exit(self): 159 | self.resetMotorGPIO() 160 | io.cleanup() 161 | 162 | -------------------------------------------------------------------------------- /L298NMotor/L298NHBridgeTest.py: -------------------------------------------------------------------------------- 1 | # Autor: Ingmar Stapel 2 | # Date: 20141229 3 | # Version: 1.0 4 | # Homepage: www.raspberry-pi-car.com 5 | 6 | import sys, tty, termios, os 7 | from L298NHBridge import HBridge 8 | 9 | speedleft = 0 10 | speedright = 0 11 | 12 | Motors = HBridge(27, 22, 23, 24, 19, 26) 13 | # Instructions for when the user has an interface 14 | print("w/s: direction") 15 | print("a/d: steering") 16 | print("q: stops the motors") 17 | print("p: print motor speed (L/R)") 18 | print("x: exit") 19 | 20 | # The catch method can determine which key has been pressed 21 | # by the user on the keyboard. 22 | def getch(): 23 | fd = sys.stdin.fileno() 24 | old_settings = termios.tcgetattr(fd) 25 | try: 26 | tty.setraw(sys.stdin.fileno()) 27 | ch = sys.stdin.read(1) 28 | finally: 29 | termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 30 | return ch 31 | 32 | # Infinite loop 33 | # The loop will not end until the user presses the 34 | # exit key 'X' or the program crashes... 35 | 36 | def printscreen(): 37 | # Print the motor speed just for interest 38 | os.system('clear') 39 | print("w/s: direction") 40 | print("a/d: steering") 41 | print("q: stops the motors") 42 | print("x: exit") 43 | print("========== Speed Control ==========") 44 | print "left motor: ", speedleft 45 | print "right motor: ", speedright 46 | 47 | while True: 48 | # Keyboard character retrieval method. This method will save 49 | # the pressed key into the variable char 50 | char = getch() 51 | 52 | 53 | 54 | # The car will drive forward when the "w" key is pressed 55 | if(char == "w"): 56 | 57 | # synchronize after a turning the motor speed 58 | 59 | # if speedleft > speedright: 60 | # speedleft = speedright 61 | 62 | # if speedright > speedleft: 63 | # speedright = speedleft 64 | 65 | # accelerate the RaPi car 66 | speedleft = speedleft + 0.1 67 | speedright = speedright + 0.1 68 | 69 | if speedleft > 1: 70 | speedleft = 1 71 | if speedright > 1: 72 | speedright = 1 73 | 74 | Motors.setMotorLeft(speedleft) 75 | Motors.setMotorRight(speedright) 76 | printscreen() 77 | 78 | # The car will reverse when the "s" key is pressed 79 | if(char == "s"): 80 | 81 | # synchronize after a turning the motor speed 82 | 83 | # if speedleft > speedright: 84 | # speedleft = speedright 85 | 86 | # if speedright > speedleft: 87 | # speedright = speedleft 88 | 89 | # slow down the RaPi car 90 | speedleft = speedleft - 0.1 91 | speedright = speedright - 0.1 92 | 93 | if speedleft < -1: 94 | speedleft = -1 95 | if speedright < -1: 96 | speedright = -1 97 | 98 | Motors.setMotorLeft(speedleft) 99 | Motors.setMotorRight(speedright) 100 | printscreen() 101 | 102 | # Stop the motors 103 | if(char == "q"): 104 | speedleft = 0 105 | speedright = 0 106 | Motors.setMotorLeft(speedleft) 107 | Motors.setMotorRight(speedright) 108 | printscreen() 109 | 110 | # The "d" key will toggle the steering right 111 | if(char == "d"): 112 | #if speedright > speedleft: 113 | speedright = speedright - 0.1 114 | speedleft = speedleft + 0.1 115 | 116 | if speedright < -1: 117 | speedright = -1 118 | 119 | if speedleft > 1: 120 | speedleft = 1 121 | 122 | Motors.setMotorLeft(speedleft) 123 | Motors.setMotorRight(speedright) 124 | printscreen() 125 | 126 | # The "a" key will toggle the steering left 127 | if(char == "a"): 128 | #if speedleft > speedright: 129 | speedleft = speedleft - 0.1 130 | speedright = speedright + 0.1 131 | 132 | if speedleft < -1: 133 | speedleft = -1 134 | 135 | if speedright > 1: 136 | speedright = 1 137 | 138 | Motors.setMotorLeft(speedleft) 139 | Motors.setMotorRight(speedright) 140 | printscreen() 141 | 142 | # The "x" key will break the loop and exit the program 143 | if(char == "x"): 144 | Motors.setMotorLeft(0) 145 | Motors.setMotorRight(0) 146 | Motors.exit() 147 | print("Program Ended") 148 | break 149 | 150 | # The keyboard character variable char has to be set blank. We need 151 | # to set it blank to save the next key pressed by the user 152 | char = "" 153 | # End 154 | -------------------------------------------------------------------------------- /LIRC/LIRCDemo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: latin-1 3 | 4 | # Autor: kevin 5 | # Date: 20160331 6 | # Version: 1.0 7 | 8 | # This module is designed to control Hcsr04 sensor get distance 9 | 10 | import lirc 11 | import time 12 | 13 | def LircDecode(): 14 | try : 15 | while True: 16 | codeIR = lirc.nextcode() 17 | if codeIR != []: 18 | if codeIR[0] == "KEY_UP": 19 | print 'The car speed add 0.1' 20 | elif codeIR[0] == "KEY_DOWN": 21 | print 'The car speed sub 0.1' 22 | elif codeIR[0] == "KEY_STOP": 23 | print 'The car stop move' 24 | elif codeIR[0] == "KEY_START": 25 | print 'The car start move ' 26 | elif codeIR[0] == "KEY_GOTO": 27 | print 'The car forward' 28 | elif codeIR[0] == "KEY_BACK": 29 | print 'The car back' 30 | elif codeIR[0] == "KEY_LEFT": 31 | print 'The car left' 32 | elif codeIR[0] == "KEY_RIGHT": 33 | print 'The car right' 34 | except KeyboardInterrupt: 35 | Motors.exit() 36 | 37 | if __name__ == "__main__": 38 | sockid=lirc.init("carremote", blocking = True) 39 | LircDecode() 40 | -------------------------------------------------------------------------------- /LIRC/Lirc.md: -------------------------------------------------------------------------------- 1 | 1,learn irc button 2 | sudo /etc/init.d/lirc stop 3 | irrecord -n -d /dev/lirc0 ~/lircd.conf 4 | 5 | 2,cp file to etc/lirc 6 | sudo mv ~/lircd.conf /etc/lirc/lircd.conf 7 | 8 | 3,modfiy lircd.conf file "name /home/pi/lircd.conf" for "name programe" 9 | 10 | 4, irsend LIST programe "" 11 | 12 | 5, sudo nano ~/.lircrc 13 | eg: 14 | begin 15 | button = KEY_UP 16 | prog = programe 17 | config = KEY_UP 18 | end 19 | 20 | 6, sudo apt-install lirc 21 | write your demo code 22 | -------------------------------------------------------------------------------- /LIRC/config/hardware.conf: -------------------------------------------------------------------------------- 1 | # /etc/lirc/hardware.conf 2 | 3 | # Arguments which will be used when launching lircd 4 | LIRCD_ARGS="--uinput" 5 | 6 | # Don't start lircmd even if there seems to be a good config file 7 | # START_LIRCMD=false 8 | 9 | # Don't start irexec, even if a good config file seems to exist. 10 | # START_IREXEC=false 11 | 12 | # Try to load appropriate kernel modules 13 | LOAD_MODULES=true 14 | 15 | # Run "lircd --driver=help" for a list of supported drivers. 16 | DRIVER="default" 17 | # usually /dev/lirc0 is the correct setting for systems using udev 18 | DEVICE="/dev/lirc0" 19 | MODULES="lirc_rpi" 20 | 21 | # Default configuration files for your hardware if any 22 | LIRCD_CONF="" 23 | LIRCMD_CONF="" 24 | -------------------------------------------------------------------------------- /LIRC/config/lircd.conf: -------------------------------------------------------------------------------- 1 | 2 | # Please make this file available to others 3 | # by sending it to 4 | # 5 | # this config file was automatically generated 6 | # using lirc-0.9.0-pre1(default) on Fri Apr 1 05:30:49 2016 7 | # 8 | # contributed by 9 | # 10 | # brand: /home/pi/lircd.conf 11 | # model no. of remote control: 12 | # devices being controlled by this remote: 13 | # 14 | 15 | begin remote 16 | 17 | name carremote 18 | bits 16 19 | flags SPACE_ENC|CONST_LENGTH 20 | eps 30 21 | aeps 100 22 | 23 | header 9152 4532 24 | one 573 1692 25 | zero 573 563 26 | ptrail 572 27 | repeat 9141 2257 28 | pre_data_bits 16 29 | pre_data 0xFF 30 | gap 108700 31 | toggle_bit_mask 0x0 32 | 33 | begin codes 34 | KEY_START 0x906F 35 | KEY_STOP 0xC23D 36 | KEY_UP 0xA857 37 | KEY_DOWN 0xE01F 38 | KEY_GOTO 0x18E7 39 | KEY_BACK 0x4AB5 40 | KEY_LEFT 0x10EF 41 | KEY_RIGHT 0x5AA5 42 | end codes 43 | 44 | end remote 45 | 46 | 47 | -------------------------------------------------------------------------------- /LIRC/config/lircrc: -------------------------------------------------------------------------------- 1 | #add car remote irc key 2 | 3 | begin 4 | button = KEY_UP 5 | prog = carremote 6 | config = KEY_UP 7 | end 8 | 9 | begin 10 | button = KEY_DOWN 11 | prog = carremote 12 | config = KEY_DOWN 13 | end 14 | 15 | begin 16 | button = KEY_GOTO 17 | prog = carremote 18 | config = KEY_GOTO 19 | end 20 | 21 | begin 22 | button = KEY_BACK 23 | prog = carremote 24 | config = KEY_BACK 25 | end 26 | 27 | begin 28 | button = KEY_START 29 | prog = carremote 30 | config = KEY_START 31 | end 32 | 33 | begin 34 | button = KEY_STOP 35 | prog = carremote 36 | config = KEY_STOP 37 | end 38 | 39 | begin 40 | button = KEY_LEFT 41 | prog = carremote 42 | config = KEY_LEFT 43 | end 44 | 45 | begin 46 | button = KEY_RIGHT 47 | prog = carremote 48 | config = KEY_RIGHT 49 | end 50 | -------------------------------------------------------------------------------- /OpenCV/Face/face.py: -------------------------------------------------------------------------------- 1 | from picamera import PiCamera 2 | import cv2 3 | import time 4 | import io 5 | import numpy 6 | 7 | camera = PiCamera() 8 | camera.resolution = (320, 240) 9 | #camera.framerate = 16 10 | stream = io.BytesIO() 11 | 12 | for f in camera.capture_continuous(stream, format="jpeg"): 13 | 14 | buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8) 15 | image = cv2.imdecode(buff, 1) 16 | face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml') 17 | 18 | gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 19 | 20 | faces = face_cascade.detectMultiScale(gray, 1.1, 5) 21 | print "Found "+str(len(faces))+" face(s)" 22 | for (x,y,w,h) in faces: 23 | cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2) 24 | 25 | cv2.imshow("face", image) 26 | key = cv2.waitKey(1) & 0xFF 27 | 28 | # if the `q` key is pressed, break from the lop 29 | if key == ord("q"): 30 | break 31 | 32 | stream.truncate() 33 | stream.seek(0) 34 | -------------------------------------------------------------------------------- /OpenCV/Motion/Motion.py: -------------------------------------------------------------------------------- 1 | # USAGE 2 | # python Motion.py --conf conf.json 3 | 4 | # import the necessary packages 5 | from picamera.array import PiRGBArray 6 | from picamera import PiCamera 7 | import argparse 8 | import warnings 9 | import datetime 10 | import imutils 11 | import json 12 | import time 13 | import cv2 14 | 15 | # construct the argument parser and parse the arguments 16 | ap = argparse.ArgumentParser() 17 | ap.add_argument("-c", "--conf", required=True, 18 | help="path to the JSON configuration file") 19 | args = vars(ap.parse_args()) 20 | 21 | # filter warnings, load the configuration and initialize the Dropbox 22 | # client 23 | warnings.filterwarnings("ignore") 24 | conf = json.load(open(args["conf"])) 25 | 26 | 27 | # initialize the camera and grab a reference to the raw camera capture 28 | camera = PiCamera() 29 | camera.resolution = tuple(conf["resolution"]) 30 | camera.framerate = conf["fps"] 31 | rawCapture = PiRGBArray(camera, size=tuple(conf["resolution"])) 32 | 33 | # allow the camera to warmup, then initialize the average frame, last 34 | # uploaded timestamp, and frame motion counter 35 | print "[INFO] warming up..." 36 | time.sleep(conf["camera_warmup_time"]) 37 | avg = None 38 | 39 | # capture frames from the camera 40 | for f in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): 41 | # grab the raw NumPy array representing the image and initialize 42 | # the timestamp and occupied/unoccupied text 43 | frame = f.array 44 | timestamp = datetime.datetime.now() 45 | text = "Unoccupied" 46 | 47 | # resize the frame, convert it to grayscale, and blur it 48 | frame = imutils.resize(frame, width=500) 49 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 50 | gray = cv2.GaussianBlur(gray, (21, 21), 0) 51 | 52 | # if the average frame is None, initialize it 53 | if avg is None: 54 | print "[INFO] starting background model..." 55 | avg = gray.copy().astype("float") 56 | rawCapture.truncate(0) 57 | continue 58 | 59 | # accumulate the weighted average between the current frame and 60 | # previous frames, then compute the difference between the current 61 | # frame and running average 62 | cv2.accumulateWeighted(gray, avg, 0.5) 63 | frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg)) 64 | 65 | # threshold the delta image, dilate the thresholded image to fill 66 | # in holes, then find contours on thresholded image 67 | thresh = cv2.threshold(frameDelta, conf["delta_thresh"], 255, 68 | cv2.THRESH_BINARY)[1] 69 | thresh = cv2.dilate(thresh, None, iterations=2) 70 | (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, 71 | cv2.CHAIN_APPROX_SIMPLE) 72 | 73 | # loop over the contours 74 | for c in cnts: 75 | # if the contour is too small, ignore it 76 | if cv2.contourArea(c) < conf["min_area"]: 77 | continue 78 | 79 | # compute the bounding box for the contour, draw it on the frame, 80 | # and update the text 81 | (x, y, w, h) = cv2.boundingRect(c) 82 | cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) 83 | text = "Occupied" 84 | 85 | # draw the text and timestamp on the frame 86 | ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p") 87 | cv2.putText(frame, "Room Status: {}".format(text), (10, 20), 88 | cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 89 | cv2.putText(frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 90 | 0.35, (0, 0, 255), 1) 91 | 92 | # check to see if the frames should be displayed to screen 93 | if conf["show_video"]: 94 | # display the security feed 95 | cv2.imshow("Security Feed", frame) 96 | key = cv2.waitKey(1) & 0xFF 97 | 98 | # if the `q` key is pressed, break from the lop 99 | if key == ord("q"): 100 | break 101 | 102 | # clear the stream in preparation for the next frame 103 | rawCapture.truncate(0) 104 | -------------------------------------------------------------------------------- /OpenCV/Motion/conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "show_video": true, 3 | "min_upload_seconds": 3.0, 4 | "min_motion_frames": 8, 5 | "camera_warmup_time": 2.5, 6 | "delta_thresh": 5, 7 | "resolution": [640, 480], 8 | "fps": 16, 9 | "min_area": 5000 10 | } 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # raspberry 2 | Some raspberry pi3 common driver python module : 3 | 4 | 1.L298N bridge driver two motors use pwm 5 | 6 | 2.Hscr04 sensor driver get distance 7 | 8 | 3.LIRC dirver for remote control 9 | Init install 10 | 1,sudo vi /boot/config.txt 11 | add: doverlay=lirc-rpi 12 | 2, sudo apt-get install lirc 13 | 3, sudo reboot 14 | 4, sudo mode2 –d /dev/lirc0 15 | press button for control and test module is not working 16 | -------------------------------------------------------------------------------- /SmartCar/L298NHBridge.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: latin-1 3 | 4 | # Autor: kevin 5 | # Date: 20160331 6 | # Version: 1.0 7 | #Thanks for origin Autor's Ingmar Stape 8 | 9 | # This module is designed to control two motors with a L298N H-Bridge 10 | 11 | # Use this module by creating an instance of the class. To do so call the Init function, then command as desired, e.g. 12 | # import L298NHBridge 13 | # HBridge = L298NHBridge.L298NHBridge() 14 | # HBridge.Init() 15 | 16 | # Import the libraries the class needs 17 | import RPi.GPIO as io 18 | import time 19 | 20 | class HBridge(object): 21 | 22 | def __init__(self, left_pin1, left_pin2, right_pin1, right_pin2, leftpwm_pin, rightpwm_pin): 23 | io.setmode(io.BCM) 24 | # Constant values 25 | self.PWM_MAX = 100 26 | # Here we configure the GPIO settings for the left and right motors spinning direction. 27 | # It defines the four GPIO pins used as input on the L298 H-Bridge to set the motor mode (forward, reverse and stopp). 28 | self.leftmotor_in1_pin = left_pin1 29 | self.leftmotor_in2_pin = left_pin2 30 | self.rightmotor_in1_pin = right_pin1 31 | self.rightmotor_in2_pin = right_pin2 32 | self.leftmotorpwm_pin = leftpwm_pin 33 | self.rightmotorpwm_pin = rightpwm_pin 34 | self.SetupGPIO() 35 | self.leftmotorpwm = io.PWM(self.leftmotorpwm_pin,100) 36 | self.rightmotorpwm = io.PWM(self.rightmotorpwm_pin,100) 37 | self.InitPWM() 38 | # Disable warning from GPIO 39 | io.setwarnings(False) 40 | 41 | def SetupGPIO(self): 42 | io.setup(self.rightmotor_in1_pin, io.OUT) 43 | io.setup(self.rightmotor_in2_pin, io.OUT) 44 | io.setup(self.leftmotor_in1_pin, io.OUT) 45 | io.setup(self.leftmotor_in2_pin, io.OUT) 46 | io.setup(self.leftmotorpwm_pin, io.OUT) 47 | io.setup(self.rightmotorpwm_pin, io.OUT) 48 | 49 | def InitPWM(self): 50 | # Here we configure the GPIO settings for the left and right motors spinning speed. 51 | # It defines the two GPIO pins used as input on the L298 H-Bridge to set the motor speed with a PWM signal. 52 | self.leftmotorpwm.start(0) 53 | self.leftmotorpwm.ChangeDutyCycle(0) 54 | self.rightmotorpwm.start(0) 55 | self.rightmotorpwm.ChangeDutyCycle(0) 56 | 57 | def resetMotorGPIO(self): 58 | io.output(self.leftmotor_in1_pin, False) 59 | io.output(self.leftmotor_in2_pin, False) 60 | io.output(self.rightmotor_in1_pin, False) 61 | io.output(self.rightmotor_in2_pin, False) 62 | 63 | # setMotorMode() 64 | 65 | # Sets the mode for the L298 H-Bridge which motor is in which mode. 66 | 67 | # This is a short explanation for a better understanding: 68 | # motor -> which motor is selected left motor or right motor 69 | # mode -> mode explains what action should be performed by the H-Bridge 70 | 71 | # setMotorMode(leftmotor, reverse) -> The left motor is called by a function and set into reverse mode 72 | # setMotorMode(rightmotor, stopp) -> The right motor is called by a function and set into stopp mode 73 | def setMotorMode(self, motor, mode): 74 | 75 | if motor == "leftmotor": 76 | if mode == "reverse": 77 | io.output(self.leftmotor_in1_pin, True) 78 | io.output(self.leftmotor_in2_pin, False) 79 | elif mode == "forward": 80 | io.output(self.leftmotor_in1_pin, False) 81 | io.output(self.leftmotor_in2_pin, True) 82 | else: 83 | io.output(self.leftmotor_in1_pin, False) 84 | io.output(self.leftmotor_in2_pin, False) 85 | 86 | elif motor == "rightmotor": 87 | if mode == "reverse": 88 | io.output(self.rightmotor_in1_pin, False) 89 | io.output(self.rightmotor_in2_pin, True) 90 | elif mode == "forward": 91 | io.output(self.rightmotor_in1_pin, True) 92 | io.output(self.rightmotor_in2_pin, False) 93 | else: 94 | io.output(self.rightmotor_in1_pin, False) 95 | io.output(self.rightmotor_in2_pin, False) 96 | else: 97 | self.resetMotorGPIO() 98 | 99 | # SetMotorLeft(power) 100 | 101 | # Sets the drive level for the left motor, from +1 (max) to -1 (min). 102 | 103 | # This is a short explanation for a better understanding: 104 | # SetMotorLeft(0) -> left motor is stopped 105 | # SetMotorLeft(0.75) -> left motor moving forward at 75% power 106 | # SetMotorLeft(-0.5) -> left motor moving reverse at 50% power 107 | # SetMotorLeft(1) -> left motor moving forward at 100% power 108 | def setMotorLeft(self, power): 109 | if power < 0: 110 | # Reverse mode for the left motor 111 | self.setMotorMode("leftmotor", "reverse") 112 | pwm = -int(self.PWM_MAX * power) 113 | if pwm > self.PWM_MAX: 114 | pwm = self.PWM_MAX 115 | elif power > 0: 116 | # Forward mode for the left motor 117 | self.setMotorMode("leftmotor", "forward") 118 | pwm = int(self.PWM_MAX * power) 119 | if pwm > self.PWM_MAX: 120 | pwm = self.PWM_MAX 121 | else: 122 | # Stopp mode for the left motor 123 | self.setMotorMode("leftmotor", "stopp") 124 | pwm = 0 125 | # print "SetMotorLeft", pwm 126 | self.leftmotorpwm.ChangeDutyCycle(pwm) 127 | 128 | # SetMotorRight(power) 129 | 130 | # Sets the drive level for the right motor, from +1 (max) to -1 (min). 131 | 132 | # This is a short explanation for a better understanding: 133 | # SetMotorRight(0) -> right motor is stopped 134 | # SetMotorRight(0.75) -> right motor moving forward at 75% power 135 | # SetMotorRight(-0.5) -> right motor moving reverse at 50% power 136 | # SetMotorRight(1) -> right motor moving forward at 100% power 137 | def setMotorRight(self, power): 138 | if power < 0: 139 | # Reverse mode for the right motor 140 | self.setMotorMode("rightmotor", "reverse") 141 | pwm = -int(self.PWM_MAX * power) 142 | if pwm > self.PWM_MAX: 143 | pwm = self.PWM_MAX 144 | elif power > 0: 145 | # Forward mode for the right motor 146 | self.setMotorMode("rightmotor", "forward") 147 | pwm = int(self.PWM_MAX * power) 148 | if pwm > self.PWM_MAX: 149 | pwm = self.PWM_MAX 150 | else: 151 | # Stopp mode for the right motor 152 | self.setMotorMode("rightmotor", "stopp") 153 | pwm = 0 154 | #print "SetMotorRight", pwm 155 | self.rightmotorpwm.ChangeDutyCycle(pwm) 156 | 157 | # Program will clean up all GPIO settings and terminates 158 | def exit(self): 159 | self.resetMotorGPIO() 160 | io.cleanup() 161 | 162 | -------------------------------------------------------------------------------- /SmartCar/SmartCar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: latin-1 3 | 4 | # Autor: kevin 5 | # Date: 20160331 6 | # Version: 1.0 7 | 8 | # This code is contorl remote car 9 | 10 | import lirc 11 | import time 12 | from L298NHBridge import HBridge 13 | 14 | 15 | class SmartCar(object): 16 | def __init__(self, motors): 17 | self.motors = motors 18 | self.speed = 0 19 | self.arrow = "forward" 20 | self.status = False 21 | 22 | def SmartCarAction(self, codeIR): 23 | if codeIR == "KEY_UP": 24 | self.SmartCarAddSpeed() 25 | elif codeIR == "KEY_DOWN": 26 | self.SmartCarSubSpeed() 27 | elif codeIR == "KEY_STOP": 28 | self.SmartCarStop() 29 | elif codeIR == "KEY_START": 30 | self.SmartCarStart() 31 | elif codeIR == "KEY_GOTO": 32 | self.SmartCarStart() 33 | elif codeIR == "KEY_BACK": 34 | self.SmartCarBack() 35 | elif codeIR == "KEY_LEFT": 36 | self.SmartCarToLeft() 37 | elif codeIR == "KEY_RIGHT": 38 | self.SmartCarToRight() 39 | 40 | def SmartCarStart(self): 41 | print 'The car start move' 42 | self.arrow = "forward" 43 | if self.speed < 0: 44 | self.speed = 0 45 | 46 | if self.speed > 1: 47 | self.speed = 1 48 | 49 | for i in range(10): 50 | self.speed += 0.1 51 | if self.speed > 0.4: 52 | self.motors.setMotorLeft(self.speed) 53 | self.motors.setMotorRight(self.speed) 54 | time.sleep(0.5) 55 | 56 | self.status = True 57 | 58 | def SmartCarBack(self): 59 | print 'The car back move' 60 | if self.speed > 0: 61 | self.speed = 0 62 | if self.speed < -1: 63 | self.speed = -1 64 | for i in range(10): 65 | self.speed -= 0.1 66 | if self.speed < -0.4: 67 | self.motors.setMotorLeft(self.speed) 68 | self.motors.setMotorRight(self.speed) 69 | time.sleep(0.5) 70 | self.arrow = "back" 71 | self.status = True 72 | 73 | def SmartCarStop(self): 74 | print 'The car stop move' 75 | self.speed = 0 76 | self.status = False 77 | self.motors.setMotorLeft(self.speed) 78 | self.motors.setMotorRight(self.speed) 79 | 80 | def SmartCarAddSpeed(self): 81 | if self.status == True : 82 | if self.arrow == "forward": 83 | self.speed += 0.1 84 | if self.speed > 0 and self.speed < 0.4: 85 | self.speed = 0.5 86 | if self.speed > 1: 87 | self.speed = 1 88 | elif self.arrow == "back": 89 | self.speed += 0.1 90 | if self.speed < 0 and self.speed > -0.4: 91 | self.speed = 0 92 | self.arrow = "forward" 93 | 94 | print ('The car speed add 0.1, speed is : %0.1f' % self.speed, self.arrow) 95 | self.motors.setMotorLeft(self.speed) 96 | self.motors.setMotorRight(self.speed) 97 | else : 98 | print 'Please Start Smart Car!' 99 | 100 | 101 | def SmartCarSubSpeed(self): 102 | if self.status == True: 103 | if self.arrow == "forward": 104 | self.speed -= 0.1 105 | if self.speed > 0 and self.speed < 0.4: 106 | self.speed = 0 107 | self.arrow = "back" 108 | elif self.arrow == "back": 109 | self.speed -= 0.1 110 | if self.speed < 0 and self.speed > -0.4: 111 | self.speed = -0.5 112 | if self.speed < -1: 113 | self.speed = -1 114 | print ('The car speed sub 0.1, speed is : %0.1f ' % self.speed, self.arrow) 115 | self.motors.setMotorLeft(self.speed) 116 | self.motors.setMotorRight(self.speed) 117 | else: 118 | print 'Please Start Smart Car!' 119 | 120 | def SmartCarToRight(self): 121 | print 'The car right' 122 | if self.status == True: 123 | self.motors.setMotorLeft(self.speed + 0.5) 124 | self.motors.setMotorRight(self.speed - 0.5) 125 | 126 | def SmartCarToLeft(self): 127 | print 'The car left' 128 | if self.status == True: 129 | self.motors.setMotorLeft(self.speed - 0.5) 130 | self.motors.setMotorRight(self.speed + 0.5) 131 | 132 | 133 | 134 | def LircDecode(): 135 | #Init motor pin 136 | print "++++++Start run programe++++" 137 | Motors = HBridge(27, 22, 23, 24, 19, 26) 138 | Car = SmartCar(Motors) 139 | try : 140 | while True: 141 | codeIR = lirc.nextcode() 142 | if codeIR != []: 143 | Car.SmartCarAction(codeIR[0]) 144 | except KeyboardInterrupt: 145 | Motors.exit() 146 | 147 | if __name__ == "__main__": 148 | sockid=lirc.init("carremote", blocking = False) 149 | LircDecode() 150 | --------------------------------------------------------------------------------