Encoders with Java

Hello,
My team is using encoders (E4P-360-250-D-H-D-B) for the very first time and we are writing in Java. We really don’t know where to even begin. We do believe that the encoders are wired correctly. Sample code would be very much appreciated.

Encoder encoder = new Encoder(portA, portB); //ask your electrical team for the pots
encoder.start();
encoder.reset();
System.out.println(encoder.get());

Where are your encoders setup? on the drive train motors?

The encoders are placed on the drive train (wheel axle) and are wired to the digital sidecar. We tried the code you posted and when we run the code we only receive a constant number (most of the time its 0 but sometimes it will gives us numbers such as -2, -32, 1)

P.S
Thank you very much for your quick response and help

Use the function setDistancePerPulse() and set it to 360 (this is based on the model you are using). That should help.

We tried that but it seems not matter what we do we only get a value of zero for everything. Could you re-post some working sample code?

Thanks again though

That sample code still works. Try playing with the PWM values. If it still doesnt work, contact your electrical team

We wired a new encoder to different PWM pins using the code you’ve (graciously) provided. We will get a single number over and over until we disable then re-enable which will then give us the updated value. Is there a way to update the
System.out.println(encoders.get());
function?
Could this problem be occurring for any other reason?

Are you moving the robot…? Is the C-RIO updated to 2013 along with the driver station?

Yes, everything is up to date and we are moving the robot (and the encoder disk is moving with the axle).

Could you post your code? I dont think your doing anything wrong…

package BasicMecanumDrive;

import edu.wpi.first.wpilibj.CANJaguar;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.can.CANTimeoutException;
import edu.wpi.first.wpilibj.*;

public class BasicMecanumDrive extends IterativeRobot {
CANJaguar frontLeft, frontRight, backLeft, backRight;

Joystick leftStick, rightStick;
RobotDrive robotDrive;

private final Encoder encoder = new Encoder(13,14);
 

public void robotInit() {

    leftStick = new Joystick(1);
    rightStick = new Joystick(2);
     


    try {
        frontLeft = new CANJaguar(4);
        frontRight = new CANJaguar(1);
        backLeft = new CANJaguar(3);
        backRight = new CANJaguar(2);

    encoder.start();
    encoder.reset();

      robotDrive = new RobotDrive(frontLeft, backLeft, frontRight, backRight);
    } catch (CANTimeoutException ex) {
        ex.printStackTrace();
    }


}



public void autonomousPeriodic() {

}


public void teleopInit () {


}
public void teleopPeriodic() {
    robotDrive.mecanumDrive_Cartesian(rightStick.getX(), rightStick.getY(), -leftStick.getY(), 0);

    
    if (encoder.getRaw()> 20000) {
        System.out.println("Gots It");
    }
    System.out.println(encoder.get());

    DriverStationLCD.getInstance().println(DriverStationLCD.Line.kUser2, 2, "Encoder"+ encoder.get());
    DriverStationLCD.getInstance().updateLCD();
    

    
}

}

Here’s our code currently. We are just running a few things to eliminate potential problems from other systems.

Hi,
I also started using encoders this year. After reading this thread and researching online in the past few weeks, I understood part of programming encoders. Now, I’m just wondering how to move the bot x feet in autonomous using encoders on drivetrain. What methods do I use if I want the bot to move forward, let’s say, for 7 feet? If you provide me with sample code for this, it would be greatly appreciated. I’m writing in java commandbased, and drivetrain that consists of left and right motors where encoders mounted on each side.