Go to Post This will be one of the nerdiest weekends in the history of the world. And I'm taking part in it! (yay!) - Dan Petrovic [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

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 17-10-2016, 00:35
Lireal Lireal is online now
Registered User
AKA: Alex Colello
FRC #2141 (Spartonics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Concord, California
Posts: 95
Lireal is on a distinguished road
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
  #2   Spotlight this post!  
Unread 19-10-2016, 20:10
bankst's Avatar
bankst bankst is offline
Registered User
FRC #0832 (OSCAR)
Team Role: Mentor
 
Join Date: Feb 2016
Rookie Year: 2012
Location: Roswell, GA
Posts: 9
bankst is an unknown quantity at this point
Re: Java help

2016 was our first year with Java too, and being the sole programmer, I kinda got thrown into the fire. Here's our code from 2016. Its mostly very simple command-based stuff, but if you have any questions you can PM me. Hope that helps!
Reply With Quote
Reply


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 23:33.

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