Go to Post Things that get said on the field should stay on the field. - Bongle [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Spotlight this post!  
Unread 17-10-2016, 00:35
Lireal Lireal is offline
Registered User
AKA: Alex Colello
FRC #2141 (Spartonics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Concord, California
Posts: 99
Lireal has a spectacular aura aboutLireal has a spectacular aura aboutLireal has a spectacular aura about
Re: Java help

Here is what the above would look like in Java:

Code:
public class MyMotorSubsystem extends Subsystem {
  ControllerType myMotor;
  public MyMotorSubsystem () {
    myMotor = new ControllerType(RobotMap.ID_MY_MOTOR);
  }
  public void setMotor(double value) {
    myMotor.set(value);
  }
}
Command to run the motor.
Code:
public class CommandBase extends Command {
  public static MyMotorSubsystem myMotorSubsystem;
  public static void init() {
    myMotorSubsystem = new MyMotorSubsystem();
  }
}

public class RunMotorCommand extends CommandBase {
  JoystickButton runMotorButton;
  public RunMotorCommand(JoystickButton runMotorButton) {
    this.runMotorButton = runMotorButton;
    runMotorButton.whenPressed(this);
  }
  void initialize() {
    requires(myMotorSubsystem);
  }
  void execute() {
    myMotorSubsystem.setMotor(1.0);
  }
  boolean isFinished() {
    return !runMotorButton.get();
  }
  void end() {
    myMotorSubsystem.setMotor(0.0);
  }
  void interrupted() {
    myMotorSubsystem.setMotor(0.0);
  }
}
Create an instance of the command
Code:
// somewhere in OI.java
new RunMotorCommand(new JoystickButton(myJoystick, something));
My team does not use the CommandBase, and we usually instantiate subsystems in the Robot.java file, so I am not 100% sure this is right, but the basic idea should be apparent. Basically, create a command that in the execute will call a method from a subsystem that sets the motor to the value you want, which is triggered by a button and will end when that button isn't pressed.
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 17:48.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi