From 1e12408a7defa48b330dd706a7cdd176061d2698 Mon Sep 17 00:00:00 2001 From: sciencewhiz Date: Thu, 28 May 2026 20:09:07 -0700 Subject: [PATCH] [examples] add motor safety and invert to motor snip (#8924) --- robotpyExamples/MotorControl/robot.py | 13 +++++++++++++ .../main/cpp/snippets/MotorControl/cpp/Robot.cpp | 13 +++++++++++++ .../org/wpilib/snippets/motorcontrol/Robot.java | 13 +++++++++++++ 3 files changed, 39 insertions(+) diff --git a/robotpyExamples/MotorControl/robot.py b/robotpyExamples/MotorControl/robot.py index c8c23d73f9..b483ea64bf 100755 --- a/robotpyExamples/MotorControl/robot.py +++ b/robotpyExamples/MotorControl/robot.py @@ -20,6 +20,10 @@ class MyRobot(wpilib.TimedRobot): In addition, the encoder value of an encoder connected to ports 0 and 1 is consistently sent to the Dashboard. + + Finally, short code snippets show how to invert the motor direction and how to use the motor + safety for frc-docs. + https://docs.wpilib.org/en/stable/docs/software/hardware-apis/motors/wpi-drive-classes.html """ kMotorPort = 0 @@ -38,6 +42,15 @@ class MyRobot(wpilib.TimedRobot): # This is set up assuming a 6 inch wheel with a 360 CPR encoder. self.encoder.setDistancePerPulse((math.pi * 6) / 360.0) + # show motor inversion + self.motor.setInverted(True) + + # show motor safety features + self.motor.setSafetyEnabled(True) + self.motor.setSafetyEnabled(False) + self.motor.setExpiration(0.1) + self.motor.feed() + def robotPeriodic(self): """The RobotPeriodic function is called every control packet no matter the robot mode.""" wpilib.SmartDashboard.putNumber("Encoder", self.encoder.getDistance()) diff --git a/wpilibcExamples/src/main/cpp/snippets/MotorControl/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/MotorControl/cpp/Robot.cpp index 960150779e..bb1112d17b 100644 --- a/wpilibcExamples/src/main/cpp/snippets/MotorControl/cpp/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/snippets/MotorControl/cpp/Robot.cpp @@ -20,6 +20,10 @@ * * In addition, the encoder value of an encoder connected to ports 0 and 1 is * consistently sent to the Dashboard. + * + * Finally, short code snippets show how to invert the motor direction and how + * to use the motor safety for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/motors/wpi-drive-classes.html */ class Robot : public wpi::TimedRobot { public: @@ -37,6 +41,15 @@ class Robot : public wpi::TimedRobot { // Use SetDistancePerPulse to set the multiplier for GetDistance // This is set up assuming a 6 inch wheel with a 360 CPR encoder. encoder.SetDistancePerPulse((std::numbers::pi * 6) / 360.0); + + // show motor inversion + motor.SetInverted(true); + + // show motor safety features + motor.SetSafetyEnabled(true); + motor.SetSafetyEnabled(false); + motor.SetExpiration(0.1_s); + motor.Feed(); } private: diff --git a/wpilibjExamples/src/main/java/org/wpilib/snippets/motorcontrol/Robot.java b/wpilibjExamples/src/main/java/org/wpilib/snippets/motorcontrol/Robot.java index 92b9c9d51d..989c25fc4c 100644 --- a/wpilibjExamples/src/main/java/org/wpilib/snippets/motorcontrol/Robot.java +++ b/wpilibjExamples/src/main/java/org/wpilib/snippets/motorcontrol/Robot.java @@ -19,6 +19,10 @@ import org.wpilib.smartdashboard.SmartDashboard; * *

In addition, the encoder value of an encoder connected to ports 0 and 1 is consistently sent * to the Dashboard. + * + *

Finally, short code snippets show how to invert the motor direction and how to use the motor + * safety for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/motors/wpi-drive-classes.html */ public class Robot extends TimedRobot { private static final int kMotorPort = 0; @@ -38,6 +42,15 @@ public class Robot extends TimedRobot { // Use SetDistancePerPulse to set the multiplier for GetDistance // This is set up assuming a 6 inch wheel with a 360 CPR encoder. encoder.setDistancePerPulse((Math.PI * 6) / 360.0); + + // show motor inversion + motor.setInverted(true); + + // show motor safety features + motor.setSafetyEnabled(true); + motor.setSafetyEnabled(false); + motor.setExpiration(0.1); + motor.feed(); } /*