Skip to content

add gravity feedforward to exhub motors#29

Open
zachwaffle4 wants to merge 5 commits into
wpilibsuite:mainfrom
zachwaffle4:exhub-feedforward
Open

add gravity feedforward to exhub motors#29
zachwaffle4 wants to merge 5 commits into
wpilibsuite:mainfrom
zachwaffle4:exhub-feedforward

Conversation

@zachwaffle4

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 / setGArmRatio setters 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, or kgArm * cos(measurement * kgArmRatio * 2π)), added to the PID + feedforward output.
  • ExpansionHubServo.java imports migrated from edu.wpi.first.* to org.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.

Comment thread expansionhub/wpilib/java/ExpansionHubServo.java Outdated
Comment thread expansionhub/wpilib/java/ExpansionHubPositionConstants.java Outdated
Comment thread expansionhub/PositionController.cpp Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@zachwaffle4 zachwaffle4 requested a review from Copilot June 2, 2026 20:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.

Comment thread expansionhub/PositionController.cpp Outdated
Comment on lines +23 to +37
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);
}
Comment on lines +87 to +103
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);
Comment on lines +3 to +9
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;
Comment on lines +230 to +231
self.velocityConstants = ExpansionHubVelocityConstants(hubNumber, motorNumber)
self.positionConstants = ExpansionHubPositionConstants(hubNumber, motorNumber)
Comment on lines +276 to +280
def getVelocityPidConstants(self) -> ExpansionHubVelocityConstants:
return self.velocityPidConstants
return self.velocityConstants

def getPositionPidConstants(self) -> ExpansionHubPositionConstants:
return self.positionPidConstants
return self.positionConstants
Comment thread expansionhub/PositionController.cpp Outdated
Comment on lines +24 to +25
const double cos = cosSubscriber.Get(0);
const double cosRatio = cosRatioSubscriber.Get(0);
Comment thread expansionhub/PositionController.cpp Outdated
std::abs(cos) > kGravityCompensationZeroTolerance) {
const double armAngleRadians =
measurement * cosRatio * 2.0 * std::numbers::pi;
gravityCompensation = cos * std::cos(armAngleRadians);
@ThadHouse

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete them from here. Its not important to have anymore now that this is in WPILib.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there we go

@zachwaffle4

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@zachwaffle4

Copy link
Copy Markdown
Contributor Author

@codex review (take 2)

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@zachwaffle4

Copy link
Copy Markdown
Contributor Author

:(

@zachwaffle4

Copy link
Copy Markdown
Contributor Author

@codex review please

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

ℹ️ 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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants