Zumo 32U4 line following black line on white surface

Hello team,

Please can someone help me.

I have a Zumo32U4 robot… I am trying to write code to so that the robot follow the black line on a white surface. As I am new to it, I wanted to get some guidance.

Hello.

The LineFollower example program in our Zumo 32U4 library was designed to make a Zumo follow a black line on a white surface, so you might check that out to get started.

- Patrick

Hello Patrick,

Thanks a lot. I tested the example and it is working.

I wanted to stop robot when the line finishes.

I tried the following condition , however, it does not seem to work as expected

if ((lineSensors.readLine(0)< 2000) && (lineSensors.readLine(1)!= 2000) && (lineSensors.readLine(2)> 2000) )
{
motors.setSpeeds(0,0);
}

Please can you inform what condition can i put to stop the robot when the line finishes.

Best regards,
Kailash

You are not using the readLine function correctly since you are not giving it any sensor value variables to update, and this is just not a good application for that function. The return value of readLine is a number that represents the estimated position of the line, so it is not a useful tool for establishing whether there is a line because it tries to keep an estimate of which direction the line is if it does lose the line.

Instead, you can evaluate whether there is a line or not by looking at the individual sensor readings stored in the lineSensorValues array. If you want to turn off the motors when all the sensors read detect white, then you might want to use a condition that looks something like this:

int16_t threshold = 500;
if ((lineSensorValues[0] < threshold) && (lineSensorValues[1] < threshold) && (lineSensorValues[2] < threshold) &&(lineSensorValues[3] < threshold) && (lineSensorValues[4] < threshold))
{
  motors.setSpeeds(0,0);
}

In case you are not putting your condition there already, you would add this after all the other code in the main loop of our lineFollower example. Keep in mind that lineSensorValues is updated whenever readLine is called, so you do not need to separately update lineSensorValues if you use readLine first.

You might also consider checking out the code for our MazeSolver example which allows the Zumo to detect the end of lines and intersections.

- Patrick

Hello Patrick,

Thanks a lot for the info.

I tried to test your suggestion and put the suggested code in the main loop for line follower code. However, it is not working as expected. The robot is hindered inch by inch during the movement. Please see below where i put the suggested code:

void loop()
{
// Get the position of the line.
int16_t position = lineSensors.readLine(lineSensorValues);

// Our “error” is how far we are away from the center of the
// line, which corresponds to position 2000.
int16_t error = position - 2000;

// Get motor speed difference using proportional and derivative
int16_t speedDifference = error / 4 + 6 * (error - lastError);

lastError = error;

// Get individual motor speeds. The sign of speedDifference
// determines if the robot turns left or right.
int16_t leftSpeed = (int16_t)maxSpeed + speedDifference;
int16_t rightSpeed = (int16_t)maxSpeed - speedDifference;

// Constrain our motor speeds to be between 0 and maxSpeed.
// One motor will always be turning at maxSpeed, and the other
// will be at maxSpeed-|speedDifference| if that is positive,
// else it will be stationary.
leftSpeed = constrain(leftSpeed, 0, (int16_t)maxSpeed);
rightSpeed = constrain(rightSpeed, 0, (int16_t)maxSpeed);

motors.setSpeeds(leftSpeed, rightSpeed);

if ((lineSensorValues[0] < threshold) && (lineSensorValues[1] < threshold) && (lineSensorValues[2] < threshold) &&(lineSensorValues[3] < threshold) && (lineSensorValues[4] < threshold))
{
motors.setSpeeds(0,0);
}

}

You probably need to adjust your threshold value. You might consider making a program that prints the sensor readings to Arduino’s Serial Monitor or the Zumo LCD so that you can move your robot over a line and see what the sensors read in different conditions. Then you will have more information to decide how to control your robot’s behavior.

By the way, that program as written will make it so that as soon as your robot stops, it will resume trying to follow the line (which might be the source of the intermittent movement you see now). If you want to make it so that once your Zumo stops you have to, for example, press the A button before it does anything else, you could add buttonA.waitForButton(); after the motors.setSpeeds(0,0); line in your if condition.

- Patrick