PRE2017 3 11 Python Code

From Control Systems Technology Group
Revision as of 15:42, 16 March 2018 by S164358 (talk | contribs)
Jump to navigation Jump to search

Pin/servo Control

import RPi.GPIO as GPIO     # GPIO pin control
import time                 # Posibility to delay
GPIO.setmode(GPIO.BOARD)    # Board numbering sceme
pin_cServo1 = 29            # Continuous servo 1
pin_cServo2 = 33            # Continuous servo 2
pin_cServo3 = 37            # Continuous servo 3
pin_mServo1 = 13            # Mini servo 1

# Sets pin as output. 
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]

# Assigns frequency to pins
cServo1 = GPIO.PWM(pin_cServo1, freq) 
cServo2 = GPIO.PWM(pin_cServo2, freq)
cServo3 = GPIO.PWM(pin_cServo3, freq)
mServo1 = GPIO.PWM(pin_mServo1, freq)

cleft   = 5             # 100% velocity to left 
ccenter = 7.5           # no movement
cright  = 10            # 100% velocity to right

mleft   = 5             # 90 degrees left
mcenter = 7.5           # center
mright  = 10            # 90 degrees right

cyldelay        = 2     # Time it takes for cylinder to rotate
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
cServo1.start(ccenter)
cServo2.start(ccenter)
cServo3.start(ccenter)
mServo1.start(mcenter)

if i=true:
    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:
    mServo1.ChangeDutyCycle(mleft)      # Lock opens
    time.delay(lockdelay)
    mServo1.ChangeDutyCycle(mcenter)    # Lock closes

QRcode reader

#!/usr/bin/python
from pyzbar.pyzbar import decode    # QR reader
import cv2                          # Image reader
import urllib                       # Url reader


i = 0       # Becomes 1 if QRcode is scanned and correct

# Retrieves figure from recorder
urllib.urlretrieve("http://192.168.178.66:8080/shot.jpg","pic01.jpg")
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
        i=1

# Followup action if code is legit
if i == 1:
    print("success")
    # make motor response here