How to program a micro sumo robot?

XS1 Micro Sumo Arduino Code & Explanation of the Robot code

In that article I will share our new microsumo robot kit’s Arduino code. But before sharing code I want you to fully understand its logic, its system. For that we will go slowly. Let’s look to hardware side of robot and try to understand.
The main idea of this article is understanding the concepts for writing sumo robot codes or developing the codes for your robot tournaments.

Understanding Sumo Robot Operation Principle

For all type sumo robots (Nano, Micro, Mini, Mega…) working principle is same but dimensions, weights are different. They are completely autonomous robots and their main idea is pushing the opponent robot from the robot arena (Dohyo).
For this completely autonomous work, we use sensors, motors, controller board and mechanical hardwares.

Robot sees with sensors, thinks with their mainboard and attacks, makes movement with motors. That is very basic definiton of robot projects.

Input/Output Concept For Programming Idea

For robot we need 4 Inputs (3 opponent sensors & 1 Line Sensor) and 2 main output (Right & Left motors). Other than these, Let’s take a closer look at the controller board which is XMotion Micro.

That Arduino enabled controller has 2 user Leds (2 output) and 2 User Buttons (2 Input). Also motor driver IC needs some outputs but we had count them at main outputs.

We connect some sensors and also motors (by soldering wires to header holes) to XMotion Micro and program the robot (board) via Micro USB cable.

The general short definition of the robot was that it processes the external world data it receives from the sensors and transfers it to the movement organs (motors, pistons etc..). So in the project we are doing this. Sensor datas coming to main controller (Arduino enabled XMotion Micro Board) and processed here and after that we send new commands to motors BUT we need to amplify the motor signals to more higher current levels. And at that we use motor driver ICs or modules.

PinDefinitionI/O TypeSignal Type
D6User Led 1OutputDigital
D4User Led 2OutputDigital
D8User Button 1InputDigital
D12User Button 2InputDigital
D13Motor 1OutputDigital
D5Motor 1OutputDigital
D10Motor 2OutputDigital
D9Motor 2OutputDigital
A2Left Opponent SensorInputDigital
A1Middle Opponent SensorInputDigital
A0Right Opponent SensorInputDigital
A3Bottom Line SensorInputAnalog

Pin Table, we always make a table before starting the code. Note that analog pins can also be used digitally. It is a topic that Arduino beginners wonder or are missing. For Arduino Nano and Leonardo, analog pins A0 to A5 can also be used digitally. As input or output. The off-topic Arduino Nano, on the other hand, can only use A6 and A7 pins as analog inputs. Xmotion Micro uses Arduino Leoardo bootloader with Atmega32u4 mcu.

Now let’s think together how should robot perform against opponent robot. (As you know at sumo robot competitions 2 opponent robot try to find, push each other from the ring).

Thinking from beginning. (Some competitions start with Remotes nowadays, Some start with traditional 5 second rule, we assume here traditonal)

  1. Robot opened and waits for start trigger like pushing button or start signal from remote.
  2. Start button pushed.
  3. Wait 5 Second, While waiting we can blink 2 x user leds at built-in XMotion Micro.
  4. Turn Right or Left depending on button push (2 Button, 2 Turning Side).
  5. Actively look for line sensor while moving forward as our 1st rule is not falling off from Dohyo.
  6. Actively look for the opponent sensors.
  7. If no sensor gave object seen data, look for which last sensor gave object seen data, So remember the last sensor event and make same turning as sensor see (Or you can write some routine as robot turn and go little forward for looking different sections of dohyo area)

Let me explain step 4 before continuing. The first move after the wait (5 seconds) is what we call robot tactics. Some competitions allow you to place the robot in diagonal quarters, while some competitions allow you to place it parallel to facing opposite sides. The turn right and turn left tactic here is for this second reverse parallel insertion. I show alternative starting position examples below.

Of the 3 separate starting positions above, the most common starting options are antiparallel directions and opposite quadrants. However, I recommend that you read the competition rules and program your robot accordingly. For our example, I will take the anti-parallel direction (2nd image) position as an example.

Here again we are looking to our robot, to the sensors. We had 3 opponent sensors and 1 Line sensor. Let’s take a look at the opponent sensors first.

Opponent Sensors of Robot (JS40Fs)

