Paparazzi UAS  v5.15_devel-105-g9de12e2
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 void electrical_ardrone2_setup(void);
45 
46 static int fd;
47 
49 {
50  // Initialize 12c device for power
51  fd = open("/dev/i2c-1", O_RDWR);
52  if (fd < 0){
53  fprintf(stderr, "Failed to open i2c-1: %m\n");
54  return;
55  }
56 
57  if (ioctl(fd, I2C_SLAVE_FORCE, 0x4a) < 0) {
58  fprintf(stderr, "Failed to set slave address: %m\n");
59  return;
60  }
61 
63 }
64 
66 {
67  // Turn on MADC in CTRL1
68  if (i2c_smbus_write_byte_data(fd, 0x00, 0x01)) {
69  fprintf(stderr, "Failed to write to I2C device. 1\n");
70  }
71  // Select ADCIN0 for conversion in SW1SELECT_LSB
72  if (i2c_smbus_write_byte_data(fd, 0x06, 0xff)) {
73  fprintf(stderr, "Failed to write to I2C device. 2\n");
74  }
75  // Select ADCIN12 for conversion in SW1SELECT_MSB
76  if (i2c_smbus_write_byte_data(fd, 0x07, 0xff)) {
77  fprintf(stderr, "Failed to write to I2C device. 3\n");
78  }
79  // Setup register for averaging
80  if (i2c_smbus_write_byte_data(fd, 0x08, 0xff)) {
81  fprintf(stderr, "Failed to write to I2C device. 4\n");
82  }
83  // Start all channel conversion by setting bit 5 to one in CTRL_SW1
84  if (i2c_smbus_write_byte_data(fd, 0x12, 0x20)) {
85  fprintf(stderr, "Failed to write to I2C device. 5\n");
86  }
87 }
88 
90 {
92 
93  unsigned char lsb, msb;
94  lsb = i2c_smbus_read_byte_data(fd, 0x37);
95  msb = i2c_smbus_read_byte_data(fd, 0x38);
96 
97  int raw_voltage = (lsb >> 6) | (msb << 2);
98 
99  // we know from spec sheet that ADCIN0 has no prescaler
100  // so that the max voltage range is 1.5 volt
101 
102  //from raw measurement we got quite a linear response
103  //9.0V=662, 9.5V=698, 10.0V=737,10.5V=774, 11.0V=811, 11.5V=848, 12.0V=886, 12.5V=923
104  //leading to our 0.013595166 magic number for volts conversion
105  electrical.vsupply = raw_voltage * 0.013595166f;
106 }
void bat_voltage_ardrone2_init(void)
void bat_voltage_ardrone2_periodic(void)
float vsupply
supply voltage in V
Definition: electrical.h:45
static 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
struct Electrical electrical
Definition: electrical.c:66