0

这是我第一次使用 Java,我似乎被卡住了。我正在尝试从命令 (DriveStraight) 中的子系统 (DriveTrain) 访问方法 (getHeading),但是当我尝试时,我不断收到“类型子系统的类型 getHeading(double) 未定义”的错误heading = Robot.DriveTrain.getHeading();。这是命令:

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++,所以我想我可能正在尝试使用指针,但我不确定。那么从子系统调用方法的正确方法是什么?

谢谢,塞思拉53

4

1 回答 1

0

您没有在任何地方管理 Robot.DriveTrain 的实例 - 您对 DriveTrain 类的所有方法调用都被编译器视为对static方法的调用(与对象无关,仅与类相关)。您在任何地方定义的最接近的匹配方法是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);
    }
    ...

但是,如果不了解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
    ...
于 2016-03-14T11:34:37.637 回答