Log in

View Full Version : Need help with encoders


CosmicCricket
06-12-2012, 22:21
I'm trying to read from a pair of US Digital E4P-250-250-D-D-D-B encoders, but I'm not sure how to get started with them. I am familiar with coding for FRC, as I have done it for past seasons. However, this is the first time I've been asked to work with encoders.

I've read up on the WPILib API and it looks as if reading from the encoders should be as simple as declaring an encoder, and then sticking encoderName.Get() in a printf statement. As is the case with many things in programming, it is not that easy.

I've already verified that the encoders and the DIO ports they are connected to are returning some sort of signal, so I'm pretty sure the problem is on my end.

Any help with would be really appreciated. Thanks in advance.

Alan Anderson
06-12-2012, 22:42
I've read up on the WPILib API and it looks as if reading from the encoders should be as simple as declaring an encoder, and then sticking encoderName.Get() in a printf statement. As is the case with many things in programming, it is not that easy.

You need to .Start() the encoder before it will return any values.

CosmicCricket
06-12-2012, 22:58
Whoops, I forgot to mention that part.

Yes, I did start the encoders :o

Alan Anderson
07-12-2012, 09:28
Let's start at the beginning.

Hardware: How do you have the encoder connected? Where exactly does each of its wires go to?

Software: Show us the relevant code. How are you defining the encoder object? How are you initializing it, and how are you reading its value?

What do you expect to see, and what do you see instead?

CosmicCricket
08-12-2012, 13:45
What I have so far is some skeleton drive code, and I'm pretty sure the encoders are giving me something because I am getting 0's and 1's depending on the way the motors are spinning. However, GetRaw() is supposed to count up or down, not return 0's and 1's.

The encoders are connected to the DIO slots indicated in the code.


#include "WPILib.h"

class DefaultRobot : public SimpleRobot{
Joystick joystick;
Jaguar leftDriveJag;
Jaguar rightDriveJag;
Jaguar combineJag;
Encoder encoderL;
Encoder encoderR;



public:
DefaultRobot(void):
joystick(1),
leftDriveJag(1),
rightDriveJag(5),
combineJag(3),
encoderL(4, 3, false, Encoder::k4X),
encoderR(2, 1, false, Encoder::k4X)

{
Watchdog().SetExpiration(.75);
}

void Autonomous(void){


}

void OperatorControl(void){
Watchdog().SetEnabled(true);
encoderL.Start();
encoderR.Start();
while(IsOperatorControl()){
Watchdog().Feed();
//Operator Control
printf("%d\n", encoderR.GetRaw());
printf("%d\n", encoderL.GetRaw());
if (joystick.GetRawAxis(4) > 0.2 || joystick.GetRawAxis(4) < -0.2) { //Deadband of .2
leftDriveJag.Set(joystick.GetRawAxis(4)); //Run left motors based on left stick value.
} else {
leftDriveJag.Set(0); //Set the motors to 0.
}
if (joystick.GetRawAxis(2) > .2 || joystick.GetRawAxis(2) < -.2) { //Deadband of .2
rightDriveJag.Set(joystick.GetRawAxis(2)); //Run right motors based on right stick value
} else {
rightDriveJag.Set(0); //Set the right drive motors to 0.
}
if (joystick.GetRawButton(2)){
combineJag.Set(.5);
} else {
combineJag.Set(0);
}
if(joystick.GetRawButton(3)){
combineJag.Set(.25);
} else {
combineJag.Set(0);
}


}
}

};

START_ROBOT_CLASS(DefaultRobot);

Joe Ross
08-12-2012, 15:15
What I have so far is some skeleton drive code, and I'm pretty sure the encoders are giving me something because I am getting 0's and 1's depending on the way the motors are spinning. However, GetRaw() is supposed to count up or down, not return 0's and 1's.


An encoder that only returns 0 or 1 indicates that the B input is not working. I would double check your wiring and encoders.