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. 
A perspective view of the VR Robot on the Disk Mover Playground at the location of the first blue disk, with the disk attached to the electromagnet. The blue goal is behind the robot.

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. Turn to heading VEXcode VR Python command, reads drivetrain dot turn underscore to underscore heading, with 90, and Degrees in parentheses.

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.

A top down view of the VR Robot in the center of a circle illustrating the headings of the robot. At the 12 o'clock position, aligned with the front of the robot, is 0 degrees. The heading run clockwise around the circle, with 90 degrees in the 3 o'clock position, 180 degrees in the 6 o'clock position, and 270 degrees in the 9 o'clock position.

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, so the project now looks like this: 

    def main():
        #1. Drive to the first disk
        while not down_eye.near_object():
            drivetrain.drive(FORWARD)
            wait(5, MSEC)
        drivetrain.stop()
    
        # 2. Pick up the first disk
        magnet.energize(BOOST)
    
        # 3. Drive to the goal
        drivetrain.turn_to_heading(180, DEGREES)
    
        # 4. Drop the disk in the goal
  • 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, so the third section of the project looks like this: 

    	# 3. Drive to the goal
        drivetrain.turn_to_heading(180, DEGREES)
        while condition:
            wait(5, MSEC)
  • 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).A close up view of the lower left corner of the playground, showing that each square of the 10 by 10 grid on the floor of the playground measure 200mm by 200mm.
  • Using those measurements, the center of the goal can be determined to be approximately 200 millimeters (mm) from the wall of the Playground.

    A close up view of the lower left corner of the Disk Mover Playground. The VR Robot has a blue disk attached to the electromagnet and is facing the blue goal. A measurement from the rear wall of the playground to the center of the blue goal is labeled 200 millimeters.
  • 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. The third section of the project should now look like this: 

    	# 3. Drive to the goal
        drivetrain.turn_to_heading(180, DEGREES)
        while front_distance.get_distance(MM) > 200:
            wait(5, MSEC)
  • Add a drive command into this while loop, so the third section of the project looks like this. This will instruct the VR Robot to drive forward while the Distance Sensor reports a distance of greater than 200 millimeters (mm).

    	# 3. Drive to the goal
        drivetrain.turn_to_heading(180, DEGREES)
        while front_distance.get_distance(MM) > 200:
            drivetrain.drive(FORWARD)
            wait(5, MSEC)
  • 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, so the third section of the project looks like this:

    	# 3. Drive to the goal
        drivetrain.turn_to_heading(180, DEGREES)
        while front_distance.get_distance(MM) > 200:
            drivetrain.drive(FORWARD)
            wait(5, MSEC)
        drivetrain.stop()
  • 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.’ The fourth section of the project should now look like this:

    	# 4. Drop the disk in the goal
        magnet.energize(DROP)
  • 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 after the final energize command as shown, to instruct the VR Robot to drive in reverse for 100 millimeters (mm) and turn back to a heading of 0 degrees. Be sure the commands are indented correctly in your project.

    	# 4. Drop the disk in the goal
        magnet.energize(DROP)
    
    	drivetrain.drive_for(REVERSE, 100, MM)
        drivetrain.turn_to_heading(0, DEGREES)
  • 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.

    A side view of the blue goal of the Disk Mover Playground, with the first blue disk dropped in the goal, and the VR Robot is turned and positioned at the front of the goal, ready to collect the next disk.

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.