Go to Post The fact that it is easy doesn't mean that it is smart. - JVN [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 16-10-2016, 11:58
Sk84d2 Sk84d2 is offline
Registered User
FRC #4592
 
Join Date: Sep 2016
Location: Florida
Posts: 2
Sk84d2 is an unknown quantity at this point
Java help

I am part of team 4592 and I took on the task of being programming captain. I don't know much and i need some help. I need to run a motor while a button is being pressed and held. I've gone through wpilib and can't figure it out. Thanks to anyone who can help it is greatly appreciated.
Reply With Quote
  #2   Spotlight this post!  
Unread 16-10-2016, 13:42
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 298
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: Java help

WPILib resources:
Screensteps
API Reference (Java)
API Reference (C++)

Using the command based architecture, you need to first create a subsystem that the motor belongs to
Code:
public class MyMotorSubsystem extends Subsystem {
  CANSomething myMotor;
  public MyMotorSubsystem {
    myMotor = new CANSomething(RobotMap.CAN_ID_MY_MOTOR);
  }
  public void setMotor(double value) {
    myMotor.set(value);
  }
}
Then you need a 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 run() {
    myMotorSubsystem->setMotor(1.0);
  }
  void isFinished() {
    return runMotorButton.get();
  }
  void end() {
    myMotorSubsystem->setMotor(0.0);
  }
  void interrupted() {
    myMotorSubsystem->setMotor(0.0);
  }
}
Then you need to create an instance of the command
Code:
// somewhere in OI.java
new RunMotorCommand(new JoystickButton(myJoystick, something));
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org
Reply With Quote
  #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: 100
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
  #4   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 08:21.

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