7. Custom Weather Station: Wind Direction Sensor
Welcome back, this post is going to cover connecting and integrating the wind direction sensor from the wind / rain sensor assembly.
Connecting
Started off by cutting off the connector the sensor came with and adding my own connector that was easier to plug wires into. I have been using the following guide as a reference for most sensors. Here I ran into my first problem as it seems that two of the four wires were meant to go to the wind speed sensor which is supposed to be plugged into the bottom of the wind direction sensor. Too bad I already cut off and redid that connection, I will have to fix it later for now its just more wires. Anyway wires one and four were for the wind direction sensor and I plugged things in as shown in the diagram. (hot pink and purple)
Long story short this did not work when I attempted to run the simple test program all I got was zero values. This is due to the diagram being slightly wrong, it shows the 10k ohm resistor connecting from ground to output 0 of the ADC. The 10k ohm resistor should instead connect from the 5V vcc rail to output 0. After this change the test program worked fine.
Adding Ros2
Added in the sensor as a new node in the project wind_direction_sensor.py. The node reports the degrees from north on ros topic /wind_direction_raw and the cardinal direction on ros topic /wind_direction.
The ROS node keeps a running average of the last five reported degree values from the sensor and uses the average of those five to report the current wind direction. Currently if you add more or less fields to the angles variable you can average over more readings. The actual averaging is done in the get_average function.
self._angles = [None, None, None, None, None]
The wind direction sensor reports voltages based on what resistors have been internally triggered by the position of the sensor. To convert this to degrees I referenced the datasheet and made a dictionary relating voltages to degree values.
self._voltage_to_degrees_dict = {3.84: 0,
1.98: 22.5,
2.25: 45,
0.41: 67.5,
0.45: 90,
0.32: 112.5,
0.9: 135,
0.62: 157.5,
1.4: 180,
1.19: 202.5,
3.08: 225,
2.93: 247.5,
4.62: 270,
4.04: 292.5,
4.33: 315,
3.43: 337.5}
Relating degrees to voltage was a bit more complicated than a simple key value pair as the reported voltage is sometimes slightly different than expected, to deal with this problem the program allows from a 0.015 voltage variance from expected value.
def get_degrees(self, raw_voltage):
for voltage in self._voltage_to_degrees_dict:
if abs(voltage - raw_voltage) <= 0.015:
return self._voltage_to_degrees_dict[voltage]
return None
Finally the sensor has no way to know how any of the degrees relate to any of the cardinal directions. Right now we are assuming that 0 degrees is North. During setup we will have to either make sure 0 degrees on the sensor is aligned to North or make sure to add an offset to all the degree calculations in the code to account for some other value being set to North.
I will be adding this sensor along with the wind speed and rain gauge to the master launch file later, for now launch with ros2 run mimir wind_direction_sensor
Please like and subscribe.
- Building a Custom Weather Station in ROS2 Parts and Plans (DIY BLOG)
- Custom Weather Station: Installing Ros2 (DIY BLOG)
- Custom Weather Station: Temperature, Pressure, Humidity Sensor Install
- Custom Weather Station: UV Sensor install
- Custom Weather Station: Ground Temperature Sensor
- Custom Weather Station: Wind Speed Sensor
HIVE (THE NON-COMMUNIST, NON-CENSORED, JUSTIN SUN EXCLUDING SOCIAL BLOCKCHAIN) IS ALIVE, COME JOIN US!!!
https://peakd.com <--- Log in with your Steem account today!!