This is a sample project which allows users to practice line following program of LEGO EV3. So let’s start now.
Step 1: Open VAT, and in Sim Basic tab, choose “Line Follower (Ev3)” sample project. Accept the default project name and click OK to close the dialog. The initial status of the project is shown below.
Step 2: The default robot model has not mounted sensors and other attachments, so I select the model and then choose “Attachments” tab item. Add left and right color sensors to the model, as shown in the below image.
Step 3: Check the port connection information by opening Object Properties dialog. We get the data that left motor and right motor are connected to port B and C respectively. Right color sensor is connected to Sensor Port 1, and left color sensor is connected to Sensor Port 3.
Step 4. Open Lego Programming software and create a new project.
Step 5. Switch back to VRT, click “Intelligent Brick” tab item to open the virtual brick. Click the center button to turn on the brick.
Step 6. At the bottom-right window of Lego Programming software, tick the connection checkbox.
Step 7. Save the project setting and finish the configuration.
Now, you could develop and debug code in LEGO programming software. For the convenience, I have written down some code for this demo project. If you have interest, you could download it as a reference.
Please note that when you download and open the project in LEGO Programming software, the connection is still valid. You do not need to reconnect them.
Step 8: click Download button in LEGO Programming software. Now the code of Test VRT project has been downloaded to the virtual brick.
Step 9: Go to VRT, find the downloaded project “Test VRT”, and then select “MoveSteer Proportional”, press center button.
The robot will run alone the black line as shown in the below video.
Explanation of the Code
The following section is a brief explanation of the LEGO programs in the downloaded “Test VRT” project. Proportional line following is used widely in LEGO EV3. We could use both Move Tank and Move Steer blocks to realize this effect.
“MoveTank Proportional” Program
The proportional line follower uses this algorithm:
At the beginning of the program, it makes the robot move forward till its right colour sensor (connect to Port 1) touches the black line.
In the loop, “error” parameter is equal to the difference between target value 24 and reflected light intensity of right colour sensor. “Turn” parameter is equal to “error” by Kp value of 0.65. “Power A” and “Power B” are equal to the base speed of 30 adding and subtracting “Turn” value, respectively.
Please note that different lighting conditions and line shape require different parameter values. Feel free to adjust the parameter values to see how they will influence the line following result.
“MoveSteer Proportional” Program
Another way to implement proportional line following is to use Move Steer block. In the following sample program, “error” parameter is calculated as the difference between the target value 30 and reflected light intensity value of right colour sensor. It then multiples a parameter 1.2 to get “Turn” parameter. This value is taken as the steering angle of the robot model.
Simple Line Follower
If you just want to implement a simple line follower, please note that VAT simulator seems having some issues of recognizing colours properly. If we use the colour mode to identify the black line, the robot model will lose the direction at all. See the following example.
To recognize the black line, we have to use Reflected Light Intensity mode of colour sensor, as shown below.
That is all for the introduction of VAT demo project. Enjoy the coding and have fun!
Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.