Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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 "modules/gps/gps.h"
33#include "generated/airframe.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
53{
54 active_com = false;
55 com_trans.status = I2CTransDone;
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
75 com_trans.buf[0] = active_com;
76 FillBufWith32bit(com_trans.buf, 1, gps.lla_pos.lat);
77 FillBufWith32bit(com_trans.buf, 5, gps.lla_pos.lon);
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)(command_get(COMMAND_THROTTLE) * 100 / MAX_PPRZ);
87 com_trans.buf[20] = autopilot_get_mode();
88 com_trans.buf[21] = get_nav_block();
89 FillBufWith16bit(com_trans.buf, 22, autopilot.flight_time);
91}
92
94{
95 // Handle I2C event
96 if (com_trans.status == I2CTransSuccess || com_trans.status == I2CTransFailed) {
97 com_trans.status = I2CTransDone;
98 }
99}
100
101void start_com(void)
102{
103 active_com = true;
104 com_trans.status = I2CTransDone;
105}
106
107void stop_com(void)
108{
109 active_com = false;
110 com_trans.buf[0] = active_com;
112}
113
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition autopilot.c:229
struct pprz_autopilot autopilot
Global autopilot structure.
Definition autopilot.c:50
Core autopilot interface common to all firmwares.
uint16_t charge[CHARGE_NB]
Definition charge_sens.c:43
Hardware independent code for commands handling.
uint8_t get_nav_block()
struct Electrical electrical
Definition electrical.c:92
Interface for electrical status: supply voltage, current, battery status, etc.
#define Min(x, y)
Definition esc_dshot.c:110
#define GENERIC_COM_I2C_DEV
Definition generic_com.c:41
#define FillBufWith16bit(_buf, _index, _value)
Definition generic_com.c:65
void generic_com_init(void)
Definition generic_com.c:52
#define FillBufWith32bit(_buf, _index, _value)
Definition generic_com.c:58
void start_com(void)
void generic_com_periodic(void)
Definition generic_com.c:70
void generic_com_event(void)
Definition generic_com.c:93
struct i2c_transaction com_trans
Definition generic_com.c:48
#define GENERIC_COM_SLAVE_ADDR
Definition generic_com.c:45
bool active_com
Definition generic_com.c:50
#define NB_DATA
Definition generic_com.c:38
void stop_com(void)
struct GpsState gps
global GPS state
Definition gps.c:74
Device independent GPS code (interface).
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:202
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:58
@ I2CTransFailed
transaction failed
Definition i2c.h:59
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:60
I2C transaction structure.
Definition i2c.h:94
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition state.h:1615
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
#define MAX_PPRZ
Definition paparazzi.h:8
API to get/set the generic vehicle states.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.