In the real scenario, the robot also needs to square up with a line. Unlike squaring up with wall, there is no physical presenting of the line. The robot needs to use color sensors to recognize the line and then think out ways to align with it. Learning how to square up with a line could help the robot navigate accurately and reduce the errors in the competition environment.
The following video illustrates the process of squaring up with the line.
Squaring Up Process
Aligning with a line requires at least two color sensors. In LEGO MindStorms 45544 education set, there is one color sensor by default. Therefore, you might need to search the online market to buy another color sensor. The two color sensors should be put symmetrically on the front sides of the vehicle.
The aligning up process can be divided in two steps.
Step 1: the vehicle moves forward with both the left and right motors turning on. when any one of the color sensor identifies red line, stop the motor on that side.
Step 2: The motor on the other side continues turning, so the vehicle actually pivots around the first motor until the 2nd color sensor also identifies red color. The vehicle stops.
There are more than one way to realize this functionality. In this post, I will choose two methods. Method 1 uses Large Motor block to control the two motors separately. Method 2 use MoveTank block to control both motor together. The code structure looks a bit different.
Method 1 – Use Big Motor Block
In the model I am using, the left motor connects to the Port B of the smart hub, while the right motor connects to the Port D. I use parallel beam to make both motors run simultaneously. However, one limitation of parallel beam is that they could not synchronize the finishing time.
If aligning up is the last step in the whole program, that is fine. The program will stop when both code beams finish running. However, if the program continues doing other actions, e.g. making the robot move forward, there will be an issue. The program requires the robot to move forward only when both color sensors identify red line and both motors stop. However, the moving forward action could only connect to one beam, and it will not know if the other beam finishes running or not. Under this condition, the robot might move forward without waiting for squaring up to finish. The final direction is wrong.
There are several ways to solve this issue. One way is to set a variable for the 2nd code beam. Meanwhile, at the end of the 1st beam, attach a loop block to check the status of the variable. The updated code is shown below.
Variable “right_motor_status” is set to False at the beginning of code beam 2. Once the right side motor stops running, this variable changes to True. At the end of the 1st beam, the program repeats checking the value of variable “right_motor_status” until it changes to True. By this way, the program could ensure that both motors have stopped.
Improve Squaring Up Accuracy
You might notice that the robot isn’t quite straight (aligned). This problem becomes more apparent when the vehicle drives toward the red line with a small angle.
In the following video, after finishing Step 2, we could notice that the right color sensor actually has fallen out of the other side of red line and the vehicle is a bit too tiled than expected.
What the program could improve is to add another adjustment block. After Step 2, the vehicle will instead pivot around the right wheel and then reverse the left motor until the left color sensor identifies the red line again. The final status is shown in Step 3.
If the vehicle drives toward the red line from the right side, the adjustment is a similar process, as shown below.
The real adjustment effect could be like this:
How to Implement?
Method 1 uses Large Motor block to control two big motors independently. Although we use variable status to ensure that both motors stop running before doing the following actions, that method could not identify which side of sensor identifies the red line first. However, we need to know that to control which motor should turn in reverse in Step 3. So here I would like to use MoveTank method to work around parallel beam and use Wait block to identify which color sensor identifies red line first. The overall process could be decomposed into three steps:
Step 1: Make the vehicle move straight until one of the color sensor identifies the red color, then stop the motors.
Step 2. Continue turning the motor on the side where color sensor has not identifies the red line.
Step 3. Make minor adjustment to ensure that both color sensors identify the red line.
In the above code segment of Step 1, the program uses MoveTank method to move vehicle straight forward, then uses a loop to continue checking the two color sensors. If either of them identifies red color, stop the loop.
In this code segment, Step 2 and Step 3 are implemented together. In the first branch of Switch block, the program checks if the left color sensor (Port 3) identifies the red color. If it does, it moves the right motor forward until the red color sensor (Port 1) identifies red line. After right motor stops, the program will instead check if the left color sensor (Port 3) still identifies the red color. If it does not, it reverses moving left motor until the left sensor identifies the red line again.
The same rule applies to the second branch, in which the right sensor identifies red line first.
That is all for the explanation of squaring up process and how to improve its accuracy. Could you think out other ways to improve squaring up result? Feel free to share your method in the below comment section.
And do not forget to 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.