Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
downlink.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Pascal Brisset, Antoine Drouin
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 
30 
31 #if (defined DATALINK) || PERIODIC_TELEMETRY
33 #endif
34 
35 #if PERIODIC_TELEMETRY
37 #include "mcu_periph/sys_time.h"
38 
39 static uint32_t last_down_nb_bytes = 0; // previous number of bytes sent
40 static uint32_t last_up_nb_msgs = 0; // previous number of received messages
41 static uint32_t last_ts = 0; // timestamp in usec when last message was send
42 
43 static void send_downlink(struct transport_tx *trans, struct link_device *dev)
44 {
45  // current timestamp
46  uint32_t now_ts = get_sys_time_msec();
47  // compute downlink byte rate
48  if (now_ts > last_ts) {
49  uint16_t down_rate = (1000 * ((uint32_t)dev->nb_bytes - last_down_nb_bytes)) / (now_ts - last_ts);
50  uint16_t up_rate = (1000 * ((uint32_t)datalink_nb_msgs - last_up_nb_msgs)) / (now_ts - last_ts);
51 
52  last_ts = now_ts;
53 #if defined DATALINK || defined SITL
54  last_down_nb_bytes = dev->nb_bytes;
56 #else
58  last_up_nb_msgs = 0;
59 #endif
60 
61  pprz_msg_send_DATALINK_REPORT(trans, dev, AC_ID, &datalink_time, &datalink_nb_msgs, &dev->nb_msgs, &down_rate,
62  &up_rate, &dev->nb_ovrn);
63  }
64 }
65 #endif
66 
67 void downlink_init(void)
68 {
69  // Set initial counters
70  (DefaultDevice).device.nb_ovrn = 0;
71  (DefaultDevice).device.nb_bytes = 0;
72  (DefaultDevice).device.nb_msgs = 0;
73 
74 #if PERIODIC_TELEMETRY
75  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_DATALINK_REPORT, send_downlink);
76 #endif
77 }
78 
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:98
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
Architecture independent timing functions.
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78