Skip to main content

Lesson 3: Using Sensors and the Electromagnet to Pick up a Disk

Using the Down Eye Sensor with the Electromagnet

This Lesson will use reported sensor feedback to solve the same challenge as Lesson 2. The VR Robot will pick up and move the first blue disk into the blue goal.

  • Start a new project and select the Disk Mover Playground when prompted. Disk Mover Playground selection tile, reads Disk Mover on the bottom.
  • Name the project Unit8Lesson3.VEXcode VR Toolbar with the project name box highlighted. The project name reads Unit 8 Lesson 3.

     

  • Add four comments to the project. Add comments for each of the steps necessary for the VR Robot to drive to the first disk, pick it up, and return it to the blue goal.

    def main():
        # 1. Drive to the first disk
    
        # 2. Pick up the first disk
    
        # 3. Drive to the goal
    
        # 4. Drop the disk in the goal

     

  • Drag or type a while loop with a not condition into the workspace beneath the first comment, so your project looks like this:

    def main():
        #1. Drive to the first disk
        while not condition:
            wait(5, MSEC)
    
        # 2. Pick up the first disk
    
        # 3. Drive to the goal
    
        # 4. Drop the disk in the goal
  • The down_eye.near_object command reports True when the Down Eye Sensor on the VR Robot detects a colored disk on the Disk Mover Playground. The disks are on the floor of the Playground and cannot be detected by the Front Eye Sensor, therefore the Down Eye Sensor will detect the disks. Drag or type the down_eye.near_object command as the condition of the while loop, so the first section of your project looks like this:

    def main():
        #1. Drive to the first disk
        while not down_eye.near_object():
            wait(5, MSEC)
  • The commands inside the while loop will run when the VR Robot does NOT detect an object. To reach one of the disks, the VR Robot will need to drive forward until the disk is detected.

    Rear view of the Disk Mover Playground, showing the VR Robot in the start location, and the three blue disks in a row stretched the length of the Playground in front of the robot.
  • Drag or type a drive command inside the while loop, so the first comment section of the project looks like this:

    	#1. Drive to the first disk
        while not down_eye.near_object():
            drivetrain.drive(FORWARD)
            wait(5, MSEC)
  • When a disk is detected, the VR Robot will need to stop driving in order to pick it up. Drag or type a stop command outside the while loop, so the first comment section of the project looks like this:

    	#1. Drive to the first disk
        while not down_eye.near_object():
            drivetrain.drive(FORWARD)
            wait(5, MSEC)
        drivetrain.stop()
  • Notice that the drive and wait commands are indented under the while loop header, meaning the commands are inside the loop. The gray line can also help indicate which commands are properly indented and inside a loop. The stop command is in line with the loop header, meaning the stop command is outside the loop. Ensure the commands are correctly indented, or the project may not run as expected. 
  • Now, instruct the VR Robot to pick up the disk with the Electromagnet by dragging or typing an energize command after the second comment, so the second section of the project looks like this:

    	# 2. Pick up the first disk
        magnet.energize(BOOST)
  • Open the Playground Window if it is not already open. Be sure the Disk Mover Playground opens, and run the project.
  • The VR Robot drives forward until the first blue disk is detected by the Down Eye Sensor, stops driving, and energizes the Electromagnet to pick up the disk.

    A side view of the VR Robot on the Playground at the first blue disk location, with the Disk attached to the Electromagnet.

Project Flow to Pick up the First Disk

Notice how the while loop with a not condition uses sensor feedback to successfully pick up the first disk:

  • The VR Robot drives forward while the Down Eye Sensor does not detect an object.
  • Once the Down Eye Sensor on the VR Robot detects an object, the project moves to the next command outside the while loop and stops driving.
  • The project continues, so the VR Robot energizes the Electromagnet to pick up the disk.
A project flow diagram of the project above, illustrating the flow with the while loop. The first two sections of the project are shown to the left, and to the right, the first section has a red cyclical arrow illustrating that while the down eye does not detect an object, the robot will drive forward, as the condition reports as false. Next a downward green arrow beside the stop command indicates that when the down eye does detect an object, the project will exit the loop and the robot will stop. Next a second downward green arrow aligns with the second section of the project, indicating that the electromagnet will then be energized.

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