Python

Helpful tips for coding the Sumo Board

Remember to include a 3 second delay at the start

The game needs a three second start at the beginning of the game. However if you use a 3 second delay, you might find that your code takes longer to start than the 3 seconds from power on. This is because the Python core needs time to start up.

The best advice is to test your board to see what delay is needed to get close to the 3 second delay needed in the game:

import time

time.sleep(2.5) #try 2.5 seconds at the start of your program - time it!

Motors move in the wrong direction?

If your motors are moving in the wrong direction, we can change the direction in code, look at the following BBRSumoClass.py:


 self.leftMotor = MotorClass.Motor(board.M1B, board.M1A)
 self.rightMotor = MotorClass.Motor(board.M2A, board.M2B)

Move the pin setup around, so if the left motor is spinning in the wrong direction, try making the code (Swap the A and B):


 self.leftMotor = MotorClass.Motor(board.M1A, board.M1B)
       

Include a small sleep in each loop

This gives the micropython core a chance to do any other important background work.

import time
import board

while True:
    #do something
    time.sleep(0.01)

Declare your pins first

Make sure that you setup all of the pins at the top of your file. Then you can use them as needed in your code / logic.

import time
import board
import digitalio
import pwmio
import analogio
import BBRSumoClass

sumo = BBRSumoClass.BBRSumo()

sumo.forward(255)
time.sleep(1)
sumo.stop()
time.sleep(1)
sumo.reverse(255)
time.sleep(1)
sumo.coast()

AC1 = analogio.AnalogIn(board.AC1)
AC2 = analogio.AnalogIn(board.AC2)
AC3 = analogio.AnalogIn(board.AC3)
AC5 = analogio.AnalogIn(board.AC5)


dip1 = digitalio.DigitalInOut(board.DIP1)
dip1.switch_to_input(pull=digitalio.Pull.UP)

dip2 = digitalio.DigitalInOut(board.DIP2)
dip2.switch_to_input(pull=digitalio.Pull.UP)

dip3 = digitalio.DigitalInOut(board.DIP3)
dip3.switch_to_input(pull=digitalio.Pull.UP)

DC4 = digitalio.DigitalInOut(board.DC4)
DC4.direction = digitalio.Direction.INPUT

DCB1 = digitalio.DigitalInOut(board.DCB1)
DCB1.direction = digitalio.Direction.INPUT

DCB2 = digitalio.DigitalInOut(board.DCB2)
DCB2.direction = digitalio.Direction.INPUT

while True:
    
    print("AC1:",AC1.value)
    print("AC2:",AC2.value)
    print("AC3:",AC3.value)
    print("AC5:",AC5.value)
    
    print("DC4:",DC4.value)
    print("DCB1:",DCB1.value)
    print("DCB2:",DCB2.value)
    
    print("Dip1:", dip1.value)
    print("Dip2:", dip2.value)
    print("Dip3:", dip3.value)
    time.sleep(0.1)


A simple example

The follow is a simple example of a basic robot's movement:

import time
import board
import digitalio
import pwmio
import analogio
import BBRSumoClass

sumo = BBRSumoClass.BBRSumo()

#two light sensors - varable names can be anything meaningful
#i.e. - left, right, front1, front2
AC1 = analogio.AnalogIn(board.AC1)
AC2 = analogio.AnalogIn(board.AC2)

#AC ports can also be digital ports
AC3 = digitalio.DigitalInOut(board.AC3)
AC3.direction = digitalio.Direction.INPUT

AC5 = digitalio.DigitalInOut(board.AC5)
AC3.direction = digitalio.Direction.INPUT


DC4 = digitalio.DigitalInOut(board.DC4)
DC4.direction = digitalio.Direction.INPUT

DCB1 = digitalio.DigitalInOut(board.DCB1)
DCB1.direction = digitalio.Direction.INPUT

DCB2 = digitalio.DigitalInOut(board.DCB2)
DCB2.direction = digitalio.Direction.INPUT

while True:
    if AC1.value > 10000 or AC2.value > 10000:
       #we have hit the white line
       sumo.stop()
       #give the robot some time to stop
       time.sleep(0.2)
       #go backwards
       sumo.reverse(255)
       time.sleep(0.4)
       #we could turn any direction here - example has left
       sumo.left(255)
       #stop left turn
       time.sleep(0.5)
       sumo.stop()
    else if AC3.value == True or AC5.value == True:
       #Something is in front - Charge!
       sumo.forward(255)
    else if DCB1.value == True:
       #something to the right?
       sumo.right(255)
    else if DCB2.value == True:
       #something to left?
       sumo.left(255)
    else:
       #not found? what to do - spin?, forward ?
       sumo.forward(255)
    
    time.sleep(0.01)


Last updated