Button programin C++

so my team is using mecanum wheels this year. we would like to make it so when you hit the trigger on the left joystick it reduces the maximum speed of the motor by half. so im my code i have this

 bool GetTrigger (Joystick hand = leftstick); 

if im right that checks if the trigger on the left joystick it pushed or not.
i was trying to create an if statement to change the speed of the motors but its not working.

any suggestions?

Wouldn’t doubling the speed make it go faster unless you puss the trigger?

What I’m looking to do is while in operator control drive mecanum drive full power, but when you get close to the pegs you press the trigger and you drive at half the speed.

Does that make sense?

Try this:

if ( !stick.GetTrigger()){
motors.Set(x);
}
else{
motors.Set(x/2);
}

This code assumes that x is the input variable that you use for speed. For driver control, this would be the value you get from your joysticks. It works by checking to see if the trigger is pressed. If not, then the speed is just as normal. If the trigger is pressed, then the speed is half of what it would normally be. Hope this is what you meant.

That’s what I ment thanks :smiley:

would i put that before:

bool GetTrigger…

of after it
cause i just tried to build it and i got a boat load of errors

If your joystick’s name in the program is “hand” then the portion of the code that needs to be the argument for the if statement is as follows: “hang.GetTrigger()”.

The code segment would look like

if ( !hand.GetTrigger()){
motors.Set(x);
}
else{
motors.Set(x/2);
}

You don’t need a boolean variable for this code, only the value coming directly from the joystick.

it dosent like motors.set(x)

do i have to declare each motor

leftmotor.set(x),rightmotor…

I am sorry, I forgot to mention that the “motors.Set()” section was just a stand in for whatever functions you use to move. You would either replace the “motors” part with the motors that you declared earlier in your program or replace the “motors.Set()” function with whatever function you were using to drive.

For example, if you declared your motors previously as LeftMotor and RightMotor, then the code would look like:

if ( !stick.GetTrigger()){
LeftMotor.Set(x);
RightMotor.Set(x);
}
else{
LeftMotor.Set(x/2);
RightMotor.Set(x/2);
}

Sorry about forgetting to include that little tidbit. If you have any more problems with your code, post a copy of the driver section so we can see what might be the problem.

i tried to put in left motor and other stuff

here is the code

#include "WPILib.h"
#include "Vision/AxisCameraParams.h"

/**
 * This is a demo program showing the use of the RobotBase class.
 * The SimpleRobot class is the base of a robot application that will automatically call your
 * Autonomous and OperatorControl methods at the right time as controlled by the switches on
 * the driver station or the field controls.
 */ 
class RobotDemo : public SimpleRobot
{
	RobotDrive myRobot; // robot drive system
	Joystick leftstick; // only joystick
	Joystick rightstick; // only joystick 
	HSLImage image;

public:
	RobotDemo(void):
		myRobot(1, 2, 3, 4),	// these must be initialized in the same order
		leftstick(1),		// as they are declared above.
		rightstick(2)		// as they are declared above.
	{
		myRobot.SetExpiration(0.1);
	}

	/**
	 * Drive left & right motors for 2 seconds then stop
	 */
	void Autonomous(void)
	{
		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
	}

	/**
	 * Runs the motors with arcade steering. 
	 */
	void OperatorControl(void)
	{
		myRobot.SetSafetyEnabled(true);
		AxisCamera &robocam = AxisCamera::GetInstance();
		robocam.WriteResolution((AxisCamera::Resolution_t)3);
		robocam.WriteBrightness(0);
		Wait(3.0);
		
		while (IsOperatorControl())
		{
			myRobot.MecanumDrive_Cartesian(leftstick.GetX(), leftstick.GetY(), rightstick.GetY(), 0); //Mecanum drice, float x,float y, float rotation, gyro
			bool GetTrigger(Joystick hand = leftstick);
			if (!leftstick.GetTrigger()){
				motors.Set(x));
			}
		else{
			motors.Set(x/2);
		}
			robocam.GetImage();
			image.GetImaqImage();
			Wait(0.005);				// wait for a motor update time
		}
	}
};

START_ROBOT_CLASS(RobotDemo);

Thanks for the help sorry to be a bothersum and ask so many question this is my first time using c++ :stuck_out_tongue:

If you want it to toggle, (untested)


bool hasJoystickBeenClicked(Joystick &joystick, bool &prev, bool &cur) {
  prev = cur;
  cur  = joystick.GetTrigger();

  // Previous joystick value should be true (pressed trigger) and 
  // current one should be false (released trigger).
  return (prev && !cur) ? true : false;
}

