Problem using GetRawButton on joystick (JAVA)

Hi,

My team and I has been trying to code a joystick (Logitech X3D Pro).
What we want to achieve is a motor to run when the trigger is pressed, and not run when the trigger is not pressed.

Here’s what we’ve got so far:

//Right Motors
private final VictorSP rightmotor1 = new VictorSP(0);
private final VictorSP rightmotor2 = new VictorSP(1);

// Left Motors
private final VictorSP leftmotor1 = new VictorSP(2);
private final VictorSP leftmotor2 = new VictorSP(3);

//Shooter
private final VictorSPX shooter1 = new VictorSPX(4);

//Speed Controller Groups
private final SpeedControllerGroup rightSpeedGroup = new SpeedControllerGroup(rightmotor1, rightmotor2);
private final SpeedControllerGroup leftSpeedGroup = new SpeedControllerGroup(leftmotor1, leftmotor2);

//drivetrain
DifferentialDrive drivetrain = new DifferentialDrive(rightSpeedGroup, leftSpeedGroup);

//Joystick
Joystick stick = new Joystick(1);

@Override
public void teleopPeriodic() {

drivetrain.arcadeDrive(stick.getY()^3, stick.getZ()^3);

if (stick.getRawButton(1)==True){
shooter.set(1);
} else {
shooter.set(0);

When I press the trigger button, it does nothing, but when I move foward (arcade drive), the motor goes foward, and when I go reverse, it goes reverse too… Im confused…

My team and I are new to programming (our programming mentor left last year). If anyone has any tutoriasl in either JAVA or Labview on how to program a robot from scratch, please foward it to me :slight_smile:

Any help would be appreciated!

Thanks,

Matt

Welcome to the forums. Your project is where we start when any of our members are learning to program. I think you are in a good place (both with what you are attempting and asking for help here).

The first piece of advice I would give is to wrap all your code in the code block by using three ` ticks before and after your code. This makes it easier to read.

When we have this issue with a joystick, we use the driver station software to ensure the buttons we think we are pressing are the ones we are sending to the software.

Then, when we know that is not the issue, we look for bugs in the code.

Take a look at the code you have, are there any typos that would make the code not work as expected? Is the code above the exact code that is on the robot?

Finally, if the code is fine, we look at the hardware. Is everything plugged in and set up correctly (and is the code referring to the correct io or CAN numbers)?

Do any of these suggestions help? Let us know if you need a bit more help, and I think we can help out.

1 Like

Thanks for you reply. I highly doubt that the problem comes from the hardware, since the motor is actually spinning.

Im actually thinking about 2 things:

  1. Why would the shooter1 run when im using the joystick (my arcane drive code only refers to the first 4 motors…)
  2. When using the dashboard, I can see that it sees the trigger when I press it (but is it really the (1)?

What we are concerned about is that you think shooter1 is on CAN id 4. When a logical explanation of this problem would be if the real shooter motor was actually CAN id 0-3. When you move the joystick do you see the light on each of the 4 motor controllers running your drive change?

There are TONS of different tutorials and example code out there. If you know anything you are specifically looking for just ask! I highly recommend starting with frc-docs which is the official documentation.

1 Like

I think you do have a hardware problem then. When this happens to us, and it does, it is almost always that the motors are not plugged into the ports (or assigned the CAN ids) we think they are.

So your shooter1 is actually assigned Canid 1-4. As a side note, this why we always lift our drive wheels off the ground when testing things until we are certain everything is assigned correct ids. Any time we change them (in hardware or software), we lift the robot off the ground and clear the body of obstacles (human or otherwise) again. These motors are powerful.

The button issue is a different one I think. I do see a typo in the code posted above. You declare shooter1, but trigger shooter with the joystick.

As I write this, I am actually surprised that it builds. I do not see shooter being constructed. This is why I asked if this code was actually what is on the robot.

If that was not the issue, my other hypothesis is that if you lifted up the robot, you may see another wheel move when you press the button. If not, press other buttons on the joystick (while staying clear of the anything that moves. We always forget if joystick buttons start at 0 or 1, but hitting all of the buttons to find the one we want works without fail. Once we know that button x = the first led in the DS, then we know where the enumeration begins and can find the others.

The troubleshooting steps I posted above help us find these common errors 90% of the time. When that fails, we take variables out of the equation (we will use a print statement in our if statement to ensure we are getting the button press. We will use the simulator to bypass hardware. We may purposely change patching to a known motor port and PDP channel or switch a breaker on the PDP, etc.

Each one of these actions changes a variable. When we change all of them at once, it becomes difficult to figure out what the problem is. But, one at a time, it works well.

1 Like

Something i forgot to mention! The VictorSPX is plugged via PWM and not CAN. We felt like it would be easier to work with.

1 Like

Welcome to the forums. This is where we start when learning programming, so I think you are in a good place (both what you are attempting and asking for help here).

The first advice I would give is to wrap all your code in the code block by using three

…backticks ` either side of your code to format it properly for readability.

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot;

import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.VictorSP;
import edu.wpi.first.wpilibj.WPI_VictorSPX;
import edu.wpi.first.wpilibj.VictorSPX;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import com.ctre.phoenix.motorcontrol.can.TalonFX;

import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import com.ctre.phoenix.motorcontrol.TalonFXInvertType;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;

import edu.wpi.first.wpilibj.SlewRateLimiter;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;




/**
 * The VM is configured to automatically run this class, and to call the functions corresponding to
 * each mode, as described in the TimedRobot documentation. If you change the name of this class or
 * the package after creating this project, you must also update the build.gradle file in the
 * project.
 */
public class Robot extends TimedRobot {
  /**
   * This function is run when the robot is first started up and should be used for any
   * initialization code.
   */

   //Right Motors
  private final VictorSP rightmotor1 = new VictorSP(0);
  private final VictorSP rightmotor2 = new VictorSP(1);
  
   // Left Motors
  private final VictorSP leftmotor1 = new VictorSP(2);
  private final VictorSP leftmotor2 = new VictorSP(3);
  
  //Shooter
  private final VictorSPX shooter1 = new VictorSPX(4);
  

  //Speed Controller Groups
  private final SpeedControllerGroup rightSpeedGroup = new SpeedControllerGroup(rightmotor1, rightmotor2);
  private final SpeedControllerGroup leftSpeedGroup = new SpeedControllerGroup(leftmotor1, leftmotor2);


  //drivetrain
   DifferentialDrive drivetrain = new DifferentialDrive(rightSpeedGroup, leftSpeedGroup);

   //Joystick
   Joystick stick = new Joystick(1);

  @Override
  public void robotInit() {}

  @Override
  public void robotPeriodic() {}

  @Override
  public void autonomousInit() {}

  @Override
  public void autonomousPeriodic() {}

  @Override
  public void teleopInit() {}

  @Override
  public void teleopPeriodic() {

    drivetrain.arcadeDrive(stick.getY()^3, stick.getZ()^3);

    if (stick.getRawButton(1)==True){
    shooter1.set(0.8);
    } else {
    shooter1.set(0);
    }
    
    


  }

  @Override
  public void disabledInit() {}

  @Override
  public void disabledPeriodic() {}

  @Override
  public void testInit() {}

  @Override
  public void testPeriodic() {}
}
1 Like

That is my fault I should have known from the VictorSP. The advice however is the same. Looks to see what the light on each motor controller is doing when you use the joystick. Are all 5 changing?

It is the highest probability that you have one of the channels swapped.

1 Like

Looking at your code, I see multiple issues:

  • boolean literals in Java are lowercase: true and false; I see True used in your code. Also, comparing to true is redundant.
  • the ^ operator in Java is likely not what you think it is; use Math.pow. FYI, arcadeDrive() squares its inputs (with sign preservation) by default, so what you’re doing with that operator might be redundant.
  • For CTRE devices over CAN, use the WPI_ variants (WPI_VictorSPX and not VictorSPX). I’m also seconding the CAN ID issue that others mentioned, use Phoenix Tuner to ensure that the motor controller is on the CAN bus with the right ID.

I hope that at least some of this helps.

The capital T True is for the capital B Boolean where true is for boolean, though through autoboxing true should become True. Though you should not need to even need the == operation as if (booleanCondition == true) is the same as if (booleanCondition)

As for the ^ operator that is a bitwise exclusive or and is definately not a power operation.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.