Первый работающий набросок для робота на Raspberry PI

28.06.2016

Пример работы с GPIO на Python для Raspberry PI

 
import RPi.GPIO as GPIO
from time import sleep

wheel_left1  = 7
wheel_left2  = 11
wheel_right1 = 13
wheel_right2 = 15
step_time = 20

GPIO.setmode(GPIO.BOARD)

GPIO.setup(wheel_left1, GPIO.OUT)
GPIO.setup(wheel_right1, GPIO.OUT)
GPIO.setup(wheel_left2, GPIO.OUT)
GPIO.setup(wheel_right2, GPIO.OUT)


GPIO.output(wheel_left1, GPIO.LOW)
GPIO.output(wheel_right1, GPIO.LOW)
GPIO.output(wheel_left2, GPIO.LOW)
#GPIO.output(wheel_right2, GPIO.HIGH)


def forward():
    GPIO.output(wheel_left1, GPIO.HIGH)
    GPIO.output(wheel_right1, GPIO.HIGH)
    sleep(step_time)
    GPIO.output(wheel_left1, GPIO.LOW)
    GPIO.output(wheel_right1, GPIO.LOW)

def reverse():
    GPIO.output(wheel_left2, GPIO.HIGH)
    GPIO.output(wheel_right2, GPIO.HIGH)
    sleep(step_time)
    GPIO.output(wheel_left2, GPIO.LOW)
    GPIO.output(wheel_right2, GPIO.LOW)

    #GPIO.output(15, GPIO.HIGH)
    sleep(step_time)
    #GPIO.output(15, GPIO.LOW)


while True:
    cmd = input()
    if cmd == 1:
        forward()
    elif cmd == 2:
        reverse()
    else:
        break

GPIO.cleanup() 


Яндекс.Метрика