Using the PWM Output with Spark Max¶
The Canandcoder easily integrates with the REV Spark Max motor controller, offering closed loop control feedback. The Canandcoder’s PWM output can be read by the Spark Max without the need of any intermediate adapter boards.
The Canandcoder, by default, ships in Spark MAX compatibility mode. As a result, the 10 pin IDC connector may be directly connected to the Spark MAX data port using the included IDC cable.
If the encoder has had its Spark MAX trace cut (for example, to configure the device in Using the PWM Output with Talon SRX mode), you may bridge the MAX solderpad to re-enable Spark MAX compatibility mode. Make sure not to bridge both the Spark MAX and Talon SRX pads at the same time, as this could damage your motor controller.
The Canandcoder acts as a PWM absolute encoder. It can be configured in the Absolute Encoder tab of the REV Hardware Client.
As the output of the Canandcoder is similar to that of a REV Through Bore, it may be used in areas where the REV encoder is used (for example, when used in a swerve module, it can be configured and used with similar code to a MaxSwerve module).
Pressing the zero button on the Canandcoder will set the currently read position as the new zero point. This can be used to quickly rezero a mechanism such as a swerve module without the use of a computer.
If the encoder is connected over CAN, manipulating the absolute position via
setAbsPosition() or inverting the direction through
either the ReduxLib API or Alchemist will also change the PWM output accordingly, overriding the offset saved by the button press.
Code Example - Java¶
// Instantiate a Spark MAX and get a handle to a PWM absolute encoder CANSparkMax sparkmax = new CANSparkMax(0, MotorType.kBrushed); var sparkencoder = sparkmax.getAbsoluteEncoder(SparkMaxAbsoluteEncoder.Type.kDutyCycle); // Change this as needed, 8 points to average for velocity measurement is a // decent starting point sparkencoder.setAverageDepth(8); // Reads the PWM Canandcoder position in rotations sparkencoder.getPosition(); // configure the Spark Max to use the PWM-connected Canandcoder for // closed-loop control sparkmax.getPIDController().setFeedbackDevice(sparkencoder); // Additionally, SparkMaxPIDController.setPositionPIDWrappingEnabled() and // similar functions may be useful for swerve pivot applications.