Driver for a PWM based sonar range sensor
Reads sensor using PWM input and outputs sonar distance to object in [cm]
Why PWM over ADC: One only needs to connect GND, VCC and one more signal wire, same as ADC but not the disadvantages of ADC sensors noise
Sensor example: Maxbotix LV-EZ1
Example for airframe file
Add to your firmware section: This example contains all possible configuration options, not all of them are mandatory!
<module name="sonar_pwm">
b'<define name="USE_SONAR" value="TRUE" />\n '
b'<define name="SONAR_OFFSET" value="0.1234" />\n '
b'<define name="SONAR_SCALE" value="SONAR_PWM_PERIOD" />\n '
b'<define name="SONAR_MIN_RANGE" value="0.44" unit="m" />\n '
b'<define name="SONAR_MAX_RANGE" value="3.0" unit="m" />\n '
b'<define name="SONAR_COMPENSATE_ROTATION" value="TRUE" />\n '
b'<define name="SONAR_UPDATE_ON_AGL" value="TRUE" />\n '
b'<define name="SONAR_PWM_PERIOD" value="4096" />\n '
b'<define name="SONAR_PWM_OFFSET" value="20" />\n '
b'<define name="SONAR_USE_PWM_FILTER" value="TRUE" />\n '
b'<define name="SONAR_MEDIAN_SIZE" value="9" />\n '
b'<define name="SENSOR_SYNC_SEND_SONAR" />\n '
b'<configure name="SONAR_PWM_CHANNEL" value="PWM_INPUTx" />\n '
</module>
Module configuration options
Configure Options
- name:
SONAR_PWM_CHANNEL
value: PWM_INPUTx
Description: Select PWM input channel for sensor to use e.g. PWM_INPUT1
Define Options
- name:
USE_SONAR
value: TRUE
Description: Activate use of sonar in INS extended filter (only rotorcraft) or not if one only wants to measure distance
- name:
SONAR_OFFSET
value: 0.1234
Description: Sensor offset in CM, as in where one onts zero to be, default is 0
- name:
SONAR_SCALE
value: SONAR_PWM_PERIOD
Description: Sensor scale
- name:
SONAR_MIN_RANGE
value: 0.44
Description: If defined, set limit to minimum value reported, default 0.15
- name:
SONAR_MAX_RANGE
value: 3.0
Description: If defined, set limit to maximum value reported, default 6.0
- name:
SONAR_COMPENSATE_ROTATION
value: TRUE
Description: Compensate AGL for body rotation, if not defined, defaults to FALSE
- name:
SONAR_UPDATE_ON_AGL
value: TRUE
Description: Updates the AGL value in state,if not defined, defaults to FALSE
- name:
SONAR_PWM_PERIOD
value: 4096
Description: period in microsec (e.g. 4096 for 12 bits sensor)
- name:
SONAR_PWM_OFFSET
value: 20
Description: initial offset on the raw pwm signal if needed (default: 820usec)
- name:
SONAR_USE_PWM_FILTER
value: TRUE
Description: Enable or disable a median filter on the sensor output values
- name:
SONAR_MEDIAN_SIZE
value: 9
Description: If median filter is enabled then set this option to filter the output more(or less), default is 7
- name:
SENSOR_SYNC_SEND_SONAR
value: None
Description: If defined, sends raw and scaled sonar values, useful for debugging sensor issues
Module functions
Init Functions
These initialization functions are called once on startup.
Periodic Functions
These functions are called periodically at the specified frequency from the module periodic loop.
- sonar_pwm_read()
- Frequency in Hz: 20
- Autorun: LOCK
Periodic function automatically starts after init and can't be stopped.
Files
Header Files
The following headers are automatically included in modules.h
Source Files
Raw sonar_pwm.xml file:
<!DOCTYPE module SYSTEM "module.dtd">
<module name="sonar_pwm" dir="sonar" task="sensors">
<doc>
<description>
Driver for a PWM based sonar range sensor
Reads sensor using PWM input and outputs sonar distance to object in [cm]
Why PWM over ADC:
One only needs to connect GND, VCC and one more signal wire, same as ADC but not the disadvantages of ADC sensors noise
Sensor example: Maxbotix LV-EZ1
</description>
<configure name="SONAR_PWM_CHANNEL" value="PWM_INPUTx" description="Select PWM input channel for sensor to use e.g. PWM_INPUT1"/>
<define name="USE_SONAR" value="TRUE" description="Activate use of sonar in INS extended filter (only rotorcraft) or not if one only wants to measure distance"/>
<define name="SONAR_OFFSET" value="0.1234" description="Sensor offset in CM, as in where one onts zero to be, default is 0"/>
<define name="SONAR_SCALE" value="SONAR_PWM_PERIOD" description="Sensor scale"/>
<define name="SONAR_MIN_RANGE" value="0.44" unit="m" description="If defined, set limit to minimum value reported, default 0.15"/>
<define name="SONAR_MAX_RANGE" value="3.0" unit="m" description="If defined, set limit to maximum value reported, default 6.0"/>
<define name="SONAR_COMPENSATE_ROTATION" value="TRUE" description="Compensate AGL for body rotation, if not defined, defaults to FALSE"/>
<define name="SONAR_UPDATE_ON_AGL" value="TRUE" description="Updates the AGL value in state,if not defined, defaults to FALSE"/>
<define name="SONAR_PWM_PERIOD" value="4096" description="period in microsec (e.g. 4096 for 12 bits sensor)"/>
<define name="SONAR_PWM_OFFSET" value="20" description="initial offset on the raw pwm signal if needed (default: 820usec)"/>
<define name="SONAR_USE_PWM_FILTER" value="TRUE" description="Enable or disable a median filter on the sensor output values"/>
<define name="SONAR_MEDIAN_SIZE" value="9" description="If median filter is enabled then set this option to filter the output more(or less), default is 7"/>
<define name="SENSOR_SYNC_SEND_SONAR" description="If defined, sends raw and scaled sonar values, useful for debugging sensor issues"/>
</doc>
<dep>
<depends>pwm_meas</depends>
<provides>sonar</provides>
</dep>
<header>
<file name="sonar_pwm.h" />
</header>
<init fun="sonar_pwm_init()" />
<periodic fun="sonar_pwm_read()" freq="20" />
<makefile target="ap">
<file name="sonar_pwm.c" />
<define name="SONAR_PWM_CHANNEL" value="$(SONAR_PWM_CHANNEL)" cond="ifdef SONAR_PWM_CHANNEL" />
<define name="$(SONAR_PWM_CHANNEL)_TICKS_PER_USEC" value="1" />
<define name="USE_$(SONAR_PWM_CHANNEL)" value="PWM_PULSE_TYPE_ACTIVE_LOW" />
</makefile>
</module>