Learn to code in Python

Python

 

To code your Trashbot using Python, we utilize the BBC micro:bit’s online coding tools but created our own class to interface with the Trashbot kit. Copy the following text and place it in the micro:bit Python editor (linked below).

from microbit import *
import ustruct

ev8_16 = 116
buf1 = bytearray([0])
buf2 = bytearray([0, 0])
buf4 = bytearray([0, 0, 0, 0])
bufEnc = bytearray([0, 3, 2, 1, 0])
bufEncOut = bytearray([0, 0, 0, 0, 0])

class Tbos:
    def __init__(self):
        self.initEnc = [0,0]
    def gyroReadReg(self,r):
        global buf1
        buf1[0] = r
        i2c.write(0x6B, buf1)
        b = i2c.read(0x6B, 1)
        return b[0]

    def gyroReadReg16(self,r):
        global buf2
        buf2[0] = tbos.gyroReadReg(r)
        buf2[1] = tbos.gyroReadReg(r + 1)
        t = ustruct.unpack_from('<h', buf2, 0)
        return t[0]

    def gyroWriteReg(self,r, v):
        global buf2
        buf2[0] = r
        buf2[1] = v
        i2c.write(0x6B, buf2)

    def gyroGetX(self):
        return int(tbos.gyroReadReg16(0x28) / 114)

    def gyroGetY(self):
        return int(tbos.gyroReadReg16(0x2A) / 114)

    def gyroGetZ(self):
        return int(tbos.gyroReadReg16(0x2C) / 114)

    def gyroInit(self):
        # Initialize control registers for the
        # L3GD20H that is on the tbot control board
        tbos.gyroWriteReg(0x20, 0x0F)
        tbos.gyroWriteReg(0x21, 0x00)
        tbos.gyroWriteReg(0x22, 0x00)
        tbos.gyroWriteReg(0x23, 0x00)
        tbos.gyroWriteReg(0x24, 0x00)
        return tbos.gyroReadReg(0x0f)
   
    def motorSetPower(self,m, p):
        global buf2
        if m == 1:
            buf2[0] = 10
        elif m == 2:
            buf2[0] = 20
        else:
            return  # no such motor
        if p > 100:
            p = 100
        if p < -100:
            p = -100
        buf2[1] = int(p)
        pin16.write_digital(0)
        spi.write(buf2)
        pin16.write_digital(1)
        return

    def playNote(self,k):
        global buf2
        buf2[0] = 62
        if k < 0:
            k = 0
        if k > 88:
            k = 88
        buf2[1] = int(k)
        pin16.write_digital(0)
        spi.write(buf2)
        pin16.write_digital(1)
        return

    def playFrequency(self,k):
        global buf4
        if k < 0:
            k = 0
        if k > 10000:
            k = 10000

        buf4[0] = 63
        buf4[1] = ev8_16
        buf4[2] = (int(k) & 0x0000ff00) >> 8
        buf4[3] = (int(k) & 0x000000ff)

        pin16.write_digital(0)
        spi.write(buf4)
        pin16.write_digital(1)
        return

    def clearEncoder(self,i):
        if i == 1:
            self.initEnc[0] = tbos.getEncoder(1)
        elif i == 2:
            self.initEnc[1] = tbos.getEncoder(2)
       
        '''global buf2
        if i == 1:
            buf2[0] = 15
        elif i == 2:
            buf2[0] = 25
        else:
            return 0  # no such encoder

        buf2[1] = 0
        pin16.write_digital(0)
        spi.write(buf2)
        pin16.write_digital(1)
        '''
        return

    def resetMotors(self):
        tbos.motorSetPower(1,0)
        tbos.motorSetPower(2,0)
    def getEncoder(self, i):
        global bufEnc
        if i == 1:
            bufEnc[0] = -15
        elif i == 2:
            bufEnc[0] = -25
        else:
            return 0  # no such encoder

        pin16.write_digital(0)
        spi.write_readinto(bufEnc, bufEncOut)
        pin16.write_digital(1)
        t = ustruct.unpack_from('<i', bufEncOut, 1)
        return t[0] - self.initEnc[i-1]

tbos = Tbos()
spi.init()
tbos.resetMotors()
tbos.clearEncoder(1)
tbos.clearEncoder(2)
tbos.gyroInit()
######### Write Your Code Here #########

For more information about the BBC micro:bit, visit microbit.org

Join newsletter
  • YouTube
  • Facebook
  • Twitter
  • Instagram

© 2020 by Trashbots