Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
electrical.c
Go to the documentation of this file.
2 
3 #include "mcu_periph/adc.h"
4 #include "commands.h"
5 
6 #include "generated/airframe.h"
7 #include BOARD_CONFIG
8 
9 #ifdef MILLIAMP_PER_PERCENT
10 #warning "deprecated MILLIAMP_PER_PERCENT --> Please use MILLIAMP_AT_FULL_THROTTLE"
11 #endif
12 #if defined BATTERY_SENS || defined BATTERY_OFFSET
13 #warning "BATTERY_SENS and BATTERY_OFFSET are deprecated, please remove them --> if you want to change the default use VoltageOfAdc"
14 #endif
15 
17 
18 static struct {
20 #ifdef ADC_CHANNEL_CURRENT
21  struct adc_buf current_adc_buf;
22 #endif
23 #ifdef MILLIAMP_AT_FULL_THROTTLE
24  float nonlin_factor;
25 #endif
27 
28 #ifndef VoltageOfAdc
29 #define VoltageOfAdc(adc) DefaultVoltageOfAdc(adc)
30 #endif
31 #ifndef MilliAmpereOfAdc
32 #define MilliAmpereOfAdc(adc) DefaultMilliAmpereOfAdc(adc)
33 #endif
34 
35 #ifndef CURRENT_ESTIMATION_NONLINEARITY
36 #define CURRENT_ESTIMATION_NONLINEARITY 1.2
37 #endif
38 
39 void electrical_init(void) {
40  electrical.vsupply = 0;
41  electrical.current = 0;
42 
44 #ifdef ADC_CHANNEL_CURRENT
45  adc_buf_channel(ADC_CHANNEL_CURRENT, &electrical_priv.current_adc_buf, DEFAULT_AV_NB_SAMPLE);
46 #endif
47 
48 #ifdef MILLIAMP_AT_FULL_THROTTLE
50 #endif
51 }
52 
53 void electrical_periodic(void) {
54 #ifndef SITL
55  electrical.vsupply = 10 * VoltageOfAdc((electrical_priv.vsupply_adc_buf.sum/electrical_priv.vsupply_adc_buf.av_nb_sample));
56 #endif
57 
58 #ifdef ADC_CHANNEL_CURRENT
59 #ifndef SITL
60  electrical.current = MilliAmpereOfAdc((electrical_priv.current_adc_buf.sum/electrical_priv.current_adc_buf.av_nb_sample));
61  /* Prevent an overflow on high current spikes when using the motor brake */
62  BoundAbs(electrical.current, 65000);
63 #endif
64 #else
65 #if defined MILLIAMP_AT_FULL_THROTTLE && defined COMMAND_THROTTLE
66  /*
67  * Superellipse: abs(x/a)^n + abs(y/b)^n = 1
68  * with a = 1
69  * b = mA at full throttle
70  * n = 1.2 This defines nonlinearity (1 = linear)
71  * x = throttle
72  * y = current
73  *
74  * define CURRENT_ESTIMATION_NONLINEARITY in your airframe file to change the default nonlinearity factor of 1.2
75  */
76  float b = (float)MILLIAMP_AT_FULL_THROTTLE;
77  float x = ((float)commands[COMMAND_THROTTLE]) / ((float)MAX_PPRZ);
78  /* electrical.current y = ( b^n - (b* x/a)^n )^1/n
79  * a=1, n = electrical_priv.nonlin_factor
80  */
81  electrical.current = b - pow((pow(b,electrical_priv.nonlin_factor)-pow((b*x),electrical_priv.nonlin_factor)), (1./electrical_priv.nonlin_factor));
82 #endif
83 #endif /* ADC_CHANNEL_CURRENT */
84 
85 }
#define ADC_CHANNEL_VSUPPLY
Definition: booz_1.0.h:114
uint16_t vsupply
Definition: electrical.h:8
struct adc_buf vsupply_adc_buf
Definition: electrical.c:19
static struct @17 electrical_priv
#define VoltageOfAdc(adc)
Definition: electrical.c:29
arch independent ADC (Analog to Digital Converter) API
#define MilliAmpereOfAdc(adc)
Definition: electrical.c:32
void electrical_init(void)
Definition: electrical.c:39
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:60
void electrical_periodic(void)
Definition: electrical.c:53
int32_t current
Definition: electrical.h:9
void adc_buf_channel(uint8_t adc_channel __attribute__((unused)), struct adc_buf *s __attribute__((unused)), uint8_t av_nb_sample __attribute__((unused)))
Required by infrared.c:ir_init()
Definition: jsbsim_ir.c:35
pprz_t commands[COMMANDS_NB]
Definition: commands.c:48
#define CURRENT_ESTIMATION_NONLINEARITY
Definition: electrical.c:36
#define DEFAULT_AV_NB_SAMPLE
Definition: adc.h:48
struct Electrical electrical
Definition: electrical.c:16
#define MAX_PPRZ
Definition: paparazzi.h:8