These opponent sensors are Left, Forward and Right facing sensors. So if any of sensor gives data we need to turn to robot to that side. And also we should add some remembering variable as which last sensor seen. Because you know, other (opponent) robot is also moving at area not stationary object.

The sensors used The JS40Fs give a clear simple digital output, one of the simplest sensor types you will encounter. It gives 5V (Logic 1) if it detects an obstacle, or 0V (Logic 0) if it does not detect an obstacle. They can see opponents from 0 to 60 cm, usually 30-40cm. (depending on reflection of other robot).

For more information on JS40F sensor you can look at main website.

Thinking About Line Sensor (ML1)

As both robot is very small and competition arena (dohyo) is small we are using 1 front bottom line sensor. At bigger projects 2 line sensors or sometimes 4 sensors (especially for mega sumo robots at 4 corners) used. Here at XS1, 1 bottom line sensor is enough for working & simplicity.

Line sensor ML1 is normally analog output sensor but here we use at full contrast black & white surface. So, this sensor gives nearly logic 0 when white line is seen. But also there is little problem, as sensor is very sensitive and also very near to surface that it can trigger false alarms. For instance it can see thin white scratches and can send data as white edge line seen at black area too.

We implement 2 methods to avoid this problem.

First, we operate the sensor as analog with analog input. Normally, on digital inputs, above 2.5V is accepted as Logic 1, below 2.5V as Logic 0 by the processor (MCU). With analog reading, we try to lower this threshold and make sure that we are on the white color. At the code we are making this thereshold from 45/1024 of 5V. Nearly 0.2V.

With this distinction, we can clearly see the white without any errors. But still not enough.

The second method is to prevent a momentary signal error from confusing the robot. For example, how thick can a scratch be? How thick is the edge white lines? Let’s consider this. Let’s assume that there is a steep transition in both cases (~ 95% probability).

The scratches are usually 0.2, 0.3mm thick, while the white line is 5mm thick. There is a huge difference here. We will use this difference.

When the line sensor gives white data, we do nothing for a while. So if the robot is going straight, it continues to go. Even if it turns, it continues to turn. If it still sees white line after this time (a second if check) then we can be better sure that we are on the white line.

Flowchart of Robot Code

Micro Sumo Robot Code

Here below you can copy & paste. For questions please add comment 🙂 For code upload I used Arduino IDE 1.8.5 version. Sure new versions will work also without any problem or extra driver. Just download Arduino IDE, choose Leonardo from Tools / Board Menu and select port number from again Tools / Port. (Shown in the right image)

So for uploading code the XS1:

  1. Download Arduino IDE, at installation it will ask some driver setup. Approve all.
  2. Open Arduino and choose board model as Arduino Leonardo (Right image 1 marked)
  3. Select port number from same menu. (Right Image 3 marked)
  4. Copy/paste code to editor. It must be blank page paste as delete all things at editor before pasting.
  5. Click the Right arrow (upload) icon. When you click it will first compile and secondly it will upload code to XMotion Micro Board.
  6. That’s all 🙂
Arduino board and port selection for micro sumo robot

You can dowload the robot code (Arduino sketch) directly by clicking here.

/* JSumo Micro Sumo Robot Code
* Model Number: XS1
* Board Model: XMotion Micro
* JS40F Opponent sensors give Logic 1 When Opponent Sensor Seen
* ML1 Line Sensor gives Logic 0 when white Line seen.
* Buttons give Logic 0 when button is pushed
* Leds Turn on with High (Logic 1)
*/

//// Opponent Sensors Decleration ////
#define Right_Op_Sensor A0
#define Front_Op_Sensor A1
#define Left_Op_Sensor A2
//// Line Sensor Decleration ////
#define Line_Sensor A3
//// User Buttons & User Leds Decleration ////
#define Right_Button 8
#define Left_Button 12
#define Led1 4
#define Led2 6
//// Motor Control Pins Decleration ////
#define Right_Motor_Direction 10
#define Right_Motor_Speed 9
#define Left_Motor_Direction 13
#define Left_Motor_Speed 5
//// Speed & Last Remembered Sensor Variables ////
int Speed2=200;
int Last_Value=1;

