【发布时间】:2018-03-03 20:04:29
【问题描述】:
我无法弄清楚为什么程序调用命令时以下命令不起作用。我没有很好的Java背景,但据我了解,当我按下调用该命令的操纵杆上的按钮时,该命令应该执行打印行语句。我不确定问题是否可能是该命令在某处需要一个动作侦听器或按钮侦听器,或者我是否需要以某种方式将命令与同一个控制台相关联。它应该只有一个可以打印到的公认控制台,我知道它可以从原始程序中的其他打印行语句中工作......对吗?
这里是 library 可能会有所帮助
计划:
/*--------------------------------------------------------------------------
--*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.
*/
/* Open Source Software - may be modified and shared by FRC teams. The code
*/
/* must be accompanied by the FIRST BSD license file in the root directory
of */
/* the project.
*/
/*--------------------------------------------------------------------------
--*/
package org.usfirst.frc.team5621.robot;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends IterativeRobot {
private DifferentialDrive m_robotDrive
= new DifferentialDrive(new Spark(0), new Spark(1));
static Joystick m_stick = new Joystick(1);
private Timer m_timer = new Timer();
static Subsystem ExampleSubsystem;
Command ExampleCommand;
Command CompressCommand;
Command DecompressCommand;
Command OpenClawCommand;
Command CloseClawCommand;
Command CompressorToggleCommand;
public static class OI {
//Create Joystick and Buttons
static Joystick m_stick = new Joystick(1);
static Button button1 = new JoystickButton(m_stick, 1);
static Button button2 = new JoystickButton(m_stick, 2);
static Button button3 = new JoystickButton(m_stick, 3);
static Button button4 = new JoystickButton(m_stick, 4);
static Button button5 = new JoystickButton(m_stick, 5);
static Button button6 = new JoystickButton(m_stick, 6);
static Button button7 = new JoystickButton(m_stick, 7);
static Button button8 = new JoystickButton(m_stick, 8);
public OI() {
// Define Commands for Joystick Buttons
OI.button1.whileHeld(new CompressorToggleCommand());
OI.button2.whileHeld(new CompressCommand());
OI.button3.whileHeld(new DecompressCommand());
OI.button4.whileHeld(new OpenClawCommand());
OI.button5.whileHeld(new CloseClawCommand());
OI.button6.whileHeld(new ExampleCommand());
OI.button7.whileHeld(new ExampleCommand());
OI.button8.whileHeld(new ExampleCommand());
}
}
public class Compressor {
Compressor c = new Compressor();
}
public class Solenoid {
Solenoid exampleSolenoid = new Solenoid();
}
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
@Override
public void robotInit() {
}
/**
* This function is run once each time the robot enters autonomous mode.
*/
@Override
public void autonomousInit() {
m_timer.reset();
m_timer.start();
}
/**
* This function is called periodically during autonomous.
*/
@Override
public void autonomousPeriodic() {
// Drive for 2 seconds
if (m_timer.get() < 2.0) {
m_robotDrive.arcadeDrive(0.5, 0.0); // drive forwards half speed
} else {
m_robotDrive.stopMotor(); // stop robot
}
}
/**
* This function is called once each time the robot enters teleoperated mode.
*/
@Override
public void teleopInit() {
System.out.println("TeleOperated Mode Enabled");
}
/**
* This function is called periodically during teleoperated mode.
*/
@Override
public void teleopPeriodic() {
m_robotDrive.arcadeDrive(m_stick.getY(), m_stick.getX());
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
}
命令
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package org.usfirst.frc.team5621.robot;
import edu.wpi.first.wpilibj.command.Command;
/**
* An example command. You can replace me with your own command.
*/
public class CompressCommand extends Command {
public CompressCommand() {
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
}
// Called repeatedly when this Command is scheduled to run
@Override
protected void execute() {
System.out.println("Compressing...");
exampleSolenoid.set(true);
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return false;
}
}
【问题讨论】:
-
您的应用程序在哪里调用命令?你能在你的代码中突出显示这一行吗?