void getMotorSpeed(float &speed, bool half) {
  return (half) ? speed / 2 : speed;
}

void toggleHalfSpeed(bool &half) {
  half = !half;
}

// ...

if (hasJoystickBeenClicked(joystick, prev, cur))
  toggleHalfSpeed(isHalfSpeed);

// ...

motor.Set(getMotorSpeed(speed, isHalfSpeed));


EDIT: More code.

void OperatorControl(void)
{
myRobot.SetSafetyEnabled(true);
AxisCamera &robocam = AxisCamera::GetInstance();
robocam.WriteResolution((AxisCamera::Resolution_t)3);
robocam.WriteBrightness(0);
Wait(3.0);

	while (IsOperatorControl())
	{
		if (!leftstick.GetTrigger()){
			myRobot.MecanumDrive_Cartesian(leftstick.GetX(), leftstick.GetY(), rightstick.GetY(), 0);// mecanum drive at full speed
		}
	        else{
			myRobot.MecanumDrive_Cartesian(leftstick.GetX()/2, leftstick.GetY()/2, rightstick.GetY()/2, 0);// mecanum drive at full speed
	        }
		robocam.GetImage();
		image.GetImaqImage();
		Wait(0.005);				// wait for a motor update time
	}

Try this for your OperatorControl function. This should work for you. And have fun with the mecanum drive, it’s alot of fun once you get used to driving it.

You just need to replace the motor.set() with myRobot.Drive().

Here is your code with the replacements made. It compiles.

#include "WPILib.h"
#include "Vision/AxisCameraParams.h"

/**
 * This is a demo program showing the use of the RobotBase class.
 * The SimpleRobot class is the base of a robot application that will automatically call your
 * Autonomous and OperatorControl methods at the right time as controlled by the switches on
 * the driver station or the field controls.
 */ 
class RobotDemo : public SimpleRobot
{
	RobotDrive myRobot; // robot drive system
	Joystick leftstick; // only joystick
	Joystick rightstick; // only joystick 
	HSLImage image;

public:
	RobotDemo(void):
		myRobot(1, 2, 3, 4),	// these must be initialized in the same order
		leftstick(1),		// as they are declared above.
		rightstick(2)		// as they are declared above.
	{
		myRobot.SetExpiration(0.1);
	}

	/**
	 * Drive left & right motors for 2 seconds then stop
	 */
	void Autonomous(void)
	{
		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
	}

	/**
	 * Runs the motors with arcade steering. 
	 */
	void OperatorControl(void)
	{
		myRobot.SetSafetyEnabled(true);
		AxisCamera &robocam = AxisCamera::GetInstance();
		robocam.WriteResolution((AxisCamera::Resolution_t)3);
		robocam.WriteBrightness(0);
		Wait(3.0);
		
		while (IsOperatorControl())
		{
			myRobot.MecanumDrive_Cartesian(leftstick.GetX(), leftstick.GetY(), rightstick.GetY(), 0); //Mecanum drice, float x,float y, float rotation, gyro
			bool GetTrigger(Joystick hand = leftstick);
			if (!leftstick.GetTrigger()){
				myRobot.Drive(1.0,0.0);
			}
		else{
			myRobot.Drive(0.5,1.0);
		}
			robocam.GetImage();
			image.GetImaqImage();
			Wait(0.005);				// wait for a motor update time
		}
	}
};

START_ROBOT_CLASS(RobotDemo);

discokittys worked

random question for you guys, i know it is off the topic but…

why isnt my camera sending an image to my driverstation?

Oops i put in myRobot.Drive(0.5,1.0); for your half speed…that would make you turn sharply! you need myRobot.Drive(0.5,0.0);

For the button, you could just use this:


if (leftStick.GetRawButton(1) == true) {

}

because the trigger on the joysticks is just button 1. All buttons on the Logitech ATK3’s are labeled. That’s why FIRST picked them. Also, that little roller thingy is the Z-axis. AKA, Axis 3.

X = 1
Y = 2
Z = 3

for controllers with 2 joysticks (like xbox or 360),

rotX = 4
rotY = 5

Have you configured the camera using the Camera Configuration Tool that was part of the Utilities Update? See the instruction in this document in the section named “How To Configure Your Camera”: http://www.usfirst.org/sites/default/files/uploadedFiles/Robotics_Programs/FRC/Game_and_Season__Info/2012_Assets/Getting%20Started%20with%20the%202012%20FRC%20Control%20System_2.pdf