View Single Post
  #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