Multiple REV Distance Sensors using an I2C Multiplexer (Java)

Using recommendations from this thread, Multiple REV Color Sensors using an I2C Multiplexer (Java), our team is attempting to use an Adafruit i2c Multiplexer for multiple REV 2m Distance Sensors. Amazingly, the code almost works save that I’m only getting values from the last sensor instantiated; m_leftSensor shows the same value as m_rightSensor. Has anyone gotten this approach to work with Distance Sensors? My multiplexed distance wrapper and Robot.java, where I instantiate the objects can be found here. Thanks.

1 Like

I don’t think you can use automatic mode without a lot more modifications to ensure the mux is switched inside the automatic mode thread.

After going through the lib, I was sort of thinking the same thing as I was driving home from the shop. Thanks for confirming that suspicion. I’ll give it a try disabling it tomorrow and see how it works. Has anyone done a multiplexer friendly override that they’re willing to share for that class?

In our multiplexer code for the Rev color sensor, we just use an array to store the instances of the color sensor when they are initailized and just pull from that array when we need to read the sensor. When we had it create a new instance of the object every time we wanted to read from the sensor, we ran into the same issue you seem to be having. You can see our code for reference here, which should be similar for the distance sensor.

I’m really stretching my knowledge of Java here, but I think it’s a lot easier to do this with the Color Sensor since they aren’t thread-based. The issue probably lies behind setAutomaticMode() for the Distance Sensor API; the last instantiated instance looks to own that thread.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.