We have a class that is an extension of the Talon class. It adds some functions such as current limiting.
As it extends the Talon class, which implements SpeedController, it would be reasoned that we should be able to pass our “Custom Talons” to other classes such as the RobotDrive class.
Unfortunately, it seems that when passing out extension of Talon, robotDrive doesn’t seem to call the overridden set function. For the sake of making our code easier to read we’ve put only the pertinent parts into a separate project and reproduced the bug there. Here is that code from an iterative robot project.
Robot.java:
public class Robot extends IterativeRobot {
final String defaultAuto = "Default";
final String customAuto = "My Auto";
String autoSelected;
SendableChooser chooser;
TestTalon A = new TestTalon(2),
B = new TestTalon(3),
C = new TestTalon(0),
D = new TestTalon(1);
RobotDrive drive = new RobotDrive(A,B,C,D);
Joystick xbox = new Joystick(0);
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
chooser = new SendableChooser();
chooser.addDefault("Default Auto", defaultAuto);
chooser.addObject("My Auto", customAuto);
SmartDashboard.putData("Auto choices", chooser);
}
/**
* This autonomous (along with the chooser code above) shows how to select between different autonomous modes
* using the dashboard. The sendable chooser code works with the Java SmartDashboard. If you prefer the LabVIEW
* Dashboard, remove all of the chooser code and uncomment the getString line to get the auto name from the text box
* below the Gyro
*
* You can add additional auto modes by adding additional comparisons to the switch structure below with additional strings.
* If using the SendableChooser make sure to add them to the chooser code above as well.
*/
public void autonomousInit() {
autoSelected = (String) chooser.getSelected();
// autoSelected = SmartDashboard.getString("Auto Selector", defaultAuto);
System.out.println("Auto selected: " + autoSelected);
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
switch(autoSelected) {
case customAuto:
//Put custom auto code here
break;
case defaultAuto:
default:
//Put default auto code here
break;
}
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic()
{
double move = xbox.getRawAxis(1);
double rotate = xbox.getRawAxis(4);
drive.arcadeDrive(move, rotate);
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
}
}
TestTalon.java:
public class TestTalon extends Talon
{
public TestTalon(int channel) {
super(channel);
// TODO Auto-generated constructor stub
}
@Override
public void set(double value)
{
System.out.println("TestTalon running");
super.set(value);
}
}
The idea here is that we should be seeing “TestTalon running” being printed to the riolog four times every loop of the function. Nothing is sent to the riolog, moreover, the robot continues to drive fine, indicating that it’s running the Talon code but not the code from our extension.
Is there something that we’re missing about how java handles inheritance or is there something strange going on with the WPILibs?