Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to calibrate color sensors in Micropython #8

Open
fll-pigeons opened this issue Sep 21, 2020 · 1 comment
Open

How to calibrate color sensors in Micropython #8

fll-pigeons opened this issue Sep 21, 2020 · 1 comment

Comments

@fll-pigeons
Copy link
Owner

from fllforums

One thing I've noticed is Pybricks ColorSensor class doesn't provide a way to calibrate. I came out with a workaround for now but I wonder whether there is any "official" way to do it like what you can do with EV3 Mindstorms Color Sensor block?

philso replied:

EV3Lessons has a tutorial on how to calibrate the color sensor using the EV3 programming language. That tutorial includes some pseudocode that can be converted to MicroPython. There is a note in that tutorial that it would be best to calibrate each color sensor connected separately. The sensors are not calibrated from the factory and different examples will generally return different values when they are exposed to the same area on the mat.

@fll-pigeons
Copy link
Owner Author

fll-pigeons commented Sep 22, 2020

philso followup:
I am sorry, I don't have any real experience with the EV3 language. My sons used Robolab on the NXT when they competed. One of them developed his own program to calibrate the light sensor while the robot was traveling so the calibration would always be current and independent of ambient light. It appears that it should be possible to do something similar in MicroPython since it is a text based language with what looks like relatively easy use of variables (unlike the NXT and EV3 languages). I believe he looked at the reflected light values being returned and constantly compared them to the variables for the maximum value and the minimum value to see if there was a new maximum value or a new minimum value. After he knew he had crossed from the white area on one side of the black line to the white area on the other side of the line, he calculated the average of the maximum and the minimum values and used it as his threshold to follow the line.

Edited to add: I found a video where they describe how they did the calibration on the fly. It should be possible to adapt the algorithm to any language as long as one can use variables.

Kcinn followup:
That's exactly what I did, the challenge is how to persist the new min/max (black/white) values at the end of the calibration program, since PyBricks ColorSensor class doesn't provide a function to save it like what EV3 color sensor block does. I ended up storing the calibrated min/max values to a file on the brick, then read the values back to calculate target value before each line following run, which has been working well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant