Go to Post Dean Kamen is my Shaquille O'niel (or any other generic sports star). - Daniel Brim [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
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 14-01-2014, 12:28
Sparkyshires Sparkyshires is offline
Registered User
AKA: Michael Shires
FRC #0384 (Sparky)
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2006
Location: Virginia
Posts: 226
Sparkyshires is an unknown quantity at this point
TOGGLE code not working

Hey guys! I know it's kinda early in the season for code issues, but I've got a question about toggle code. How come this:

Code:
bool Toggle, Button2Value;
Button2Value = false;
Toggle = false;
if(stick1.GetRawButton(2))
{
	if(Button2Value == false)
	{
		Button2Value = true;
		Toggle = !Toggle;
	}
}
else
{
	Button2Value = false;
}
would not change the value of Toggle afterwards? the idea is that button 2 can be pressed for an unlimited amount of time, then let go and the value of Toggle would be switched, until it was pressed again for an undefined amount of time. However, whenever I run it the Toggle variable never seems to change value. If the rest of the code is required, just ask and I'll post it.
__________________
"Measure with a micrometer, mark with chalk, cut with an axe."

Last edited by Sparkyshires : 14-01-2014 at 12:43.
Reply With Quote
  #2   Spotlight this post!  
Unread 14-01-2014, 13:17
Caleb Sykes's Avatar
Caleb Sykes Caleb Sykes is offline
Registered User
FRC #4536 (MinuteBots)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2009
Location: St. Paul, Minnesota
Posts: 1,057
Caleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond repute
Re: TOGGLE code not working

Where is the loop for this program? Is everything that you posted inside of the same loop? If so, it seems that you are redeclaring and reinitializing your variables at every cycle, which is generally not good.
Reply With Quote
  #3   Spotlight this post!  
Unread 14-01-2014, 13:43
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,170
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Re: TOGGLE code not working

It would help to see the code. This looks like it should kinda work. You are setting the variable you intend to toggle to zero each time through this code, hopefully it doesn't really work that way. And if you are not using the Toggle variable anywhere the compiler may have optimized (deduced that the value of Toggle does not really matter) the whole thing away.

HTH
__________________
Fast, cheap or working - pick any two!
Reply With Quote
  #4   Spotlight this post!  
Unread 14-01-2014, 14:39
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 132
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: TOGGLE code not working

Code:
Button2Value = false;
Toggle = false;

if(stick1.GetRawButton(2))
{
  	if(Button2Value == false)
  	{
  	  	Button2Value = true;
  	  	Toggle = !Toggle;
  	}
}
else
{
  	Button2Value = false;
}
The rest of the code may be important. For instance if this is called in a function the toggle value will be reset every time the function is called. Are you sure the same Toggle is in scope when you check? If either of these is the case put the declaration and initialization of Toggle at a level where is is seen and changed appropriately (class level as public member perhaps).

When button 2 is pressed Toggle will change immediately. When the button is released it will only change the Button2Value on the next call, not right away. Are you sure the calling code allows for that? For instance if you check button 2 is true before calling this code Button2Value would never reset.

There is also a chance that 'bounce' is your problem. Where the button connection is bouncing between true and false at the moment contact is just being made or broken. See http://en.wikipedia.org/wiki/Switch 'contact bounce'. There are a couple of ways to handle that, usually a timer that enforces a minimum time between state changes. A tenth of a second is probably more than sufficient.
Reply With Quote
  #5   Spotlight this post!  
Unread 14-01-2014, 17:58
Sparkyshires Sparkyshires is offline
Registered User
AKA: Michael Shires
FRC #0384 (Sparky)
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2006
Location: Virginia
Posts: 226
Sparkyshires is an unknown quantity at this point
Re: TOGGLE code not working

Shoot!!! yup...the initialization should be outside of the while loop. We don't work on tuesdays so I'll test it tomorrow, but ya'll think this should work?

Code:
#include "WPILib.h"

class RobotDemo : public SimpleRobot
{
	RobotDrive myRobot; // robot drive system
	Joystick stick1; // only joystick
	Victor Left1, Left2, Right1, Right2; // drive motors

public:
	RobotDemo():
		myRobot(1, 2),	// these must be initialized in the same order
		stick1(1),	// as they are declared above.
		Left1(9),
		Left2(7),
		Right1(8),
		Right2(6)
	{
		myRobot.SetExpiration(0.1);
	}

	/**
	 * Drive left & right motors for 2 seconds then stop
	 */
	void Autonomous()
	{
		myRobot.SetSafetyEnabled(false);
		myRobot.Drive(-0.5, 0.0); 	// drive forwards half speed
		Wait(2.0); 				//    for 2 seconds
		myRobot.Drive(0.0, 0.0); 	// stop robot
	}
	void OperatorControl()
	{
		myRobot.SetSafetyEnabled(true);
		bool BackwardToggle, Button2Value;
		float Xaxis, Yaxis, LeftDrive, RightDrive, NecessaryTest;
		BackwardToggle = false;
		Button2Value = false;
		while (IsOperatorControl())
		{
			if(stick1.GetRawButton(2))
			{
				if(Button2Value == false)
				{
					Button2Value = true;
					BackwardToggle = !BackwardToggle;
				}
			}
			else
			{
				Button2Value = false;
			}
			Yaxis = stick1.GetRawAxis(2);
			Xaxis = stick1.GetRawAxis(1); // x is 1, y is 2
			RightDrive = Xaxis + Yaxis;
			LeftDrive = Xaxis - Yaxis;
			NecessaryTest = LeftDrive - RightDrive;
			if(NecessaryTest < 0.1 && NecessaryTest > -0.1)
			{
				NecessaryTest = (LeftDrive+RightDrive)/2;
				LeftDrive = NecessaryTest;
				RightDrive = NecessaryTest;
			}
			if(stick1.GetTrigger())
			{
				if(stick1.GetRawButton(2) == false)
				{
					Left1.Set(LeftDrive);
					Left2.Set(LeftDrive);
					Right1.Set(RightDrive);
					Right2.Set(RightDrive);
				}
				else if(stick1.GetRawButton(2) == true)
				{
					Left1.Set(RightDrive);
					Left2.Set(RightDrive);
					Right1.Set(LeftDrive);
					Right2.Set(LeftDrive);
				}
			}
			else
			{
				Left1.Set(0.0);
				Left2.Set(0.0);
				Right1.Set(0.0);
				Right2.Set(0.0);
			}
			Wait(0.005);				// wait for a motor update time
		}
	}
	
	/**
	 * Runs during test mode
	 */
	void Test() {

	}
};

START_ROBOT_CLASS(RobotDemo);
__________________
"Measure with a micrometer, mark with chalk, cut with an axe."
Reply With Quote
  #6   Spotlight this post!  
Unread 18-01-2014, 08:16
MrRoboSteve MrRoboSteve is offline
Mentor
AKA: Steve Peterson
FRC #3081 (Kennedy RoboEagles)
Team Role: Mentor
 
Join Date: Mar 2012
Rookie Year: 2011
Location: Bloomington, MN
Posts: 578
MrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond reputeMrRoboSteve has a reputation beyond repute
Re: TOGGLE code not working

One subtlety of programming that new programmers often miss out on is correctly naming variables.

In your program, I'd consider renaming Button2Value to Button2WasPressed, BackwardToggle to DriveBackwards, NecessaryTest to something like SpeedDifference, and LeftDrive/RightDrive to LeftPower/RightPower. You can easily do so by right clicking on the variable, and selecting Refactor -> Rename.

Making those changes makes your program easier to comprehend.

Down in the if (stick1.getTrigger()) blocks are you intending to change those to use BackwardToggle?

You don't need to say

else if(stick1.GetRawButton(2) == true)

because it's the opposite of the first test for false -- you can just say "else".

Is it your intent for driving to work only when the trigger is pulled?
__________________
2016-17 events: 10000 Lakes Regional, Northern Lights Regional, FTC Burnsville Qualifying Tournament

2011 - present · FRC 3081 Kennedy RoboEagles mentor
2013 - present · event volunteer at 10000 Lakes Regional, Northern Lights Regional, North Star Regional, Lake Superior Regional, Minnesota State Tournament, PNW District 4 Glacier Peak, MN FTC, CMP
http://twitter.com/MrRoboSteve · www.linkedin.com/in/speterson
Reply With Quote
  #7   Spotlight this post!  
Unread 18-01-2014, 15:15
Sparkyshires Sparkyshires is offline
Registered User
AKA: Michael Shires
FRC #0384 (Sparky)
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2006
Location: Virginia
Posts: 226
Sparkyshires is an unknown quantity at this point
Re: TOGGLE code not working

yeah, it's designed to only be driven with the trigger just so that people cant accidentally knock the joystick and have the robot move.

and yeah my mentor didn't want to be able to switch it into reverse unless the trigger was not being pressed, that way the driver couldn't damage the motors.
__________________
"Measure with a micrometer, mark with chalk, cut with an axe."
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 14:01.

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