View Single Post
  #3   Spotlight this post!  
Unread 25-05-2012, 22:41
NotInControl NotInControl is offline
Controls Engineer
AKA: Kevin
FRC #2168 (Aluminum Falcons)
Team Role: Engineer
 
Join Date: Oct 2011
Rookie Year: 2004
Location: Groton, CT
Posts: 261
NotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond repute
Re: Java button help

In the Subsystem objects you want to include all the methods which control the hardware of that subsystem.

So for instance in the Shooter subsystem you would want a method that would drive the shooter wheel:

i.e.
Code:
//drives the left motor and the right motor with the same value
public void spinMotor(double speed)
{
	
			leftMotor.set(speed);
			RightMotor.set(speed);

}
You don't need the shootBall method. And can safely delete it.
Also Delete the defaultCommand, because that will cause that command to run, even without pushing a button (it runs by default).

Once you have that method in your Shooter Subsystem, you create a command that requires that subsystem and calls the "drive" method in the execute method.

In this case, since you are using a button, you will not have variable control of the motor, but on off control. So the idea is everytime the button is pressed, drive the shooter wheel with a constant value between -1 and 1.

Your execute method in the ShootBall command should look something like this:

Code:
    protected void execute() {
        shooter.spinMotor(1);

/*change value, you should really use the RobotMap class to control
 constants so this call should look more like
 shooter.spinMotor(RobotMap.motorVal), where in RobotMap.java you 
declare a public static int motorVal =1; 
   */     
    }
I also trust in the CommandBase.java definition you instantiate a Shooter object called "shooter".

Lastly, now that you have your subsystem and command set up, in OI map all you need to do is:

Code:
public OI()
{

stick = new Joystick(JOYSTICK_PORT);
        a = new JoystickButton(stick, 1);

a.whileHeld(new shootBall());
}
You should add methods to the OI.java class which return the joystick values of your joystick

i.e one method could be
Code:
public double getRightStickVal()
{
return stick.getRaw(3);
}
You should do this for all joysticks, pots, etc.

You could then create another command call shootBallWithJoystick, have it require shooter as well and in the execute method of that command do something like this:


Code:
protected void execute() {
        shooter.spinMotor(OI.getRightStickVal());
}
You could make the command ShootBallWithJoystick your default command of your subsystem, so now when the robot starts-up you have default control of the motors with your joystick for maintaining manual control, but when you press the button it will drive the wheel with the constant speed. A very nice way to maintain automated, and manual control at all times.


We used the CommandBase Robot template for our 2012 robot in Java, and find it to be an extremely nice way to control the robot.

Hope that helps,
Kevin
__________________
Controls Engineer, Team 2168 - The Aluminum Falcons
[2016 Season] - World Championship Controls Award, District Controls Award, 3rd BlueBanner
-World Championship- #45 seed in Quals, World Championship Innovation in Controls Award - Curie
-NE Championship- #26 seed in Quals, winner(195,125,2168)
[2015 Season] - NE Championship Controls Award, 2nd Blue Banner
-NE Championship- #26 seed in Quals, NE Championship Innovation in Controls Award
-MA District Event- #17 seed in Quals, Winner(2168,3718,3146)
[2014 Season] - NE Championship Controls Award & Semi-finalists, District Controls Award, Creativity Award, & Finalists
-NE Championship- #36 seed in Quals, SemiFinalist(228,2168,3525), NE Championship Innovation in Controls Award
-RI District Event- #7 seed in Quals, Finalist(1519,2168,5163), Innovation in Controls Award
-Groton District Event- #9 seed in Quals, QuarterFinalist(2168, 125, 5112), Creativity Award
[2013 Season] - WPI Regional Winner - 1st Blue Banner

Last edited by NotInControl : 25-05-2012 at 23:09.
Reply With Quote