Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
bat_voltage_ardrone2.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 
31 
32 #include <stdio.h>
33 #include <string.h>
34 //#include <stdlib.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <sys/time.h>
38 #include <unistd.h>
39 #include <math.h>
40 #include "mcu_periph/i2c_smbus.h"
41 
42 #include "subsystems/electrical.h"
43 
44 
45 
46 void electrical_ardrone2_setup(void);
47 
48 int fd;
49 
51 {
52  // Initialize 12c device for power
53  fd = open("/dev/i2c-1", O_RDWR);
54  if (ioctl(fd, I2C_SLAVE_FORCE, 0x4a) < 0) {
55  fprintf(stderr, "Failed to set slave address: %m\n");
56  }
57 
59 
60 }
61 
63 {
64  // Turn on MADC in CTRL1
65  if (i2c_smbus_write_byte_data(fd, 0x00, 0x01)) {
66  fprintf(stderr, "Failed to write to I2C device. 1\n");
67  }
68  // Select ADCIN0 for conversion in SW1SELECT_LSB
69  if (i2c_smbus_write_byte_data(fd, 0x06, 0xff)) {
70  fprintf(stderr, "Failed to write to I2C device. 2\n");
71  }
72  // Select ADCIN12 for conversion in SW1SELECT_MSB
73  if (i2c_smbus_write_byte_data(fd, 0x07, 0xff)) {
74  fprintf(stderr, "Failed to write to I2C device. 3\n");
75  }
76  // Setup register for averaging
77  if (i2c_smbus_write_byte_data(fd, 0x08, 0xff)) {
78  fprintf(stderr, "Failed to write to I2C device. 4\n");
79  }
80  // Start all channel conversion by setting bit 5 to one in CTRL_SW1
81  if (i2c_smbus_write_byte_data(fd, 0x12, 0x20)) {
82  fprintf(stderr, "Failed to write to I2C device. 5\n");
83  }
84 }
85 
87 {
88 
90 
91  unsigned char lsb, msb;
92  lsb = i2c_smbus_read_byte_data(fd, 0x37);
93  msb = i2c_smbus_read_byte_data(fd, 0x38);
94 
95  int raw_voltage = (lsb >> 6) | (msb << 2);
96 
97  // we know from spec sheet that ADCIN0 has no prescaler
98  // so that the max voltage range is 1.5 volt
99  // multiply by ten to get decivolts
100 
101  //from raw measurement we got quite a lineair response
102  //9.0V=662, 9.5V=698, 10.0V=737,10.5V=774, 11.0V=811, 11.5V=848, 12.0V=886, 12.5V=923
103  //leading to our 0.13595166 magic number for decivolts conversion
104  electrical.vsupply = raw_voltage * 0.13595166;
105 
106 }
void bat_voltage_ardrone2_init(void)
void bat_voltage_ardrone2_periodic(void)
int fd
I2C-bus driver.
Interface for electrical status: supply voltage, current, battery status, etc.
void electrical_ardrone2_setup(void)
static __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value)
Definition: i2c_smbus.h:194
static __s32 i2c_smbus_read_byte_data(int file, __u8 command)
Definition: i2c_smbus.h:183
#define I2C_SLAVE_FORCE
Definition: i2c_smbus.h:131
uint16_t vsupply
supply voltage in decivolts
Definition: electrical.h:48
struct Electrical electrical
Definition: electrical.c:65