View Single Post
  #8   Spotlight this post!  
Unread 03-06-2012, 21:44
Djur's Avatar
Djur Djur is offline
WPILib
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 182
Djur will become famous soon enough
Re: Java Robot Template

Well, you would need to define a "setSpeed()" method, something like this:
Code:
public void setSpeed(double speed) {
    shooterJaguar.set(speed);
}
I would recommend that you change the name of your jaguar to something like "shooterJag" or at least not "shooter" which is the name of your class - it could get confusing.

Now, on to your question. The Joystick class has getX(), getY(), and getZ() methods that return values between -1 and 1 based on the Joystick's movement on those axes. A simple way to do it would be
Code:
setSpeed(controller.getY());
but it would spin the shooter backwards if you moved the joystick backwards on the y-axis. A good way to do this would be
Code:
setSpeed(Math.abs(controller.getY()));
or

Code:
setSpeed(controller.getY() < 0 ? 0 : controller.getY());
The first snippet spins the shooter no matter what value the joystick is sending out -- if it goes into the negative range, it just uses the absolute value as if you were moving it in the positive range.

The second snippet will only spin the shooter if the joystick is in the positive range. If it is returning values that are less than zero, it will set the shooter speed to zero, else it will use the value that it's giving.

Also, you should have an else statement in your "shoot()" method that resets the speed to zero because Jaguars will keep running at the last speed they have been set to. If you don't have this, it will keep spinning at the speed it had been set to when you take your finger off of buttonA. It's really easy to do this -- just add this to the end of the if statement in the "shoot()" method.

Code:
} else {
    setSpeed(0);
}
So your class would look something like this:

Code:
package bilal.robotics.code;

import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.ButtonType;


public class Shooter {

    Jaguar shooterJaguar = new Jaguar(5);
    
    Joystick controller = new Joystick(2);
    
    final int buttonA = 1;
    
    public Shooter() {
        
    }

    public void setSpeed(double speed) {
        shooterJaguar.set(speed);
    }
    
    public void shoot() {
        if(controller.getRawButton(buttonA)) {
            setSpeed(controller.getY() < 0 ? 0 : controller.getY());
        } else {
            setSpeed(0);
        }
    }
    
}
__________________
WPILib dev (RobotBuilder, SmartDashboard, GRIP)
Reply With Quote