Redline Encoder

Can someone give me an example code on how to program redline encoders?

2 Likes

Here is the example that comes in the product page on andymark. It works just as a normal encoder on the WPILib API, so for any more info you can use the docs.wpilib.org website (Thanks to the developers for such amazing job!)

Java with the use of the Encoder class:

package org.usfirst.frc.team3940.robot;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.Encoder;

/*************************************************
 * This is a basic Example how to use the am-3749
 * with the Encoder Class in WPILib.
 * For more elaborate usage, consult the WPILib 
 * Documentation or the ScreenSteps
 ************************************************/


public class Robot extends IterativeRobot {
	//Initialize the Encoder with the Encoder Class
	Encoder MagEncoder;
	// Define the distance as diameter over pulses per revolution
	double diameter = 6/12; // 6 inch wheels
	double dist =0.5*3.14/1024;  // ft per pulse
	
	@Override
	public void robotInit() {
		
		//Define an encoder on Channels 0 and 1 without reversing direction and 4x encoding type
		MagEncoder = new Encoder(0,1, false, Encoder.EncodingType.k4X);
		//Set the distance per pulse to the predetermined distance
		MagEncoder.setDistancePerPulse(dist);
	}
		
	@Override
	public void teleopInit()
	{
		// Reset the Encoder before enabling Teleop Mode for demonstration purposes
		MagEncoder.reset();
	} 
	
	@Override
	public void teleopPeriodic() {
		//Write Encoder ticks and Distance on the dashboard
		SmartDashboard.putNumber("Encoder Ticks", MagEncoder.get());
		SmartDashboard.putNumber("Distance", MagEncoder.getDistance());
	}

}

Java via PWM input:

package org.usfirst.frc.team3940.robot;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Counter;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;


/*************************************************
 * This is a basic Example how to use the am-3749
 * PWM Output for angle tracking using Counters
 ************************************************/


public class Robot extends IterativeRobot {
	// This counter is attached to DIO 0 and measures PWM
	Counter AmMag;   
	// This counter counts the index pulses (revolutions)
	Counter AmIndex; //full period counter
	
	@Override
	public void robotInit() {
		//Initialize the Counters 
		AmMag = new Counter(0); 
		AmIndex = new Counter(1);
		//Set Semi-Period Mode in order to Measure the Pulse Width
		AmMag.setSemiPeriodMode(true);
	}

	@Override
	public void teleopInit() {
		//Reset the counters
		AmMag.reset();
		AmIndex.reset();
	}
	
	@Override
	public void teleopPeriodic() {
		//read the pulse width
		double value = AmMag.getPeriod();
		SmartDashboard.putNumber("Rotations", AmIndex.get());
		SmartDashboard.putNumber("Intermediate", value);
		// The 9.73e-4 is the total period of the PWM output on the am-3749
		// The value will then be divided by the period to get duty cycle.
		// This is converted to degrees and Radians
		double angleDEG = (value/9.739499999999999E-4)*361 -1;
		double angleRAD = (value/9.739499999999999E-4)*2*(Math.PI) ;
		SmartDashboard.putNumber("Angle in Degrees", angleDEG);
		SmartDashboard.putNumber("Angle in Radians", angleRAD);
	}

}

For a C++ example, you can use the documentation I put before, it’s very complete and everything it’s well explained.

1 Like

What’s the difference between these 2?

Both of them read the values that your encoder output, the encoder class one works as an intermediary for an easy use of the values you get, the second example reads the raw values of the encoder and outputs the revolutions the encoder have moved with the AmIndex counter and the angle it is via the AmMag counter, this last counter reads Pulse Width.

The encoder class make the job easier because it already have several methods that you might need, things like the velocity, the position, the distance traveled and many others.

The encoder class provides output relative to where the shaft was positioned when the roborio software starts, but will keep counting the shaft makes multiple rotation. The second example reads the absolute position of the encoder, so that if the shaft is a quarter turn when the software starts, it will know it’s a quarter turn. However, it can’t track multiple rotations, it will roll around when the shaft completes a rotation.

The encoder class is probably better for something like a drive train or shooter that makes many rotations. Absolute is probably better for something like an arm that might start in multiple positions but only needs one rotation. You could also wire both methods and read the absolute position on startup, and then track relative position after that if you have a mechanism where absolute position matters and it can make multiple turns.

Note that you can use the DutyCycleEncoder class to instead of the PWM input example above which will be cleaner.