Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
intermcu_ap.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Freek van Tienen <freek.v.tienen@gmail.com>
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 
27 #include "intermcu_ap.h"
28 #include "pprzlink/intermcu_msg.h"
30 #include "mcu_periph/uart.h"
31 
32 #include "subsystems/electrical.h"
33 #include "autopilot.h"
34 
35 #if COMMANDS_NB > 8
36 #error "INTERMCU UART CAN ONLY SEND 8 COMMANDS OR THE UART WILL BE OVERFILLED"
37 #endif
38 
39 
40 /* Main InterMCU defines */
41 struct intermcu_t intermcu = {
42  .device = (&((INTERMCU_LINK).device)),
43  .enabled = true,
44  .msg_available = false,
45 };
46 uint8_t imcu_msg_buf[128] __attribute__((aligned));
47 static struct fbw_status_t fbw_status;
48 static inline void intermcu_parse_msg(void (*rc_frame_handler)(void));
49 
50 
51 #if PERIODIC_TELEMETRY
53 
54 /* Send FBW status */
55 static void send_status(struct transport_tx *trans, struct link_device *dev)
56 {
57  pprz_msg_send_FBW_STATUS(trans, dev, AC_ID,
60 }
61 #endif
62 
63 /* InterMCU initialization */
64 void intermcu_init(void)
65 {
66  pprz_transport_init(&intermcu.transport);
67 
68 #if PERIODIC_TELEMETRY
70 #endif
71 }
72 
73 /* Check for InterMCU loss */
75 {
76  /* Check for interMCU loss */
77  if (intermcu.time_since_last_frame >= INTERMCU_LOST_CNT) {
78  intermcu.status = INTERMCU_LOST;
79  } else {
80  intermcu.time_since_last_frame++;
81  }
82 }
83 
84 /* Enable or disable the communication of the InterMCU */
85 void intermcu_set_enabled(bool value)
86 {
87  intermcu.enabled = value;
88 }
89 
90 /* Send the actuators to the FBW */
91 void intermcu_set_actuators(pprz_t *command_values, uint8_t ap_mode __attribute__((unused)))
92 {
93  if (!intermcu.enabled) {
94  return;
95  }
96 
97  // Set the autopilot motors on status
98  if (autopilot_motors_on) {
100  }
101 
102  // Send the message and reset cmd_status
103  pprz_msg_send_IMCU_COMMANDS(&(intermcu.transport.trans_tx), intermcu.device,
104  INTERMCU_AP, &intermcu.cmd_status, COMMANDS_NB, command_values); //TODO: Append more status
105  intermcu.cmd_status = 0;
106 }
107 
108 /* Send the spektrum Bind message */
110 {
111  if (intermcu.enabled) {
112  pprz_msg_send_IMCU_SPEKTRUM_SOFT_BIND(&(intermcu.transport.trans_tx), intermcu.device, INTERMCU_AP);
113  }
114 }
115 
116 /* Parse incomming InterMCU messages */
117 #pragma GCC diagnostic ignored "-Wcast-align"
118 static inline void intermcu_parse_msg(void (*rc_frame_handler)(void))
119 {
120  /* Parse the Inter MCU message */
121  uint8_t msg_id = imcu_msg_buf[1];
122  switch (msg_id) {
123  case DL_IMCU_RADIO_COMMANDS: {
124  uint8_t i;
125  uint8_t size = DL_IMCU_RADIO_COMMANDS_values_length(imcu_msg_buf);
126  intermcu.status = DL_IMCU_RADIO_COMMANDS_status(imcu_msg_buf);
127  for (i = 0; i < size; i++) {
128  radio_control.values[i] = DL_IMCU_RADIO_COMMANDS_values(imcu_msg_buf)[i];
129  }
130 
134  rc_frame_handler();
135  break;
136  }
137 
138  case DL_IMCU_FBW_STATUS: {
139  fbw_status.rc_status = DL_IMCU_FBW_STATUS_rc_status(imcu_msg_buf);
140  fbw_status.frame_rate = DL_IMCU_FBW_STATUS_frame_rate(imcu_msg_buf);
141  fbw_status.mode = DL_IMCU_FBW_STATUS_mode(imcu_msg_buf);
142  fbw_status.vsupply = DL_IMCU_FBW_STATUS_vsupply(imcu_msg_buf);
143  fbw_status.current = DL_IMCU_FBW_STATUS_current(imcu_msg_buf);
144  break;
145  }
146 
147  default:
148  break;
149  }
150 }
151 #pragma GCC diagnostic pop
152 
153 /* Radio control event misused as InterMCU event for frame_handler */
154 void RadioControlEvent(void (*frame_handler)(void))
155 {
156  /* Parse incoming bytes */
157  if (intermcu.enabled) {
158  pprz_check_and_parse(intermcu.device, &intermcu.transport, imcu_msg_buf, &intermcu.msg_available);
159 
160  if (intermcu.msg_available) {
161  intermcu_parse_msg(frame_handler);
162  intermcu.msg_available = false;
163  }
164  }
165 }
uint8_t frame_rate
Definition: intermcu_ap.h:53
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define INTERMCU_LOST_CNT
Definition: intermcu.h:38
void RadioControlEvent(void(*frame_handler)(void))
Definition: intermcu_ap.c:154
static struct fbw_status_t fbw_status
Definition: intermcu_ap.c:47
void intermcu_set_enabled(bool value)
Definition: intermcu_ap.c:85
int32_t current
Definition: intermcu_ap.h:56
struct link_device * device
Device used for communication.
Definition: intermcu.h:69
Periodic telemetry system header (includes downlink utility and generated code).
uint8_t cmd_status
Command status information that is transfered (intermcu_cmd_status)
Definition: intermcu.h:75
uint8_t status
Definition: radio_control.h:53
#define INTERMCU_SET_CMD_STATUS(_bit)
Definition: intermcu.h:63
struct intermcu_t intermcu
Definition: intermcu_ap.c:41
int16_t pprz_t
Definition: paparazzi.h:6
void intermcu_periodic(void)
Definition: intermcu_ap.c:74
#define INTERMCU_AP
Definition: intermcu.h:34
enum intermcu_status status
Status of the INTERMCU.
Definition: intermcu.h:71
bool autopilot_motors_on
Definition: autopilot.c:76
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:58
bool msg_available
If we have an InterMCU message.
Definition: intermcu.h:74
uint8_t imcu_msg_buf[128]
The InterMCU message buffer.
Definition: intermcu_ap.c:46
bool enabled
If the InterMCU communication is enabled.
Definition: intermcu.h:73
No interMCU communication anymore.
Definition: intermcu.h:46
static void intermcu_parse_msg(void(*rc_frame_handler)(void))
Definition: intermcu_ap.c:118
Interface for electrical status: supply voltage, current, battery status, etc.
The status of autopilot_motors_on.
Definition: intermcu.h:58
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
struct RadioControl radio_control
Definition: radio_control.c:30
void intermcu_send_spektrum_bind(void)
Definition: intermcu_ap.c:109
struct pprz_transport transport
Transport over communication line (PPRZ)
Definition: intermcu.h:70
Rotorcraft Inter-MCU on the autopilot.
static void send_status(struct transport_tx *trans, struct link_device *dev)
Definition: intermcu_ap.c:55
uint16_t vsupply
Definition: intermcu_ap.h:55
#define RC_OK
Definition: radio_control.h:48
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
void intermcu_init(void)
Definition: intermcu_ap.c:64
uint8_t rc_status
Definition: intermcu_ap.h:52
unsigned char uint8_t
Definition: types.h:14
uint8_t time_since_last_frame
Time since last frame.
Definition: intermcu.h:72
uint8_t time_since_last_frame
Definition: radio_control.h:54
void intermcu_set_actuators(pprz_t *command_values, uint8_t ap_mode)
Definition: intermcu_ap.c:91
uint8_t mode
Definition: intermcu_ap.h:54
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
uint8_t frame_cpt
Definition: radio_control.h:57