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
88 
89  // Digital Channel 1
90  if (sbus_union_.frame.digichannels & (1<<0))
91  raw_[16] = 1811;
92  else
93  raw_[16] = 172;
94 
95  // Digital Channel 2
96  if (sbus_union_.frame.digichannels & (1<<1))
97  raw_[17] = 1811;
98  else
99  raw_[17] = 172;
100 
101  // Failsafe
103 // if (sbus_union_.frame.digichannels & (1<<2))
104 // failsafe_status_ = SBUS_SIGNAL_LOST;
105  if (sbus_union_.frame.digichannels & (1<<3))
107 }
108 
109 void RC_SBUS::read_cb(uint8_t byte)
110 {
111  if (byte == START_BYTE && prev_byte_ == END_BYTE)
112  {
113  buffer_pos_ = 0;
114  frame_started_ = true;
116  }
117 
118  if (frame_started_)
119  {
120  sbus_union_.data[buffer_pos_++] = byte;
121 
122  if (buffer_pos_ == 25)
123  {
124  // If we have a complete packet, decode
126  decode_buffer();
127  else
128  errors_++;
129  frame_started_ = false;
130  }
131  }
132  prev_byte_ = byte;
133 }
134 
void rx_callback(uint8_t byte)
Definition: rc_sbus.cpp:37
unsigned int chan2
Definition: rc_sbus.h:63
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:90
RC_SBUS * rc_ptr
Definition: rc_sbus.cpp:36
uint8_t data[25]
Definition: rc_sbus.h:82
unsigned int chan12
Definition: rc_sbus.h:73
bool lost()
Definition: rc_sbus.cpp:62
void decode_buffer()
Definition: rc_sbus.cpp:67
unsigned int chan7
Definition: rc_sbus.h:68
unsigned int chan5
Definition: rc_sbus.h:66
unsigned int chan0
Definition: rc_sbus.h:61
unsigned int chan11
Definition: rc_sbus.h:72
uint32_t frame_start_ms_
Definition: rc_sbus.h:93
uint8_t startByte
Definition: rc_sbus.h:60
uint32_t errors_
Definition: rc_sbus.h:96
uint8_t prev_byte_
Definition: rc_sbus.h:97
unsigned int chan3
Definition: rc_sbus.h:64
unsigned int chan14
Definition: rc_sbus.h:75
void read_cb(uint8_t byte)
Definition: rc_sbus.cpp:109
unsigned int chan1
Definition: rc_sbus.h:62
failsafe_state_t failsafe_status_
Definition: rc_sbus.h:88
Definition: uart.h:43
UART * uart_
Definition: rc_sbus.h:91
void write(gpio_write_t state)
Definition: gpio.cpp:41
SBUS_t sbus_union_
Definition: rc_sbus.h:86
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:92
unsigned int chan4
Definition: rc_sbus.h:65
unsigned int chan8
Definition: rc_sbus.h:69
void init(GPIO *inv_pin, UART *uart)
Definition: rc_sbus.cpp:42
unsigned int chan15
Definition: rc_sbus.h:76
bool frame_started_
Definition: rc_sbus.h:98
unsigned int chan13
Definition: rc_sbus.h:74
uint8_t buffer_pos_
Definition: rc_sbus.h:95
struct dataFrame_s frame
Definition: rc_sbus.h:83
uint8_t digichannels
Definition: rc_sbus.h:77
unsigned int chan6
Definition: rc_sbus.h:67
float read(uint8_t channel)
Definition: rc_sbus.cpp:57
unsigned int chan9
Definition: rc_sbus.h:70
unsigned int chan10
Definition: rc_sbus.h:71


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Oct 24 2019 03:17:19