Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
generic_com.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Gautier Hattenberger
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
23 /* Generic module to send data to a gsm/satcom module controlled by arduino over i2c */
24 
25 #include "std.h"
26 
28 #include "mcu_periph/i2c.h"
29 
30 #include "state.h"
31 #include "subsystems/gps.h"
32 #include "subsystems/electrical.h"
33 #include "generated/airframe.h"
34 #include "inter_mcu.h"
35 #include "autopilot.h"
37 
38 #define NB_DATA 24
39 
40 #ifndef GENERIC_COM_I2C_DEV
41 #define GENERIC_COM_I2C_DEV i2c0
42 #endif
43 
44 #ifndef GENERIC_COM_SLAVE_ADDR
45 #define GENERIC_COM_SLAVE_ADDR 0x26
46 #endif
47 
49 
51 
52 void generic_com_init(void)
53 {
54  active_com = false;
56 }
57 
58 #define FillBufWith32bit(_buf, _index, _value) { \
59  _buf[_index] = (uint8_t) (_value); \
60  _buf[_index+1] = (uint8_t) ((_value) >> 8); \
61  _buf[_index+2] = (uint8_t) ((_value) >> 16); \
62  _buf[_index+3] = (uint8_t) ((_value) >> 24); \
63  }
64 
65 #define FillBufWith16bit(_buf, _index, _value) { \
66  _buf[_index] = (uint8_t) (_value); \
67  _buf[_index+1] = (uint8_t) ((_value) >> 8); \
68  }
69 
71 {
72 
73  if (com_trans.status != I2CTransDone) { return; }
74 
78  FillBufWith16bit(com_trans.buf, 9, (int16_t)(gps.lla_pos.alt / 1000)); // altitude (meters)
79  FillBufWith16bit(com_trans.buf, 11, gps.gspeed); // ground speed (cm/s)
80  FillBufWith16bit(com_trans.buf, 13, (int16_t)(gps.course / 1e4)); // course (1e3rad)
81  FillBufWith16bit(com_trans.buf, 15, (uint16_t)(stateGetAirspeed_f() * 100)); // TAS (cm/s)
82  uint8_t vsupply = Min(electrical.vsupply * 10.f, 255); // deciVolt
83  uint8_t charge = Min(electrical.vsupply * 10.f, 255); // deciAh
84  com_trans.buf[17] = vsupply;
85  com_trans.buf[18] = charge;
86  com_trans.buf[19] = (uint8_t)(imcu_get_command(COMMAND_THROTTLE) * 100 / MAX_PPRZ);
88  com_trans.buf[21] = nav_block;
91 }
92 
94 {
95  // Handle I2C event
98  }
99 }
100 
101 void start_com(void)
102 {
103  active_com = true;
105 }
106 
107 void stop_com(void)
108 {
109  active_com = false;
110  com_trans.buf[0] = active_com;
112 }
113 
electrical.h
active_com
bool active_com
Definition: generic_com.c:50
MAX_PPRZ
#define MAX_PPRZ
Definition: paparazzi.h:8
LlaCoor_i::lon
int32_t lon
in degrees*1e7
Definition: pprz_geodetic_int.h:61
uint16_t
unsigned short uint16_t
Definition: types.h:16
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
LlaCoor_i::alt
int32_t alt
in millimeters above WGS84 reference ellipsoid
Definition: pprz_geodetic_int.h:62
start_com
void start_com(void)
Definition: generic_com.c:101
generic_com_event
void generic_com_event(void)
Definition: generic_com.c:93
NB_DATA
#define NB_DATA
Definition: generic_com.c:38
pprz_autopilot::flight_time
uint16_t flight_time
flight time in seconds
Definition: autopilot.h:65
inter_mcu.h
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
common_nav.h
com_trans
struct i2c_transaction com_trans
Definition: generic_com.c:48
Electrical::vsupply
float vsupply
supply voltage in V
Definition: electrical.h:45
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
LlaCoor_i::lat
int32_t lat
in degrees*1e7
Definition: pprz_geodetic_int.h:60
std.h
i2c_transmit
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:324
gps.h
Device independent GPS code (interface)
autopilot
struct pprz_autopilot autopilot
Global autopilot structure.
Definition: autopilot.c:50
stateGetAirspeed_f
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition: state.h:1407
nav_block
uint8_t nav_block
Definition: common_flight_plan.c:35
int16_t
signed short int16_t
Definition: types.h:17
uint8_t
unsigned char uint8_t
Definition: types.h:14
GpsState::gspeed
uint16_t gspeed
norm of 2d ground speed in cm/s
Definition: gps.h:97
charge
uint16_t charge[CHARGE_NB]
Definition: charge_sens.c:43
FillBufWith32bit
#define FillBufWith32bit(_buf, _index, _value)
Definition: generic_com.c:58
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
autopilot.h
GENERIC_COM_I2C_DEV
#define GENERIC_COM_I2C_DEV
Definition: generic_com.c:41
generic_com_init
void generic_com_init(void)
Definition: generic_com.c:52
i2c_transaction
I2C transaction structure.
Definition: i2c.h:93
GpsState::course
int32_t course
GPS course over ground in rad*1e7, [0, 2*Pi]*1e7 (CW/north)
Definition: gps.h:99
generic_com_periodic
void generic_com_periodic(void)
Definition: generic_com.c:70
GpsState::lla_pos
struct LlaCoor_i lla_pos
position in LLA (lat,lon: deg*1e7; alt: mm over ellipsoid)
Definition: gps.h:92
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
stop_com
void stop_com(void)
Definition: generic_com.c:107
FillBufWith16bit
#define FillBufWith16bit(_buf, _index, _value)
Definition: generic_com.c:65
electrical
struct Electrical electrical
Definition: electrical.c:66
autopilot_get_mode
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition: autopilot.c:184
state.h
i2c.h
GENERIC_COM_SLAVE_ADDR
#define GENERIC_COM_SLAVE_ADDR
Definition: generic_com.c:45
gps
struct GpsState gps
global GPS state
Definition: gps.c:69
generic_com.h
Min
#define Min(x, y)
Definition: esc_dshot.c:85