From 9b8ad8006b2003191e7e713904e02c14743f6933 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 5 Jun 2025 18:35:19 +0100 Subject: [PATCH 1/3] Add documentation to hopefully avoid subclassing the robot --- programming/robot_api/index.md | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/programming/robot_api/index.md b/programming/robot_api/index.md index 6171ccb2..f7c6e50c 100644 --- a/programming/robot_api/index.md +++ b/programming/robot_api/index.md @@ -196,3 +196,70 @@ 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. + +
+Attempting to subclass the `Robot` class directly will raise an exception. + +~~~~~ python +from sr.robot3 import Robot + +# This is not supported nor recommended. +class MyRobot(Robot): + ... +~~~~~ + +Subclassing like this can interfere with the general operating of the `Robot` object. Instead, we recommend using the examples above to wrap the existing `Robot` object. + +
From cf7684d7d960f331bc0acaa781a17df9b5554f19 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 9 Sep 2025 22:00:56 +0100 Subject: [PATCH 2/3] Consistent indent width --- programming/robot_api/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programming/robot_api/index.md b/programming/robot_api/index.md index f7c6e50c..6eede00a 100644 --- a/programming/robot_api/index.md +++ b/programming/robot_api/index.md @@ -257,7 +257,7 @@ from sr.robot3 import Robot # This is not supported nor recommended. class MyRobot(Robot): - ... + ... ~~~~~ Subclassing like this can interfere with the general operating of the `Robot` object. Instead, we recommend using the examples above to wrap the existing `Robot` object. From aa7a68204e3eafb84d0c51a4b21fe25fe78720b9 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 9 Sep 2025 22:02:35 +0100 Subject: [PATCH 3/3] Remove explanation of why subclassing is a bad idea --- programming/robot_api/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programming/robot_api/index.md b/programming/robot_api/index.md index 6eede00a..ef022604 100644 --- a/programming/robot_api/index.md +++ b/programming/robot_api/index.md @@ -260,6 +260,6 @@ class MyRobot(Robot): ... ~~~~~ -Subclassing like this can interfere with the general operating of the `Robot` object. Instead, we recommend using the examples above to wrap the existing `Robot` object. +Instead, we recommend using the examples above to wrap the existing `Robot` object.