We are having issues getting the counter object to count down when we reverse the motor we are trying to use the setReverseDirection function. When we display the count using SmartDashboard the value given by the counter is only increasing, no matter if setReverseDirection is set to true or false.
Here is our code:
if (speed > 0){
m_sensor_Count.setReverseDirection(false);
}
else{
m_sensor_Count.setReverseDirection(true);
}
We only have 1 encoder channel on our seat motor that we are using this code with.
How are you constructing/setting up the counter? The Counter needs to be in external direction mode for setReverseDirection() to work. This can be done at construction time by passing the Mode to the constructor, or by calling setExternalDirectionMode().
EDIT: I think you can just set an arbitrary unused DIO as the direction (“down”) source. It’s always going to read the same direction, so setReverseDirection() will “reverse” it.
Oh. Yeah, it doesn’t quite work, as the setReverseDirection() just reverses the down source edge. Another way to do what you’re trying to do is to create an up/down counter where both the up and down sources are the same DIO, and then change the Up and Down source edges to enable/disable either the up or down direction?
Counter counter = new Counter(Counter.Mode.k2Pulse);
DigitalInput in = new DigitalInput(1);
counter.setUpSource(in);
counter.setDownSource(in);
counter.setUpSourceEdge(true, false);
counter.setDownSourceEdge(false, false);
...
if (speed >= 0) {
counter.setDownSourceEdge(false, false);
counter.setUpSourceEdge(true, false);
} else {
counter.setUpSourceEdge(false, false);
counter.setDownSourceEdge(true, false);
}
I tried doing something similar years ago and there was enough lag from when the commanded speed flipped direction until the motor flipped direction that you couldn’t get accurate position. I’d recommend looking at other sensors rather then trying a kludge like this.