Go to Post I, as a programmer, always strive to have the entire robot operate with as little driver input as possible. Those pesky humans.... - dellagd [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 13-02-2015, 23:07
shindigo shindigo is offline
Registered User
AKA: Mike Parker
FRC #0102
Team Role: Mentor
 
Join Date: Feb 2012
Rookie Year: 2009
Location: Somerville, NJ
Posts: 33
shindigo is an unknown quantity at this point
Counting up and down with a switch

Hi We are using counters with switches on digital inputs to check for rapid changes in the switch. We would like the counter to count up when the switch goes from unpressed to pressed and count down (to negative numbers) when it goes
from pressed to unpressed. So far the counter appears to only count up on either state change.

Any thoughts about how to get it to count down when the switch is released?

mp
Reply With Quote
  #2   Spotlight this post!  
Unread 13-02-2015, 23:52
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,609
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Counting up and down with a switch

If you want a switch that counts both up and down, you really need something like a quadrature encoder, or a gray code encoder. Either of these has at least two separate switches. The simplest quadrature encoder is for rotation, and has one switch "on" for angles between 0 and 180 degrees, "off" for 180 to 360. The other switch is rotated 90 degrees, so it will be "on" for 90 to 270 degrees, and "off" for 270 through 0 to 90 degrees. With this sort of setup, you can tell which way the wheel is rotating based on which switch is "leading" the other towards each state. If the first switch "leads", rotation is in the positive direction, if the second switch "leads", rotation is in the negative direction.

I'll leave gray code for you to google.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #3   Spotlight this post!  
Unread 14-02-2015, 23:45
mshafer1 mshafer1 is offline
Programming Mentor
AKA: Matthew Shafer
FRC #3937 (Breakaway)
Team Role: Mentor
 
Join Date: Jan 2015
Rookie Year: 2008
Location: Arkansas
Posts: 56
mshafer1 is an unknown quantity at this point
Re: Counting up and down with a switch

Quote:
Originally Posted by GeeTwo View Post
If you want a switch that counts both up and down, you really need something like a quadrature encoder, or a gray code encoder. Either of these has at least two separate switches. The simplest quadrature encoder is for rotation, and has one switch "on" for angles between 0 and 180 degrees, "off" for 180 to 360. The other switch is rotated 90 degrees, so it will be "on" for 90 to 270 degrees, and "off" for 270 through 0 to 90 degrees. With this sort of setup, you can tell which way the wheel is rotating based on which switch is "leading" the other towards each state. If the first switch "leads", rotation is in the positive direction, if the second switch "leads", rotation is in the negative direction.

I'll leave gray code for you to google.
As GeeTwo stated, something more is really needed for what you are saying, it is possible to either put two switches, or the encoders that he suggested.
I am attaching a snippet that would count up and down as described, - note that it will go 1 or 0 always.
I am also attaching a snippet of what it could look like with the second switch.

I don't have access to LabVIEW for FRC right now, so I am posting a vi that could have the switch read wired into it. I have made it in the form of an FGV, but the point is just the memory of the last value should be used (through either shift register or feedback node)
Attached Thumbnails
Click image for larger version

Name:	counter_one.png
Views:	14
Size:	18.8 KB
ID:	18363  Click image for larger version

Name:	counter_two.png
Views:	10
Size:	22.7 KB
ID:	18364  
Reply With Quote
  #4   Spotlight this post!  
Unread 15-02-2015, 13:07
Zaque Zaque is offline
FF: Bobo Brigade
AKA: Zachary H.
FRC #3352 (Flaming Monkeys)
Team Role: Programmer
 
Join Date: Mar 2013
Rookie Year: 2013
Location: Belvidere, Illinois
Posts: 91
Zaque has a spectacular aura aboutZaque has a spectacular aura aboutZaque has a spectacular aura about
Re: Counting up and down with a switch

Quote:
Originally Posted by mshafer1 View Post
As GeeTwo stated, something more is really needed for what you are saying, it is possible to either put two switches, or the encoders that he suggested.
I am attaching a snippet that would count up and down as described, - note that it will go 1 or 0 always.
I am also attaching a snippet of what it could look like with the second switch.

I don't have access to LabVIEW for FRC right now, so I am posting a vi that could have the switch read wired into it. I have made it in the form of an FGV, but the point is just the memory of the last value should be used (through either shift register or feedback node)
Not to be rude, but this is the Java sub-forum, so some who do not have experience with LabVIEW may not be able to make heads or tails of the code you posted. If I get time I may be able to translate what you posted into Java pseudocode though .
__________________
Zaque (Zach) H.
President
Programming/Electrical Lead
Reply With Quote
  #5   Spotlight this post!  
Unread 15-02-2015, 14:35
mshafer1 mshafer1 is offline
Programming Mentor
AKA: Matthew Shafer
FRC #3937 (Breakaway)
Team Role: Mentor
 
Join Date: Jan 2015
Rookie Year: 2008
Location: Arkansas
Posts: 56
mshafer1 is an unknown quantity at this point
Re: Counting up and down with a switch

Quote:
Originally Posted by Zaque View Post
Not to be rude, but this is the Java sub-forum, so some who do not have experience with LabVIEW may not be able to make heads or tails of the code you posted. If I get time I may be able to translate what you posted into Java pseudocode though .
Thanks for pointing that out, Zaque. I guess I got a little ahead of myself. This is still a generic counter utility based on edges of a boolean input that could come from a DIO read, but I am sure there are other ways (like the one posted by jhersh)
To accomplish an equivalent of what I posted in Java, it would require static variables. i.e.

class Counter
{
private static boolean LastUpCount;
private static boolean LastDownCount;
private static int count;
public static int counter(boolean upCounter, boolean downCounter)
{
if(upCounter && !LastUpCount)
{
count++;
}
if(downCounter && !LastDownCount)
{
count--;
}
LastDownCount = downCounter;
LastUpCount = upCounter;
return count;
}
};

Last edited by mshafer1 : 15-02-2015 at 14:37. Reason: clarify
Reply With Quote
  #6   Spotlight this post!  
Unread 15-02-2015, 21:58
shindigo shindigo is offline
Registered User
AKA: Mike Parker
FRC #0102
Team Role: Mentor
 
Join Date: Feb 2012
Rookie Year: 2009
Location: Somerville, NJ
Posts: 33
shindigo is an unknown quantity at this point
Re: Counting up and down with a switch

Thanks everyone for all the replies. The Java code that sets the up and down source makes the most sense to me so will try that first. I'll report back my findings.
Reply With Quote
  #7   Spotlight this post!  
Unread 15-02-2015, 12:16
jhersh jhersh is offline
National Instruments
AKA: Joe Hershberger
FRC #2468 (Appreciate)
Team Role: Mentor
 
Join Date: May 2008
Rookie Year: 1997
Location: Austin, TX
Posts: 1,006
jhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond repute
Re: Counting up and down with a switch

Quote:
Originally Posted by shindigo View Post
Hi We are using counters with switches on digital inputs to check for rapid changes in the switch. We would like the counter to count up when the switch goes from unpressed to pressed and count down (to negative numbers) when it goes
from pressed to unpressed. So far the counter appears to only count up on either state change.

Any thoughts about how to get it to count down when the switch is released?

mp
You need to construct the counter in Up/Down mode.

Code:
myCounter.setUpDownCounterMode();
Then set the up source to the channel with a rising edge and the down source to the channel with a falling edge.

Code:
myCounter.setUpSource(myDigitalIn);
myCounter.setUpSourceEdge(true, false);
myCounter.setDownSource(myDigitalIn);
myCounter.setDownSourceEdge(false, true);
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 22:26.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi