commands activated by trigger, how?

I have recently begun learning Java as my team is in need of a programmer next year and have hit a road block. I am trying to make the trigger on the standard joystick run a specific command when pressed. The tutorials I have been following so far are these. My initial thoughts were it would be very similar to making a button run a specific command however I can’t seem to get it to work. I have searched the web high and low but can’t find anything useful.

You are correct. It would be helpful if you posted the code that you tried and what didn’t work so that people can provide more helpful suggestions.

Is is a boolean trigger or a variable one? If it’s variable, like the triggers on an xBox controller, it’s called as a separate joystick axis. Which joystick are you using?

Some of the code that I have tried thus far is:

//code trial 1
Trigger DT1 = new Joystick.getTrigger(DriveStick);

//code trial 2
package edu.wpi.first.Command_Learner;


import edu.wpi.first.wpilibj.Joystick;

public class OI {
    Joystick DriveStick = new Joystick(1);
    Joystick SecondaryStick = new Joystick(2);
    
    public OI(){
        if(DriveStick.getTrigger()){
            LauncherSpinWheels();
        }
        else{
            LauncherStopWheels();
        }
}
        
        
    }
//code trial 3
package edu.wpi.first.Command_Learner;


import edu.wpi.first.Command_Learner.commands.LauncherSpinWheels;
import edu.wpi.first.wpilibj.Joystick;


public class OI {
    Joystick DriveStick = new Joystick(1);
    Joystick SecondaryStick = new Joystick(2);

    public boolean ST1;
    
    public OI(){
        ST1 = DriveStick.getTrigger();
        if(ST1=true){
            new LauncherSpinWheels();}

I have been working on this more tonight and think I have gotten close in my 3rd trial of code(seen above). The only issue I am having is that the line with the command “LauncherSpinWheels.” In netbeans it underlines in yellow indicating an error and says “New Instance ignored.” If you need more in terms of my code please just let me know.

As for the Joystick, I am trying to get the trigger off of an attack 3 Logitech joystick(so it’s a Boolean trigger).

[strike]I don’t think the getTrigger() method does what you are hoping it does.
If you take a look at thejavadoc for the Joystick class, then look at the getTrigger() method, you’ll see that it doesn’t return a boolean value.[/strike]

The triggers on the logitech [edit] F310 [/edit](and xbox if I remember correctly) are on axis 3. Axes in the Joystick class return doubles, in this case, a numerical value from 1.0 to -1.0. So when one trigger is pulled all the way, axis 3 reads 1.0. When the other is pulled all the way, axis 3 reads -1.0. When they are both pulled simultaneously, the values are essentially added. So if you were to pull them both down all the way you would read 0.0 on axis three. This is important to understand. It is the reason why you wouldn’t put two commands that needed to be used at the same time onto the same axis. Both triggers pressed looks the same as neither trigger pressed.

To get back to your question though… if you want to kick off commands from an analog axis on a joystick, then you need to turn that axis into a button. You need to turn a value of 1.0 to -1.0 (double) into true or false (boolean).

I developed a class for my team last year which does just this. I’ve tried to clean up the code and document how it should be used. It is available here. There is an example of its use in the comment at the top of the code.

Please let me know if you have any questions.

The getTrigger method does return a boolean. The trigger on the Logitech Attack 3 Joystick is button 1.

To use the trigger treat it exactly like a button.

You’re right, I was thinking about the the Logitech F310… and I apparently can’t read the javdoc I linked lol.

Well what I posted earlier is applicable to using using axes as buttons if anyone needs that lol.

Thank you for the help, this is will be very useful for later on(as I think my team will be switching to Gamepads in the near future) however I think there has been a misunderstanding as to what I would like to control the robot with. I am trying to use the standard Attack 3 Joystick, not a gamepad like this one.

From what I can tell the attack 3 should be returning a Boolean as there is only one per joystick and there is only 2 states(pressed and not pressed). Please correct me if I am wrong as I know very little so far and could easily be mistaken.

Thank you very much! I couldn’t find anything saying it was actually just button 1 and didn’t realize that in all the documentation that button one was never listed(just trigger).

The first step I would recommend when using any USB input device for FRC is to document the button and axis mapping and ranges. The easiest way to do this is to (on Windows 7, XP may or may not vary slightly):

  1. Click the Start Menu
  2. In the run box type “joy.cpl” and press Enter
  3. Select the desired device from the list and click Properties
  4. Select the Test tab if it is not selected already

The X axis (horizontal) and Y axis (vertical) are represented by the crosshair in the box. Axes 3-6 are represented in order, top to bottom, by bars to the right of the box. The buttons are represented below and light up when pressed. The first 6 axes (including X/Y) and 12 buttons will be transmitted, if your device has more they will be ignored.

There is online documentation for CommandBased programming, in particular this link shows how to attach commands to a joystick.


Joystick DriveStick = new Joystick(1);
Button button = new JoystickButton(joystick, BUTTON_NUMBER);
public OI() {
    button.whenPressed(new LauncherSpinWheels());
    // ...
}

Replace BUTTON_NUMBER with the button you want to run the command.

In addition to the online documentation, you might find these videos helpful if you’re having trouble understanding CommandBasedProgram.:
CommandBased Programming with RobotBuilder: http://www.youtube.com/watch?v=k7PaYcjDEDc&list=PLYA9eZLlgz7t9Oleid2wtlgnvhGObeKzp
CommandBased Programming without RobotBuilder: http://www.youtube.com/watch?v=v0vt9yKLxUQ