i have a script which gets the air pressure from a website. and i wrote this function
which says if the pressure gets different value if it rains or sunny weather or else
now i have a servo which should change the angle depending on each value. Well i now that my servo goes from 0 to 360 degree. But how can I correspond my 360 Degree values to the air pressure value.
def barometer(wert): if wert <= 1000: print 'ES REGNET BALD' if wert >1000 and wert <=1040: print 'WETTER VERAENDERLICH' if wert >1040 and wert < 1060: print barometer(wert) or " "
Lets say
if air pressure value = 1000:
vector value = 10 #in Degrees
Should i go to a list?
Or is there a easier solution.
thanks for help
Chris
This question is related to
python
You could define a mapping of air pressure to servo angle, for example:
def calc_angle(pressure, min_p=1000, max_p=1200): return 360 * ((pressure - min_p) / float(max_p - min_p)) angle = calc_angle(pressure)
This will linearly convert pressure
values between min_p
and max_p
to angles between 0 and 360 (you could include min_a
and max_a
to constrain the angle, too).
To pick a data structure, I wouldn't use a list but you could look up values in a dictionary:
d = {1000:0, 1001: 1.8, ...} angle = d[pressure]
but this would be rather time-consuming to type out!
Source: Stackoverflow.com