The method getRawAxis(int) is undefined for the type Joystick Java(6710864)

I’m a rookie programmer and I am trying to make a simple program that moves a single motor with increasing speed. I have 3 values the motor can hit based on the position of the joystick, these are constants creep, slow drive and, fast drive. This is the command I wrote, I get an error “the method getRawAxis(int) is undefined for the type Joystick Java(6710864)” whenever I use “getRawAxis”. I think that for some reason getRawAxis isn’t importing, but I looked it up and edu.wpi.first.wpilibj.GenericHID; should work. I have no idea what to do and no other programmer in my team can figure it out. Forgive my weird code, I’m a scrub.

package frc.robot.commands;

import edu.wpi.first.wpilibj.Joystick;

import edu.wpi.first.wpilibj.command.Command;

import frc.robot.constants;

import frc.robot.Robot;

import edu.wpi.first.wpilibj.GenericHID;

public class JoyStick extends Command {

private JoyStick stick;

public JoyStick(JoyStick stick, double deadzone) {

super("Joystick");

requires(Robot.drivetrain);

this.stick = stick;

this.deadzone = deadzone; 

}

protected void initialize() {

}

protected void execute() {

double thrust=0;

double turn=stick.getRawAxis(3);

if(stick.getRawAxis(3)){

    thrust = 0;

  }

else if (stick.getRawAxis(3)>=0.1&&stick.getRawAxis(3)<=0.4){

  thrust = constants.kCreep; 

}

else if (stick.getRawAxis(3)>0.4&&stick.getRawAxis(3)<=0.7){

  thrust = constants.kSlowDrive; 

}

else if (stick.getRawAxis(3)>0.7&&stick.getRawAxis(3)<=1.0){

  thrust = constants.kFastDrive; 

}

}

}

@Override

protected boolean isFinished() {

return false;

}

@Override

protected void end() {

}

@Override

protected void interrupted() {

}

}

You have named your class JoyStick and then defined the stick as a JoyStick. The real java class for a joystick is Joystick. So change private JoyStick stick; to private joystick stick;.

Actually you should avoid naming your classes with names close to other classes, your code gets too confusing and in the panic between matches you will get errors.

-Jim

Also, you can condense the execute code to:

double speed=stick.getRawAxis(3);

if(speed > 0.7)
{
  thrust = constants.kFastDrive; 
}
else
{
  if(speed > 0.4)
  {
    thrust = constants.kSlowDrive;
  }
  else
  {
    if(speed > 0.1)
    {
      thrust = constants.kCreep;
    }
    else
    {
      thrust = 0;
    }
  }
}

This is a step if statement, it will step through the speeds without needing to check the upper range.

-Jim

Thank you so much. I’ll be sure to be careful with my class names next time :slight_smile:

Actually, you don’t even need all the else blocks. Java supports else if { perfectly fine, so long as you get your conditional logic worked out correctly.

1 Like

That is true, I use the Joint Strike Fighter Air Vehicle Coding Standard, which is a very wordy standard.

-Jim

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