Based on the stack trace, my best guess is that there is more than one place in your code where an AnalogInput object is created on port 0. However, I do not see anything in the code you posted that would cause this. Is it possible that one of the following is true?
- You have have other locations in your source code that construct an AnalogInput using the same channel number?
- You have have other locations in your source code that construct an AnalogUltrasonicSensor2017 command (that would result in an attempt to construct an AnalogInput on the same channel).
For the latter case, you can change your AnalogUltrasonicSensor2017 class so that multiple instances will refer to a single static/shared AnalogInput. This can be done with the following changes:
Code:
private static AnalogInput AI = null;
public AnalogUltrasonicSensor2017(Ports ports) {
if (AI == null) {
AI = new AnalogInput(ports.ULTRASONIC_SENSOR_ANALOG_PORT);
}
}
That should allow you to construct multiple instances of this command. If you are willing to declare your constants in Ports as static, it could be simplified to:
Code:
private static AnalogInput AI = new AnalogInput(ports.ULTRASONIC_SENSOR_ANALOG_PORT);
public AnalogUltrasonicSensor2017(Ports ports) {
}