基于命令 Java:从命令中的子系统访问方法 (Java)
Command based Java: Accessing a method from a subsystem in a command (Java)
这是我第一次使用 Java,我似乎被卡住了。我正在尝试在命令 (DriveStraight) 中从子系统 (DriveTrain) 访问方法 (getHeading),但是当我尝试 heading = Robot.DriveTrain.getHeading();
时,我一直收到 "the type getHeading(double) is undefined for the type Subsystem" 的错误。这是命令:
public class DriveStraight extends Command {
private double speed;
private double duration;
private double heading;
public DriveStraight(float driveSpeed, float duration) {
requires(Robot.DriveTrain);
**heading = Robot.DriveTrain.getHeading();**
}
// Called just before this Command runs the first time
protected void initialize() {
setTimeout(duration);
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
**float currentheading = Robot.DriveTrain.getHeading();**
Robot.DriveTrain.arcadeDrive(speed, (heading - currentheading) * 0.08);
}
这是子系统:
public class DriveTrain extends Subsystem {
AnalogGyro gyro;
RobotDrive drive;
VictorSP frontLeftMotor, rearLeftMotor, frontRightMotor, rearRightMotor;
public DriveTrain() {
frontLeftMotor = new VictorSP(RobotMap.frontLeftMotor);
rearLeftMotor = new VictorSP(RobotMap.rearLeftMotor);
frontRightMotor = new VictorSP(RobotMap.frontRightMotor);
rearRightMotor = new VictorSP(RobotMap.rearRightMotor);
gyro = new AnalogGyro(RobotMap.analogGyro);
gyro.setSensitivity(0.00666);
gyro.calibrate();
}
public void arcadeDrive(float speed, float turn) {
drive.arcadeDrive(OI.joy.getRawAxis(OI.LEFT_Y_AXIS),
OI.joy.getRawAxis(OI.RIGHT_X_AXIS), true);
}
public void tankDrive(float leftValue, float rightValue) {
drive.tankDrive(OI.joy.getRawAxis(OI.LEFT_Y_AXIS),
OI.joy.getRawAxis(OI.RIGHT_Y_AXIS), true);
}
public double getHeading() {
return gyro.getAngle();
}
protected void initDefaultCommand() {
arcadeDrive(0, 0);
}
}
我刚开始使用 C++,所以我想我可能会尝试使用指针,但我不确定。那么从子系统调用方法的正确方法是什么?
谢谢,
Sethra53
您没有在任何地方管理 Robot.DriveTrain 的实例 - 您对 DriveTrain class 的所有方法调用都被编译器视为对 static
方法的调用(不' 与对象相关,仅与 class) 相关。您在任何地方定义的最接近的匹配方法是 public double getHeading() {
,这是一个实例方法,因此应该在实例上调用。您的代码中有 4 个地方引用了 Robot.DriveTrain
,我不确定您的 requires
方法在做什么,所以很难知道您应该将什么传递给它。然而,其他三个地方应该指的是 Robot.DriveTrain
.
的实例
例如
public class DriveStraight extends Command {
private double speed;
private double duration;
private double heading;
private Robot.DriveTrain driveTrain;
public DriveStraight(float driveSpeed, float duration) {
requires(Robot.DriveTrain);
driveTrain = new Robot.DriveTrain() // create new shared instance
heading = driveTrain.getHeading(); // use instance.
}
// Called just before this Command runs the first time
protected void initialize() {
setTimeout(duration);
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
float currentheading = driveTrain.getHeading(); // use instance created in constructor.
driveTrain.arcadeDrive(speed, (heading - currentheading) * 0.08);
}
...
我不能保证其中任何一个 'work' 但是,如果不了解 requires
方法调用的工作原理。
更好的方法是将实例传递给 DriveStraight
构造函数...
public class DriveStraight extends Command {
private Robot.DriveTrain driveTrain;
public DriveStraight(float driveSpeed, float duration, DriveTrain driveTrain) {
this.driveTrain = driveTrain; // use instance created elsewhere
...
这是我第一次使用 Java,我似乎被卡住了。我正在尝试在命令 (DriveStraight) 中从子系统 (DriveTrain) 访问方法 (getHeading),但是当我尝试 heading = Robot.DriveTrain.getHeading();
时,我一直收到 "the type getHeading(double) is undefined for the type Subsystem" 的错误。这是命令:
public class DriveStraight extends Command {
private double speed;
private double duration;
private double heading;
public DriveStraight(float driveSpeed, float duration) {
requires(Robot.DriveTrain);
**heading = Robot.DriveTrain.getHeading();**
}
// Called just before this Command runs the first time
protected void initialize() {
setTimeout(duration);
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
**float currentheading = Robot.DriveTrain.getHeading();**
Robot.DriveTrain.arcadeDrive(speed, (heading - currentheading) * 0.08);
}
这是子系统:
public class DriveTrain extends Subsystem {
AnalogGyro gyro;
RobotDrive drive;
VictorSP frontLeftMotor, rearLeftMotor, frontRightMotor, rearRightMotor;
public DriveTrain() {
frontLeftMotor = new VictorSP(RobotMap.frontLeftMotor);
rearLeftMotor = new VictorSP(RobotMap.rearLeftMotor);
frontRightMotor = new VictorSP(RobotMap.frontRightMotor);
rearRightMotor = new VictorSP(RobotMap.rearRightMotor);
gyro = new AnalogGyro(RobotMap.analogGyro);
gyro.setSensitivity(0.00666);
gyro.calibrate();
}
public void arcadeDrive(float speed, float turn) {
drive.arcadeDrive(OI.joy.getRawAxis(OI.LEFT_Y_AXIS),
OI.joy.getRawAxis(OI.RIGHT_X_AXIS), true);
}
public void tankDrive(float leftValue, float rightValue) {
drive.tankDrive(OI.joy.getRawAxis(OI.LEFT_Y_AXIS),
OI.joy.getRawAxis(OI.RIGHT_Y_AXIS), true);
}
public double getHeading() {
return gyro.getAngle();
}
protected void initDefaultCommand() {
arcadeDrive(0, 0);
}
}
我刚开始使用 C++,所以我想我可能会尝试使用指针,但我不确定。那么从子系统调用方法的正确方法是什么?
谢谢, Sethra53
您没有在任何地方管理 Robot.DriveTrain 的实例 - 您对 DriveTrain class 的所有方法调用都被编译器视为对 static
方法的调用(不' 与对象相关,仅与 class) 相关。您在任何地方定义的最接近的匹配方法是 public double getHeading() {
,这是一个实例方法,因此应该在实例上调用。您的代码中有 4 个地方引用了 Robot.DriveTrain
,我不确定您的 requires
方法在做什么,所以很难知道您应该将什么传递给它。然而,其他三个地方应该指的是 Robot.DriveTrain
.
例如
public class DriveStraight extends Command {
private double speed;
private double duration;
private double heading;
private Robot.DriveTrain driveTrain;
public DriveStraight(float driveSpeed, float duration) {
requires(Robot.DriveTrain);
driveTrain = new Robot.DriveTrain() // create new shared instance
heading = driveTrain.getHeading(); // use instance.
}
// Called just before this Command runs the first time
protected void initialize() {
setTimeout(duration);
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
float currentheading = driveTrain.getHeading(); // use instance created in constructor.
driveTrain.arcadeDrive(speed, (heading - currentheading) * 0.08);
}
...
我不能保证其中任何一个 'work' 但是,如果不了解 requires
方法调用的工作原理。
更好的方法是将实例传递给 DriveStraight
构造函数...
public class DriveStraight extends Command {
private Robot.DriveTrain driveTrain;
public DriveStraight(float driveSpeed, float duration, DriveTrain driveTrain) {
this.driveTrain = driveTrain; // use instance created elsewhere
...