rc_sbus.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #include "rc_sbus.h"
34 #include <functional>
35 
37 void rx_callback(uint8_t byte)
38 {
39  rc_ptr->read_cb(byte);
40 }
41 
42 void RC_SBUS::init(GPIO *inv_pin, UART *uart)
43 {
44  rc_ptr = this;
45  uart_ = uart;
46  inv_pin_ = inv_pin;
47 
48  uart_->set_mode(100000, UART::MODE_8E2);
50 
51  // turn on the serial inverter
53 
54  decode_buffer();
55 }
56 
57 float RC_SBUS::read(uint8_t channel)
58 {
59  return (static_cast<float>(raw_[channel]) - 172.0)/1639.0;
60 }
61 
63 {
65 }
66 
68 {
70 
71  // process actual sbus data, use union to decode
72  // (Map to AETR to keep with ROSflight convention)
89 
90  // Digital Channel 1
91  if (sbus_union_.frame.digichannels & (1<<0))
92  raw_[16] = 1811;
93  else
94  raw_[16] = 172;
95 
96  // Digital Channel 2
97  if (sbus_union_.frame.digichannels & (1<<1))
98  raw_[17] = 1811;
99  else
100  raw_[17] = 172;
101 
102  // Failsafe
104 // if (sbus_union_.frame.digichannels & (1<<2))
105 // failsafe_status_ = SBUS_SIGNAL_LOST;
106  if (sbus_union_.frame.digichannels & (1<<3))
108 }
109 
110 void RC_SBUS::read_cb(uint8_t byte)
111 {
112  if (byte == START_BYTE && prev_byte_ == END_BYTE)
113  {
114  buffer_pos_ = 0;
115  frame_started_ = true;
117  }
118 
119  if (frame_started_)
120  {
121  sbus_union_.data[buffer_pos_++] = byte;
122 
123  if (buffer_pos_ == 25)
124  {
125  // If we have a complete packet, decode
127  decode_buffer();
128  else
129  errors_++;
130  frame_started_ = false;
131  }
132  }
133  prev_byte_ = byte;
134 }
135 
void rx_callback(uint8_t byte)
Definition: rc_sbus.cpp:37
unsigned int chan2
Definition: rc_sbus.h:64
void register_rx_callback(void(*cb)(uint8_t data)) override
Definition: uart.cpp:325
volatile uint32_t millis(void)
Definition: system.c:50
GPIO * inv_pin_
Definition: rc_sbus.h:92
RC_SBUS * rc_ptr
Definition: rc_sbus.cpp:36
uint8_t data[25]
Definition: rc_sbus.h:84
unsigned int chan12
Definition: rc_sbus.h:74
bool lost()
Definition: rc_sbus.cpp:62
void decode_buffer()
Definition: rc_sbus.cpp:67
unsigned int chan7
Definition: rc_sbus.h:69
unsigned int chan5
Definition: rc_sbus.h:67
unsigned int chan0
Definition: rc_sbus.h:62
unsigned int chan11
Definition: rc_sbus.h:73
uint32_t frame_start_ms_
Definition: rc_sbus.h:95
uint8_t startByte
Definition: rc_sbus.h:61
uint32_t errors_
Definition: rc_sbus.h:98
uint8_t prev_byte_
Definition: rc_sbus.h:99
unsigned int chan3
Definition: rc_sbus.h:65
unsigned int chan14
Definition: rc_sbus.h:76
void read_cb(uint8_t byte)
Definition: rc_sbus.cpp:110
unsigned int chan1
Definition: rc_sbus.h:63
failsafe_state_t failsafe_status_
Definition: rc_sbus.h:90
Definition: uart.h:43
UART * uart_
Definition: rc_sbus.h:93
void write(gpio_write_t state)
Definition: gpio.cpp:41
SBUS_t sbus_union_
Definition: rc_sbus.h:88
bool set_mode(uint32_t baud, uart_mode_t mode)
Definition: uart.cpp:273
Definition: gpio.h:37
uint32_t raw_[18]
Definition: rc_sbus.h:94
unsigned int chan4
Definition: rc_sbus.h:66
unsigned int chan8
Definition: rc_sbus.h:70
void init(GPIO *inv_pin, UART *uart)
Definition: rc_sbus.cpp:42
unsigned int chan15
Definition: rc_sbus.h:77
bool frame_started_
Definition: rc_sbus.h:100
unsigned int chan13
Definition: rc_sbus.h:75
uint8_t buffer_pos_
Definition: rc_sbus.h:97
struct dataFrame_s frame
Definition: rc_sbus.h:85
uint8_t digichannels
Definition: rc_sbus.h:78
unsigned int chan6
Definition: rc_sbus.h:68
float read(uint8_t channel)
Definition: rc_sbus.cpp:57
unsigned int chan9
Definition: rc_sbus.h:71
unsigned int chan10
Definition: rc_sbus.h:72


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:25