Bumper#
Introduction#
The Bumper is a mechanical switch that completes a circuit when pressed, allowing the robot to detect presses and releases.
Below is a list of all available methods:
Getters – Check if the Bumper Sensor is currently pressed.
pressed– Returns whether the Bumper Sensor is being pressed.
Callback – Run code when the bumper is pressed or released.
on_pressed– Registers a function to be called when the Bumper Sensor is pressed.on_released– Registers a function to be called when the Bumper Sensor is released.
Getters#
pressed#
pressed returns a Boolean indicating whether the Bumper Switch is currently being pressed. This can be used to check if the robot bumps into other objects.
True– The Bumper Switch is being pressed.False– The Bumper Switch is not being pressed.
Usage:
bumper.pressed()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Stop when the Left Bumper is pressed
drivetrain.drive(FORWARD)
while True:
if left_bumper.pressed():
drivetrain.stop()
wait(5, MSEC)
# VR threads — Do not delete
vr_thread(main)
Callback#
on_pressed#
on_pressed registers a function to be called when the Bumper Sensor is pressed.
Usage:
bumper.on_pressed(callback)
Parameters |
Description |
|---|---|
|
A function that is previously defined to execute when the Bumper Sensor is being pressed. |
def bumper_pressed():
# Reverse when the bumper is pressed
drivetrain.drive_for(REVERSE, 100, MM)
drivetrain.stop()
def main():
# Drive forward until the left bumper is pressed
drivetrain.drive(FORWARD)
left_bumper.on_pressed(bumper_pressed)
# VR threads — Do not delete
vr_thread(main)
on_released#
on_released registers a function to be called when the Bumper Sensor is released.
Usage:
bumper.on_released(callback)
Parameters |
Description |
|---|---|
|
A function that is previously defined to execute when the Bumper Sensor is being pressed. |
def bumper_released():
# Display a message
brain.print("Bumper released!")
def main():
# Hit and back away from an object
left_bumper.on_released(bumper_released)
drivetrain.drive_for(FORWARD, 600, MM)
drivetrain.drive_for(REVERSE, 100, MM)
# VR threads — Do not delete
vr_thread(main)