Skip to content

Add documentation to hopefully avoid subclassing the robot #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions programming/robot_api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,67 @@ RobotMode.COMP.value == "COMP" # True
~~~~~

In general, the enum should be used and compared directly, rather than using its inner value.

## Creating your own helpers

The provided `Robot` object provides access to the boards provided with the kit. However, you may feel the urge to extend it to add your own methods to make development clearer.. However, this can introduce issues if the methods or properties you add interfere with the inner workings of the `Robot` object.

Instead, you can wrap the `Robot` object with your own class:

~~~~~ python
from sr.robot3 import Robot
import time

class MyRobot:
def __init__(self):
self.robot = Robot()

def drive_forwards(self, seconds):
"""
Drive forwards for a given number of seconds
"""
robot.motor_boards["srABC1"].motors[0].power = 0.5
robot.motor_boards["srABC1"].motors[1].power = 0.5
time.sleep(seconds)
robot.motor_boards["srABC1"].motors[0].power = 0
robot.motor_boards["srABC1"].motors[1].power = 0

# Now, use your class instead
robot = MyRobot()
robot.drive_forwards(3)
~~~~~

This is not the only way to design your own robot API. You may instead want to define helper functions:

~~~~~ python
from sr.robot3 import Robot
import time

def drive_forwards(robot, seconds):
"""
Drive forwards for a given number of seconds
"""
robot.motor_boards["srABC1"].motors[0].power = 0.5
robot.motor_boards["srABC1"].motors[1].power = 0.5
time.sleep(seconds)
robot.motor_boards["srABC1"].motors[0].power = 0
robot.motor_boards["srABC1"].motors[1].power = 0

# Now, use your class instead
robot = Robot()
drive_forwards(robot, 3)
~~~~~

Both of these approaches are equally valid. Choosing which you want to use will likely come down to your own preferences.

<div class="warning" markdown="1">
Attempting to subclass the `Robot` class directly will raise an exception.

~~~~~ python
from sr.robot3 import Robot

# This is not supported
class MyRobot(Robot):
...
~~~~~
</div>