2020-01-11

Micro:bitPythonでモータードライバを動かすサンプル Micro:bitのPythonでモータードライバを動かすサンプル - Nao_uの日記 を含むブックマーク はてなブックマーク - Micro:bitのPythonでモータードライバを動かすサンプル - Nao_uの日記 Micro:bitのPythonでモータードライバを動かすサンプル - Nao_uの日記 のブックマークコメント

これのアドレスをSuper:Bitのmain.tsに合わせて書き換えると動きそう

このサンプルも参考になる

http://wiki.labplus.cn/index.php?title=Extend:bit%E2%85%A1

これで動いた

# Add your Python code here. E.g.
from microbit import *

PCA9685_ADDRESS = 0x40   #device address

#define register
MODE1 = 0x00
MODE2 = 0x01
SUBADR1 = 0x02
SUBADR2 = 0x03
SUBADR3 = 0x04

PRESCALE = 0xFE

LED0_ON_L = 0x06
LED0_ON_H = 0x07
LED0_OFF_L = 0x08
LED0_OFF_H = 0x09
ALL_LED_ON_L = 0xFA
ALL_LED_ON_H = 0xFB
ALL_LED_OFF_L = 0xFC
ALL_LED_OFF_H = 0xFD

STP_CHA_L = 2047
STP_CHA_H = 4095
STP_CHB_L = 1
STP_CHB_H = 2047
STP_CHC_L = 1023
STP_CHC_H = 3071
STP_CHD_L = 3071
STP_CHD_H = 1023

# Servos define 
S1 = 0x01
S2 = 0x02
S3 = 0x03
S4 = 0x04
# Motors define
M1 = 8
M2 = 10
M4 = 12
M3 = 14


# device init
def initPCA9685(): 
    i2c.write(PCA9685_ADDRESS, bytearray([MODE1, 0x00]))
    setFreq(50)
    for idx in range(0, 16, 1):
        setPwm(idx, 0 ,0)
    #initialized = true

#set frequency    
def setFreq(freq):
    #Constrain the frequency
    prescaleval = int(25000000/(4096*freq)) - 1
    #prescale = prescaleval;  #Math.Floor(prescaleval + 0.5)
    i2c.write(PCA9685_ADDRESS, bytearray([MODE1]))
    oldmode = i2c.read(PCA9685_ADDRESS, 1)
    newmode = (oldmode[0] & 0x7F) | 0x10  # sleep
    i2c.write(PCA9685_ADDRESS, bytearray([MODE1, newmode])) # go to sleep
    i2c.write(PCA9685_ADDRESS, bytearray([PRESCALE, prescaleval]))  # set the prescaler
    i2c.write(PCA9685_ADDRESS, bytearray([MODE1, oldmode[0]]))
    sleep(4)
    i2c.write(PCA9685_ADDRESS, bytearray([MODE1, oldmode[0] | 0xa1]))

# function: setPwm()     
def setPwm(channel, on, off):
    if (channel >= 0 and channel <= 15):
        buf = bytearray([LED0_ON_L + 4 * channel, on & 0xff, (on >> 8) & 0xff, off & 0xff, (off >> 8) & 0xff])
        i2c.write(PCA9685_ADDRESS, buf)

# function: motorRun()
# @param motor:  M1A, M1B, M2A, M2B
# @param speed1: -255 - 255  0 is stop      
def MotorRun(Motors, speed):
    speed = speed * 16  # map 255 to 4096
    if (speed >= 4096):
        speed = 4095
    if (speed <= -4096):
        speed = -4095
    
    a = Motors
    b = Motors+1
    if (speed >= 0):
        setPwm(a, 0, speed)
        setPwm(b, 0, 0)
    else :
        setPwm(a, 0, 0)
        setPwm(b, 0, -speed)
    

while True:
    initPCA9685()
    display.show(Image.HEART)
    
    for idx in range(0, 120, 1):
        display.show(idx%10)
        #MotorRun(M1, idx)
        sleep(16)

M5CameraでGoogle Driveにアップロードする M5CameraでGoogle Driveにアップロードする  - Nao_uの日記 を含むブックマーク はてなブックマーク - M5CameraでGoogle Driveにアップロードする  - Nao_uの日記 M5CameraでGoogle Driveにアップロードする  - Nao_uの日記 のブックマークコメント

やってみたい

micro:bitをWeb Bluetooth APIで通信するときにハマったポイント micro:bitをWeb Bluetooth APIで通信するときにハマったポイント - Nao_uの日記 を含むブックマーク はてなブックマーク - micro:bitをWeb Bluetooth APIで通信するときにハマったポイント - Nao_uの日記 micro:bitをWeb Bluetooth APIで通信するときにハマったポイント - Nao_uの日記 のブックマークコメント