Skip to content

Prevent divide by zero errors in temp calculation #20

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

Closed
wants to merge 1 commit into from

Conversation

dsiee
Copy link

@dsiee dsiee commented Jul 19, 2019

If a black object or if it is low light causes R2 in _temperature_and_lux_dn40 to be zero the line for color temperature calculation will throw a divide by zero error. This can be repeated by running tcs34725_simpletest.py which prints lux and temp, disabling the LED by pulling LED pin to GND and placing something on the sensor to block ambient light (e.g. your finger). This can also occur, although less reliably, with a very black object with the LED on.

Proposed fix is just an if to check if R2 is greater then zero. If it isn't use a small value in it's place to allow B2 to still be considered. Not sure that this is the best solution.

If a black object or if it is low light causes R2 in _temperature_and_lux_dn40 to be zero the line for color temperature calculation will throw a divide by zero error. This can be repeated by running tcs34725_simpletest.py which prints lux and temp, disabling the LED by pulling LED pin to GND and placing something on the sensor to block ambient light (e.g. your finger). This can also occur, although less reliably, with a very black object with the LED on.

Proposed fix is just an if to check if R2 is greater then zero. If it isn't use a small value in it's place to allow B2 to still be considered.
@ladyada ladyada requested a review from a team July 20, 2019 05:23
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment on the change.

if R2 > 0:
CT = CT_Coef * B2 / R2 + CT_Offset
else:
CT = CT_Coef * B2 / 0.001 + CT_Offset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of copying and changing the computation line please invert the if to check R2 == 0 and then set R2 to 0.001 instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about limiting all the values in the initial computation of R2, G2, and B2?

R2 = max(0.001, R - IR)
G2 = max(0.001, G - IR)
B2 = max(0.001, B - IR)

and also define a const up to for the 0.001 value

This was referenced Nov 14, 2019
@caternuson
Copy link
Contributor

Closing due to lack of activity. This was taken care of by #24.

@caternuson caternuson closed this Nov 15, 2019
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

Successfully merging this pull request may close these issues.

3 participants