Paparazzi UAS  v6.0_unstable-41-g5cfda00-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sonar_pwm.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020 OpenuAS
3 *
4 * Thanks to Jean-Fran├žois Erdelyi & Gautier Hattenberger for ADC one
5 *
6 * This file is part of paparazzi
7 *
8 * paparazzi is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * paparazzi is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with paparazzi; see the file COPYING. If not, see
20 * <http://www.gnu.org/licenses/>.
21 */
22 
38 #include "mcu_periph/pwm_input.h"
39 #include "subsystems/abi.h"
40 
41 #include "filters/median_filter.h"
42 
43 #if PERIODIC_TELEMETRY
45 #include "pprzlink/messages.h"
46 #endif
47 #ifdef SENSOR_SYNC_SEND_SONAR
49 #endif
50 #include "generated/airframe.h"
51 #ifdef SITL
52 #include "state.h"
53 #endif
54 
55 #ifdef SONAR_USE_PWM_FILTER
57 #endif
58 
59 /* Config parameters for sensor */
60 
62 #ifndef SONAR_PWM_CHANNEL
63 #error "No SONAR_PWM_CHANNEL defined to use sonar_pwm module, add a line like define=SONAR_PWM_CHANNEL value=PWM_INPUT1"
64 #endif
65 
67 #ifndef SONAR_OFFSET
68 #define SONAR_OFFSET 0
69 #endif
70 
72 #ifndef SONAR_PWM_PERIOD
73 #define SONAR_PWM_PERIOD 4096
74 #endif
75 
77 #ifndef SONAR_SCALE
78 #define SONAR_SCALE SONAR_PWM_PERIOD
79 #endif
80 
82 #ifndef SONAR_MEDIAN_SIZE
83 #define SONAR_MEDIAN_SIZE 7 //Good for a noisy sonar
84 #endif
85 
87 #ifndef SONAR_MIN_RANGE
88 #define SONAR_MIN_RANGE 0.15f //Default is a common value for regular sonars. Cannot measure closer than that
89 #endif
90 
92 #ifndef SONAR_MAX_RANGE
93 #define SONAR_MAX_RANGE 7.0f //Reasonable maximum value for regular sonars.
94 #endif
95 
97 #ifndef SONAR_PWM_OFFSET
98 #define SONAR_PWM_OFFSET 820 //822
99 #endif
100 
101 // Enable telemetry report
102 #ifndef SENSOR_SYNC_SEND_SONAR
103 #define SENSOR_SYNC_SEND_SONAR TRUE
104 #endif
105 
107 
108 /* Set the default values at initialization */
109 void sonar_pwm_init(void)
110 {
113  sonar_pwm.raw = 0.0f;
114  #ifdef SONAR_USE_PWM_FILTER
116  #endif
117 }
118 
119 /* Read the sensor current measured value */
120 void sonar_pwm_read(void) {
121 
122 #ifndef SITL
123  // raw duty cycle in usec
124  uint16_t sonar_duty_raw = get_pwm_input_duty_in_usec(SONAR_PWM_CHANNEL);
125 
126  // remove PWM offset where needed
127  sonar_pwm.raw = sonar_duty_raw - SONAR_PWM_OFFSET;
128 
129 #ifdef SONAR_USE_PWM_FILTER
131 #else
133 #endif
134 
135  Bound(sonar_pwm.distance, (float)SONAR_MIN_RANGE, (float)SONAR_MAX_RANGE);
136 
137 #if SONAR_COMPENSATE_ROTATION
138  float phi = stateGetNedToBodyEulers_f()->phi;
139  float theta = stateGetNedToBodyEulers_f()->theta;
140  float gain = (float)fabs( (double) (cosf(phi) * cosf(theta)));
142 #endif
143 
144 #else // SITL
146 #endif // SITL
147 
148 #if USE_SONAR
149  uint32_t now_ts = get_sys_time_usec();
150  AbiSendMsgAGL(AGL_SONAR_PWM_ID, now_ts, sonar_pwm.distance);
151 #endif
152 
153 #ifdef SENSOR_SYNC_SEND_SONAR
154  DOWNLINK_SEND_SONAR(DefaultChannel, DefaultDevice, &sonar_pwm.raw, &sonar_pwm.distance);
155 #endif
156 }
float phi
in radians
#define SONAR_MEDIAN_SIZE
The Median Filter strength.
Definition: sonar_pwm.c:83
#define SONAR_OFFSET
The input channel of the PWM based sensor.
Definition: sonar_pwm.c:68
#define SONAR_MAX_RANGE
The maximum range for the device to be able to measure.
Definition: sonar_pwm.c:93
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
Periodic telemetry system header (includes downlink utility and generated code).
#define AGL_SONAR_PWM_ID
arch independent PWM input capture API
float distance
Distance measured in meters.
Definition: sonar_adc.h:35
Main include for ABI (AirBorneInterface).
float distance
Distance measured.
Definition: sonar_pwm.h:46
#define SONAR_PWM_OFFSET
Some sensor may need an initial PWM offset (823 usec in the case of an EZ1 sensor) ...
Definition: sonar_pwm.c:98
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
void sonar_pwm_read(void)
Definition: sonar_pwm.c:120
float scale
scale to convert raw to a real distance
Definition: sonar_pwm.h:45
float theta
in radians
#define SONAR_MIN_RANGE
The minimum range for the device to be able to measure.
Definition: sonar_pwm.c:88
struct SonarAdc sonar_adc
Definition: sonar_adc.c:50
float offset
offset
Definition: sonar_pwm.h:44
struct MedianFilterFloat sonar_filt
Definition: sonar_bebop.c:47
static void init_median_filter_f(struct MedianFilterFloat *filter, uint8_t size)
static float update_median_filter_f(struct MedianFilterFloat *filter, float new_data)
API to get/set the generic vehicle states.
struct SonarPwm sonar_pwm
Definition: sonar_pwm.c:106
uint32_t get_pwm_input_duty_in_usec(uint32_t channel)
Definition: pwm_input.c:39
float z
in meters
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
#define SONAR_SCALE
Ranger scale or sensitivity as you wish.
Definition: sonar_pwm.c:78
uint16_t raw
raw PWM value
Definition: sonar_pwm.h:43
void sonar_pwm_init(void)
Definition: sonar_pwm.c:109
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78