Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
electrical.c
Go to the documentation of this file.
2 
3 #include "mcu_periph/adc.h"
4 #include "subsystems/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 
16 #if defined COMMAND_THROTTLE
17 #define COMMAND_CURRENT_ESTIMATION COMMAND_THROTTLE
18 #elif defined COMMAND_THRUST
19 #define COMMAND_CURRENT_ESTIMATION COMMAND_THRUST
20 #endif
21 
23 
24 #if defined ADC_CHANNEL_VSUPPLY || defined ADC_CHANNEL_CURRENT || defined MILLIAMP_AT_FULL_THROTTLE
25 static struct {
26 #ifdef ADC_CHANNEL_VSUPPLY
27  struct adc_buf vsupply_adc_buf;
28 #endif
29 #ifdef ADC_CHANNEL_CURRENT
30  struct adc_buf current_adc_buf;
31 #endif
32 #ifdef MILLIAMP_AT_FULL_THROTTLE
33  float nonlin_factor;
34 #endif
35 } electrical_priv;
36 #endif
37 
38 #ifndef VoltageOfAdc
39 #define VoltageOfAdc(adc) DefaultVoltageOfAdc(adc)
40 #endif
41 #ifndef MilliAmpereOfAdc
42 #define MilliAmpereOfAdc(adc) DefaultMilliAmpereOfAdc(adc)
43 #endif
44 
45 #ifndef CURRENT_ESTIMATION_NONLINEARITY
46 #define CURRENT_ESTIMATION_NONLINEARITY 1.2
47 #endif
48 
49 void electrical_init(void) {
50  electrical.vsupply = 0;
51  electrical.current = 0;
52 
53 #if defined ADC_CHANNEL_VSUPPLY
54  adc_buf_channel(ADC_CHANNEL_VSUPPLY, &electrical_priv.vsupply_adc_buf, DEFAULT_AV_NB_SAMPLE);
55 #endif
56 
57  /* measure current if available, otherwise estimate it */
58 #if defined ADC_CHANNEL_CURRENT && !defined SITL
59  adc_buf_channel(ADC_CHANNEL_CURRENT, &electrical_priv.current_adc_buf, DEFAULT_AV_NB_SAMPLE);
60 #elif defined MILLIAMP_AT_FULL_THROTTLE
61 PRINT_CONFIG_VAR(CURRENT_ESTIMATION_NONLINEARITY)
62  electrical_priv.nonlin_factor = CURRENT_ESTIMATION_NONLINEARITY;
63 #endif
64 }
65 
66 void electrical_periodic(void) {
67 #if defined(ADC_CHANNEL_VSUPPLY) && !defined(SITL)
68  electrical.vsupply = 10 * VoltageOfAdc((electrical_priv.vsupply_adc_buf.sum/electrical_priv.vsupply_adc_buf.av_nb_sample));
69 #endif
70 
71 #ifdef ADC_CHANNEL_CURRENT
72 #ifndef SITL
73  electrical.current = MilliAmpereOfAdc((electrical_priv.current_adc_buf.sum/electrical_priv.current_adc_buf.av_nb_sample));
74  /* Prevent an overflow on high current spikes when using the motor brake */
75  BoundAbs(electrical.current, 65000);
76 #endif
77 #elif defined MILLIAMP_AT_FULL_THROTTLE && defined COMMAND_CURRENT_ESTIMATION
78  /*
79  * Superellipse: abs(x/a)^n + abs(y/b)^n = 1
80  * with a = 1
81  * b = mA at full throttle
82  * n = 1.2 This defines nonlinearity (1 = linear)
83  * x = throttle
84  * y = current
85  *
86  * define CURRENT_ESTIMATION_NONLINEARITY in your airframe file to change the default nonlinearity factor of 1.2
87  */
88  float b = (float)MILLIAMP_AT_FULL_THROTTLE;
89  float x = ((float)commands[COMMAND_CURRENT_ESTIMATION]) / ((float)MAX_PPRZ);
90  /* electrical.current y = ( b^n - (b* x/a)^n )^1/n
91  * a=1, n = electrical_priv.nonlin_factor
92  */
93  electrical.current = b - pow((pow(b,electrical_priv.nonlin_factor)-pow((b*x),electrical_priv.nonlin_factor)), (1./electrical_priv.nonlin_factor));
94 #endif /* ADC_CHANNEL_CURRENT */
95 
96 }
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Registers a buffer to be used to store the specified converted channel Usage:
Definition: adc_arch.c:59
uint16_t vsupply
supply voltage in decivolts
Definition: electrical.h:8
#define VoltageOfAdc(adc)
Definition: electrical.c:39
arch independent ADC (Analog to Digital Converter) API
#define ADC_CHANNEL_VSUPPLY
Definition: apogee_0.99.h:129
#define MilliAmpereOfAdc(adc)
Definition: electrical.c:42
void electrical_init(void)
Definition: electrical.c:49
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:60
Hardware independent code for commands handling.
void electrical_periodic(void)
Definition: electrical.c:66
int32_t current
current in milliamps
Definition: electrical.h:9
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
Definition: commands.c:30
#define CURRENT_ESTIMATION_NONLINEARITY
Definition: electrical.c:46
#define DEFAULT_AV_NB_SAMPLE
Definition: adc.h:48
struct Electrical electrical
Definition: electrical.c:22
#define MAX_PPRZ
Definition: paparazzi.h:8