Question

My motors of my NXT2 2.0 is backwards and now everytime I run the command forward() it rolls backwards instead. I am using the DifferentialPilot to move my robot, and therefore want to overwrite several functions in the DifferentialPilot class.

I inherited from that class and overwrote all constructors with my custom ones. Then I overwrote forward() which should now call backward(), and vice versa.

However, Now I'm getting exceptions, and I can't really tell why.

Here is the overwritten class:

package lejos.robotics.navigation;
import lejos.robotics.RegulatedMotor;

public class CustomDifferentialPilot extends DifferentialPilot {

    public CustomDifferentialPilot(final double wheelDiameter, final double trackWidth, final RegulatedMotor leftMotor,
            final RegulatedMotor rightMotor) {
        super(wheelDiameter, trackWidth, leftMotor, rightMotor);

    }

    public CustomDifferentialPilot(final double wheelDiameter, final double trackWidth, final RegulatedMotor leftMotor,
            final RegulatedMotor rightMotor, final boolean reverse) {
        super(wheelDiameter, trackWidth, leftMotor, rightMotor, reverse);
    }

    public CustomDifferentialPilot(double leftWheelDiameter, double rightWheelDiameter, double trackWidth,
            RegulatedMotor leftMotor, RegulatedMotor rightMotor, boolean reverse) {
        super(leftWheelDiameter, rightWheelDiameter, trackWidth, leftMotor, rightMotor, reverse);
    }

    public void forward() {
        backward();
    }

    public void backward() {
        forward();
    }

}

And my example code for the robot:

import lejos.robotics.navigation.CustomDifferentialPilot;


public class DriveTest {

    private static CustomDifferentialPilot diffPilot;
    public static void main(String[] args) {
        diffPilot = new CustomDifferentialPilot(10, 20, Motor.A, Motor.B);
        diffPilot.forward();
    }

}

Is calling a function from the superclass, in the subclass within an overwritten method not allowed?

Was it helpful?

Solution 2

Forgot to pass super() to the overridden methods.

public void forward() {
    super.backward();
}

public void backward() {
    super.forward();
}

OTHER TIPS

Your main method does not actually create an instance of diffPilot. You should be getting a NullPointerException at diffPilot.forward();.

You should be doing

diffPilot = new CustomDifferentialPilot(...);

in your main method before calling .forward();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top