Lesson: Exercise: Using a Touch Sensor

Overview: 
Explore using the Touch Sensor device to gather information about the robot's environment.
Objectives: 

Understand how to use the Touch  Sensor to gather information about the robot's environment.

Content: 

Lets look a using sensors by starting with the simple touch sensor. This is simply a button that can be used to signal your code that something has happened by pushing the button. We will add a Modern Robotics Touch Sensor to the robot and plug it into digital port 0 on the Device Interface Module. We modify our controller phone configuration to identify digital port 0 as a touch sensor and name it touch_sensor. We will take the earlier DriveCircle exercise and modify it to use the touch sensor to determine when to stop driving:

Copy and paste this code into a new OpMode class called DriveCircleTouch and demonstrate your robot driving in a circle. It should keep driving until you press the touch sensor button or 5 seconds pass. Note the use of the while loop to run the robot while watching for 5 seconds to pass and also watching for the touch sensor button press. The TouchSensor isPressed() function returns false when not pressed and true when pressed. For us to continue to run while not pressed, we invert the result of the isPressed() function by preceeding it with the ! (invert) operator.

When using the REV Robotics touch sensor, there is no specific touch sensor class. The sensor is treated as a simple digital (input) device. Here is the above code sample modified to use the REV sensor:

Connecting the touch sensor or other digital device to the REV Hub has some nuances. Be sure to read the discussion here.

 

Navigation: