gpt4 book ai didi

JAVA:如何让这个命令监听按钮按下?

转载 作者:行者123 更新时间:2023-12-02 11:33:55 29 4
gpt4 key购买 nike

我不明白为什么当程序调用该命令时以下命令不起作用。我没有很好的 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;
}
}

最佳答案

首先,我想说很高兴在 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());
}

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

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

同时从 OI 类内的语句中删除 OI 标记。不需要。这样:

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

变成这样:

button1.whileHeld(new CompressorToggleCommand());

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

关于JAVA:如何让这个命令监听按钮按下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49088681/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com