0

Line Robot - Basic lessons - Lidar

Lidar is a sensor that can measure distance precisely using infrared (not visible) laser. If You made the robot according to the instructions, this program will display distances, measured by the left sensor on the front side of the robot:
void RobotLine::loop() {
	print("%i mm\n\r", frontLeft());
	delayMs(200);
}
First command is print() which has a very unfortunate format that saves precious microcontroller memory, but strains the programmer. We will have to learn this syntax. Here is just a simple example, displaying an integer. This part of the instruction we know already:
print(
The next one is the trickiest:
"%i mm\n\r"
We read it like this: leave a placeholder for an integer ("%i"), print the next few characters (" mm"), jump to (beginning of the) next line ("\n\r"). Now the end:
, frontLeft());
After a comma, a function that returns lidar's distance (an integer) follows. That will be the integer that will replace the placeholder. Let's say that the lidar measured 107 mm. 107 will be the number that
frontLeft()
returns. Therefore, our print command will look like:
print("%i mm\n\r", 107);
As "%i" is replaced by 107, the displayed result will be:
107 mm

Task: stop the robot before obstacle.

Let the robot go straight ahead. When an obstacle emerges, that is less than 100 mm ahead, stop the robot.