Python 小车代码

小车底盘结构 如封面一样,两轮

前后左右 只需要控制 两个驱动轮即可。舵机是用来控制 放置摄像头云台 转动的

具体小车部分的代码

#!/usr/bin/env python
#coding: utf8

import RPi.GPIO as GPIO
import time

class Car:
    LED = 37 # LED

    PWM1 = 7    # PWM 调速
    OUT1 = 11    # 对应 轮子
    OUT2 = 13

    PWM2 = 12
    OUT3 = 18
    OUT4 = 16

    # 定义 舵机
    RUDDER_GPIO = {'camera-1': 35}
    RUDDER_PWM = {}

    PWM_MIN = 20
    PWM_OUT = [0, 0]

    STATUS_GO = False
    STATUS_BACK = False

    # 初始化
    def __init__(self):

        GPIO.setmode(GPIO.BOARD)

        GPIO.setup(self.LED, GPIO.OUT)
        GPIO.output(self.LED, GPIO.HIGH)

        GPIO.setup(self.OUT1, GPIO.OUT)
        GPIO.setup(self.OUT2, GPIO.OUT)
        GPIO.setup(self.OUT3, GPIO.OUT)
        GPIO.setup(self.OUT4, GPIO.OUT)
        GPIO.setup(self.PWM1, GPIO.OUT)
        GPIO.setup(self.PWM2, GPIO.OUT)
        self.PWM_OUT[0] = GPIO.PWM(self.PWM1, 50)
        self.PWM_OUT[1] = GPIO.PWM(self.PWM2, 50)

        for (k, v) in self.RUDDER_GPIO.items():
            GPIO.setup(v, GPIO.OUT, initial = False)
            self.RUDDER_PWM[k] = GPIO.PWM(v, 50)
            self.RUDDER_PWM[k].start(0)

        self.speed(0)

    # 舵机
    def rudder(self, args):
        if len(args) == 2 and args[0] in self.RUDDER_GPIO:
            self.RUDDER_PWM.get(args[0]).ChangeDutyCycle(2.00 + 10.00 / 180 * int(args[1]))

    # 速度
    def speed(self, speed):
        speed = int(speed)
        speed = self.PWM_MIN + (100 - self.PWM_MIN) / 100 * speed;
        speed = int(speed)
        speed = min(speed, 100)
        speed = max(speed, self.PWM_MIN)
        # print('speed: `%s`' % speed)
        self.PWM_OUT[0].start(speed)
        self.PWM_OUT[1].start(speed)

    def __del__(self):
        GPIO.cleanup()

    # LED
    def l(self):
        GPIO.output(self.LED, GPIO.LOW)
        time.sleep(.5)
        GPIO.output(self.LED, GPIO.HIGH)

    # 前进
    def go(self):
        self.STATUS_GO = True
        GPIO.output(self.OUT1, GPIO.HIGH)
        GPIO.output(self.OUT2, GPIO.LOW)
        GPIO.output(self.OUT3, GPIO.HIGH)
        GPIO.output(self.OUT4, GPIO.LOW)

    # 后退
    def back(self):
        self.STATUS_BACK = True
        GPIO.output(self.OUT1, GPIO.LOW)
        GPIO.output(self.OUT2, GPIO.HIGH)
        GPIO.output(self.OUT3, GPIO.LOW)
        GPIO.output(self.OUT4, GPIO.HIGH)

    # 右转
    def right(self):
        if self.STATUS_GO:
            GPIO.output(self.OUT1, False)
            GPIO.output(self.OUT2, False)
        else:
            GPIO.output(self.OUT1, GPIO.LOW)
            GPIO.output(self.OUT2, GPIO.HIGH)

        if self.STATUS_BACK:
            GPIO.output(self.OUT3, False)
            GPIO.output(self.OUT4, False)
        else:
            GPIO.output(self.OUT3, GPIO.HIGH)
            GPIO.output(self.OUT4, GPIO.LOW)

    # 左转
    def left(self):
        if self.STATUS_BACK:
            GPIO.output(self.OUT1, False)
            GPIO.output(self.OUT2, False)
        else:
            GPIO.output(self.OUT1, GPIO.HIGH)
            GPIO.output(self.OUT2, GPIO.LOW)

        if self.STATUS_GO:
            GPIO.output(self.OUT3, False)
            GPIO.output(self.OUT4, False)
        else:
            GPIO.output(self.OUT3, GPIO.LOW)
            GPIO.output(self.OUT4, GPIO.HIGH)

    # 停止
    def stop (self, type):
        type = str(type)
        if type == 'go':
            self.STATUS_GO = False
        elif type == 'back':
            self.STATUS_BACK = False
        GPIO.output(self.OUT1, False)
        GPIO.output(self.OUT2, False)
        GPIO.output(self.OUT3, False)
        GPIO.output(self.OUT4, False)

Post Author: admin