//// Main Motor Control Function ////
void Motor(int LeftMotorValue, int RightMotorValue) {

if (LeftMotorValue < 0) {
LeftMotorValue = abs(LeftMotorValue);
digitalWrite(Right_Motor_Direction, LOW);
analogWrite(Right_Motor_Speed, LeftMotorValue);
}
else if (LeftMotorValue > 0){
digitalWrite(Right_Motor_Direction, HIGH);
analogWrite(Right_Motor_Speed, 255 -LeftMotorValue);
}
else
{
digitalWrite(Right_Motor_Direction, HIGH);
analogWrite(Right_Motor_Speed, 255);
}
if (RightMotorValue < 0) {
RightMotorValue = abs(RightMotorValue);
digitalWrite(Left_Motor_Direction, LOW);
analogWrite(Left_Motor_Speed, RightMotorValue);
}
else if (RightMotorValue > 0) {
digitalWrite(Left_Motor_Direction, HIGH);
analogWrite(Left_Motor_Speed, 255 – RightMotorValue);
}
else
{
digitalWrite(Left_Motor_Direction, HIGH);
analogWrite(Left_Motor_Speed, 255);
}
}

//// Motors Stopping Function ////
void MotorStop()
{
digitalWrite(Left_Motor_Direction, HIGH);
analogWrite(Left_Motor_Speed, 255);
digitalWrite(Right_Motor_Direction, HIGH);
analogWrite(Right_Motor_Speed, 255);
}

//// Main Setup Function for Defining Input & Outputs ////
void setup() {
pinMode(Left_Button, INPUT_PULLUP);
pinMode(Right_Button, INPUT_PULLUP);
pinMode(Left_Op_Sensor, INPUT_PULLUP);
pinMode(Right_Op_Sensor, INPUT_PULLUP);
pinMode(Left_Motor_Direction, OUTPUT);
pinMode(Right_Motor_Direction, OUTPUT);
pinMode(Left_Motor_Speed, OUTPUT);
pinMode(Right_Motor_Speed, OUTPUT);
//Serial.begin(9600);
}

//// Main Robot Code Routine ////

void loop() {

while(digitalRead(Right_Button)==1 && digitalRead(Left_Button)==1) // While loop for waiting start button push. (That pus can be one of both button)
{
// Serial.println(analogRead(Line_Sensor));
MotorStop();
}

// Depending on Button Push we give different Values to Last_Value Variable.
// That Last_Value variables will be used later to turning the robot Left or Right
if(digitalRead(Right_Button)==0)Last_Value=2;
if(digitalRead(Left_Button)==0)Last_Value=0;

//// 5 Seconde Delay Routine (500ms x 10 Times)
for(int x=0;x<10;x++)
{
if(x%2==0){digitalWrite(Led1,HIGH); digitalWrite(Led2,LOW); }
else{digitalWrite(Led1,LOW);digitalWrite(Led2,HIGH);}
delay(500);
}
////////

Motor(Speed2,Speed2);

while(digitalRead(Right_Button)==1 && digitalRead(Left_Button)==1){ // When no button is pushed.

//// Line Sensor Control Function ////
if(analogRead(Line_Sensor)<45) // Analog Reading from 45/1024 level.
{
delay(15); // Extra delay threshold for not seeing thin scratches!
if(analogRead(Line_Sensor)<45) // Analog Reading from 45/1024 level.
{
Motor(-Speed2,-Speed2);
delay(150); // 150mS Retreat
Motor(-Speed2,Speed2);
delay(200); // 200mS Turning
Motor(Speed2,Speed2);
}

}
////////////////
//// Opponent Sensor Control Functions, 3 sensor 3 If statement ////
else if(digitalRead(Front_Op_Sensor)==1) // If Front sensor sees opponent
{
Motor(Speed2,Speed2); // Both Motors full speed forward.
Last_Value=1;
}
else if(digitalRead(Left_Op_Sensor)==1&&digitalRead(Right_Op_Sensor)==0) // If Left sensor sees opponent
{
Motor(-Speed2,Speed2);
Last_Value=0;
}
else if(digitalRead(Left_Op_Sensor)==0&&digitalRead(Right_Op_Sensor)==1) // If Right sensor sees opponent
{
Motor(Speed2,-Speed2);
Last_Value=2;
}

//// If no data is received from any sensor, we finally look at the Last_Value variable.
////With this Last_Value variable, all we do is give a value to the Last_Value variable in that function,
///if the last data came from a sensor. We also enable it to remember the last sensor status with the following functions.

else if(Last_Value==0)
{
Motor(-Speed2,Speed2);
}
else if(Last_Value==1)
{
Motor(Speed2,Speed2);
}
else if(Last_Value==2)
{
Motor(Speed2,-Speed2);
}
}
// It will enter this part only when the while loop is broken. (When any button is pressed to stop the robot while it is running.)
delay(100); // That delay is passed when again button is pressed main routine break from while. Used for debouncing button.
while(digitalRead(Right_Button)==0 || digitalRead(Left_Button)==0);
delay(100);
}

