Skip to content
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

Add Setting the Absolute Encoder Offset #105

Merged
merged 8 commits into from
Dec 12, 2023

Conversation

Technologyman00
Copy link
Collaborator

Add Setting the Absolute Encoder Offset though code.

Supported Encoders

  • Attached Sparkmax Alternate Encoder
  • CANcoder
  • PWMDutyCycle AbsoluteEncoder

Unsupported Encoders

  • CanAndCoder
  • AnalogEncoder

Analog Encoder could be supported If it was using the WPILib analog encoder methods , but currently it uses digital inputs

Add Setting the Absolute Encoder Offset though code
@Technologyman00
Copy link
Collaborator Author

Addresses #100

Copy link
Contributor

@thenetworkgrinch thenetworkgrinch left a comment

Choose a reason for hiding this comment

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

Use /** for javadocs not /*

@thenetworkgrinch
Copy link
Contributor

This needs to tie in from SwerveModule and SwerveDrove the SwerveModule tie in should set the offset using angleOffset and clear the one in SwerveModule. The function may need to raise an error saying its not supported if it isnt supported as well, you could do this by using the EncoderSwerve class, maybe.

Great base work!

@Technologyman00
Copy link
Collaborator Author

So do you want this to replace the offset built into YAGSL? Or replace the functionality of it? I'm a bit confused by your last comment

@thenetworkgrinch
Copy link
Contributor

thenetworkgrinch commented Nov 30, 2023

The use case for this is not universal so you would have to treat it as such. Your current implementation is fine except no one would use it bc its a pain to setup without modifying YAGSL. So a more generic solution would be "modes" for the offset in SwerveModule the default being calculated offset, however your alternative which WILL NOT ALWAYS WORK, would be integrated. To switch modes ideally you would call a function from SwerveModule which uses SwerveModule.angleOffset to set the integrated offset of the absolute encoder THEN sets SwerveModule.angleOffset to 0 (keeping in mind there needs to be a way to restore it later, you can use moduleConfigOptions for this). IF you cannot set the integrated offset then the mode switching fails, prints an error message and SwerveModule.angleOffset stays the same. In addition to this there would need to be a way to switch back from integrated offsets to calculated. Ideally all of this does not increase necessary cycles, meaning minimal or no new conditionals.

@Technologyman00
Copy link
Collaborator Author

Ok so how do you feel about me putting it as optional in the JSON so if teams want to use it it's available and if they don't it's not?

Like offset type and offset. If no type specified then use the default built-in one. If type specified of "encoder-level" or something then it uses the alternative method.

@thenetworkgrinch
Copy link
Contributor

thenetworkgrinch commented Nov 30, 2023

I try to avoid adding to JSONs as much as possible to reduce confusion and keep a stable schema. I dont think this issue arises to a point where it seems reasonable to add to the schema, mainly because it is not something that affects swerve drives so much that it would significantly add to cycles. I like the idea but I think it would be unnecessary clutter in module JSONs.

An easy way to trigger it programmatically would be SwerveDrive.setIntegratedOffsets() which would call SwerveModule.setIntegratedOffsets() which would then call if(angleOffset==0 && moduleConfiguration.angleOffset !=0){encoder.setZero(0);angleOffset=moduleConfiguration.angleOffset;}else if(encoder.setZero(angleOffset)){angleOffset = 0;}

… on the encoder

Allows SwerveDrive to change the absolute encoder offsets

Checks to see if successful before undoing the internal YAGSL offsets

Has warnings for unsupported devices and errors for why
@Technologyman00
Copy link
Collaborator Author

Okay I updated it based on your requests. Besides maybe the phrasing I think its really good.

@Technologyman00
Copy link
Collaborator Author

Not sure how to resolve the conflict, but it just missing } and /**

Copy link
Contributor

@thenetworkgrinch thenetworkgrinch left a comment

Choose a reason for hiding this comment

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

While I understand why you would want to save the offset from the encoder, it gets blown away at initialization of the encoder through factory resets anyways (supposedly, SparkMAX with throughbore needs investigation still). Sorry that i dont think so much of it is needed offends you, i am willing to debate whether or not it may be useful!

This is pretty great work and I appreciate all of your effort put into it!

Comment on lines 57 to 61

/**
* Get the current Absolute Encoder offset as reported by the Encoder.
*/
public abstract double getAbsoluteEncoderOffset();
Copy link
Contributor

Choose a reason for hiding this comment

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

You do not need this function.

