Bumper Switch#
Introduction#
The Bumper Switch is a mechanical switch that can detect when it is pressed and released.
For the example belows, the configured Bumper Switch will be named bumper_1
and will be used in all subsequent examples throughout this API documentation when referring to Bumper
class methods.
Below is a list of all methods:
Getters – Check if the Bumper Switch is currently pressed.
pressing – Returns whether the Bumper Switch is being pressed.
Callback – Run code when the Bumper Switch is pressed or released.
pressed – Calls a function when the Bumper Switch is pressed.
released – Calls a function when the Bumper Switch is released.
Constructors – Manually initialize and configure the Bumper Switch.
Bumper – Create a Bumper Switch.
Getters#
pressing#
pressing
returns an integer indicating whether the Bumper Switch is currently being pressed.
1
- The Bumper Switch is being pressed.0
- The Bumper Switch is not being pressed.
Usage:
bumper_1.pressing()
Parameters |
Description |
---|---|
This method has no parameters. |
# Back up and turn if bumper switch
# is pressed
while True:
drivetrain.drive(FORWARD)
if bumper_1.pressing():
drivetrain.drive_for(REVERSE, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
Callback#
pressed#
pressed
registers a function to be called when the Bumper Switch is pressed.
Usage:
bumper_1.pressed(callback, arg)
Parameters |
Description |
---|---|
callback |
A function that is previously defined to execute when the Bumper Switch is being pressed. |
arg |
Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information. |
# Drive forward when bumper switch
# is pressed
def bumper_pressed():
drivetrain.drive_for(FORWARD,100,MM)
bumper_1.pressed(bumper_pressed)
released#
released
registers a function to be called when the Bumper Switch is released.
Usage:
bumper_1.released(callback, arg)
Parameters |
Description |
---|---|
|
A function that is previously defined to execute when the Bumper Switch is released. |
|
Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information. |
# Drive reverse when bumper switch
# is released
def bumper_released():
drivetrain.drive_for(REVERSE,100,MM)
bumper_1.released(bumper_released)
Constructors#
Constructors are used to manually create Bumper
objects, which are necessary for configuring a Bumper Switch outside of VEXcode.
Bumper Switch#
Bumper
creates a Bumper Switch.
Usage:
Bumper(port)
Parameter |
Description |
---|---|
|
Which Smart Port that the Bumper Switch is connected to as |
bumper_1 = Bumper(PORT1)
# Back up and turn if bumper switch pressed
while True:
drivetrain.drive(FORWARD)
if bumper_1.pressing():
drivetrain.drive_for(REVERSE, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)