FIRST Robotics Competition Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Keeping track of shaft encoder using WPI Counter VI in 2 directions?

My team is using a Light Sensor with tape on shaft as Encoder to keep track of the extension arm position. We are using Lab View. To keep track of Count (every revolution reflective tape passes light sensor) I am using the WPI Counter VI. After testing, it is not as accurate as I would hope... may have to live with it since this is what I was given and they can not change it now. I believe it is the mechanical binding that is the problem, not sensor bandwidth.

The counter will only increment in either direction. When one button pressed extension arm extends, other button, reverse motor and retracts. I am not sure why, but I can not keep proper count in reverse direction. I am using a shift register, add count in forward (check if forward button pressed), subtract count in reverse (check if reverse button pressed). I set while loop stop to True, so executes once, having shift register return previous count and compare new count.  I am not sure why it is not working, timing issue with counter & while loop/shift register? If no button pressed, I pass shift register previous straight thru. I tried other ways than shift register, appears to be similar problem. This seems like a simple thing to do, but is not working. I would appreciate any support or new ideals.  I attached sample of counter with retract button pressed, no button pressed and extend button pressed.  Thanx... Appreciate help, in theory code should be simple, something else must be wrong

0 Kudos
Message 1 of 4
(4,157 Views)

There's an error in your logic, perhaps due to a misunderstanding about the encoder.  The counter is keeping track of total count.  You need a second shift register to keep track of the previous counter value, then add or subtract the difference between the current and previous value.

0 Kudos
Message 2 of 4
(2,842 Views)

Nathand

Thanx for reply...

I am not sure why i still can not get it working, may be my inexperience with LAB View... How am suppose to wire the two together?

0 Kudos
Message 3 of 4
(2,842 Views)

In the up direction you're OK at first, since it's basically just using the WPI Counter value directly.  Let's look at what happens in the reverse direction.  Say we assume that you've gone up, and both the WPI Counter and your shift register have reached a value of 20.  We'll also assume that the motor is rotating at 4 counts per iteration of your code.  On the first iteration with the motor going down, the WPI Counter is now at 24.  Your code computes 20 - (24-20) = 16, which is correct.  On the next iteration the motor rotates another 4 times, so the WPI Counter is at 28.  You compute 16 - (28 - 16) = 16-12 = 4, but the correct value would be 12.

What you need instead is a second shift register tracking the previous value of the WPI Counter.  Calculate the difference between that and the current WPI Counter value, then add or subtract that from the shift register total depending on whether you're going up or down.

It would be a bit like this (this is a "snippet" which you can drag directly onto a block diagram and run on your computer; with some web browsers you may need to drag it to the desktop first, then drag it from there onto a block diagram):

encoder forward reverse.png

Don't worry about the bit of extra code that runs the loop every 200 milliseconds but only increments the counter every 1 second.

0 Kudos
Message 4 of 4
(2,842 Views)