nathankellenicki / node-poweredup

A Javascript module to interface with LEGO Powered Up components.
https://nathankellenicki.github.io/node-poweredup/
MIT License
477 stars 59 forks source link

DuploTrainBaseColorSensor unreliable color detection #137

Closed thschenk closed 3 years ago

thschenk commented 3 years ago

I recently bought a Duplo train (10874) an I now try to control it using node-poweredup. Everything works great so far, except for the color detection sensor. It just keeps spitting out random colors.

After playing around a bit I have very reliable color detection when I apply the following change to the code: In devices/duplotrainbasecolorsensor.ts, I changed the value of the COLOR mode like this:

From:

export enum Mode {
  COLOR = 0x00,
  REFLECTIVITY = 0x02,
  RGB = 0x03
}

To:

export enum Mode {
  COLOR = 0x01,
  REFLECTIVITY = 0x02,
  RGB = 0x03
}

After this change I get very reliable color readings.

I am also using the corneliusmunz/legoino library on an ESP32. There I observe the same pattern. The standard behavior (using a mode 0) gives unreliable color readings, while using mode 1 gives great color detection.

Do you think this is something that should be added to node-poweredup? I can implement that. For backwards compatibility (if required) it would be possible to introduce a new COLOR_NEW mode that maps to 1, while keeping the old mode set to 0. Or do you have a different view?

Regards,

Thijs

thschenk commented 3 years ago

Also this documentation confirms that mode 1 should be used for the color detection.

Mode 0 - Color, intensity of the color
Mode 1 - ColorTag, discrete list of understand colors
Mode 2 - Reflection
Mode 3 - RGB
nathankellenicki commented 3 years ago

@thschenk Thanks for the report, and you're absolutely right. I've published v7.0.0 which fixes this.

thschenk commented 3 years ago

Thanks for the fix. It works great!