Skip to content

Commit

Permalink
Added new SparkMaxAnalogEncoderSwerve to refer to absolute encoder at…
Browse files Browse the repository at this point in the history
…tached to spark max analog pins.

Signed-off-by: thenetworkgrinch <thenetworkgrinch@users.noreply.github.com>
  • Loading branch information
thenetworkgrinch committed Dec 4, 2023
1 parent cb559f1 commit f2bc382
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ <h1 class="d-inline display-6">Front Left Module</h1>
name="encoder_type">
<option value="none">None</option>
<option value="attached">Attached (to SparkMAX)</option>
<option value="sparkmax_analog">Attached (to SparkMAX Analog Pin)</option>
<option value="sparkmax_analog">Attached (to SparkMAX Analog Pin)</option>
<option value="cancoder">CANCoder</option>
<option value="canandcoder">CANandCoder (attached to SparkMAX)</option>
<option value="canandcoder_can">CANandCoder (CAN)</option>
Expand Down Expand Up @@ -536,6 +538,7 @@ <h1 class="d-inline display-6">Front Right Module</h1>
name="encoder_type">
<option value="none">None</option>
<option value="attached">Attached (to SparkMAX)</option>
<option value="sparkmax_analog">Attached (to SparkMAX Analog Pin)</option>
<option value="cancoder">CANCoder</option>
<option value="canandcoder">CANandCoder (attached to SparkMAX)</option>
<option value="canandcoder_can">CANandCoder (CAN)</option>
Expand Down Expand Up @@ -731,6 +734,7 @@ <h1 class="d-inline display-6">Back Left Module</h1>
name="encoder_type">
<option value="none">None</option>
<option value="attached">Attached (to SparkMAX)</option>
<option value="sparkmax_analog">Attached (to SparkMAX Analog Pin)</option>
<option value="cancoder">CANCoder</option>
<option value="canandcoder">CANandCoder (attached to SparkMAX)</option>
<option value="canandcoder_can">CANandCoder (CAN)</option>
Expand Down Expand Up @@ -928,6 +932,7 @@ <h1 class="d-inline display-6">Back Right Module</h1>
name="encoder_type">
<option value="none">None</option>
<option value="attached">Attached (to SparkMAX)</option>
<option value="sparkmax_analog">Attached (to SparkMAX Analog Pin)</option>
<option value="cancoder">CANCoder</option>
<option value="canandcoder">CANandCoder (attached to SparkMAX)</option>
<option value="canandcoder_can">CANandCoder (CAN)</option>
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/swervelib/encoders/SparkMaxAnalogEncoderSwerve.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package swervelib.encoders;

import com.revrobotics.AbsoluteEncoder;
import com.revrobotics.CANSparkMax;
import com.revrobotics.REVLibError;
import com.revrobotics.SparkMaxAnalogSensor;
import com.revrobotics.SparkMaxAnalogSensor.Mode;
import edu.wpi.first.wpilibj.DriverStation;
import java.util.function.Supplier;
import swervelib.motors.SwerveMotor;

/**
* SparkMax absolute encoder, attached through the data port analog pin.
*/
public class SparkMaxAnalogEncoderSwerve extends SwerveAbsoluteEncoder
{

/**
* The {@link AbsoluteEncoder} representing the duty cycle encoder attached to the SparkMax.
*/
public SparkMaxAnalogSensor encoder;

/**
* Create the {@link SparkMaxAnalogEncoderSwerve} object as a analog sensor from the {@link CANSparkMax} motor data port analog pin.
*
* @param motor Motor to create the encoder from.
*/
public SparkMaxAnalogEncoderSwerve(SwerveMotor motor)
{
if (motor.getMotor() instanceof CANSparkMax)

This comment has been minimized.

Copy link
@cosmosified

cosmosified Dec 5, 2023

id ont' know if it's a java 17 thing but you should be able to do (if motor.getMotor() instanceof CANSparkMax canSparkMaxMotor){ encoder = canSparkMaxMotor.getAnalag(Mode.kAbsolute); }

then you don't have to cast it.

This comment has been minimized.

Copy link
@thenetworkgrinch

thenetworkgrinch Dec 5, 2023

Author Contributor

Interesting, i will keep it casted since that shouldnt incur any time penalty's or extra cycles if it was saved off like it should be (idk if javabyte code does this but i hope it does). I have never seen that before. Feel free to test it with WPILib 2023 and if it works i will consider adding it!

This comment has been minimized.

Copy link
@cosmosified

cosmosified via email Dec 6, 2023

{
encoder = ((CANSparkMax) motor.getMotor()).getAnalog(Mode.kAbsolute);
} else
{
throw new RuntimeException("Motor given to instantiate SparkMaxEncoder is not a CANSparkMax");
}
}

/**
* Run the configuration until it succeeds or times out.
*
* @param config Lambda supplier returning the error state.
*/
private void configureSparkMax(Supplier<REVLibError> config)
{
for (int i = 0; i < maximumRetries; i++)
{
if (config.get() == REVLibError.kOk)
{
return;
}
}
DriverStation.reportWarning("Failure configuring encoder", true);
}

/**
* Reset the encoder to factory defaults.
*/
@Override
public void factoryDefault()
{
// Do nothing
}

/**
* Clear sticky faults on the encoder.
*/
@Override
public void clearStickyFaults()
{
// Do nothing
}

/**
* Configure the absolute encoder to read from [0, 360) per second.
*
* @param inverted Whether the encoder is inverted.
*/
@Override
public void configure(boolean inverted)
{
encoder.setInverted(inverted);
}

/**
* Get the absolute position of the encoder.
*
* @return Absolute position in degrees from [0, 360).
*/
@Override
public double getAbsolutePosition()
{
return encoder.getPosition();
}

/**
* Get the instantiated absolute encoder Object.
*
* @return Absolute encoder object.
*/
@Override
public Object getAbsoluteEncoder()
{
return encoder;
}
}
3 changes: 3 additions & 0 deletions src/main/java/swervelib/parser/json/DeviceJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import swervelib.encoders.CANCoderSwerve;
import swervelib.encoders.CanAndCoderSwerve;
import swervelib.encoders.PWMDutyCycleEncoderSwerve;
import swervelib.encoders.SparkMaxAnalogEncoderSwerve;
import swervelib.encoders.SparkMaxEncoderSwerve;
import swervelib.encoders.SwerveAbsoluteEncoder;
import swervelib.imu.ADIS16448Swerve;
Expand Down Expand Up @@ -64,6 +65,8 @@ public SwerveAbsoluteEncoder createEncoder(SwerveMotor motor)
case "integrated":
case "attached":
return null;
case "sparkmax_analog":
return new SparkMaxAnalogEncoderSwerve(motor);
case "canandcoder":
return new SparkMaxEncoderSwerve(motor, 360);
case "canandcoder_can":
Expand Down

0 comments on commit f2bc382

Please sign in to comment.