add gravity feedforward to exhub motors#29
Conversation
# Conflicts: # expansionhub/PositionController.cpp
There was a problem hiding this comment.
Pull request overview
Adds gravity-feedforward support (lift, arm, and arm-ratio constants) to expansion hub position-mode motors. New kgLift/kgArm/kgArmRatio NT entries are published from the Java and Python client APIs and consumed by the C++ PositionController to add a gravity-compensation voltage term on top of the existing PID + simple feedforward output. The Python side also renames internal attributes (*PidConstants → *Constants), and ExpansionHubServo.java is migrated to the new org.wpilib.* package paths (matching the linked allwpilib PR).
Changes:
- New
setGLift/setGArm/setGArmRatiosetters and corresponding NT publishers in both Java (ExpansionHubPositionConstants) and Python (expansion_hub.py). PositionController(C++) adds three new subscribers and computes a gravity-compensation voltage (kgLift, orkgArm * cos(measurement * kgArmRatio * 2π)), added to the PID + feedforward output.ExpansionHubServo.javaimports migrated fromedu.wpi.first.*toorg.wpilib.*.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| expansionhub/wpilib/python/expansion_hub.py | Adds kgLift/kgArm/kgArmRatio publishers and setters; renames *PidConstants attributes to *Constants. |
| expansionhub/wpilib/java/ExpansionHubServo.java | Migrates imports to org.wpilib.* package namespace. |
| expansionhub/wpilib/java/ExpansionHubPositionConstants.java | Adds gravity-compensation publishers, setters, and close() cleanup. |
| expansionhub/PositionController.h | Declares three new gravity-compensation NT subscribers. |
| expansionhub/PositionController.cpp | Subscribes to the new constants and adds a gravity-compensation voltage term to the controller output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| const double g = gSubscriber.Get(0); | ||
| const double cos = cosSubscriber.Get(0); | ||
| const double cosRatio = cosRatioSubscriber.Get(0); | ||
| constexpr double kGravityCompensationZeroTolerance = 1e-9; | ||
|
|
||
| // Precedence rule: g wins when it is configured to a nonzero value. | ||
| // Only when g is effectively zero do we fall back to arm-style | ||
| // gravity compensation based on cos and cosRatio. | ||
| double gravityCompensation = g; | ||
| if (std::abs(gravityCompensation) <= kGravityCompensationZeroTolerance && | ||
| std::abs(cos) > kGravityCompensationZeroTolerance) { | ||
| const double armAngleRadians = | ||
| measurement * cosRatio * 2.0 * std::numbers::pi; | ||
| gravityCompensation = cos * std::cos(armAngleRadians); | ||
| } |
| gSubscriber = | ||
| instance | ||
| .GetDoubleTopic("/rhsp/" + busIdStr + "/motor" + motorNum + | ||
| "/constants/position/kg") | ||
| .Subscribe(0, options); | ||
|
|
||
| cosSubscriber = | ||
| instance | ||
| .GetDoubleTopic("/rhsp/" + busIdStr + "/motor" + motorNum + | ||
| "/constants/position/kcos") | ||
| .Subscribe(0, options); | ||
|
|
||
| cosRatioSubscriber = | ||
| instance | ||
| .GetDoubleTopic("/rhsp/" + busIdStr + "/motor" + motorNum + | ||
| "/constants/position/kcosRatio") | ||
| .Subscribe(0, options); |
| import org.wpilib.math.util.MathUtil; | ||
| import org.wpilib.networktables.BooleanPublisher; | ||
| import org.wpilib.networktables.BooleanSubscriber; | ||
| import org.wpilib.networktables.IntegerPublisher; | ||
| import org.wpilib.networktables.NetworkTableInstance; | ||
| import org.wpilib.networktables.PubSubOption; | ||
| import org.wpilib.system.SystemServer; |
| self.velocityConstants = ExpansionHubVelocityConstants(hubNumber, motorNumber) | ||
| self.positionConstants = ExpansionHubPositionConstants(hubNumber, motorNumber) |
| def getVelocityPidConstants(self) -> ExpansionHubVelocityConstants: | ||
| return self.velocityPidConstants | ||
| return self.velocityConstants | ||
|
|
||
| def getPositionPidConstants(self) -> ExpansionHubPositionConstants: | ||
| return self.positionPidConstants | ||
| return self.positionConstants |
| const double cos = cosSubscriber.Get(0); | ||
| const double cosRatio = cosRatioSubscriber.Get(0); |
| std::abs(cos) > kGravityCompensationZeroTolerance) { | ||
| const double armAngleRadians = | ||
| measurement * cosRatio * 2.0 * std::numbers::pi; | ||
| gravityCompensation = cos * std::cos(armAngleRadians); |
|
@codex review |
63d80bd to
05c4a50
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 63d80bd6d0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| m_gLiftPublisher = | ||
| systemServer | ||
| .getDoubleTopic( | ||
| "/rhsp/" + hubNumber + "/motor" + motorNumber + "/constants/position" + "/kgLift") |
There was a problem hiding this comment.
Align gravity constant topics with daemon subscribers
When callers use the new Java/Python setGLift()/setGArm() APIs, these publishers write to /kgLift, /kgArm, and /kgArmRatio, but the daemon added in this commit subscribes to /constants/position/kg, /kcos, and /kcosRatio in PositionController::Initialize. Because NetworkTables topics must match exactly, the new feedforward constants stay at their default 0, so gravity compensation is never applied for normal client usage.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
this is why I don't like having the java and python classes randomly copied in here; they are correct in the corresponding allwpilib pr
There was a problem hiding this comment.
You can delete them from here. Its not important to have anymore now that this is in WPILib.
|
@codex review |
|
To use Codex here, create a Codex account and connect to github. |
|
@codex review (take 2) |
|
To use Codex here, create a Codex account and connect to github. |
|
:( |
|
@codex review please |
|
Codex Review: Didn't find any major issues. Hooray! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
works with wpilibsuite/allwpilib#8890