Project 4: Explore the Field
Share
Project 4: Explore the field
Now we will make our robot move around an area while staying in bounds.
If you don't have a field with white lines, take some white tape on a dark floor or black tape on a light floor to make a region to test your robot.
Now that we have a field, its time to see how the robot senses the field.
Connect your robot, turn it on, and press sensor monitor on your computer.
Press start monitor. There should be a number under CN2. This number is what the Line sensor on your robot sees right now. The red dot under the robot is where the robot is measuring.
Bring the sensor over the field to measure the color of the field.
Bring the sensor over the boundary (line) to measure the color of the boundary.
Now calculate a good threshold between the two. For example if the field measures 10% and the line measures 30%, then a good threshold would be 20%.
If we picked a threshold of 20%, we will be saying to the robot “if the sensor is more than 20%, you see a line. Otherwise you see a field”.
Now we can close the sensor monitor and make the program.
For this program we will need an IF statement. An IF statement lets you check a sensor, and do something if a sensor is above some value, while doing something else if the sensor is below that value.
Select CN2 (which stands for channel 2 which is the line sensor on your robot). Then adjust the threshold number to the number we calculated earlier.
If your line is brighter than the field, your line color value should be higher than the field, so click the inequality to flip the if statement. (You should skip this step if you used dark tape on a bright floor, and therefore have the line color value lower than the field color value)
Your if statement should look like this now:
Putting blocks inside the if statement is exactly like putting blocks inside the infinite loop.
Now create this program to go backwards if we see a line, and go forward if we don't:
Build, upload, and test with your robot!
Hopefully the robot goes forward until it reaches a boundary, and then goes backwards forever.
If you have issues, try doing a sensor monitor again to double check your threshold value. Also try flipping the inequality around if the robot keeps going backwards in the field.
If the robot is working as intended, we still have a problem. The robot is just stuck in one part of the field and not exploring.
How do we make it explore?
Maybe instead of just going backwards when the robot sees a line, it could turn instead or in addition to going backwards. Try building a program that make the robot go around the field without going out!
Hints:
Make sure to use wait commands between motor commands to leave time for the motors to move! If you don't, the next motor command will just override the previous one and the robot wont do the first motor command.
Try modifying the go backwards motor command in the above program to turn with a command of 50% -50%. Does that work? Are there any flaws or times this doesn't work?