Questions about Arduino Code?

Have any questions about the code? Ask in the comments.

Video of Micro Sumo Robot & Code

You can watch the uncut video of the above code below. When the mechanics and electronics are compatible with each other, it is quite possible to get good results even with such a simple code.

As you will see, the robot is fast and agile, so there are situations where my hands cannot reach the robot. 🙂

Development of Micro Sumo Robot, Going Further

Here some extra modifications, developments can be made

  1. Adding Start Module (Microstart) to robot
    For this you may use few extra IO pins of XMotion Micro Board. I am sharing the table of free pins on the right. There are many pins available for making connections. You can get the power line (5V) connections from the 4 x 5V and GND pins on the bottom of the board. (If soldered, you may need to solder wires in parallel)
  2. Adding other start tactics like zigzag.
    You can add different rotation angles or routines. You can also use the sensors as a button to do this at the time of start-up. (For example, pressing the button while the right sensor sees your hand may mean zigzag, but just pressing the button may mean turn right and start)
    For this type of update, it’s helpful to know how far your micro sumo robot has covered in how long. For example, how far does the robot go in 100 milliseconds? Or how many milliseconds should be delayed for a 90 degree turn? To answer these questions, you should study the project well. Don’t be afraid to test and try.
  3. Adding different “hunt” modes
    In the code above, the robot makes its turns according to the last sensor data it saw. And since the detection range of the JS40F sensor is longer than the entire dohyo diameter, there is no detection problem from far. But instead of turning, robot can go forward and scan from a different point, or it can scan dohyo with “star” or “square” moving routines.
  4. Adding Bluetooth Control
    Bluetooth module can be added for IOT features, phone control or online robot tournaments, robotics competitions. If you want to add it, you can make Bluetooth module RX-Tx pins 0 and 1 hardware serial or any two pins with software serial (must be tested). For example applications, you can google “the use of Bluetooth with Arduino Leonardo”. (The XMotion Micro board that we are using is an Arduino Leonardo based controller.) The HC06 module can be used successfully.

Free pins of XMotion Micro Controller

These are extra free pins that can be used for development of robot. But remember, for microsumo; your robot weight must be under 100 grams! (XS1 Micro sumo weight is 94 grams.)

PinDefinitionI/O TypeSignal Type
A4FreeInput or OutputDigital & Analog
A5FreeInput or OutputDigital & Analog
D0FreeInput or OutputDigital
D1FreeInput or OutputDigital
D2FreeInput or OutputDigital
D3FreeInput or OutputDigital
D7FreeInput or OutputDigital

My Final Recommendations

Always clean the wheels! Clean them with alcohol or cologne-based wet wipes. Clean wheels greatly affect the robot’s operation, speed and pushing force.

Battery must be fully charged. Keep the robot fully charged using the charging module that comes with the micro sumo robot. The charged battery will last 5-10 minutes in full operation. However, you can keep the robot ready at maximum efficiency by connecting your robot to the charger after each encounter.

Take the first test in your hand, not in dohyo. Start the robot by holding it from behind, while the sensors should be on. (Sensors may be affected by surrounding objects, walls, please check)

3 Comments

  1. Reply

    very informative for robot makers.

  2. Reply

    I want to know after you press the button. And you put the robot down , how the robot moving?? Or you use Bluetooth for remote control

    • Reply

      Robot moves by turning wheels connected to Dc motors, we didn’t used bluetooth at this example. It moves same as flowchart I shared at article. It is not remote controlled, it decides moving based on sensor data and Arduino based controller processes this data.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>