PRE2017 3 11 Python Code: Difference between revisions

From Control Systems Technology Group
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
<pre>
#!/usr/bin/python
from pyzbar.pyzbar import decode   # QR reader
from pyzbar.pyzbar import decode # QR reader
from PIL import Image              # Image reader
import cv2                    # Image reader
import cv2                    # Image reader
import urllib                  # Url reader
import RPi.GPIO as GPIO       # GPIO pin control
import RPi.GPIO as GPIO   # GPIO pin control
import time              # Posibility to delay
import time              # Posibility to delay
import signal # Cleanup when ending script
import signal # Cleanup when ending script
import sys # Needed to stop script if no camera


pin_servo1 = 29                       # Continuous servo 1
pin_s1  = 29 # Continuous servo 1
pin_servo2 = 33                       # Continuous servo 2
pin_s2  = 33           # Continuous servo 2
freq = 50        # Continuous servo frequency [Hz]


running = True
s1left  = 5.00        # 100% velocity to left
s1center = 6.70        # no movement
s1right  = 8.00        # 100% velocity to right
 
s2left  = 5.00        # 100% velocity to left
s2center = 6.60        # no movement
s2right  = 8.00        # 100% velocity to right
 
cyldelay      = 2 # Time it takes for cylinder to rotate
dropdelay    = 5    # Time it takes to drop package
pauzedelay    = 2 # Time between cylinder rotation and platform movement
platformdelay = 2    # Time it takes for platform to move down
 
imgname = "image2.jpg"  # File frame is temporarily saved in
camslot = 0 # Default camera slot
 
# Imports allowed QRcodes
passcodelist = []
with open("authcodes") as file:
passcodelist = [line.strip() for line in file]


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


# Takes picture from camera, reads and evaluates QRcode
# Takes picture from camera, reads and evaluates QRcode
def QRscanner(passcode):
def QRscanner(passcodelist, camslot):
grabbed, im = camera.read()       # Reads figure
camera = cv2.VideoCapture(camslot) # Initializes camera viewer
decodedObjects = decode(im)        # Decodes figure
grabbed, im = camera.read()     # Reads figure
for obj in decodedObjects:         # Makes sure every code is read
 
  qrdata = obj.data          # Reads the alphanumerical code
if not grabbed: # Searches for working camera
    # Cuts important information out of data and prints it
if camslot < 5:
    qrdata = qrdata.split("Open The Box ")[1]
camstr = "No camera connected in slot: " + str(camslot)
    qrdata = qrdata[:8]
print(camstr)
    if qrdata == passcode:        # Verifies if code is legit
camslot = camslot + 1
print "Correct code"
return False, camslot
        return True
else:
else:
print "Wrong code"
print("No camera connected")
return False
servo1.stop()
servo2.stop()
GPIO.cleanup()
sys.exit()


GPIO.setmode(GPIO.BOARD)   # Board numbering sceme
cv2.imwrite(imgname,im) # Converts image to jpg
img = Image.open(imgname) # Reads jpg
decodedObjects = decode(img)    # Decodes image
for obj in decodedObjects:    # Makes sure every code is read
  qrdata = obj.data      # Reads the alphanumerical code
for passcode in passcodelist:
    if passcode in qrdata:      # Verifies if code is legit
print("Correct code, cylinders start opening")
        return True, camslot
else:
print("Wrong code")
return False, camslot


GPIO.setup(pin_servo1,GPIO.OUT) # Sets pin as output
GPIO.setmode(GPIO.BOARD)    # Board numbering sceme pins
GPIO.setup(pin_servo2,GPIO.OUT) # Sets pin as output
GPIO.setup(pin_s1, GPIO.OUT) # Sets pins as output
GPIO.setup(pin_s2, GPIO.OUT)
servo1 = GPIO.PWM(pin_s1, freq) # Assigns frequency to pins
servo2 = GPIO.PWM(pin_s2, freq)
servo1.start(s1center) # Starts servos in neutral position
servo2.start(s2center)


freq = 50                  # Servo frequency [Hz]
running = True
 
print("Program Running, provide QR-code")
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:
while running:
time.sleep(0.1)
rightcode, camslot = QRscanner(passcodelist, camslot) # Runs QR-reader
rightcode = QRscanner(passcode)
if rightcode:
if rightcode:
print "Cylinders start opening"
servo1.ChangeDutyCycle(s1left) # Cylinders open
    servo1.ChangeDutyCycle(s1left) # Cylinders open
     time.sleep(cyldelay)            # Duration of cylinder movement
     time.sleep(cyldelay)            # Duration of cylinder movement
     servo1.ChangeDutyCycle(s1center) # Cylinders stop moving
     servo1.ChangeDutyCycle(s1center) # Cylinders stop moving
