Wind direction Maths

Hi,

I'm pulling some weather information from online and want to display it on a analog gauge.

The gauge looks like this.

N
W E
S | S
|

I could easily put South at the top and everything would work fine, but i think it looks better with North at the top.

I'm having trouble with the maths to get the directions to be the correct PWM output - has anyone got some clever ideas?

Order on gauge Degrees from website Desired PWM output
S 180 0
W 270 256
N 0 512
E 90 767
S 180 1023

thecusen wrote (in part):

Order on gauge Degrees from website Desired PWM output
S 180 0
W 270 256
N 0 512
E 90 767
S 180 1023

This is very confusing.

  • 180 degrees from the website is supposed to produce a desired PWM output of 0. No, wait, it shows up again with an output of 1023.
  • 90 degrees from 180 to 270 produces an output that varies by 256 from 0 to 256. 90 degrees from 90 to 180 produces an output that varies by 256 from 767 to 1023. BUT: 90 degrees from 0 to 90 produces an output that varies only by 255 from 512 to 767.

How can anyone work with a specification like that?!
Some 'if' statements and the map(...) function could handle this, but the requirements are needed FIRST.

I know it's confusing!

it must be possible with some maths i think?

A deadband for south may be necessary lest the needle be pounded to dust. It could be based on time or degrees difference or both.

How often to you plan to update the wind direction indicator?

Cool! Especially since the first post said nothing about 360, 45, 135, 225, or 315 degrees. I still do not see anything about the Desired PWM Output, though.

The "gauge" design is silly.

Why does South appear twice?

Where does the "desired PWM" come from?

How is the indicator movement supposed to work?

Make an indicator corresponding to the diagram in reply #2 instead. No maths necessary.

Attached it the photo of the gauges i'musing.

2 souths, or norths are necessary i think, unless people have a better idea?

No, two Norths or Souths are not necessary, as there is only one of each.

Without a South at the start and one at the end how can you display South-East?

I might be missing something here

If you must have two of anything, take West. It is better than any other direction.

//wind direction correction
    if (wind_degrees >= 180) {
    wind_degrees = wind_degrees - 180;} 
    else {wind_degrees = wind_degrees + 180;
    }

this seems to do the trick

Well they are rotated 90 degree counter clockwise than normal angle and they count in opposite direction...

So if sensor says x, then bring it back to normal with (360-x) and then rotate it by 90 deg
=> (360-x)+90 = 450-x

If x is less than 90 then you are above 360, which does not create a pb for sinus or cosinus calculations, but in your case if you want to fit always between 0 and 360 then just take the modulo by 360 - assuming you are working with int.

So your angle on a traditional angle representation is ((450-x)%360)

I think you need some hysteresis / dead-band around south (if that's where the branch-cut is going to be),
so that you can stray past south a small amount without constantly moving the needle all the way back and
forth. Perhaps have the range so SE-S-SW-W-NW-N-NE-E-SE-S-SW, so there's quite a definite overlap.

The alternative is use some sort of stepper-motor guage that does 360 rotation... Assuming you
can find one without an internal stop.

  //wind direction correction
  if (wind_degrees >= 180) {
    wind_degrees = wind_degrees - 180;
  }
  else {
    wind_degrees = wind_degrees + 180;
  }

So far, so good.
(Note: I edited the formatting to make it easier to read.)
Here is the next step:

  // convert "0 to 360" scale to "0 to 1023"
  int output_pwm = (wind_degrees * 1023L) / 360;

The reason I wrote 1023L instead of 1023 is to force the use of long arithmetic.
wind_degrees can go as high as 359. Multiplying 359 by 1023 will get you 367257. An int cannot handle numbers this high, but a long can.
After the division by 360, the result will again fit in an int, which is why I am using an int variable to store it.

Note that if the wind direction changes , say, from 175 degrees (i.e. slightly East of South) to 185 degrees (i.e. slightly West of South), that the needle will swing wildly. As long as you are not reading the wind direction too frequently, this should not be a problem. But if you are reading the wind direction, say, once every second or two, then it might be a problem.