# Overview

BBR Sumo Kit, sensors, motors and basic helpers to get into Sumo

This document provides an overview of the Sumo board and how to get started with CircuitPython.

Students will need to design a robot chassis and implement code to search out an opponent and respond to the white line on the ring.

A number of examples are provided.


# Hardware

The Sumo board is a capable controller, based on RP2040 supporting CircuitPython and Arduino

The main board has the following pins:

<figure><img src="https://2251603990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUEpKUKA4iMi1RRhp4nJB%2Fuploads%2Fa9RmmsrvUS8jm6O5JCxB%2FSumoBoard.png?alt=media&amp;token=2ab2f7b2-dab7-4108-8a67-dd6b2888e067" alt="" width="563"><figcaption></figcaption></figure>

### Resetting the Board

The board can be reset holding the board in the orientation above. Ensure you have a USB connection to your computer.

Press both buttons at once

Release the top button first, then release the bottom button.

You will see the RPI-RP2 Drive appear on your computer.

From here the board can be updated with a new version of CircuitPython, or it can be flashed with your Arduino code.


# Software

The best place to find information on how to get started with the Sumo board is the [Adafruit CircuitPython page](https://learn.adafruit.com/welcome-to-circuitpython/overview).

This page details the two main options that you can use for software:

[Mu Editor](https://codewith.mu/en/download)

[Thonny Editor](https://thonny.org/)

To get started with the Thonny editor, please review this [page](https://learn.adafruit.com/circuitpython-libraries-on-micropython-using-the-raspberry-pi-pico/thonny-setup).

### Helper Libraries

The Sumo board comes installed with a number of helper libraries to get the motors to move easily.

These can also be downloaded below.

{% file src="/files/rKwsw0Hjr67UqlGaJelQ" %}

### CircuitPython

The following firmware can be used to put CircuitPython back onto the board. This is useful if you have been developing with Arduino and would like to move back to Python.

{% file src="/files/yfR0CszdilHeJxNluxjA" %}


# Distance Sensors

Fast Digital Distance Sensors

The distance sensors that are provided are digital distance sensors. These do not provide a distance to an object, they just provide you with a true or false value regarding the space that is in front of the sensor. It either detects and object, or it does not.

The distance to an object is not that important in sumo, it's more important too quickly know if an opponent is near. By using digital sensors, our main code does not have to do any processing or waiting to work out if an opponent is near. We just need to check to see if the sensor is returning a value.

{% hint style="info" %}
Look for the red LED on the back of the sensor, it will light up if an object is detected.\
Some sensors are set for 50cm, others are set for 25cm.
{% endhint %}

These digital sensors can be used on any port of the Sumo board.

```python
import time
import board
import digitalio


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


while True:
    #write the value of the sensor - look for the red LED on the sensor 
    print("DCB1:",DCB1.value)
    #small delay in the loop
    time.sleep(0.1)


```


# Line Sensor

Detect the edge white line, using reflected light.

The line sensors return a value that represents the amount of reflected light read into the sensor. If the sensor is over the white line it will reflect more light to when it is over the black surface.

The line sensors are Analog sensors, that is the Controller will convert the sensor's analog voltage value into a digital number. The sumo board only has 4 ports that are capable of doing this: AC1, AC2, AC3 and AC5.

Experimentation is needed to work out the best value to use to detect the white lines.

```python
import time
import board
import analogio

AC1 = analogio.AnalogIn(board.AC1)

while True:
    #read the value of the sensor. White will be higher than black - experiment
    print("AC1:",AC1.value)
    time.sleep(0.1)
```


# DIP Switches

While not a sensor, the DIP switches on the board allow you to set values that your code can read. This might play into your strategy. You can read these values and use them to control things like speed, starting movements or anything else you can come up with.

The DIP Switch inputs are digital inputs:

```python
import time
import board
import digitalio

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)

speed = 210
strategy = "spin"

if dip1.value:
   speed = 255 # go faster if DIP 1 is set
   
if dip2.value:
   strategy = "move" # use a different strategy if DIP 2 is set.
   
```


# Motor Control

Get your robot moving with the Sumo Helpers

To help get you moving, your board comes with a helper library that can be used to drive your robot.

From here you'll be able to combine your sensor readings and build a robot that can respond to other robots and the white line.

```python
import time
import BBRSumoClass

sumo = BBRSumoClass.BBRSumo()

#go forwards
sumo.forward(255)
time.sleep(1)
#stop with brakes
sumo.stop()
time.sleep(1)
#go backwards
sumo.reverse(255)
time.sleep(1)
#coasting is releasing the motors - can be pushed
sumo.coast()

```


# Arduino

Looking to harness the full potential of the Sumo board. Program in c++

{% hint style="info" %}
This is an advanced feature, the setup of Arduino is outside of the scope of this documentation. Basic information is provided.
{% endhint %}

The board will make use of the Arduino RP2040 board package found at:

{% embed url="<https://github.com/earlephilhower/arduino-pico>" %}

Follow the install instructions from the above site.

To select a board, use the 'Raspberry Pi Pico'

<figure><img src="https://2251603990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUEpKUKA4iMi1RRhp4nJB%2Fuploads%2FH5J1rLmFJSYKwGadQ2Ro%2FPicoSettings.png?alt=media&amp;token=bd6c5bbd-a787-4b5d-ae89-489ae97e24a2" alt=""><figcaption></figcaption></figure>

Next, use the Arduino library manager to add the following library.

{% file src="/files/VLis1WTqWjP1j0qsDZvM" %}

Once this library is added, the Sumo board can be used:

```cpp
#include <Sumo.h>

Sumo sumo = Sumo();

void setup() {

  sumo.forward(255);
 
  delay(2000);
}

void loop() {
  // put your main code here, to run repeatedly:

}

```

An example that shows all of the board features:

```cpp
#include <Arduino.h>
#include <Sumo.h>

Sumo sumo = Sumo();


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Ready");
  delay(4000);
  sumo.forward(255);
  Serial.println("Forward");
  delay(2000);
  sumo.stop();
  Serial.println("Stop");
  delay(2000);
  sumo.reverse(255);
  Serial.println("Reverse");
  delay(1000);
  sumo.coast();
  Serial.println("Coast");

  //SETUP PINS
  pinMode(DIP1, INPUT_PULLUP);
  pinMode(DIP2, INPUT_PULLUP);
  pinMode(DIP3, INPUT_PULLUP);

  pinMode(AC1, INPUT);
  pinMode(AC2, INPUT);
  pinMode(AC3, INPUT);
  pinMode(AC5, INPUT);

  pinMode(DC4, INPUT);

  pinMode(DCB1, INPUT);
  pinMode(DCB2, INPUT);

  
}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.print("DIP1: ");
  Serial.println(digitalRead(DIP1));

  Serial.print("DIP2: ");
  Serial.println(digitalRead(DIP2));

  Serial.print("DIP3: ");
  Serial.println(digitalRead(DIP3));

  Serial.print("DC4: ");
  Serial.println(digitalRead(DC4));

  Serial.print("AC1: ");
  Serial.println(analogRead(AC1));

  Serial.print("AC2: ");
  Serial.println(analogRead(AC2));

  Serial.print("AC3: ");
  Serial.println(analogRead(AC3));

  Serial.print("AC5: ");
  Serial.println(analogRead(AC5));

  Serial.print("DCB1: ");
  Serial.println(digitalRead(DCB1));

  Serial.print("DCB2: ");
  Serial.println(digitalRead(DCB2));
  

  delay(250);

}

```


# Adjust Motor Direction

If you find that the motors are spinning in the wrong direction, take a look at the code in Sumo.h and update the following code:

```cpp

 Motor _motor1 = Motor(MOTOR_1B,MOTOR_1A);
 Motor _motor2 = Motor(MOTOR_2A,MOTOR_2B);
```

Swapping the pins around will make the motor spin in the opposite direction.


# 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:

```python
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:

```python

 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):

```python

 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.

```python
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.

```python
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:

```python
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)



```


