Merge pull request #1666 from fvantienen/rpm_sensor

[modules] Add RPM sensor
This commit is contained in:
Gautier Hattenberger
2016-05-24 17:58:10 +02:00
7 changed files with 177 additions and 8 deletions
@@ -31,6 +31,8 @@
<module name="current_sensor"/>
<module name="opa_controller"/>
<module name="mag_pitot_uart" />
<module name="pwm_meas"/>
<module name="rpm_sensor"/>
<!--module name="geo_mag"/-->
<module name="air_data"/>
+25
View File
@@ -0,0 +1,25 @@
<!DOCTYPE module SYSTEM "module.dtd">
<module name="rpm_sensor" dir="sensors">
<doc>
<description>RPM sensor based on time difference between pulses
It uses an PWM input to measure the period between pulses.
</description>
<configure name="RPM_PWM_CHANNEL" value="PWM_INPUTX" description="Select PWM input channel for RPM sensor"/>
<define name="RPM_PULSE_PER_RND" value="14" description="Amount of pulses per round"/>
</doc>
<depends>pwm_meas</depends>
<header>
<file name="rpm_sensor.h"/>
</header>
<init fun="rpm_sensor_init()"/>
<makefile>
<file name="rpm_sensor.c"/>
<configure name="RPM_PWM_CHANNEL" default="PWM_INPUT1"/>
<define name="RPM_PWM_CHANNEL" value="$(RPM_PWM_CHANNEL)"/>
<define name="$(RPM_PWM_CHANNEL)_TICKS_PER_USEC" value="1" />
<define name="USE_$(RPM_PWM_CHANNEL)" value="PWM_PULSE_TYPE_ACTIVE_HIGH" />
</makefile>
</module>
@@ -81,27 +81,31 @@ void pwm_input_init(void)
*/
#if USE_PWM_INPUT_TIM1
rcc_periph_clock_enable(RCC_TIM1);
pwm_input_set_timer(TIM1, PWM_INPUT_TIM1_TICKS_PER_USEC);
pwm_input_set_timer(TIM1, TIM1_TICKS_PER_USEC);
#endif
#if USE_PWM_INPUT_TIM2
rcc_periph_clock_enable(RCC_TIM2);
pwm_input_set_timer(TIM2, PWM_INPUT_TIM2_TICKS_PER_USEC);
pwm_input_set_timer(TIM2, TIM2_TICKS_PER_USEC);
#endif
#if USE_PWM_INPUT_TIM3
rcc_periph_clock_enable(RCC_TIM3);
pwm_input_set_timer(TIM3, PWM_INPUT_TIM3_TICKS_PER_USEC);
pwm_input_set_timer(TIM3, TIM3_TICKS_PER_USEC);
#endif
#if USE_PWM_INPUT_TIM4
rcc_periph_clock_enable(RCC_TIM4);
pwm_input_set_timer(TIM4, TIM4_TICKS_PER_USEC);
#endif
#if USE_PWM_INPUT_TIM5
rcc_periph_clock_enable(RCC_TIM5);
pwm_input_set_timer(TIM5, PWM_INPUT_TIM5_TICKS_PER_USEC);
pwm_input_set_timer(TIM5, TIM5_TICKS_PER_USEC);
#endif
#if USE_PWM_INPUT_TIM8
rcc_periph_clock_enable(RCC_TIM8);
pwm_input_set_timer(TIM8, PWM_INPUT_TIM8_TICKS_PER_USEC);
pwm_input_set_timer(TIM8, TIM8_TICKS_PER_USEC);
#endif
#if USE_PWM_INPUT_TIM9
rcc_periph_clock_enable(RCC_TIM9);
pwm_input_set_timer(TIM9, PWM_INPUT_TIM9_TICKS_PER_USEC);
pwm_input_set_timer(TIM9, TIM9_TICKS_PER_USEC);
#endif
#ifdef USE_PWM_INPUT1
@@ -261,6 +265,27 @@ void tim3_isr(void) {
#endif
#if USE_PWM_INPUT_TIM4
void tim4_isr(void) {
if ((TIM4_SR & TIM4_CC_IF_PERIOD) != 0) {
timer_clear_flag(TIM4, TIM4_CC_IF_PERIOD);
pwm_input_period_tics[TIM4_PWM_INPUT_IDX] = TIM4_CCR_PERIOD;
pwm_input_period_valid[TIM4_PWM_INPUT_IDX] = true;
}
if ((TIM4_SR & TIM4_CC_IF_DUTY) != 0) {
timer_clear_flag(TIM4, TIM4_CC_IF_DUTY);
pwm_input_duty_tics[TIM4_PWM_INPUT_IDX] = TIM4_CCR_DUTY;
pwm_input_duty_valid[TIM4_PWM_INPUT_IDX] = true;
}
if ((TIM4_SR & TIM_SR_UIF) != 0) {
timer_clear_flag(TIM4, TIM_SR_UIF);
// FIXME clear overflow interrupt but what else ?
}
}
#endif
#if USE_PWM_INPUT_TIM5
void tim5_isr(void) {
+2 -2
View File
@@ -358,7 +358,7 @@
// PWM_INPUT1 on TIM1
#define PWM_INPUT1_TIMER TIM1
#ifdef PWM_INPUT1_TICKS_PER_USEC
#define PWM_INPUT_TIM1_TICKS_PER_USEC PWM_INPUT1_TICKS_PER_USEC
#define TIM1_TICKS_PER_USEC PWM_INPUT1_TICKS_PER_USEC
#endif
#define PWM_INPUT1_CHANNEL_PERIOD TIM_IC1
#define PWM_INPUT1_CHANNEL_DUTY TIM_IC2
@@ -381,7 +381,7 @@
// PWM_INPUT2 on TIM9
#define PWM_INPUT2_TIMER TIM9
#ifdef PWM_INPUT2_TICKS_PER_USEC
#define PWM_INPUT_TIM9_TICKS_PER_USEC PWM_INPUT2_TICKS_PER_USEC
#define TIM9_TICKS_PER_USEC PWM_INPUT2_TICKS_PER_USEC
#endif
#define PWM_INPUT2_CHANNEL_PERIOD TIM_IC1
#define PWM_INPUT2_CHANNEL_DUTY TIM_IC2
+28
View File
@@ -113,6 +113,34 @@
#define SPI_SELECT_SLAVE2_PORT GPIOC // BARO (on spi2)
#define SPI_SELECT_SLAVE2_PIN GPIO13
/*
* RPM sensor
*/
#ifdef USE_PWM_INPUT1
#define PWM_INPUT1_GPIO_PORT GPIOB
#define PWM_INPUT1_GPIO_PIN GPIO6
#define PWM_INPUT1_GPIO_AF GPIO_AF2
#define PWM_INPUT1_TIMER TIM4
#define PWM_INPUT1_CHANNEL_PERIOD TIM_IC1
#define PWM_INPUT1_CHANNEL_DUTY TIM_IC2
#define PWM_INPUT1_TIMER_INPUT TIM_IC_IN_TI1
#define PWM_INPUT1_SLAVE_TRIG TIM_SMCR_TS_IT1FP1
#define PWM_INPUT1_IRQ NVIC_TIM4_IRQ
#define PWM_INPUT1_CC_IE (TIM_DIER_CC1IE | TIM_DIER_CC2IE)
#define USE_PWM_INPUT_TIM4 TRUE
#ifdef PWM_INPUT1_TICKS_PER_USEC
#define TIM4_TICKS_PER_USEC PWM_INPUT1_TICKS_PER_USEC
#endif
#define TIM4_PWM_INPUT_IDX 0
#define TIM4_CC_IF_PERIOD TIM_SR_CC1IF
#define TIM4_CC_IF_DUTY TIM_SR_CC2IF
#define TIM4_CCR_PERIOD TIM4_CCR1
#define TIM4_CCR_DUTY TIM4_CCR2
#endif
/*
* ADC
*/
+50
View File
@@ -0,0 +1,50 @@
/*
* Copyright (C) Freek van Tienen <freek.v.tienen@gmail.com>
*
* This file is part of paparazzi
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file "modules/sensors/rpm_sensor.c"
* @author Freek van Tienen <freek.v.tienen@gmail.com>
* RPM sensor based on time difference between pulses
*/
#include "modules/sensors/rpm_sensor.h"
#include "mcu_periph/pwm_input.h"
#include "subsystems/electrical.h"
#if PERIODIC_TELEMETRY
#include "subsystems/datalink/telemetry.h"
static void rpm_sensor_send_motor(struct transport_tx *trans, struct link_device *dev)
{
uint16_t rpm = 0;
uint32_t period_us = get_pwm_input_period_in_usec(RPM_PWM_CHANNEL) * RPM_PULSE_PER_RND;
if(period_us > 0)
rpm = ((uint32_t)1000000 * 60) / period_us;
pprz_msg_send_MOTOR(trans, dev, AC_ID, &rpm, &electrical.current);
}
#endif
/* Initialize the RPM measurement by configuring the telemetry */
void rpm_sensor_init(void)
{
#if PERIODIC_TELEMETRY
register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_MOTOR, rpm_sensor_send_motor);
#endif
}
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (C) Freek van Tienen <freek.v.tienen@gmail.com>
*
* This file is part of paparazzi
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file "modules/sensors/rpm_sensor.h"
* @author Freek van Tienen <freek.v.tienen@gmail.com>
* RPM sensor based on time difference between pulses
*/
#ifndef RPM_SENSOR_H
#define RPM_SENSOR_H
#include "std.h"
/* Default pulses per round */
#ifndef RPM_PULSE_PER_RND
#define RPM_PULSE_PER_RND 14
#endif
extern void rpm_sensor_init(void);
#endif