
One of the challenges with the Robocup Junior Australia Line Rescue challenge is handling when the tile colours are reversed, that is a white line on a black background. This tile is called “Night Drive”.
There is also a tile which has a wider black line but is still on a white background. This tile is called “Wide Road” but is probably only used for the Western Australian competition.
This article explains techniques that can be used to update the line following algorithm for a two-sensor line following robot to handle these two tiles.
The starting point for this example is using a simple proportional line follower using the difference between the two sensors multiplied by a turn factor (value between 1 and 3 recommended). This is our “Steering” value that we can use for movement. Normally, this can be all in a single line of code, but we are separating this into two lines using a “Steering” variable to facilitate the changes we need to make.
Proportional Line Following
[code language=”vb”]
Set Steering = (Sensor A – Sensor B) * TurnFactor
Start Moving (Steering)
[/code]
For the line follower to work in reverse on a black background, all you need to do is swap the sign of the Steering value when it is in Night Drive mode. We can use a variable “Night” with a value of 0 or 1 to identify if we are in Night Drive mode.
Proportional Line Following with Night Drive mode
[code language=”vb”]
Set Steering = (Sensor A – Sensor B) * TurnFactor
If (Night = 1) Then
Set Steering = Steering * -1
End If
Start Moving (Steering)
[/code]
To detect if we are on a Night Drive tile and activate Night Drive mode, we need to check if the sensors read double black. However, we need to ensure that we see double black continuously while the motors rotate more than the number of degrees that it takes to cross a standard black line, as seen on the gridlock tile. You can test how many degrees your motors need to rotate to get past a crossroads.
We can track how far we have travelled using the Motor Relative Position (under More Motors in Lego Spike Prime). When we first see double black, we can store the Motor Relative Position of one of our drive motors as a variable of our start position, say “DblBlkPos” for Double Black Position. Once we have stored our start position, we can compare the current Motor Relative Position minus the start position to see how many degrees we have moved.
To ensure that we have continuously seen double black during this check, we will reset the start position variable if the sensors don’t see double back. Once we have determined we have seen double black for the specified number of degrees (40 in this example), we can turn Night Drive mode on. It also makes sense to add a beep sound to signify that Night Drive mode is enabled.
The next piece of the puzzle is knowing when to turn Night Drive mode off. This is fairly simple as while Night Drive mode is enabled, we can just check for double white. Once double white is seen, we can turn off Night Drive mode, reset the Double Black Position variable and play a beep to signify that Night Drive mode has been disabled.
Note: This method will only work reliably when the colour sensors are a sensor width (3 pegs) apart to ensure that the sensors are either side of the line.
Night Drive Detection
[code language=”vb”]
If (Night = 0) Then
If (Sensor A = Black And Sensor B = Black) Then
If (DblBlkPos = 0) Then
Set DblBlkPos = Motor Relative Position C
End If
If ((Motor Relative Position C – DblBlkPos) > 40) Then
Set Night = 1
Beep
End If
Else
DblBlkPos = 0
End If
Else
If (Sensor A = White And Sensor B = White) Then
Set Night = 0
Set DblBlkPos = 0
Beep
End If
End If
[/code]
There you have it. A line follower that can handle being reversed for Night Drive and a method of detecting when entering and existing the Night Drive tile.
Now, we can complicate this a bit more by adding the “Wide Road” tile.
The “Wide Road” tile is still a black line on a white background, so it does not actually need any changes to the standard proportional line following algorithm to work.
However, as the sensors will see double black for a longer period than crossing a black line, the Night Drive mode that we previously added will be activated and the line following will fail.
The question now is how can we tell the difference between the “Night Drive” tile and the “Wide Road” tile and prevent Night Drive mode being enabled for the “Wide Road” tile?
We are going to add a “Wide” variable to signify when we are in Wide Road mode and add that to our code.
The trick to identifying if you are on a “Night Drive” or “Wide Road” tile is to stop and rotate the robot to the side. If the outside colour sensor sees white, you are on a “Wide Road”, and if the inside colour sensor sees white”, you are on a “Night Drive”. You will need to straighten the robot after detecting the type of tile. I would also recommend backing up a little before checking to avoid any turns that might be on a “Night Drive” tile. In the 300mm tile sets there is a reversed tile with turns.
We can use the Relative Motor Position of one of the motors to reverse the sideways turn by resetting the value before turning and then returning to zero afterwards.
Night Drive and Wide Road Detection
[code language=”vb”]
If (Night = 0 And Wide = 0) Then
If (Sensor A = Black And Sensor B = Black) Then
If (DblBlkPos = 0) Then
Set DblBlkPos = Motor Relative Position C
End If
If ((Motor Relative Position C – DblBlkPos) > 40) Then
Stop Moving
Move Backwards (30 degrees)
Set Relative Motor Position D to 0
Start Moving (Left -100)
Wait Until (Sensor A = White Or Sensor B = White)
Stop Moving
If (Sensor A = White) Then
Set Wide = 1
Beep
End If
If (Sensor B = White) Then
Set Night = 1
Beep
End If
Start Moving (Right 100)
Wait Until (Relative Motor Position D < 0)
Stop Moving
End If
Else
DblBlkPos = 0
End If
Else
If (Sensor A = White And Sensor B = White) Then
Set Night = 0
Set Wide = 0
Set DblBlkPos = 0
Beep
End If
End If
[/code]
With this additional check, the robot will now only enter Night Drive mode once it has confirmed it is on the “Night Drive” tile and not on the “Wide Road” tile.
More Information
For more information on robotics and the EV3 Basic extensions to Microsoft Small Basic, check out the following links:
Hope you find these techniques useful.
David
This article was originally posted on http://www.winthropdc.com/blog.

