PART B: Line Tracking Using A Camera Module

(...continued from this tutorial)

After getting the Mid_Pos, steering the robot will be easy.  In this tutorial we are going to use the FI40 to steer our robot.  However we will not be going through the details of programming the FI40 as this is done on a separate tutorial “Differential drive for a general purpose Bot”.  You can attach VBOT.vbC file to the project in the IDE to access the library.

The following code is self explanatory.


IF Mid_Pos > 0 THEN
    IF Mid_Pos > 90 THEN
        VBOT.LEFT() 'MOVE RIGHT
    ELSEIF Mid_Pos < 70 THEN
        VBOT.RIGHT() 'MOVE LEFT
    ELSE
        VBOT.FORW() 'MOVE FOR
    END IF                
END IF          

 

 
Using the algorithm to track a black line, we need to make a few assumptions.
Since the camera view is relatively small (about the width of the robot itself), we assume that at any time there is only one line.

 

One Line in Camera FrameAssume only 1 line in camera frame

 

This really simplifies our task.  The next task we would need to do is to look for a cross junction.  Again we must make a few assumptions about how a cross junction will look like.  Essentially, the cross junction should be made up of 2 fairly straight lines.

 

cross junction in camera framecross junction in camera frame

The above example shows how a good cross junction should look like.  Now we need an algorithm to determine if there is a cross section.

In Part A, we were looking for a line from y = 60 to y = 100.  Basically we are tracking the line at the lower part of the screen. We can now look for the cross junction at the top part of the screen ie y=0 to y = 55.

Cross Junction Layout

 

A simple way is to look for a black line at x = 30 and at x = 130.  If we find black pixels at both regions, we can calculate the mid point and check if that point is black.

 

Cross Junction

 

The code should look something like this.

CL = -1
PTR = 30
FOR I = 0 TO 50
    IF Image(PTR) = 0 THEN
        CL = I+1
        EXIT FOR        
    END IF
    PTR = PTR + 160
NEXT
CR = -1
PTR = 130
FOR I = 0 TO 50
    IF Image(PTR) = 0 THEN
        CR = I+1
        EXIT FOR        
    END IF
    PTR = PTR + 160
NEXT            
IF (CL >= 0) AND (CR >= 0) THEN
    CM = (CL + CR) \ 2
    IF Image(80 + 160*CM) > 0 THEN
        CM = -1
    END IF

    IF CM >= 0 THEN
        OLED.Foreground.Colour = Const.Colour.Aqua  
        P1.X = 30
        P1.Y = CL
        P2.X = 80
        P2.Y = CM
        OLED.Draw.Line(P1,P2)
        P1.X = 130
        P1.Y = CR
        OLED.Draw.Line(P1,P2)
        OLED.Foreground.Colour = Const.Colour.White
    END IF
END IF
DEBUG.Print " > ";CSTR(CL);", ";CSTR(CM);", ";CSTR(CR)

Note:
CL is the left cross line position
CM is the mid cross line position
CR is the right cross line position

As the code is getting longer (about 140 lines), I will not be displaying all the code in this document.  You can download the full code at www.aiscube.com resource page.  The project is zipped up in a file called “CAM – Cross Junction”.

It is obvious by now that the algorithm is only looking for a horizontal line.  If we find this horizontal line we will need to figure out if we are facing with a cross junction or a T-junction.

Detecting a T Junction  or Detecting a T Junction

From the illustration above, it is obvious that we could easily look for a line above the horizontal line that we obtained.  In order to do this, we should allow the bot to move until the horizontal line is about 30 < y < 40 (that is the CM value must be in between 30 to 40).  We can now use the same technique in Part I to look for the vertical line.  If the vertical line exists, then it’s a cross junction.  If it does not exist then it’s a T-junction.

 

Download the full source code for the BlazingCore controlling the OLED and CMOS Camera here.

Download the full source code for the FlamingICE controlling the tracked robot here.


Good luck and happy coding.