Comment on lines 127 to 136
/**
* Get the Absolute Encoder Offset inside of the SparkMax's Memory.
*
* @return The zero offset of the absolute encoder with the position conversion factor applied from the Sparkmax's Memory.
*/
@Override
public double getAbsoluteEncoderOffset()
{
return encoder.getZeroOffset();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Unneeded.

Comment on lines 101 to 110
/**
* Get the Offset of the Encoder in the WPILib Encoder Library.
*
* @return Current Zero Point Offset.
*/
@Override
public double getAbsoluteEncoderOffset()
{
return encoder.getPositionOffset();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Uneeded

Comment on lines 98 to 107
* Cannot get or set the offset of the CanAndCoder.
*
* @return Will always return 0.0
*/
@Override
public double getAbsoluteEncoderOffset()
{
//CanAndCoder does not support Absolute Offset Changing
return 0.0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Uneeded

{
return true;
}
DriverStation.reportWarning("Failure to set Absolute Encoder Offset Error: "+error, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Include the ID and the encoder type in message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Idk where I can get those. It doesn't seem to be accessible for the variables that currently exist in either SwerveModule or the Encoder

public void pushOffsetsToControllers(double offset)
{
oldAbsoluteEncoderOffset = absoluteEncoder.getAbsoluteEncoderOffset();
if(absoluteEncoder.setAbsoluteEncoderOffset(offset) == true){
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be simplified, also remember absoluteEncoder could be null so a check is required. Also should use angleOffset

*
* @param offset the offset the AbsoluteEncoder should use.
*/
public void pushOffsetsToControllers(double offset)
Copy link
Contributor

Choose a reason for hiding this comment

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

I disagree with passing the offset here, solely because it could introduce extreme confusion and questions on "why isnt the angle being set" by teams who switched programmers. Offset should be angleOffset

*
* @param restoreMemory Whether or not the origional offset value should be restored.
*/
public void restoreInternalOffset(boolean restoreMemory)
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should not use the memory from the motor controller, parameter isnt necessary.

Comment on lines 421 to 423
if(restoreMemory){
absoluteEncoder.setAbsoluteEncoderOffset(oldAbsoluteEncoderOffset);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Uneeded

absoluteEncoder.setAbsoluteEncoderOffset(oldAbsoluteEncoderOffset);
}
else{
absoluteEncoder.setAbsoluteEncoderOffset(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

absoluteEncoder could be null and should be checked

@thenetworkgrinch
Copy link
Contributor

Not sure how to resolve the conflict, but it just missing } and /**

I can resolve it later, its not a big deal rn

@Technologyman00
Copy link
Collaborator Author

Honestly I'm chilling I'm not an amazing programmer so I'm learning. It doesn't offend me at all. I completely understand your concerns. I think it's super difficult to convey tone over text so I apologize if I gave you that impression.

@Technologyman00
Copy link
Collaborator Author

If you mean my phrasing comment I meant it's hard to explain the difference between the YAGSL offsets and the offsets stored in the encoder/motor controller

@thenetworkgrinch
Copy link
Contributor

thenetworkgrinch commented Nov 30, 2023

Not a problem. It is very hard to explain. I had to abstract alot to keep it generic enough for any configuration unfortunately in keeping with a paradigm where nothing from configuration classes gets changes (mostly, i broke it a few times and need to fix those later) i duplicated some variables 2-3 times over where only 1 is used, including the one burned to motor controllers.

Copy link
Contributor

@thenetworkgrinch thenetworkgrinch left a comment

Choose a reason for hiding this comment

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

A few nitpicky things. I like this version much better!

Keep up the good work!

*
* @param restoreMemory Whether the memory inside the encoder is restored or not.
*/
public void restoreInternalOffset(boolean restoreMemory)
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary parameter

public boolean setAbsoluteEncoderOffset(double offset)
{
//CanAndCoder does not support Absolute Offset Changing
DriverStation.reportWarning("Cannot Set Absolute Encoder Offset of CanAndCoders", false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Add PWM ID to this error message

@Override
public boolean setAbsoluteEncoderOffset(double offset)
{
REVLibError error = encoder.setZeroOffset(offset);
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrap this in a for loop from 0 to maximumRetries. SparkMAX's can be finnicky.

@Technologyman00
Copy link
Collaborator Author

The CanAndCoder seems to actually allow setting the encoder offset they just dont have good documentation how. https://docs.reduxrobotics.com/canspec/Canandcoder#settings

Also additionally if the intention is to reset the encoders to factory defaults including the zero offset that is currently disabled for CanAndCoder

@thenetworkgrinch
Copy link
Contributor

The CANandCoder is an exception bc it has the button to zero it which some people may want to take advantage of.

Added Device Ports in the warnings. However Sparkmaxes cannot easily report the ID from the encoder object.
Copy link
Contributor

@thenetworkgrinch thenetworkgrinch left a comment

Choose a reason for hiding this comment

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

One tiny detail before this can be merged!

Copy link
Contributor

@thenetworkgrinch thenetworkgrinch left a comment

Choose a reason for hiding this comment

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

Looks great!

@thenetworkgrinch
Copy link
Contributor

I will fix up the merge conflicts tomorrow hopefully.

Signed-off-by: thenetworkgrinch <thenetworkgrinch@users.noreply.github.com>
@thenetworkgrinch thenetworkgrinch changed the base branch from main to abs_encoder December 12, 2023 16:32
@thenetworkgrinch thenetworkgrinch changed the base branch from abs_encoder to main December 12, 2023 16:37
@thenetworkgrinch thenetworkgrinch merged commit 95fb973 into BroncBotz3481:main Dec 12, 2023
@thenetworkgrinch
Copy link
Contributor

A week late, but I think this change will go over really well!

@Technologyman00
Copy link
Collaborator Author

Yeah I think it should be helpful to the teams that need it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add Setting Rev Absolute Encoder offset through YAGSL
2 participants