Paparazzi UAS  v5.15_devel-88-gb3ad7fe
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
actuators_spektrum.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 
27 #include "subsystems/actuators.h"
29 #include "generated/airframe.h"
30 #include "mcu_periph/uart.h"
31 
32 /* Currently we only support 7 channels, but could be extended to 14+ */
33 #if SERVOS_SPEKTRUM_NB > ACTUATORS_SPEKTRUM_MAX_NB
34 #error Spektrum actuators only support less then 7 servos
35 #endif
36 
37 /* Calculate the frequency divider to aim at 11ms (~90Hz) */
38 #if PERIODIC_FREQUENCY < 90
39 #error Spektrum actuators need at leest a frequency of Hz
40 #else
41 static uint8_t freq_trig = PERIODIC_FREQUENCY / 90.0 + 0.5; // Round it to nearest value
42 #endif
43 
44 /* Main actuator structure */
46 static inline void actuators_spektrum_send(struct link_device *dev);
47 
48 /*
49  * Initialize the spektrum devices (UART output devices)
50  */
52 {
53  actuators_spektrum.device = &((ACTUATORS_SPEKTRUM_DEV).device);
54 
55 #ifdef ACTUATORS_SPEKTRUM_DEV2
56  actuators_spektrum.device2 = &((ACTUATORS_SPEKTRUM_DEV2).device);
57 #endif
58 }
59 
60 /*
61  * Transmit the spektrum output at ~90Hz
62  */
64 {
65  static uint8_t cnt = 0;
66 
67  // Only send every 11 ms
68  cnt++;
69  if (cnt == freq_trig) {
71 
72 #ifdef ACTUATORS_SPEKTRUM_DEV2
74 #endif
75 
76  cnt = 0;
77  }
78 }
79 
80 /*
81  * Actually transmit the spektrum output on a link device
82  */
83 static inline void actuators_spektrum_send(struct link_device *dev)
84 {
85  uint8_t i = 0;
86  dev->put_byte(dev->periph, 0, 0x00); // number missed frames
87  dev->put_byte(dev->periph, 0, 0x12); // 7 channels, 11 bit, 11ms
88 
89  /* Transmit all channels */
90  for (i = 0; i < ACTUATORS_SPEKTRUM_MAX_NB; i++) {
91  dev->put_byte(dev->periph, 0, i << 3 | actuators_spektrum.cmds[i] >> 8);
92  dev->put_byte(dev->periph, 0, actuators_spektrum.cmds[i] & 0xFF);
93  }
94 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void actuators_spektrum_init(void)
int32_t cmds[ACTUATORS_SPEKTRUM_MAX_NB]
void actuators_spektrum_set(void)
struct link_device * device
Hardware independent API for actuators (servos, motor controllers).
struct link_device * device2
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
Spektrum actuator driver, which can output as 7 spektrum channels at ~11ms.
unsigned char uint8_t
Definition: types.h:14
#define ACTUATORS_SPEKTRUM_MAX_NB
static void actuators_spektrum_send(struct link_device *dev)
struct ActuatorsSpektrum actuators_spektrum