Skip to content

Commit 209d463

Browse files
committed
pybricks.pupdevices.ColorSensor: Split corrections to sensor.
This didn't work great for both sensor types, so move them to their own variants. This still needs to be revisited properly, but this is less bad than before. Also make the h adjustment monotonic so it doesn't skip ahead.
1 parent c1d934c commit 209d463

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

pybricks/pupdevices/pb_type_pupdevices_colordistancesensor.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ static void get_hsv_data(pupdevices_ColorDistanceSensor_obj_t *self, pbio_color_
9090
rgb.g = 1187 * raw[1] / 2048;
9191
rgb.b = 1187 * raw[2] / 2048;
9292
pb_color_map_rgb_to_hsv(&rgb, hsv);
93+
94+
// Approximately double low values to get similar results
95+
// as with other sensors.
96+
hsv->v = hsv->v * (200 - hsv->v) / 100;
9397
}
9498

9599
// pybricks.pupdevices.ColorDistanceSensor.color

pybricks/pupdevices/pb_type_pupdevices_colorsensor.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ static void get_hsv_reflected(mp_obj_t self_in, pbio_color_hsv_t *hsv) {
7171
.b = data[2] == 1024 ? 255 : data[2] >> 2,
7272
};
7373
pb_color_map_rgb_to_hsv(&rgb, hsv);
74+
75+
// Approximately double saturation for low values to get similar results
76+
// as other sensors.
77+
hsv->s = hsv->s * (200 - hsv->s) / 100;
78+
79+
// Approximately +50% low values to get similar results
80+
// as with other sensors.
81+
hsv->v = hsv->v * (150 - hsv->v / 2) / 100;
7482
}
7583

7684
// Helper for getting HSV with the light off, scale saturation and value to

pybricks/util_pb/pb_color_map.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ void pb_color_map_rgb_to_hsv(const pbio_color_rgb_t *rgb, pbio_color_hsv_t *hsv)
3333
pbio_color_rgb_to_hsv(rgb, hsv);
3434

3535
// Slight shift for lower hues to make yellow somewhat more accurate
36-
if (hsv->h < 40) {
37-
uint8_t offset = ((hsv->h - 20) << 8) / 20;
38-
int32_t scale = 200 - ((100 * (offset * offset)) >> 16);
39-
hsv->h = hsv->h * scale / 100;
36+
if (hsv->h >= 350) {
37+
hsv->h = (350 + 2 * (hsv->h - 350)) % 360;
38+
} else if (hsv->h < 40) {
39+
hsv->h += 10;
40+
} else if (hsv->h < 60) {
41+
hsv->h = 50 + (hsv->h - 40) / 2;
4042
}
41-
42-
// Value and saturation correction
43-
hsv->s = hsv->s * (200 - hsv->s) / 100;
44-
hsv->v = hsv->v * (200 - hsv->v) / 100;
4543
}
4644

4745
static const mp_rom_obj_tuple_t pb_color_map_default = {

0 commit comments

Comments
 (0)