Go to Post The bleachers at a FIRST competition should be full of people blasting energy out of every pore of their bodies. - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-02-2005, 02:13
Fourevilmonkies's Avatar
Fourevilmonkies Fourevilmonkies is offline
Code monkey
#1369 (Minotaur)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2005
Location: Florida
Posts: 11
Fourevilmonkies is an unknown quantity at this point
Send a message via AIM to Fourevilmonkies Send a message via Yahoo to Fourevilmonkies
Unhappy One button wonder

If there is a thread about this please redirect me to it.

I have been fiddling around with this piece of code for quite a while (to me 1 week during build season is much too long). I have come up with an alternative but I found it way too messy for the drivers of the bot. Here is the unhappy code.

//somewhere at the top of user_routines.c
unsigned static int countify = 0;

//else where in the file
if (p1_sw_top == 1){
++countify;
if (countify % 2 == 0){
relay1_fwd = 1;
relay1_rev = 0;
}
if (countify % 2 != 0){
relay1_fwd = 0;
relay1_rev = 1;
}

What the above code is supposed to do is have the driver have to only hit the button once for it to do something then the driver hits it again for it to reset itself or do something else. Idealy we don't want the driver to have to tap the top switch on the joystick.

I only get a spaztastic numatic cylinder. I already know the cause is due to the fact that the value for p1_sw_top is read about every 26.2 ms so it goes through this code like a hot knife through butter. Is there a better way to do this?

The alternate code I am using is this

if ((p1_sw_top == 1) && (p1_sw_trig == 1)){
relay1_fwd = 1;
relay1_rev = 0;
}
if ((p1_sw_top == 1) && (p1_sw_trig == 0)){
relay1_fwd = 0;
relay1_rev = 1;
}

This makes it so I can have multiple buttons set up this way. This code does what its supposed to which is when someone hits the trigger then the button and something happens then they hit the button without the trigger and the inverse happens.
__________________
Come one guys, I just want to make you go pop.
  #2   Spotlight this post!  
Unread 10-02-2005, 02:31
russell's Avatar
russell russell is offline
Registered User
#1430 (WRONG)
Team Role: Electrical
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Anchorage AK
Posts: 402
russell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to all
Re: One button wonder

how about this

Code:
//somewhere at the top of user_routines.c
int buttonstate=0;


//then later....

if(buttonstate==0 && p1_sw_trig==1)
{
//do some stuff here
buttonstate=1;
}

if(p1_sw_trig==0)
{
buttonstate=0
}
I am really sleepy and not thinking quite right just now, but that should do sort of a latching thingy. Maybe where it says to put the stuff you could increment another variable then based on the value of that variable decide what to do... something like that.
  #3   Spotlight this post!  
Unread 10-02-2005, 02:42
Bleric's Avatar
Bleric Bleric is offline
Registered User
#0624 (CRyptonite)
Team Role: Programmer
 
Join Date: Jan 2005
Location: Houston
Posts: 19
Bleric is on a distinguished road
Send a message via AIM to Bleric Send a message via MSN to Bleric
Re: One button wonder

You want to switch the solonoids once, then skip over the code each following loop, untill you let go of the trigger and push it down again, right?

Our team is doing exactly that with our pneumatics. Here's how we're doing it. (blatantly stolen from the v2.4 default code )

Code:
//somewhere at the top of user_routines.c
        int latch = 0;
//somewhere in default_routine()
	if(p1_sw_trig)
	{
		latch++;
		if(latch==1)
		{
			relay1_fwd=!relay1_fwd;
			relay1_rev=!relay1_fwd;
		}
		latch=1;
	}
	else
	{
		relay1_fwd=relay1_rev=0;
		latch=0;
	}
Works pretty good for us.
  #4   Spotlight this post!  
Unread 10-02-2005, 03:52
russell's Avatar
russell russell is offline
Registered User
#1430 (WRONG)
Team Role: Electrical
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Anchorage AK
Posts: 402
russell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to all
Re: One button wonder

Yeah, that looks good. Except what is with the latch++ in there? I would think it would work better if you took that out... Then again like I said, Im sleepy. I think I will go to bed now.


[disregard]We might get a snow day tomorrow here in Alaska. Theyre pretty rare. We have to have a good six inches everywhere before they even consider it. Yeah I know that was irrelevant . Sorry.[/disregard]
  #5   Spotlight this post!  
Unread 10-02-2005, 04:39
Fourevilmonkies's Avatar
Fourevilmonkies Fourevilmonkies is offline
Code monkey
#1369 (Minotaur)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2005
Location: Florida
Posts: 11
Fourevilmonkies is an unknown quantity at this point
Send a message via AIM to Fourevilmonkies Send a message via Yahoo to Fourevilmonkies
Re: One button wonder

I dunno about that it seems a bit iffy to me but I'll try it out tonight when I can get my hands on the programing computer for the teams robot. Thanks.

Scratch that... It looks good to me except for the latch = 1; at the end of the if statement but other than that it looks good. 5 ish AM is not exactily my prime for thinking.
__________________
Come one guys, I just want to make you go pop.

Last edited by Fourevilmonkies : 10-02-2005 at 04:56.
  #6   Spotlight this post!  
Unread 10-02-2005, 09:43
zarf's Avatar
zarf zarf is offline
Only C Programming Mentor on Team
AKA: Joseph Pride
#0500 (Coast Guard Raptors)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2005
Location: Connecticut
Posts: 3
zarf is an unknown quantity at this point
Send a message via AIM to zarf
Re: One button wonder

Here's what seems to be the problem, at least to me. You're doing this on your slow loop, every time, right? Because it seems to me that, with the if statement how it is, it will re-analyze every time it goes through the slow loop, and keep turning the value "on" and "off" until you let go of the joystick button. This makes it basically random, which value it's going to end up with when you let go. I think in most cases, the slow loop still runs faster than a player can reasonably press and release the joystick button.

So. You want to activate this routine only when you have _just_ turned on the button, and not again and again as long as you have the button held.

Better would be to define a global variable unsigned char ButtonON = 0; and then check to see if the button was already on during the last loop before you act on it. So inside your slow loop, here's what you would do:

unsigned char Countify = 0;

if (p1_sw_top == 1 && ButtonON = 0){
ButtonOn = 1;
if (relay1_fwd == 0 || relay1_rev == 1){
relay1_fwd = 1;
relay1_rev = 0;
}
else{
relay1_fwd = 0;
relay1_rev = 1;
}
}
else if (ButtonON == 1 && p1_sw_top == 0)
ButtonON = 0;

This means, that if your button was on the last time you came through the slow loop and it's still on now, it doesn't count as another press of the button.

Good luck!
--Zarf
  #7   Spotlight this post!  
Unread 10-02-2005, 11:05
russell's Avatar
russell russell is offline
Registered User
#1430 (WRONG)
Team Role: Electrical
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Anchorage AK
Posts: 402
russell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to allrussell is a name known to all
Re: One button wonder

Thats the same thing that Bleric's code does, except I am still wondering about the latch++ and I am thinking you might want to change the if(latch==1) to say if(latch==0). Then again maybe I am just reading it wrong.
  #8   Spotlight this post!  
Unread 10-02-2005, 13:17
Bleric's Avatar
Bleric Bleric is offline
Registered User
#0624 (CRyptonite)
Team Role: Programmer
 
Join Date: Jan 2005
Location: Houston
Posts: 19
Bleric is on a distinguished road
Send a message via AIM to Bleric Send a message via MSN to Bleric
Re: One button wonder

Any of the above code segments will work =P

Hmm... come to think of it, you're right russell. latch++ doesn't really do anything

In the original code, we didn't have "latch=1" at the end of the first if statement. Meaning, latch would count the number of cycles we'd held the button down. "if(latch==1)" made sure we only executed that bit of code on the first loop we'd held the button.

Later, we added latch=1 to make sure that variable wouldn't wrap around to zero after 32,767 loops

That being said... I'll take out that "latch++" stuff as soon as I can get my hands on the code.
  #9   Spotlight this post!  
Unread 10-02-2005, 14:39
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: One button wonder

Ask any of my students what my favorite question is and they'll answer "What is the definition of a button tap?". I grilled them with that question until they finally understood it. A button tap is when the button is currently pressed and it wasn't pressed the last time you looked at it. That's exactly what you want. You want to transition the relay only once per button press.

Code:
static char p1_sw_top_prev = 0;

// check if the button is pressed and last loop it wasn't pressed
if ((p1_sw_top == 1) && (p1_sw_top_prev == 0))
{
  // now transition your relay
  if (relay1.fwd == 1)
  {
    relay1.fwd = 0;
    relay1.rev = 1;
  }
  else
  {
    relay1.fwd = 1;
    relay1.rev = 0;
  }
}

p1_sw_top_prev = p1_sw_top;
Closed Thread


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking to borrow a button maker... 1685isalive General Forum 0 31-01-2005 14:03
disabling a button gomez6760 Programming 1 12-08-2004 11:54
Button Programing BobcatProgramer Programming 6 25-02-2004 16:43
which button machine do you use and do you like it? KenWittlief General Forum 10 16-10-2003 00:01
Select button and user mode n[ate]vw Programming 2 20-01-2003 16:33


All times are GMT -5. The time now is 19:03.

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