Skip to main content

Lesson 3: Using Sensors and the Electromagnet to Place a Disk in the Goal

Adding the Distance Sensor to the Project

In Lesson 2, the VR Robot drove in reverse back to the starting point to drop off the disk. This required the user to calculate the exact distance for the VR Robot to travel. This may not always be possible. Instead, the VR Robot can use reported sensor values to pick up and drop disks with consistent precision. 

In this Lesson, the Distance Sensor will be used to determine where the VR Robot should stop driving when returning to the goal. This sensor reports the distance from the front of the VR Robot to the nearest object using the get_distance command. To use the Distance Sensor to drive the VR Robot back to the goal the following needs to happen: 

  • The VR Robot will need to turn around 180 degrees so the Distance Sensor is facing the goal. The turn_to_heading command can be used to ensure the VR Robot turns to the correct heading.
  • The VR Robot will use the Distance Sensor with the back wall of the Playground in order to determine where to drop the disk. 
  • The VR Robot will drive to the goal. 
u8_Lesson3_FirstPuck-lookBack

For Your Information

The turn_to_heading command turns the VR Robot to a specific heading using the Gyro Sensor. The direction the VR Robot will turn (left or right) is determined based on the current heading of the Gyro Sensor. image of the turn to heading command in the workspace

The turn_to_heading command is used to direct the VR Robot to a specific heading regardless of the VR Robot’s position. As you pick up or drop a disk, the VR Robot may be knocked off course. Using the turn_to_heading command ensures that the VR Robot will turn to the absolute position rather than a position relative to the prior heading.

VR_TopDownBot

Continue building the project 

  • Continue working in the Unit8Lesson3 project.
  • Add a turn_to_heading command after the third comment. Set the parameter to 180 degrees.
    image of the turn to heading command added beneath the third comment
  • Now that the VR Robot is oriented towards the goal, the VR Robot will need to drive back to the goal. Add a while loop beneath the turn_to_heading command. image of the while loop added beneath the turn to heading command
  • To drive back to the goal, the VR Robot will use data from the Distance Sensor to determine when the VR Robot is inside the goal. As discussed in the previous lesson, each grid square on the Disk Mover Playground measures 200 millimeters (mm) by 200 millimeters (mm).u8_Lesson2_GridMeasurement_0
  • Using those measurements, the center of the goal can be determined to be approximately 200 millimeters (mm) from the wall of the Playground.
    u8_Lesson3_GoalMeasurement
  • The VR Robot should drive while the Distance Sensor reports that the VR Robot is greater than 200 millimeters (mm) from the wall of the Playground. Set the condition of the while loop to the get_distance command with the millimeters (mm) parameter. Then add the comparison operator to greater than (>) 200.
    image of the get distance command used with a comparison operator in the while loop
  • Add a drive command into this while loop. This will instruct the VR Robot to drive forward while the Distance Sensor reports a distance of greater than 200 millimeters (mm).
    image of the drivetrain command in the while loop

  • Once the VR Robot has reached the goal (200 mm from the wall), the VR Robot will need to stop driving. Add a stop command outside the while loop.
    image of the stop command outside the while loop

  • Now that the VR Robot is stopped in the goal, it can drop the disk. Add an energize command beneath the fourth comment and set the parameter to ‘DROP.’
    image of the energize command beneath the fourth comment
  • The VR Robot will need to drive in reverse a short distance to avoid knocking over or colliding with any disks. Colliding with a disk could potentially knock the VR Robot off course. The VR Robot will then need to turn so it is facing the disks once again. Add the following commands to instruct the VR Robot to drive in reverse for 100 millimeters (mm) and turn back to a heading of 0 degrees.
    image of drivetrain commands to reverse and turn to avoid colliding with disks
  • Open the Disk Mover Playground if it is not already open and run the project.

  • The VR Robot drives forward, picks up the first disk, then turns to a heading of 180 degrees to face the goal. The VR Robot then uses the values from the Distance Sensor to determine when to stop driving and drop the disk.
    u8_Lesson3_DiskInGoal

Project Flow to Drop the First Disk in the Goal

Notice how the while loop functions with the Distance Sensor in this part of the project.

  • The VR Robot will drive forward while the Distance Sensor is greater than 200 millimeters from the wall.

  • When the Distance Sensor detects that the wall of the Playground is less than 200 millimeters (mm) away, the project will move to the next command outside the while loop, and the VR Robot will stop driving.

  • The project will continue, to energize the Electromagnet to drop the disk in the goal.

  • Then the VR Robot will reverse and turn 180 degrees to be ready to face the next disk.

Select the Next button to continue with the rest of this Lesson.