PRE2017 3 11 Python Code

From Control Systems Technology Group
Revision as of 21:09, 21 March 2018 by S164358 (talk | contribs)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
from pyzbar.pyzbar import decode    	# QR reader
import cv2                     		# Image reader
import urllib                  	 	# Url reader
import RPi.GPIO as GPIO    	 	# GPIO pin control
import time               	 	# Posibility to delay
import signal				# Cleanup when ending script

pin_servo1 = 29                        # Continuous servo 1
pin_servo2 = 33                        # Continuous servo 2

running = True

# Releases pins when script is interrupted
def end_run(signal,frame):
	global running
	print "Ctrl+C captured, ending script"
	running = False
	servo1.stop()
	servo2.stop()
	GPIO.cleanup()

# Runs end_run if ctrl+C is clicked
signal.signal(signal.SIGINT, end_run)

# Takes picture from camera, reads and evaluates QRcode
def QRscanner(passcode):
	grabbed, im = camera.read()       # Reads figure
	decodedObjects = decode(im)         # Decodes figure
	for obj in decodedObjects:          # Makes sure every code is read
	   	qrdata = obj.data           # Reads the alphanumerical code
    		# Cuts important information out of data and prints it
    		qrdata = qrdata.split("Open The Box ")[1]
    		qrdata = qrdata[:8]
    		if qrdata == passcode:        # Verifies if code is legit
			print "Correct code"
        		return True
		else:
			print "Wrong code"
	return False

GPIO.setmode(GPIO.BOARD)    		# Board numbering sceme

GPIO.setup(pin_servo1,GPIO.OUT)	# Sets pin as output
GPIO.setup(pin_servo2,GPIO.OUT)	# Sets pin as output

freq = 50                  		# Servo frequency [Hz]

servo1 = GPIO.PWM(pin_servo1, freq)	# Assigns frequency to pins
servo2 = GPIO.PWM(pin_servo2, freq)	# Assigns frequency to pins

s1left   = 5.0           # 100% velocity to left
s1center = 6.6           # no movement
s1right  = 8.0           # 100% velocity to right

s2left   = 5.0           # 100% velocity to left
s2center = 6.6           # no movement
s2right  = 8.0           # 100% velocity to right

cyldelay        = 2     # Time it takes for cylinder to rotate
dropdelay       = 5    # Time it takes to drop package
platformdelay   = 2     # Time it takes for platform to move down

servo1.start(s1center)	# Sets all servos still at centered position
servo2.start(s2center)	# Sets all servos still at centered position

passcode = "QL5WST4S"
camera = cv2.VideoCapture(0)
print "Program Running, provide QRcode"

while running:
	time.sleep(0.1)
	rightcode = QRscanner(passcode)
	if rightcode:
		print "Cylinders start opening"
	    	servo1.ChangeDutyCycle(s1left)	 # Cylinders open
    		time.sleep(cyldelay)             # Duration of cylinder movement
    		servo1.ChangeDutyCycle(s1center) # Cylinders stop moving
		print "Cylinders are open, package can be dropped"
    		time.sleep(dropdelay)            # Duration of package dropping
    		print "Cylinders start closing"
		servo1.ChangeDutyCycle(s1right)  # Cylinders close
   		time.sleep(cyldelay)             # Duration of cylinder movement
    		servo1.ChangeDutyCycle(s1center) # Cylinders stop moving
		print "Cylinders are closed, platform starts moving down"
		servo2.ChangeDutyCycle(s2left)   # Platform moves down
    		time.sleep(platformdelay)        # Duration of platform movement
    		servo2.ChangeDutyCycle(s2center) # Platform stops moving
		print "Platform stopped moving down, new code can be scanned"