I am new to Java, but have a new mentor who is fluent in Java but not the FRC plugins. We have been trying to get the encoders to update their raw values on the Java debugging screen but have been only getting the output of 0.0. We know the encoders work because they work fine in LabVIEW, but not Java. This is the code I have so far:
private Encoder rightEncoder = new Encoder(4,5,4,6,true,CounterBase.EncodingType.k4X);
private Encoder leftEncoder = new Encoder(4,7,4,8,true,CounterBase.EncodingType.k4X);
I’ve personally never had to deal with encoders before, but here’s one thing you can try: see if doing rightEncoder.start(); leftEncoder.start(); does anything. If that fails, I’m as puzzled as you are.
Patrick is correct inside of robotInit() you should call the start() method of each encoder that you are using. After that you should get values when you call getDistance().
One thing to note is that my team has had problems with getRate() and had to write our own getRate() method to get accurate results.
Yes: the encoder.start()… 3 programmers literally spent 3 hours trying to figure that out… But it turned out that we also wired it up all wrong because I am an idiot… But the team captain said my soldering was pretty good.
Here are the current revisions of our code: you will be surprised how long I had to fight to get the other programmers to agree on using arrays… They thought arrays will just complicate things. Now since I was the lead programmer, I pretty much said “I am doing it” and after I did it, they liked it… -__-;;
BTW the DataStructure is abridged; I only included the encoder stuff and took the rest out.
CVEncoder.java:
package edu.wpi.first.wpilibj.Robot2011.Perceive;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Robot2011.DataStructure;
/**
*
* @author Robotics2011
*/
public class CVEncoder
{
private Encoder spin];
private int numOfEncoders;
public CVEncoder(int n)//takes in number of encoders
{
numOfEncoders = n;
if(numOfEncoders > 8)
{
numOfEncoders = 8;
}
else if(numOfEncoders < 0)
{
numOfEncoders = 0;
}
spin = new Encoder[numOfEncoders];
int counter = 1;
for(int i = 0; i < numOfEncoders; i ++)
{
spin* = new Encoder(i + counter, i + counter + 1);
counter ++;
}
for(int i = 0; i < numOfEncoders; i ++)
{
spin*.start();//took 3 hours to figure this out, but electronics was still at fault
}
}
public void setRotation(double circumference, int c)//to caculate rate and dist; circumference in feet
{
spin[c].setDistancePerPulse(circumference / 360.0);//cirumference divided by 360 degrees [per pulse]
}
//rate - ft/s
//dist - ft
//direction - 1 is forward, -1 if backward
public void update(DataStructure dat)//update inhited privates with dist, dir, and rate
{
for(int num = 0; num < numOfEncoders; num ++)
{
//Direction formatting
double direction = 0;
if(spin[num].getStopped() == false && spin[num].getDirection() == true)//allegedly, Encoder class returns negative int for forward
direction = 1.0;
else if(spin[num].getStopped() == false && spin[num].getDirection() == false)
direction = -1.0;
dat.setDirection(num, direction);
dat.setDistance(num, spin[num].getDistance());
dat.setRate(num, spin[num].getRate());
dat.setStopped(num, spin[num].getStopped());
dat.setPulses(num, spin[num].get());
}
}
public void neutralize(DataStructure dat)
{
for(int num = 0; num < numOfEncoders; num ++)
{
spin[num].stop();
spin[num].start();
spin[num].reset();
}
update(dat);
}
public int numberOfEncoders()
{
return numOfEncoders;
}
}
DataStructure.java:
package edu.wpi.first.wpilibj.Robot2011;
/**
*
* @author Robotics2011
*/
public class DataStructure
{
//Encoder Variables
private int numEncoders;//set number of encoders
private double rate];
private double distance];
private double direction];
private double pulses];
private boolean stopped];
public DataStructure()
{
//Setting Encoder Variables to 0
numEncoders = 0;
}
public void constructEncoders(int num)
{
numEncoders = num;
rate = new double[numEncoders];
distance = new double[numEncoders];
direction = new double[numEncoders];
pulses = new double[numEncoders];
stopped = new boolean[numEncoders];
for(int i = 0; i < numEncoders; i ++)
{
rate* = 0.0;
distance* = 0.0;
direction* = 0.0;
pulses* = 0.0;
stopped* = true;
}
}
//Start Encoder Accessors and Manipulators
public void setRate(int num, double x)
{
rate[num] = x;
}
public double getRate(int num)
{
return rate[num];
}
public void setDistance(int num, double d)
{
distance[num] = d;
}
public double getDistance(int num)
{
return distance[num];
}
public void setDirection(int num, double d)
{
direction[num] = d;
}
public double getDirection(int num)
{
return direction[num];
}
public void setPulses(int num, double i)
{
pulses[num] = i;
}
public double getPulses(int num)
{
return pulses[num];
}
public void setStopped(int num, boolean b)
{
stopped[num] = b;
}
public boolean getStopped(int num)
{
return stopped[num];
}
//End Encoder Accessors and Manipulators
}
Our team ran into the same exact problem. For some reason, the FPGA counting mechanism doesn’t work right. Instead of using the CounterBase.EncodingType.k4X encoding type, try CounterBase.EncodingType.k1X instead. Worked for us.