Wpilibj Counter Object

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.

Can you share the rest of your code? Ideally as a link to github or similar

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().

https://github.com/Snake5151/MainArmLift-5900/new/main

That repo is not public.

1 Like

Yeah, as @Peter_Johnson suggested, you should use External Direction mode

1 Like

the motor we are using is a bosch seat motor which only has one hall effect sensor channel would it still be possible to use this method

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.

1 Like

All this assumes you got the AndyMark kit am-3812.
If you didn’t, this worked for us years ago:

thank you peter for the code it worked great!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.