print "Cylinders are open, package can be dropped"
print("Cylinders are open, package can be dropped")
     time.sleep(dropdelay)            # Duration of package dropping
     time.sleep(dropdelay)            # Duration of package dropping
     print "Cylinders start closing"
     print("Package dropped, cylinders start closing")
servo1.ChangeDutyCycle(s1right)  # Cylinders close
servo1.ChangeDutyCycle(s1right)  # Cylinders close
   time.sleep(cyldelay)            # Duration of cylinder movement
   time.sleep(cyldelay)            # Duration of cylinder movement
     servo1.ChangeDutyCycle(s1center) # Cylinders stop moving
     servo1.ChangeDutyCycle(s1center) # Cylinders stop moving
print "Cylinders are closed, platform starts moving down"
print("Cylinders are closed")
time.sleep(pauzedelay)          # Time buffer
print("Platform starts moving down")
servo2.ChangeDutyCycle(s2left)  # Platform moves down
servo2.ChangeDutyCycle(s2left)  # Platform moves down
     time.sleep(platformdelay)        # Duration of platform movement
     time.sleep(platformdelay)        # Duration of platform movement
     servo2.ChangeDutyCycle(s2center) # Platform stops moving
     servo2.ChangeDutyCycle(s2center) # Platform stops moving
print "Platform stopped moving down, new code can be scanned"
print("Platform stopped moving down, new code can be scanned")
</pre>

Revision as of 18:36, 24 March 2018

  1. !/usr/bin/python

from pyzbar.pyzbar import decode # QR reader from PIL import Image # Image reader import cv2 # Image reader import RPi.GPIO as GPIO # GPIO pin control import time # Posibility to delay import signal # Cleanup when ending script import sys # Needed to stop script if no camera

pin_s1 = 29 # Continuous servo 1 pin_s2 = 33 # Continuous servo 2 freq = 50 # Continuous servo frequency [Hz]

s1left = 5.00 # 100% velocity to left s1center = 6.70 # no movement s1right = 8.00 # 100% velocity to right

s2left = 5.00 # 100% velocity to left s2center = 6.60 # no movement s2right = 8.00 # 100% velocity to right

cyldelay = 2 # Time it takes for cylinder to rotate dropdelay = 5 # Time it takes to drop package pauzedelay = 2 # Time between cylinder rotation and platform movement platformdelay = 2 # Time it takes for platform to move down

imgname = "image2.jpg" # File frame is temporarily saved in camslot = 0 # Default camera slot

  1. Imports allowed QRcodes

passcodelist = [] with open("authcodes") as file: passcodelist = [line.strip() for line in file]

  1. 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()

  1. Runs end_run if ctrl+C is clicked

signal.signal(signal.SIGINT, end_run)

  1. Takes picture from camera, reads and evaluates QRcode

def QRscanner(passcodelist, camslot): camera = cv2.VideoCapture(camslot) # Initializes camera viewer grabbed, im = camera.read() # Reads figure

if not grabbed: # Searches for working camera if camslot < 5: camstr = "No camera connected in slot: " + str(camslot) print(camstr) camslot = camslot + 1 return False, camslot else: print("No camera connected") servo1.stop() servo2.stop() GPIO.cleanup() sys.exit()

cv2.imwrite(imgname,im) # Converts image to jpg img = Image.open(imgname) # Reads jpg decodedObjects = decode(img) # Decodes image for obj in decodedObjects: # Makes sure every code is read qrdata = obj.data # Reads the alphanumerical code for passcode in passcodelist:

   			if passcode in qrdata:      # Verifies if code is legit

print("Correct code, cylinders start opening")

       			return True, camslot

else: print("Wrong code") return False, camslot

GPIO.setmode(GPIO.BOARD) # Board numbering sceme pins GPIO.setup(pin_s1, GPIO.OUT) # Sets pins as output GPIO.setup(pin_s2, GPIO.OUT) servo1 = GPIO.PWM(pin_s1, freq) # Assigns frequency to pins servo2 = GPIO.PWM(pin_s2, freq) servo1.start(s1center) # Starts servos in neutral position servo2.start(s2center)

running = True print("Program Running, provide QR-code")

while running: rightcode, camslot = QRscanner(passcodelist, camslot) # Runs QR-reader if rightcode: 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("Package dropped, 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") time.sleep(pauzedelay) # Time buffer print("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")