Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
intermcu_ap.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Freek van Tienen <freek.v.tienen@gmail.com>
3  * Copyright (C) 2022 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  */
22 
27 #define PERIODIC_C_INTERMCU
28 
30 #include "pprzlink/intermcu_msg.h"
32 #include "generated/modules.h"
33 #include "autopilot.h"
34 #if TELEMETRY_INTERMCU
36 #endif
37 #include "generated/periodic_telemetry.h"
38 
39 #if COMMANDS_NB > 8
40 #warning "INTERMCU UART CAN ONLY SEND 8 COMMANDS OR THE UART MIGHT BE OVERFILLED"
41 #endif
42 
43 
44 /* Main InterMCU defines */
45 struct intermcu_t intermcu = {
46  .device = (&((INTERMCU_LINK).device)),
47  .enabled = true,
48  .msg_available = false,
49 };
50 
51 uint8_t imcu_msg_buf[128] __attribute__((aligned));
52 
53 static struct fbw_status_t fbw_status;
54 
55 
56 #if PERIODIC_TELEMETRY
58 
59 /* Send FBW status */
60 static void send_status(struct transport_tx *trans, struct link_device *dev)
61 {
62  pprz_msg_send_FBW_STATUS(trans, dev, AC_ID,
65 }
66 #endif
67 
68 /* InterMCU initialization */
69 void intermcu_init(void)
70 {
71  pprz_transport_init(&intermcu.transport);
72 
73 #if PERIODIC_TELEMETRY
75 #endif
76 }
77 
78 /* Check for InterMCU loss */
80 {
81  /* Check for interMCU loss */
84  } else {
86  }
87 
88 #ifdef TELEMETRY_PROCESS_InterMCU
89  // send periodic InterMCU if defined in telemetry file
90  periodic_telemetry_send_InterMCU(DefaultPeriodic, &intermcu.transport.trans_tx, intermcu.device);
91 #endif
92 }
93 
94 /* Check new characters */
95 void intermcu_event(void)
96 {
97  /* Parse incoming bytes */
98  if (intermcu.enabled) {
100  if (intermcu.msg_available) {
101  uint8_t class_id = pprzlink_get_msg_class_id(imcu_msg_buf);
102  // reset intermcu timer, don't touch msg_available flag
104 
105  if (class_id == DL_intermcu_CLASS_ID) {
106  // parse intermcu messages and call callbacks
108  } else {
109  // reset datalink_time if message is not intermcu class
110  datalink_time = 0;
112 #if TELEMETRY_INTERMCU
113  // forward all other messages if needed
114  intermcu_dl_on_msg(imcu_msg_buf, intermcu.transport.trans_rx.payload_len);
115 #endif
116  }
117  intermcu.msg_available = false;
118  }
119  }
120 }
121 
122 /* Enable or disable the communication of the InterMCU */
123 void intermcu_set_enabled(bool value)
124 {
125  intermcu.enabled = value;
126 }
127 
128 /* Send the commands to the FBW */
129 void intermcu_send_commands(pprz_t *command_values, uint8_t ap_mode __attribute__((unused)))
130 {
131  if (!intermcu.enabled) {
132  return;
133  }
134 
135  // Set the autopilot motors on status
136  if (autopilot_get_motors_on()) {
138  }
139 
140  // Send the message and reset cmd_status
141  pprz_msg_send_IMCU_COMMANDS(&(intermcu.transport.trans_tx), intermcu.device,
142  AC_ID, &intermcu.cmd_status, COMMANDS_NB, command_values); //TODO: Append more status
143  intermcu.cmd_status = 0;
144 }
145 
146 /* Send the spektrum Bind message */
148 {
149  if (intermcu.enabled) {
150  pprz_msg_send_IMCU_SPEKTRUM_SOFT_BIND(&(intermcu.transport.trans_tx), intermcu.device, AC_ID);
151  }
152 }
153 
155 {
156  fbw_status.rc_status = DL_IMCU_FBW_STATUS_rc_status(buf);
157  fbw_status.frame_rate = DL_IMCU_FBW_STATUS_frame_rate(buf);
158  fbw_status.mode = DL_IMCU_FBW_STATUS_mode(buf);
159  electrical.vsupply = DL_IMCU_FBW_STATUS_vsupply(buf);
160  electrical.current = DL_IMCU_FBW_STATUS_current(buf);
161 }
162 
autopilot_get_motors_on
bool autopilot_get_motors_on(void)
get motors status
Definition: autopilot.c:244
electrical.h
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
intermcu_ap.h
Inter-MCU on the AP side.
intermcu_t::device
struct link_device * device
Device used for communication.
Definition: intermcu.h:70
INTERMCU_LOST_CNT
#define INTERMCU_LOST_CNT
Definition: intermcu.h:35
INTERMCU_SET_CMD_STATUS
#define INTERMCU_SET_CMD_STATUS(_bit)
Definition: intermcu.h:64
intermcu_t::status
enum intermcu_status status
Status of the INTERMCU.
Definition: intermcu.h:72
intermcu_t::time_since_last_frame
uint8_t time_since_last_frame
Time since last frame.
Definition: intermcu.h:73
intermcu_t
Definition: intermcu.h:69
Electrical::vsupply
float vsupply
supply voltage in V
Definition: electrical.h:45
intermcu_t::enabled
bool enabled
If the InterMCU communication is enabled.
Definition: intermcu.h:74
fbw_status_t
Definition: intermcu_ap.h:50
intermcu_dl.h
datalink forwarder for InterMCU
INTERMCU_LOST
@ INTERMCU_LOST
No interMCU communication anymore.
Definition: intermcu.h:43
telemetry.h
pprz_t
int16_t pprz_t
Definition: paparazzi.h:6
intermcu
struct intermcu_t intermcu
Definition: intermcu_ap.c:45
intermcu_send_spektrum_bind
void intermcu_send_spektrum_bind(void)
send binding signal for spektrum receiver
Definition: intermcu_ap.c:147
dev
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
INTERMCU_CMD_MOTORS_ON
@ INTERMCU_CMD_MOTORS_ON
The status of intermcu_ap_motors_on.
Definition: intermcu.h:57
intermcu_init
void intermcu_init(void)
Definition: intermcu_ap.c:69
register_periodic_telemetry
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
dl_parse_msg
void dl_parse_msg(struct link_device *dev, struct transport_tx *trans, uint8_t *buf)
Should be called when chars are available in dl_buffer.
Definition: setup_actuators.c:112
intermcu_dl_on_msg
void intermcu_dl_on_msg(uint8_t *msg, uint8_t size)
function to forward telemetry from AP to the ground
Definition: intermcu_dl.c:79
autopilot.h
intermcu_t::msg_available
bool msg_available
If we have an InterMCU message.
Definition: intermcu.h:75
intermcu_t::cmd_status
uint8_t cmd_status
Command status information that is transfered (intermcu_cmd_status)
Definition: intermcu.h:76
intermcu_periodic
void intermcu_periodic(void)
Definition: intermcu_ap.c:79
fbw_status_t::mode
uint8_t mode
Definition: intermcu_ap.h:53
fbw_status
static struct fbw_status_t fbw_status
Definition: intermcu_ap.c:53
intermcu_set_enabled
void intermcu_set_enabled(bool value)
enable/disable intermcu link
Definition: intermcu_ap.c:123
fbw_status_t::frame_rate
uint8_t frame_rate
Definition: intermcu_ap.h:52
intermcu_event
void intermcu_event(void)
Definition: intermcu_ap.c:95
imcu_msg_buf
uint8_t imcu_msg_buf[128]
The InterMCU message buffer.
Definition: intermcu_ap.c:51
intermcu_t::transport
struct pprz_transport transport
Transport over communication line (PPRZ)
Definition: intermcu.h:71
send_status
static void send_status(struct transport_tx *trans, struct link_device *dev)
Definition: intermcu_ap.c:60
intermcu_send_commands
void intermcu_send_commands(pprz_t *command_values, uint8_t ap_mode)
send command vector over intermcu link instead of actuators
Definition: intermcu_ap.c:129
electrical
struct Electrical electrical
Definition: electrical.c:66
intermcu_parse_IMCU_FBW_STATUS
void intermcu_parse_IMCU_FBW_STATUS(uint8_t *buf)
Datalink event functions.
Definition: intermcu_ap.c:154
fbw_status_t::rc_status
uint8_t rc_status
Definition: intermcu_ap.h:51
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
Electrical::current
float current
current in A
Definition: electrical.h:46