<< return to Pixycam.com

How to view the line tracking algorithm source code

I want to learn your line tracking algorithm but I can’t find the location of the source code. Can you tell me which file it is in?

Hi, the Arduino source code for the line tracking algorithm is here: https://github.com/charmedlabs/pixy2/blob/master/src/host/arduino/libraries/Pixy2/Pixy2Line.h

You might also find these references useful:

https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_api
https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:protocol_reference#line-tracking-packet-reference

Hope this helps!

Well thanks for reply I find those files,but there are only some send and receive requests. I want some line tracking algorithm about Image Processing in other words can i get the pixy->sendPacket() target’s code?

Hello,
The core line tracking code is here:

I’ve looked at the code. It’s not the simplest code!

Edward

Hello Pixy ,I want to learn this line tracking algorithm but I can’t understand it by looking at the code.
Can you provide some details about the algorithm? Such as how to deal with each frame of the picture, how to judge a line.
Thank for your help~

Hello,
Happy to help. The line algorithm starts by taking a simple difference between horizontal pixels spaces 2 or 3 pixels apart and thresholding the difference. (differencing pixels across a line results in larger differences than if the difference is across a uniform section of image.) This creates a thresholded image where lines are likely – these are essentially line edges. A fairly simple algorithm then assembles the line edges into continuous lines. A final stage of the algorithm determines if there are intersections (how many and how many branches).

Hope this helps!

Edward

WOW !
Thanks lot for your words,it is very helpful and I want to ask if you judge the edge only based on green pixels? I only see g_dist g_thresh in code.

That is correct. The other pixels (red and blue) are tossed out at the acquisition phase (within the M0 core), and only the green pixels are considered.

Hello
I imitated your line_zumo_demo for my line tracking but I dont know how to deal with Right angle turn
Can I do Right angle turn by set PID?

Hello,
I’m not sure what you mean by “Right angle turn”. Do you want your robot to make a 90 degree turn to the right? Or do you want your robot to take the rightmost branch at an intersection?

Edward

Hello thanks for reply ~~
Well I mean to make a 90 degree turn follow the line. I use the line_zumo_demo but my car just ignored the 90 degrees turn.

Hello,
If you call setNextTurn(), your robot should follow the turn that you specified at the next intersection.

There is more information about setNextTurn() here:

https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_api

Edward

Hello, I use the following code to control my car but those code can not make a 90 degree turn
if (res&LINE_VECTOR)
{
// Calculate heading error with respect to m_x1, which is the far-end of the vector,
// the part of the vector we’re heading toward.
error = (int32_t)pixy.line.vectors->m_x1 - (int32_t)X_CENTER;

pixy.line.vectors->print();

// Perform PID calcs on heading error.
headingLoop.update(error);

// separate heading into left and right wheel velocities.
left = headingLoop.m_command;
right = -headingLoop.m_command;

// If vector is heading away from us (arrow pointing up), things are normal.
if (pixy.line.vectors->m_y0 > pixy.line.vectors->m_y1)
{
  // ... but slow down a little if intersection is present, so we don't miss it.
  if (pixy.line.vectors->m_flags&LINE_FLAG_INTERSECTION_PRESENT)
  {
    left += ZUMO_SLOW;
    right += ZUMO_SLOW;
  }
  else // otherwise, pedal to the metal!
  {
    left += ZUMO_FAST;
    right += ZUMO_FAST;
  }    
}
else  // If the vector is pointing down, or down-ish, we need to go backwards to follow.
{
  left -= ZUMO_SLOW;
  right -= ZUMO_SLOW;  
} 
motors.setLeftSpeed(left);
motors.setRightSpeed(right);

}

Well
Furthermore, did those code can only check black and white line ? Can I modify some code to check any color line ?

Hello,
Currently, the line tracking algorithm only looks at the green channel and will find a dark line on a light background (black (ish)) or a light line on a dark background (white (ish)). Color information from the red and blue channels is not used. (sorry)

Edward

Hello,
I adjusted the code to complete the ability to find different color lines. Can I find multiple lines at the same time by setting some parameters, or adjust them in the code? Thanks a lot~~

Hello,
Did you modify the firmware to find different color lines? If so, I’m not sure what you’re asking. I would imagine that you can also modify the code to find multiple lines by setting parameters. (right?)

Edward

Hello
Yeah,I modified the firmware to find different color lines. But I can only find 1 line at the same time ,And I want to ask how to find 2 or more lines in one image at the same time.

Hello,
I think finding more than 1 line is just a matter of building a table of line candidates and tracking them from image to image.

Edward

Hello,
Thanks for your suggest Edward ,And I meet a problem how to ignore the line I already find,if there are several black lines I can only find the left one for many times and I can not ignore the left one to find other lines
The function I choose is line_process()