Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 "pprzlink/messages.h"
34 #include "generated/airframe.h"
35 
36 #if LOG_AOA
38 bool log_started;
39 #endif
40 
41 #ifndef AOA_PWM_CHANNEL
42 #error "AOA_PWM_CHANNEL needs to be defined to use AOA_pwm module"
43 #endif
44 
46 #ifndef AOA_PWM_PERIOD
47 #define AOA_PWM_PERIOD 4096
48 #endif
49 #ifndef AOA_PWM_OFFSET
51 #define AOA_PWM_OFFSET 1
52 #endif
53 #ifndef AOA_ANGLE_OFFSET
55 #define AOA_ANGLE_OFFSET M_PI
56 #endif
57 #ifndef AOA_OFFSET
59 #define AOA_OFFSET 0.0f
60 #endif
61 #ifndef AOA_FILTER
63 #define AOA_FILTER 0.0f
64 #endif
65 #ifndef AOA_SENS
67 #define AOA_SENS ((2.0f*M_PI)/AOA_PWM_PERIOD)
68 #endif
69 // Set AOA_REVERSE to TRUE to change rotation direction
70 #if AOA_REVERSE
71 #define AOA_SIGN -1
72 #else
73 #define AOA_SIGN 1
74 #endif
75 // Enable telemetry report
76 #ifndef SEND_SYNC_AOA
77 #define SEND_SYNC_AOA TRUE
78 #endif
79 // Enable SD card logging
80 #ifndef LOG_AOA
81 #define LOG_AOA FALSE
82 #endif
83 
85 
86 #if PERIODIC_TELEMETRY
88 
89 static void send_aoa(struct transport_tx *trans, struct link_device *dev)
90 {
91  pprz_msg_send_AOA(trans, dev, AC_ID, &aoa_pwm.raw, &aoa_pwm.angle);
92 }
93 
94 #endif
95 
96 void aoa_pwm_init(void)
97 {
101  aoa_pwm.angle = 0.0f;
102  aoa_pwm.raw = 0.0f;
103 #if LOG_AOA
104  log_started = false;
105 #endif
106 #if PERIODIC_TELEMETRY
108 #endif
109 }
110 
111 void aoa_pwm_update(void) {
112  static float prev_aoa = 0.0f;
113 
114  // raw duty cycle in usec
115  uint32_t duty_raw = get_pwm_input_duty_in_usec(AOA_PWM_CHANNEL);
116 
117  // remove some offset if needed
118  aoa_pwm.raw = duty_raw - AOA_PWM_OFFSET;
119  // FIXME for some reason, the last value of the MA3 encoder is not 4096 but 4097
120  // this case is not handled since we don't care about angles close to +- 180 deg
122  // filter angle
123  aoa_pwm.angle = aoa_pwm.filter * prev_aoa + (1.0f - aoa_pwm.filter) * aoa_pwm.angle;
124  prev_aoa = aoa_pwm.angle;
125 
126 #if USE_AOA
128 #endif
129 
130 #if SEND_SYNC_AOA
131  RunOnceEvery(10, DOWNLINK_SEND_AOA(DefaultChannel, DefaultDevice, &aoa_pwm.raw, &aoa_pwm.angle));
132 #endif
133 
134 #if LOG_AOA
135  if(pprzLogFile != -1) {
136  if (!log_started) {
137  sdLogWriteLog(pprzLogFile, "AOA_PWM: ANGLE(deg) RAW(int16)\n");
138  log_started = true;
139  } else {
140  float angle = DegOfRad(aoa_pwm.angle);
141  sdLogWriteLog(pprzLogFile, "AOA_PWM: %.3f %d\n", angle, aoa_pwm.raw);
142  }
143  }
144 #endif
145 }
146 
Periodic telemetry system header (includes downlink utility and generated code).
arch independent PWM input capture API
#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:51
#define AOA_OFFSET
Default extra offset that can be ajusted from settings.
Definition: aoa_pwm.c:59
void aoa_pwm_init(void)
Definition: aoa_pwm.c:96
struct Aoa_Pwm aoa_pwm
Definition: aoa_pwm.c:84
bool log_started
static void stateSetAngleOfAttack_f(float aoa)
Set angle of attack in radians (float).
Definition: state.h:1300
Angle of Attack sensor on PWM.
void aoa_pwm_update(void)
Definition: aoa_pwm.c:111
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:67
#define AOA_FILTER
Default filter value.
Definition: aoa_pwm.c:63
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
FileDes pprzLogFile
Definition: sdlog_chibios.c:76
#define AOA_SIGN
Definition: aoa_pwm.c:73
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
float sens
sensitiviy, i.e. scale to conver raw to angle
Definition: aoa_pwm.h:38
uint32_t get_pwm_input_duty_in_usec(uint32_t channel)
float filter
Filtering value [0-1] 0: no filtering 1: output is a constant value.
Definition: aoa_pwm.h:44
#define AOA_ANGLE_OFFSET
Default offset value (assuming 0 AOA is in the middle of the range)
Definition: aoa_pwm.c:55
static void send_aoa(struct transport_tx *trans, struct link_device *dev)
Definition: aoa_pwm.c:89
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