掌控板PCA9685电机驱动

MicroPython相关代码、库、工具
回复
头像
shaoziyang
帖子: 710
注册时间: 2019年 10月 21日 13:48

掌控板PCA9685电机驱动

#1

帖子 shaoziyang » 2019年 11月 28日 12:28

原帖作者:chpczx  发表于 2019-8-2

在microbit上写一百多行程序,因内存太小就要崩溃。

于是,把我的四驱车的掌控板电机驱动就弄了出来。扩展板还是使用DFROBOT出品的电机驱动扩展板,此扩展板兼容掌控板和microbit. 并可同时支持四个电机,8个servo.

四驱车清单:
  1. 四驱车(在淘宝上花二十几元买一个四驱车)
  2. 电机扩展板,
  3. 掌控板或microbit板
  4. 小车电源(我使用的是家里的小米移动电源)
只需要这4样,你的四驱车就能跑起来了。

程序清单:
  1. pca9685.py
  2. df_motor.py
pca9685.py源程序:

代码: 全选

import ustruct
import time


class PCA9685:
    def __init__(self, i2c, address=0x40):
        self.i2c = i2c
        self.address = address
        self.reset()

    def _write(self, address, value):
        self.i2c.writeto_mem(self.address, address, bytearray([value]))

    def _read(self, address):
        return self.i2c.readfrom_mem(self.address, address, 1)[0]

    def reset(self):
        self._write(0x00, 0x00) # Mode1

    def freq(self, freq=None):
        if freq is None:
            return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
        prescale = int(25000000.0 / 4096.0 / freq + 0.5)
        old_mode = self._read(0x00) # Mode 1
        self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
        self._write(0xfe, prescale) # Prescale
        self._write(0x00, old_mode) # Mode 1
        time.sleep_us(5)
        self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on

    def pwm(self, index, on=None, off=None):
        if on is None or off is None:
            data = self.i2c.readfrom_mem(self.address, 0x06 + 4 * index, 4)
            return ustruct.unpack('<HH', data)
        data = ustruct.pack('<HH', on, off)
        self.i2c.writeto_mem(self.address, 0x06 + 4 * index,  data)

    def duty(self, index, value=None, invert=False):
        if value is None:
            pwm = self.pwm(index)
            if pwm == (0, 4096):
                value = 0
            elif pwm == (4096, 0):
                value = 4095
            value = pwm[1]
            if invert:
                value = 4095 - value
            return value
        if not 0 <= value <= 4095:
            raise ValueError("Out of range")
        if invert:
            value = 4095 - value
        if value == 0:
            self.pwm(index, 0, 4096)
        elif value == 4095:
            self.pwm(index, 4096, 0)
        else:
            self.pwm(index, 0, value)
df_motor.py源程序

代码: 全选

import pca9685

CW, CCW = 1, -1
M1, M2, M3, M4 = 0, 1, 2, 3

class DF_Motors:
    def __init__(self, i2c):
        self.pca = pca9685.PCA9685(i2c)
        self.pca.freq(500)
        self.motorList = [[7, 6], [5, 4], [3, 2], [1, 0]]
        # 设置电机功率最小值,防止因功率太小,电机不转
        self.speed_min = 30

    # speed 电机速度,取值0-255
    def run(self, motorIndex, direction, speed=100 ):
        if motorIndex not in range(4):
            return 0
        if speed < 0:
            speed = 0
        elif speed > 255:
            speed = 255
        if direction not in [CW, CCW]:
            return 0
        if direction == CW:
            channel_high, channel_low = self.motorList[motorIndex]
        else:
            channel_low, channel_high = self.motorList[motorIndex]

        if speed < self.speed_min:
            self.pca.duty(channel_low, 0)
            self.pca.duty(channel_high, 0)

        else:
            self.pca.duty(channel_low, 0)
            self.pca.duty(channel_high, speed*16)


if __name__ == "__main__":
    from machine import I2C, Pin
    import time
    i2c = I2C(scl=Pin(Pin.P19), sda=Pin(Pin.P20), freq=400000)
    motors = DF_Motors(i2c)
    motors.speed_min = 60
    for i in range(4):
        motors.run(i, -1, 100)
    time.sleep_ms(5000)

    for i in range(4):
        motors.run(i, 1, 0)
 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章