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:
==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
pin_servo1 = 29                       # Continuous servo 1
pin_servo2 = 33                       # Continuous servo 2


# Sets pin as output.
running = True
GPIO.setup(cServo1,GPIO.OUT)
GPIO.setup(cServo2,GPIO.OUT)
GPIO.setup(cServo3,GPIO.OUT)
GPIO.setup(mServo1,GPIO.OUT)


freq = 100                  # Servo frequency [Hz]
# 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()


# Assigns frequency to pins
# Runs end_run if ctrl+C is clicked
cServo1 = GPIO.PWM(pin_cServo1, freq)
signal.signal(signal.SIGINT, end_run)
cServo2 = GPIO.PWM(pin_cServo2, freq)
cServo3 = GPIO.PWM(pin_cServo3, freq)
mServo1 = GPIO.PWM(pin_mServo1, freq)


cleft  = 5            # 100% velocity to left
# Takes picture from camera, reads and evaluates QRcode
ccenter = 7.5           # no movement
def QRscanner(passcode):
cright  = 10            # 100% velocity to 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


mleft  = 5            # 90 degrees left
GPIO.setmode(GPIO.BOARD)    # Board numbering sceme
mcenter = 7.5          # center
mright  = 10            # 90 degrees right


cyldelay        = 2    # Time it takes for cylinder to rotate
GPIO.setup(pin_servo1,GPIO.OUT) # Sets pin as output
dropdelay      = 15    # Time it takes to drop package
GPIO.setup(pin_servo2,GPIO.OUT) # Sets pin as output
platformdelay  = 2    # Time it takes for platform to move down
lockdelay      = 20    # Time lock stays opened


# Sets all servos still at centered position
freq = 50                  # Servo frequency [Hz]
cServo1.start(ccenter)
cServo2.start(ccenter)
cServo3.start(ccenter)
mServo1.start(mcenter)


if i:
servo1 = GPIO.PWM(pin_servo1, freq) # Assigns frequency to pins
    cServo1.ChangeDutyCycle(cleft)      # Inner cylinder opens
servo2 = GPIO.PWM(pin_servo2, freq) # Assigns frequency to pins
    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:
s1left  = 5.0          # 100% velocity to left
    mServo1.ChangeDutyCycle(mleft)      # Lock opens
s1center = 6.6          # no movement
    time.delay(lockdelay)
s1right  = 8.0          # 100% velocity to right
    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


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


i = 0       # Becomes 1 if QRcode is scanned and correct
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 20: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"