pimoroni / enviro

MIT License
101 stars 79 forks source link

Update weather.py to show 16 wind directions #221

Open MrDrem opened 2 months ago

MrDrem commented 2 months ago

This also resolves the issue with having to tare readings more than once.

MrDrem commented 2 months ago

This seems to crash my enviro-weather when the wind parts are plugged in. I am trying to work out what the issue is, but the board seems to need to be reflashed, so I can't get to the log file sadly.

MrDrem commented 2 months ago

I've updated the code to try and improve pull #163 slightly.

I'm not certain why there is the addition of an extra 360 at line 157, and suspect that this could be removed.

I've not yet tested this again with the board connected to the the anemometer.

MrDrem commented 2 months ago

I've now been running this code for ~1 hour, and it's returning good wind directions. WIll update again in the morning, but it looks good to include.

MrDrem commented 2 months ago

I now understand why the extra 360 degrees is added

(The addition of 360 degrees is a common technique used in programming to handle negative numbers when calculating directions or angles.

In this case, config.wind_direction_offset could potentially be a negative number. If wind_direction (which is non-negative) is added to a negative offset, the result could be a negative angle, which doesn’t make sense in this context because angles are typically expressed in the range of 0 to 360 degrees.

By adding 360 before adding the offset, we ensure that the intermediate result is always non-negative, even if the offset is negative. The modulus operation (% 360) at the end then brings the final result back into the range of 0 to 360 degrees.

So, the expression (wind_direction + 360 + config.wind_direction_offset) % 360 ensures that the final wind direction is a non-negative number less than 360, regardless of the value of config.wind_direction_offset. This is a standard way to normalize an angle to the range of 0 to 360 degrees.)

My board crashed when the wind sensors were added last night, I'm not sure which part of the code causes this issue, or if it's a physical hardware issue. When there are no sensors connected, it's fine, but when they are it regularly crashes.

sjefferson99 commented 2 months ago

@MrDrem As #163 was working ok when I last tested it I wanted to see what improvements you were making and see if I could help get one of these PRs in. I note you caught my copy paste error on the default wind direction config, which I have fixed in my branch and I have also rebased my PR branch against main to make comparisons easier. Comparing yours and my branch I can only see some comments removed which I failed to remove with the associated code (will adjust in my branch as well), am I messing up the compare and there's other changes made I should take a look at? I can then test both branches with mine and see if it's code or board specific.

MrDrem commented 2 months ago

@sjefferson99 I'd done it slightly differently initially, but looking at your method yours seemed better so I changed it to match that and bring it up to the new codebase. The changes in the end were just the tidying.

I'm fairly sure that my issues are board specific, but as my board was gifted by in-laws, and it works intermittently (no issues for the last ~5 hours, but it will stop soon I suspect), getting hold of Pimorini to try and exchange it is a bit of a hassle that I've not had time to do, and I've had it for so long now that it's going to be out of warranty.

sjefferson99 commented 2 months ago

@MrDrem OK, as I've rebased mine on updated main and committed the bits I missed first time (Thanks for catching), my original PR should be the same. I'll copy the code from it to my board and test when I get a minute and see if it still runs...