Skip to content

ServoImpl#getPosition incorrectly reversing a scaled servo. #1470

Open

Description

@kevinthegreat1

In ServoImpl#getPosition,reverse is called on the unscaled position in getPosition. While in ServoImpl#setPosition, reverse is called on scaled values. This means Servo#getPosition return incorrect values for reversed scaled servos.

Example

Let's say we have a reversed servo scaled from 0 to 0.5 instead of from 0 to 1:

Setting:

  1. setPosition(0.5) -> sets an actual position of 0.25 for the servo.

Getting:

  1. ServoController#getServoPosition returns 0.25 (line 178)
  2. reverse reverses the value from 0.25 to 0.75 (line 179)
  3. Range.scale scales the value from 0.75 to 1.5 (line 180)
  4. Range.clip clips the value to 1

Result

We expect to get 0.5, not 1

Code Screenshot

Image

Activity

  1. kevinthegreat1 commented on Mar 2, 2025

    @kevinthegreat1
    Author

    I know you guys don't accept prs, but I'd be happy to submit one if needed.

  2. alan412 commented on Mar 2, 2025

    @alan412

    Thank you for this catch. I have put it as an issue on our internal repo. We'll likely fix it for the off-season release.

  3. alan412 commented on Mar 2, 2025

    @alan412

    Also @kevinthegreat1 , all of the detail was very helpful!!

  4. alan412 commented on Mar 2, 2025

    @alan412

    I think the issue is actually in the setting. and it should be like this:

    Setting:

    setPosition(0.5) -> SHOULD set an actual position of 0.75 for the servo.

    Getting:

    ServoController#getServoPosition returns 0.75 (line 178)
    reverse reverses the value from 0.75 to 0.25 (line 179)
    Range.scale scales the value from 0.25 to 0.5 (line 180)
    Range.clip does nothing

    @kevinthegreat1 - agree?

  5. kevinthegreat1 commented on Mar 2, 2025

    @kevinthegreat1
    Author

    Thanks for the quick response. I believe both approaches will have the same effect, however, changing setPosition would break current servos.

    Since right now, setPosition(0.5) sets 0.25, but if it's changed to set 0.75, then it will break more current contraptions and people would have to recalibrate their servos. Changing getPosition will break less stuff, but both approaches will fix the issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions