Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
aoa_pwm.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2015 Jean-Fran├žois Erdelyi, Gautier Hattenberger
3 *
4 * This file is part of paparazzi
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20 
31 #include "mcu_periph/pwm_input.h"
32 #include "messages.h"
34 #include "generated/airframe.h"
35 
36 #if LOG_AOA
37 #include "sdLog.h"
38 #include "subsystems/chibios-libopencm3/chibios_sdlog.h"
39 bool_t log_started;
40 #endif
41 
42 #ifndef AOA_PWM_CHANNEL
43 #error "AOA_PWM_CHANNEL needs to be defined to use AOA_pwm module"
44 #endif
45 
47 #ifndef AOA_PWM_PERIOD
48 #define AOA_PWM_PERIOD 4096
49 #endif
50 #ifndef AOA_PWM_OFFSET
52 #define AOA_PWM_OFFSET 1
53 #endif
54 #ifndef AOA_ANGLE_OFFSET
56 #define AOA_ANGLE_OFFSET M_PI
57 #endif
58 #ifndef AOA_OFFSET
60 #define AOA_OFFSET 0.0f
61 #endif
62 #ifndef AOA_FILTER
64 #define AOA_FILTER 0.0f
65 #endif
66 #ifndef AOA_SENS
68 #define AOA_SENS ((2.0f*M_PI)/AOA_PWM_PERIOD)
69 #endif
70 // Set AOA_REVERSE to TRUE to change rotation direction
71 #if AOA_REVERSE
72 #define AOA_SIGN -1
73 #else
74 #define AOA_SIGN 1
75 #endif
76 // Enable telemetry report
77 #ifndef SEND_SYNC_AOA
78 #define SEND_SYNC_AOA TRUE
79 #endif
80 // Enable SD card logging
81 #ifndef LOG_AOA
82 #define LOG_AOA FALSE
83 #endif
84 
86 
87 #if PERIODIC_TELEMETRY
89 
90 static void send_aoa(struct transport_tx *trans, struct link_device *dev)
91 {
92  pprz_msg_send_AOA(trans, dev, AC_ID, &aoa_pwm.raw, &aoa_pwm.angle);
93 }
94 
95 #endif
96 
97 void aoa_pwm_init(void)
98 {
102  aoa_pwm.angle = 0.0f;
103  aoa_pwm.raw = 0.0f;
104 #if LOG_AOA
105  log_started = FALSE;
106 #endif
107 #if PERIODIC_TELEMETRY
108  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AOA, send_aoa);
109 #endif
110 }
111 
112 void aoa_pwm_update(void) {
113  static float prev_aoa = 0.0f;
114 
115  // raw duty cycle in usec
116  uint32_t duty_raw = get_pwm_input_duty_in_usec(AOA_PWM_CHANNEL);
117 
118  // remove some offset if needed
119  aoa_pwm.raw = duty_raw - AOA_PWM_OFFSET;
120  // FIXME for some reason, the last value of the MA3 encoder is not 4096 but 4097
121  // this case is not handled since we don't care about angles close to +- 180 deg
123  // filter angle
124  aoa_pwm.angle = aoa_pwm.filter * prev_aoa + (1.0f - aoa_pwm.filter) * aoa_pwm.angle;
125  prev_aoa = aoa_pwm.angle;
126 
127 #if USE_AOA
129 #endif
130 
131 #if SEND_SYNC_AOA
132  RunOnceEvery(10, DOWNLINK_SEND_AOA(DefaultChannel, DefaultDevice, &aoa_pwm.raw, &aoa_pwm.angle));
133 #endif
134 
135 #if LOG_AOA
136  if(pprzLogFile != -1) {
137  if (!log_started) {
138  sdLogWriteLog(pprzLogFile, "AOA_PWM: ANGLE(deg) RAW(int16)\n");
139  log_started = TRUE;
140  } else {
141  float angle = DegOfRad(aoa_pwm.angle);
142  sdLogWriteLog(pprzLogFile, "AOA_PWM: %.3f %d\n", angle, aoa_pwm.raw);
143  }
144  }
145 #endif
146 }
147 
Generic transmission transport header.
Definition: transport.h:89
Periodic telemetry system header (includes downlink utility and generated code).
uint32_t get_pwm_input_duty_in_usec(uint32_t channel)
arch independent PWM input capture API
bool_t log_started
#define AOA_PWM_OFFSET
Some sensor may need an initial PWM offset (1 usec in the case of an MA3 sensor)
Definition: aoa_pwm.c:52
#define AOA_OFFSET
Default extra offset that can be ajusted from settings.
Definition: aoa_pwm.c:60
void aoa_pwm_init(void)
Definition: aoa_pwm.c:97
#define FALSE
Definition: std.h:5
struct Aoa_Pwm aoa_pwm
Definition: aoa_pwm.c:85
static void stateSetAngleOfAttack_f(float aoa)
Set angle of attack in radians (float).
Definition: state.h:1263
Angle of Attack sensor on PWM.
#define TRUE
Definition: std.h:4
void aoa_pwm_update(void)
Definition: aoa_pwm.c:112
float angle
Angle of attack in radians.
Definition: aoa_pwm.h:36
#define AOA_SENS
Default sensitivity (2*pi on a PWM of period AOA_PWM_PERIOD)
Definition: aoa_pwm.c:68
#define AOA_FILTER
Default filter value.
Definition: aoa_pwm.c:64
unsigned long uint32_t
Definition: types.h:18
uint32_t raw
raw PWM value
Definition: aoa_pwm.h:35
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
#define AOA_SIGN
Definition: aoa_pwm.c:74
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
float sens
sensitiviy, i.e. scale to conver raw to angle
Definition: aoa_pwm.h:38
struct Aoa_Adc aoa_adc
Definition: aoa_adc.c:64
float filter
Filtering value [0-1] 0: no filtering 1: output is a constant value.
Definition: aoa_pwm.h:44
float angle
Angle of attack in radians.
Definition: aoa_adc.h:42
#define AOA_ANGLE_OFFSET
Default offset value (assuming 0 AOA is in the middle of the range)
Definition: aoa_pwm.c:56
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
float offset
Angle of attack offset in radians.
Definition: aoa_pwm.h:37