PRE2017 3 11 Python Code

From Control Systems Technology Group

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
==Pin/servo Control==
+
#!/usr/bin/python
-
<pre>
+
from pyzbar.pyzbar import decode    # QR reader
-
import RPi.GPIO as GPIO     # GPIO pin control
+
import cv2                    # Image reader
-
import time                 # Posibility to delay
+
import urllib                  # Url reader
-
GPIO.setmode(GPIO.BOARD)    # Board numbering sceme
+
import RPi.GPIO as GPIO   # GPIO pin control
-
pin_cServo1 = 29            # Continuous servo 1
+
import time               # Posibility to delay
-
pin_cServo2 = 33            # Continuous servo 2
+
import signal # Cleanup when ending script
-
pin_cServo3 = 37            # Continuous servo 3
+
-
pin_mServo1 = 13            # Mini servo 1
+
-
# Sets pin as output.
+
pin_servo1 = 29                        # Continuous servo 1
-
GPIO.setup(cServo1,GPIO.OUT)
+
pin_servo2 = 33                        # Continuous servo 2
-
GPIO.setup(cServo2,GPIO.OUT)
+
-
GPIO.setup(cServo3,GPIO.OUT)
+
-
GPIO.setup(mServo1,GPIO.OUT)
+
-
freq = 100                  # Servo frequency [Hz]
+
running = True
-
# Assigns frequency to pins
+
# Releases pins when script is interrupted
-
cServo1 = GPIO.PWM(pin_cServo1, freq)  
+
def end_run(signal,frame):
-
cServo2 = GPIO.PWM(pin_cServo2, freq)
+
global running
-
cServo3 = GPIO.PWM(pin_cServo3, freq)
+
print "Ctrl+C captured, ending script"
-
mServo1 = GPIO.PWM(pin_mServo1, freq)
+
running = False
 +
servo1.stop()
 +
servo2.stop()
 +
GPIO.cleanup()
-
cleft  = 5            # 100% velocity to left
+
# Runs end_run if ctrl+C is clicked
-
ccenter = 7.5          # no movement
+
signal.signal(signal.SIGINT, end_run)
-
cright  = 10            # 100% velocity to right
+
-
mleft  = 5            # 90 degrees left
+
# Takes picture from camera, reads and evaluates QRcode
-
mcenter = 7.5           # center
+
def QRscanner(passcode):
-
mright  = 10            # 90 degrees right
+
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
-
cyldelay        = 2    # Time it takes for cylinder to rotate
+
GPIO.setmode(GPIO.BOARD)    # Board numbering sceme
-
dropdelay      = 15    # Time it takes to drop package
+
-
platformdelay  = 2    # Time it takes for platform to move down
+
-
lockdelay      = 20    # Time lock stays opened
+
-
# Sets all servos still at centered position
+
GPIO.setup(pin_servo1,GPIO.OUT) # Sets pin as output
-
cServo1.start(ccenter)
+
GPIO.setup(pin_servo2,GPIO.OUT) # Sets pin as output
-
cServo2.start(ccenter)
+
-
cServo3.start(ccenter)
+
-
mServo1.start(mcenter)
+
-
if i:
+
freq = 50                  # Servo frequency [Hz]
-
    cServo1.ChangeDutyCycle(cleft)      # Inner cylinder opens
+
-
    cServo2.ChangeDutyCycle(cright)    # Outer cylinder moves away
+
-
    time.delay(cyldelay)                # Duration of cylinder movement
+
-
    cServo1.ChangeDutyCycle(ccenter)    # Inner Cylinder stops moving
+
-
    cServo2.ChangeDutyCycle(ccenter)    # Outer Cylinder stops moving
+
-
    time.delay(dropdelay)              # Duration of package dropping
+
-
    cServo1.ChangeDutyCycle(cright)    # Inner cylinder closes
+
-
    cServo2.ChangeDutyCycle(cleft)      # Outer cylinder moves back
+
-
    time.delay(cyldelay)                # Duration of cylinder movement
+
-
    cServo1.ChangeDutyCycle(ccenter)
+
-
    cServo2.ChangeDutyCycle(ccenter) 
+
-
   
+
-
    cServo3.ChangeDutyCycle(cleft)      # Platform moves down
+
-
    time.delay(platformdelay)          # Duration of platform moving down
+
-
    cServo3.ChangeDutyCycle(ccenter)    # Platform stops moving
+
-
if j=true:
+
servo1 = GPIO.PWM(pin_servo1, freq) # Assigns frequency to pins
-
    mServo1.ChangeDutyCycle(mleft)     # Lock opens
+
servo2 = GPIO.PWM(pin_servo2, freq) # Assigns frequency to pins
-
    time.delay(lockdelay)
+
-
    mServo1.ChangeDutyCycle(mcenter)    # Lock closes
+
-
</pre>
+
-
==QRcode reader==
+
-
<pre>
+
-
#!/usr/bin/python
+
-
from pyzbar.pyzbar import decode    # QR reader
+
-
import cv2                          # Image reader
+
-
import urllib                      # Url reader
+
 +
s1left  = 5.0          # 100% velocity to left
 +
s1center = 6.6          # no movement
 +
s1right  = 8.0          # 100% velocity to right
-
i = 0      # Becomes 1 if QRcode is scanned and correct
+
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
-
# Retrieves figure from recorder
+
servo1.start(s1center) # Sets all servos still at centered position
-
urllib.urlretrieve("http://192.168.178.66:8080/shot.jpg","pic01.jpg")
+
servo2.start(s2center) # Sets all servos still at centered position
-
im = cv2.imread("pic01.jpg")       # 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]
+
-
    print qrdata
+
-
    if qrdata == "QL5WST4S":        # Verifies if code is legit
+
passcode = "QL5WST4S"
-
        i=1
+
camera = cv2.VideoCapture(0)
 +
print "Program Running, provide QRcode"
-
# Followup action if code is legit
+
while running:
-
if i == 1:
+
time.sleep(0.1)
-
    print("success")
+
rightcode = QRscanner(passcode)
-
    # make motor response here
+
if rightcode:
-
</pre>
+
print "Cylinders start opening"
-
==NFC Reader==
+
    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"

Revision as of 19:09, 21 March 2018

  1. !/usr/bin/python

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

  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(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"

Personal tools