Soccer Tactics 1 - Stay on the Soccer Field!

The first thing we want our robot to do is to be able to stay inside the Soccer Field; click here.

Robot Soccer Lego NXT Gen II Tactics 1 Free tutorial

To stay on the Soccer field our Robot must avoid the Soccer field's white borders. How can we teach our robot to know when it sees white?

The LEGO Light Sensor readings I obtained this morning are shown in the "white clouds" below.  Be careful - your measurements  will almost certainly be different from mine, because the light in your robot Soccer room will be different from the light in my robot Soccer room. Carefully read, and then write down, the LEGO Light Sensor measurements that you obtain by placing your Soccer Robot over each of the different parts of the Robot Soccer Field.

Robot GEN II Soccer Field Light Sensor measurements free tutorial Lego NXT MindStorms 

Which readings will we use to tell our robot that it has left the Soccer field?

The highest reading inside my Soccer field is 60, obtained in the "light green" part of the Soccer field. To be able to tell my Robot how to see the difference between the light green and white parts of the Soccer mat, I could use a value:-

= (light sensor reading of "white" + light sensor reading of "light green")/2
= (65 + 60)/2
= 62.5

A light sensor reading value less than either 62 or 63  (say 63, but your value will be different) will mean the robot is inside the Soccer field, and thus the Robot can continue playing Soccer. However a light sensor reading greater than 63 would mean that the robot is outside the Soccer field, and that is a problem we must fix.

How could we tell the Robot to fix this problem?? Let us look at the following code.

In the code below, my LEGO Light Sensor is plugged into computer brick socket 1. If your robot has the light sensor plugged in to a different socket, you will have to change this code.

Robot Soccer NXT-G Program free tutorial step 1 

If a robot Soccer player that has gone over the edge of the green and black playing field into the white surround, the code above will cause the Robot to retreat backwards on to the Soccer field. It works like this:-

Robot Soccer NXT-G Program free tutorial step 2 

Robot Soccer for Lego NXT GEN II free tutorial

The command above will tell motors A and C start to go backwards, taking the Robot back towards the playing field. I have used power 50. However a greater power could be used when testing is complete.

Robot Soccer NXT-G Program free tutorial step 3 

Robot Soccer NXT-G Program free tutorial step 4 

The circled loop above will allow motors A and C to continue running backwards (sending the Robot backwards) until the Robot is back in the Soccer field. The circled loop does this by waiting until Light Sensor 1 sees a light value less than  63.

The code above is quite sufficient to tell our Soccer Robot to go back to the mat. However we have not turned off motors A & C. They will continue running after leaving the loop. This may confuse us when this code is inserted in to our Soccer program. It is a good precaution to add code to stop both motors A & C after leaving the loop, as shown below:-

Robot Soccer NXT-G Program free tutorial step 5

Robot Soccer NXT-G Program free tutorial step 5a

This extra step will ensure that our Soccer Robot is waiting for the next program step, without the motors A & C continuing to run.

Testing the Code

 It is important to test that this code works, before using it as part of a bigger Soccer program. To do this we download this program into our Soccer Robot, and run it.

Robot Soccer NXT-G Program free tutorial step 6 

When placed on your Robot Soccer mat, the robot should do nothing if placed inside the playing field, but should retreat backwards if placed anywhere on the white border surrounding the Soccer playing field. This video demonstrates this.

If your robot goes backwards into the Soccer field when placed on white, and does nothing when placed in other parts of the Soccer field, your code snippet has been tested and proven reliable. It can now be used as part of a Soccer playing program.

If your robot does not behave this way, they are several possible reasons.

  • Has the light changed? This could cause the light sensor reading taken from the Soccer mat to be no longer relevant. Check these readings.
  • Is your light sensor securely mounted on your robot? The difference between the "white" reading of 65 and the "light green" reading of 60 is quite small. If the light sensor on your robot bounces up and down as your robot moves, the light readings will change. It the light readings change by more than 2 or 3, your Robot may be confused and not be able find the edge of the Soccer field. It is important that the light sensors are firmly fixed on your robot and that they do not bounce up and down as your robot moves. Check your light sensor mounting.
  • Have you understood the steps above? Carefully check each of the steps above to see if you have understood the code and that your program is similar to the code above.

When everything is good, go on to the next step where we can find out how our Soccer Robot finds the ball! - Family Fun With Robotics - LEGO NXT MindStorms - NXT-G - Robot Gen II Soccer  step 4