Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
electrical_raw.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2009-2013 The Paparazzi Team
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  */
23 
29 #include "electrical_raw.h"
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <sys/time.h>
36 #include <unistd.h>
37 #include <math.h>
38 #include "i2c-dev.h"
39 #include "subsystems/commands.h"
40 #include "generated/airframe.h"
41 
43 
44 #if defined ADC_CHANNEL_VSUPPLY || defined ADC_CHANNEL_CURRENT || defined MILLIAMP_AT_FULL_THROTTLE
45 static struct {
46 #ifdef ADC_CHANNEL_VSUPPLY
47  struct adc_buf vsupply_adc_buf;
48 #endif
49 #ifdef ADC_CHANNEL_CURRENT
50  struct adc_buf current_adc_buf;
51 #endif
52 #ifdef MILLIAMP_AT_FULL_THROTTLE
53  float nonlin_factor;
54 #endif
55 } electrical_priv;
56 #endif
57 
58 #if defined COMMAND_THROTTLE
59 #define COMMAND_CURRENT_ESTIMATION COMMAND_THROTTLE
60 #elif defined COMMAND_THRUST
61 #define COMMAND_CURRENT_ESTIMATION COMMAND_THRUST
62 #endif
63 
64 #ifndef CURRENT_ESTIMATION_NONLINEARITY
65 #define CURRENT_ESTIMATION_NONLINEARITY 1.2
66 #endif
67 
68 int fd;
69 
70 void electrical_init(void) {
71  // First we try to kill the program.elf and its respawner if it is running (done here because initializes first)
72  int ret = system("killall -9 program.elf.respawner.sh; killall -9 program.elf");
73  (void) ret;
74 
75  // Initialize 12c device for power
76  fd = open( "/dev/i2c-1", O_RDWR );
77  if ( ioctl( fd, I2C_SLAVE_FORCE, 0x4a) < 0 ) {
78  fprintf( stderr, "Failed to set slave address: %m\n" );
79  }
80 
82  electrical_priv.nonlin_factor = CURRENT_ESTIMATION_NONLINEARITY;
83 }
84 
85 void electrical_setup(void) {
86  // Turn on MADC in CTRL1
87  if( i2c_smbus_write_byte_data( fd, 0x00, 0x01)) {
88  fprintf( stderr, "Failed to write to I2C device. 1\n" );
89  }
90  // Select ADCIN0 for conversion in SW1SELECT_LSB
91  if( i2c_smbus_write_byte_data( fd, 0x06, 0xff)){
92  fprintf( stderr, "Failed to write to I2C device. 2\n" );
93  }
94  // Select ADCIN12 for conversion in SW1SELECT_MSB
95  if( i2c_smbus_write_byte_data( fd, 0x07, 0xff)) {
96  fprintf( stderr, "Failed to write to I2C device. 3\n" );
97  }
98  // Setup register for averaging
99  if( i2c_smbus_write_byte_data( fd, 0x08, 0xff)) {
100  fprintf( stderr, "Failed to write to I2C device. 4\n" );
101  }
102  // Start all channel conversion by setting bit 5 to one in CTRL_SW1
103  if( i2c_smbus_write_byte_data( fd, 0x12, 0x20)) {
104  fprintf( stderr, "Failed to write to I2C device. 5\n" );
105  }
106 }
107 
109 
111 
112  unsigned char lsb, msb;
113  lsb = i2c_smbus_read_byte_data(fd, 0x37);
114  msb = i2c_smbus_read_byte_data(fd, 0x38);
115 
116  int raw_voltage = (lsb >> 6) | (msb << 2);
117 
118  // we know from spec sheet that ADCIN0 has no prescaler
119  // so that the max voltage range is 1.5 volt
120  // multiply by ten to get decivolts
121 
122  //from raw measurement we got quite a lineair response
123  //9.0V=662, 9.5V=698, 10.0V=737,10.5V=774, 11.0V=811, 11.5V=848, 12.0V=886, 12.5V=923
124  //leading to our 0.13595166 magic number for decivolts conversion
125  electrical.vsupply = raw_voltage*0.13595166;
126 
127  /*
128  * Superellipse: abs(x/a)^n + abs(y/b)^n = 1
129  * with a = 1
130  * b = mA at full throttle
131  * n = 1.2 This defines nonlinearity (1 = linear)
132  * x = throttle
133  * y = current
134  *
135  * define CURRENT_ESTIMATION_NONLINEARITY in your airframe file to change the default nonlinearity factor of 1.2
136  */
137  float b = (float)MILLIAMP_AT_FULL_THROTTLE;
138  float x = ((float)commands[COMMAND_CURRENT_ESTIMATION]) / ((float)MAX_PPRZ);
139  /* electrical.current y = ( b^n - (b* x/a)^n )^1/n
140  * a=1, n = electrical_priv.nonlin_factor
141  */
142  electrical.current = b - pow((pow(b,electrical_priv.nonlin_factor)-pow((b*x),electrical_priv.nonlin_factor)), (1./electrical_priv.nonlin_factor));
143 }
void electrical_periodic(void)
#define CURRENT_ESTIMATION_NONLINEARITY
arch specific electrical status readings
uint16_t vsupply
supply voltage in decivolts
Definition: electrical.h:48
int fd
struct Electrical electrical
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
I2C-bus driver.
Hardware independent code for commands handling.
static __s32 i2c_smbus_read_byte_data(int file, __u8 command)
Definition: i2c-dev.h:195
int32_t current
current in milliamps
Definition: electrical.h:49
void electrical_setup(void)
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
Definition: commands.c:30
static __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value)
Definition: i2c-dev.h:205
#define MAX_PPRZ
Definition: paparazzi.h:8
#define I2C_SLAVE_FORCE
Definition: i2c-dev.h:130
void electrical_init(void)