Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
rpm_sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Freek van Tienen <freek.v.tienen@gmail.com>
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  */
27 #include "mcu_periph/pwm_input.h"
28 #include "subsystems/electrical.h"
29 #include "subsystems/abi.h"
31 
32 static struct FirstOrderLowPass rpm_lp;
34 
35 #ifndef RPM_FILTER_TAU
36 #define RPM_FILTER_TAU RPM_SENSOR_PERIODIC_PERIOD
37 #endif
38 
39 
40 #if PERIODIC_TELEMETRY
42 
43 static void rpm_sensor_send_motor(struct transport_tx *trans, struct link_device *dev)
44 {
45  pprz_msg_send_MOTOR(trans, dev, AC_ID, &rpm, &electrical.current);
46 }
47 #endif
48 
49 /* Initialize the RPM measurement by configuring the telemetry */
50 void rpm_sensor_init(void)
51 {
52  rpm = 0;
53  init_first_order_low_pass(&rpm_lp, RPM_FILTER_TAU, RPM_SENSOR_PERIODIC_PERIOD, 0);
54 
55 #if PERIODIC_TELEMETRY
57 #endif
58 }
59 
60 /* RPM periodic */
62 {
64  AbiSendMsgRPM(RPM_SENSOR_ID, &rpm, 1);
65 }
66 
67 /* Get the RPM sensor */
69 {
70  uint16_t rpm_meas = 0;
71  uint32_t period_us = get_pwm_input_period_in_usec(RPM_PWM_CHANNEL) * RPM_PULSE_PER_RND;
72  if (period_us > 0) {
73  rpm_meas = ((uint32_t)1000000 * 60) / period_us;
74  }
75 
76  return rpm_meas;
77 }
electrical.h
uint16_t
unsigned short uint16_t
Definition: types.h:16
RPM_PULSE_PER_RND
#define RPM_PULSE_PER_RND
Definition: rpm_sensor.h:33
rpm_sensor_init
void rpm_sensor_init(void)
Definition: rpm_sensor.c:50
abi.h
init_first_order_low_pass
static void init_first_order_low_pass(struct FirstOrderLowPass *filter, float tau, float sample_time, float value)
Init first order low pass filter.
Definition: low_pass_filter.h:57
update_first_order_low_pass
static float update_first_order_low_pass(struct FirstOrderLowPass *filter, float value)
Update first order low pass filter state with a new value.
Definition: low_pass_filter.h:71
uint32_t
unsigned long uint32_t
Definition: types.h:18
RPM_FILTER_TAU
#define RPM_FILTER_TAU
Definition: rpm_sensor.c:36
rpm
uint16_t rpm
Definition: rpm_sensor.c:33
rpm_sensor.h
telemetry.h
RPM_SENSOR_ID
#define RPM_SENSOR_ID
Definition: abi_sender_ids.h:420
get_pwm_input_period_in_usec
uint32_t get_pwm_input_period_in_usec(uint32_t channel)
Definition: pwm_input.c:44
dev
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
register_periodic_telemetry
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
rpm_sensor_send_motor
static void rpm_sensor_send_motor(struct transport_tx *trans, struct link_device *dev)
Definition: rpm_sensor.c:43
pwm_input.h
arch independent PWM input capture API
rpm_sensor_get_rpm
uint16_t rpm_sensor_get_rpm(void)
Definition: rpm_sensor.c:68
rpm_lp
static struct FirstOrderLowPass rpm_lp
Definition: rpm_sensor.c:32
rpm_sensor_periodic
void rpm_sensor_periodic(void)
Definition: rpm_sensor.c:61
electrical
struct Electrical electrical
Definition: electrical.c:66
FirstOrderLowPass
First order low pass filter structure.
Definition: low_pass_filter.h:39
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
Electrical::current
float current
current in A
Definition: electrical.h:46
low_pass_filter.h
Simple first order low pass filter with bilinear transform.