-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mutating returned SwerveModuleState[] from SwerveDriveKinematics.toSwerveModuleStates() affects angles returned when called again with ChassisSpeeds(0,0,0) #5029
Comments
Heh, this suppressed warning seems relevant: @SuppressWarnings("PMD.MethodReturnsInternalArray")
public SwerveModuleState[] toSwerveModuleStates( |
I'm apprehensive that deep-copying everywhere would be a lot of extra allocations. |
The other option would be to have the method take an array as an out param and copy the states into the passed array. |
Out params aren't intuitive for Java programmers (especially those in high school), and we shouldn't use them in our API. |
The simplest solution is making the Java API match the C++ API by returning a new object (C++ returns by value). The GC should be able to handle small, short-lived objects because that's a very common memory usage pattern. |
FWIW, desaturateWheelSpeeds already takes in module states as an in/out parameter, so it's not like out params are entirely unused as it is now. I do agree with the sentiment that it'd be better to just return a new object, though (out params aren't really used particularly much [in wpilib] afaik). |
Given that it's been roughly a week without comment, should I make a PR for this? Or is debate still open on the approach? |
Describe the bug
If you take the states returned from
SwerveDriveKinematics.toSwerveModuleStates
, and take one of the angles, and mutate it (e.g.states[0].angle = new Rotation2d()
), the returned array of states is the same object as the one in m_moduleStates, thus affecting it. This is visible when you callSwerveDriveKinematics.toSwerveModuleStates(new ChassisSpeeds())
, which tries to persist the angles using the states inm_moduleStates
.To Reproduce
Run the following code:
Output:
Expected behavior
Desktop:
OpenJDK Runtime Environment (build 11.0.18+10)
The text was updated successfully, but these errors were encountered: