【问题标题】:JAVA: How do I get this command to listen for button press?JAVA:如何让这个命令监听按钮按下?
【发布时间】: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;
}
}

【问题讨论】:

  • 您的应用程序在哪里调用命令?你能在你的代码中突出显示这一行吗?

标签: java command


【解决方案1】:

首先,我想说很高兴在 Stack Overflow 上见到一位 FRC 程序员。关于您的问题,您的问题可能在于您在同一个文件中声明了多个 public 类。这是 Java 编程中非常糟糕的做法。您要解决的问题在这里:

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());
}

再一次,嵌套公共类是不好的做法,但真正导致您的问题是您在 OI 类的构造函数中为每个按钮设置了 Button-On-Press 操作,但您从未真正制作一个 OI 对象,因此永远不会调用构造函数,并且永远不会运行代码。在你的 robotsInit 函数中,创建一个新的 OI 对象:

void robotInit() {
    OI myOI = new OI();
}

还要从 OI 类中的语句中删除 OI 标记。它不是必需的。这样:

OI.button1.whileHeld(new CompressorToggleCommand());

变成这样:

button1.whileHeld(new CompressorToggleCommand());

我还建议您将所有对象初始化移至robotInit。我建议您将所有 许多 嵌套类都放在自己的文件中,并确保在您的 Robot 主类中创建所有这些类的对象。我还建议阅读一些基本的面向对象编程概念,例如how constructors workhow to use classes and objects in Java。也可以阅读你在FRC API Reference中使用的FRC函数

【讨论】:

  • 您好,首先我想说的是我的荣幸,非常感谢您!我知道公共类不应该嵌套,本来我自己就有的。但是,Eclipse 返回一个错误,我必须将返回值更改为 void。除此之外,它返回的错误是我必须将“按钮 1、2 等”声明为变量,直到我在它之前添加了“OI”部分。然后我意识到,如果我如上所示嵌套类,这些错误就不会发生。我把“OI”重新考虑到“为什么不”。我现在有code Public void () OI{ OI.Button } //etc code (class unnested)
  • 然而,这里真正的主题似乎是当我在code OI 中添加时 myOI = new OI(); codeEclipse 给出一个“!”说明我需要开设一个 OI 课程。似乎更喜欢这个 code public void robotInit() { new OI(); } code
  • 没问题。希望您的代码按现在预期的方式运行。一定要把你学到的东西传授给你的队友,这样你以后就可以避免这个问题。此外,如果您可以通过单击它旁边的复选标记来接受此答案,我们将不胜感激
  • 另外,如果您希望能够引用您的 OI 对象,则必须对其进行初始化。在代码public void robotInit() { new OI(); } 中,您没有创建对象。您的 OI 类应该在一个单独的文件中。确保您在 Eclipse 中创建了 OI 类作为机器人项目的一部分。 Eclipse 有时可能很难操作,尤其是处理复杂的事情,例如 FRC 项目。如果您不确定,请询问。并确保您的 OI 类是 public
猜你喜欢
  • 2012-11-23
  • 2016-09-16
  • 2012-07-31
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多