Library decoding Futaba SBUS data and control SBUS Servos. Hint! The TTL signal from the Futaba SBUS receiver / the TTL signal to the SBUS servos must be inverted. Use some 3.3V compatibel TTL logic chip such as 74LVT04 etc.

Committer:
Digixx
Date:
Thu Feb 09 21:50:00 2012 +0000
Revision:
1:e3c92fba87f2
Parent:
0:6618cf21c95c
1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Digixx 0:6618cf21c95c 1 /* mbed R/C Futaba SBUS Library
Digixx 0:6618cf21c95c 2 * Copyright (c) 2011-2012 digixx
Digixx 0:6618cf21c95c 3 *
Digixx 0:6618cf21c95c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Digixx 0:6618cf21c95c 5 * of this software and associated documentation files (the "Software"), to deal
Digixx 0:6618cf21c95c 6 * in the Software without restriction, including without limitation the rights
Digixx 0:6618cf21c95c 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Digixx 0:6618cf21c95c 8 * copies of the Software, and to permit persons to whom the Software is
Digixx 0:6618cf21c95c 9 * furnished to do so, subject to the following conditions:
Digixx 0:6618cf21c95c 10 *
Digixx 0:6618cf21c95c 11 * The above copyright notice and this permission notice shall be included in
Digixx 0:6618cf21c95c 12 * all copies or substantial portions of the Software.
Digixx 0:6618cf21c95c 13 *
Digixx 0:6618cf21c95c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Digixx 0:6618cf21c95c 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Digixx 0:6618cf21c95c 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Digixx 0:6618cf21c95c 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Digixx 0:6618cf21c95c 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Digixx 0:6618cf21c95c 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Digixx 0:6618cf21c95c 20 * THE SOFTWARE.
Digixx 0:6618cf21c95c 21 */
Digixx 0:6618cf21c95c 22
Digixx 0:6618cf21c95c 23
Digixx 0:6618cf21c95c 24 #ifndef MBED_FUTABA_SBUS_H
Digixx 0:6618cf21c95c 25 #define MBED_FUTABA_SBUS_H
Digixx 0:6618cf21c95c 26
Digixx 0:6618cf21c95c 27 #define SBUS_SIGNAL_OK 0x00
Digixx 0:6618cf21c95c 28 #define SBUS_SIGNAL_LOST 0x01
Digixx 0:6618cf21c95c 29 #define SBUS_SIGNAL_FAILSAFE 0x03
Digixx 0:6618cf21c95c 30
Digixx 0:6618cf21c95c 31 #include "MODSERIAL.h"
Digixx 0:6618cf21c95c 32 #include "mbed.h"
Digixx 0:6618cf21c95c 33
Digixx 0:6618cf21c95c 34 /** SBUS control class, based on MODSERIAL
Digixx 0:6618cf21c95c 35 *
Digixx 0:6618cf21c95c 36 * Example:
Digixx 0:6618cf21c95c 37 * @code
Digixx 0:6618cf21c95c 38 * // Continuously sweep the servo through it's full range
Digixx 0:6618cf21c95c 39 * #include "FutabaSBUS.h"
Digixx 0:6618cf21c95c 40 * #include "mbed.h"
Digixx 0:6618cf21c95c 41 *
Digixx 0:6618cf21c95c 42 * FutabaSBUS sbus(p28, p27);
Digixx 0:6618cf21c95c 43 *
Digixx 0:6618cf21c95c 44 * int main() {
Digixx 0:6618cf21c95c 45 * sbus.passthrough(false);
Digixx 0:6618cf21c95c 46 * while(1) {
Digixx 0:6618cf21c95c 47 * for(int i=0; i<100; i++) {
Digixx 0:6618cf21c95c 48 * sbus.servo(1) = i/100.0;
Digixx 0:6618cf21c95c 49 * wait(0.01);
Digixx 0:6618cf21c95c 50 * }
Digixx 0:6618cf21c95c 51 * for(int i=100; i>0; i--) {
Digixx 0:6618cf21c95c 52 * sbus.servo(1) = i/100.0;
Digixx 0:6618cf21c95c 53 * wait(0.01);
Digixx 0:6618cf21c95c 54 * }
Digixx 0:6618cf21c95c 55 * }
Digixx 0:6618cf21c95c 56 * }
Digixx 0:6618cf21c95c 57 * @endcode
Digixx 0:6618cf21c95c 58 */
Digixx 0:6618cf21c95c 59
Digixx 0:6618cf21c95c 60 class FutabaSBUS {
Digixx 0:6618cf21c95c 61 public:
Digixx 1:e3c92fba87f2 62 /** create a FutabaSBUS object connected to the specified serial pins
Digixx 1:e3c92fba87f2 63 *
Digixx 1:e3c92fba87f2 64 * &param pin serial tx,rx to connect to
Digixx 1:e3c92fba87f2 65 */
Digixx 0:6618cf21c95c 66 FutabaSBUS(PinName tx, PinName rx);
Digixx 1:e3c92fba87f2 67
Digixx 1:e3c92fba87f2 68 /** Read channel(1..16), digital raw data
Digixx 1:e3c92fba87f2 69 *
Digixx 1:e3c92fba87f2 70 * &param raw data from receiver range from 0 to 4096, normal from 352 1696
Digixx 1:e3c92fba87f2 71 */
Digixx 0:6618cf21c95c 72 int16_t channel(uint8_t ch);
Digixx 1:e3c92fba87f2 73
Digixx 1:e3c92fba87f2 74 /** Read digital channel(1..2), range 0..1
Digixx 1:e3c92fba87f2 75 *
Digixx 1:e3c92fba87f2 76 * &param range 0..1
Digixx 1:e3c92fba87f2 77 */
Digixx 0:6618cf21c95c 78 uint8_t digichannel(uint8_t ch);
Digixx 1:e3c92fba87f2 79
Digixx 1:e3c92fba87f2 80 /** Set servo position, raw data, range 200..2000?
Digixx 1:e3c92fba87f2 81 *
Digixx 1:e3c92fba87f2 82 * &param raw data 0..2048
Digixx 1:e3c92fba87f2 83 */
Digixx 0:6618cf21c95c 84 void servo(uint8_t ch, int16_t position);
Digixx 1:e3c92fba87f2 85
Digixx 1:e3c92fba87f2 86 /** Set digital channel, 0..1
Digixx 1:e3c92fba87f2 87 *
Digixx 1:e3c92fba87f2 88 * &param range 0..1
Digixx 1:e3c92fba87f2 89 */
Digixx 0:6618cf21c95c 90 void digiservo(uint8_t ch, uint8_t position);
Digixx 1:e3c92fba87f2 91
Digixx 1:e3c92fba87f2 92 /** Read failsafe condition
Digixx 1:e3c92fba87f2 93 *
Digixx 1:e3c92fba87f2 94 * &param 0=no failsafe 1=lost signal 3=failsafe
Digixx 1:e3c92fba87f2 95 */
Digixx 0:6618cf21c95c 96 uint8_t failsafe(void);
Digixx 1:e3c92fba87f2 97
Digixx 1:e3c92fba87f2 98 /** Set logical data passtrough - servo values are ignored, using received data
Digixx 1:e3c92fba87f2 99 *
Digixx 1:e3c92fba87f2 100 * &param bool
Digixx 1:e3c92fba87f2 101 */
Digixx 0:6618cf21c95c 102 void passthrough(bool mode);
Digixx 1:e3c92fba87f2 103
Digixx 1:e3c92fba87f2 104 /** Read logical data passtrough
Digixx 1:e3c92fba87f2 105 *
Digixx 1:e3c92fba87f2 106 * &param bool
Digixx 1:e3c92fba87f2 107 */
Digixx 0:6618cf21c95c 108 bool passthrough(void);
Digixx 0:6618cf21c95c 109
Digixx 0:6618cf21c95c 110 private:
Digixx 0:6618cf21c95c 111 MODSERIAL sbus_;
Digixx 0:6618cf21c95c 112 Ticker rxSBUS;
Digixx 0:6618cf21c95c 113 void SBUS_irq_rx(MODSERIAL_IRQ_INFO *q);
Digixx 0:6618cf21c95c 114 void rx_ticker_500us(void);
Digixx 0:6618cf21c95c 115 void update_channels(void);
Digixx 0:6618cf21c95c 116 void update_servos(void);
Digixx 0:6618cf21c95c 117 volatile int rx_timeout;
Digixx 0:6618cf21c95c 118 volatile int tx_timeout;
Digixx 0:6618cf21c95c 119 };
Digixx 0:6618cf21c95c 120
Digixx 0:6618cf21c95c 121 #endif