Futaba S-BUS Library. Let you control 16 servos and 2 digital channels

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FutabaSBUS.h Source File

FutabaSBUS.h

00001 /* mbed R/C Futaba SBUS Library
00002  * Copyright (c) 2011-2012 digixx
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 
00024 #ifndef MBED_FUTABA_SBUS_H
00025 #define MBED_FUTABA_SBUS_H
00026 
00027 #define SBUS_SIGNAL_OK          0x00
00028 #define SBUS_SIGNAL_LOST        0x01
00029 #define SBUS_SIGNAL_FAILSAFE    0x03
00030 
00031 #include "MODSERIAL.h"
00032 #include "mbed.h"
00033 
00034 /** SBUS control class, based on MODSERIAL
00035  *
00036  * Example:
00037  * @code
00038  * // Continuously sweep the servo through it's full range
00039  * #include "FutabaSBUS.h"
00040  * #include "mbed.h"
00041  * 
00042  * FutabaSBUS sbus(p28, p27);
00043  * 
00044  * int main() {
00045  *     sbus.passthrough(false);
00046  *     while(1) {
00047  *         for(int i=0; i<100; i++) {
00048  *             sbus.servo(1) = i/100.0;
00049  *             wait(0.01);
00050  *         }
00051  *         for(int i=100; i>0; i--) {
00052  *             sbus.servo(1) = i/100.0;
00053  *             wait(0.01);
00054  *         }
00055  *     }
00056  * }
00057  * @endcode
00058  */
00059 
00060 class FutabaSBUS {
00061 public:
00062     /** create a FutabaSBUS object connected to the specified serial pins
00063     *
00064     * &param pin serial tx,rx to connect to
00065     */
00066     FutabaSBUS(PinName tx, PinName rx);
00067     
00068     /** Read channel(1..16), digital raw data
00069     *
00070     * &param raw data from receiver range from 0 to 4096, normal from 352 1696
00071     */
00072     int16_t channel(uint8_t ch);
00073     
00074     /** Read digital channel(1..2), range 0..1
00075     *
00076     * &param range 0..1
00077     */
00078     uint8_t digichannel(uint8_t ch);
00079 
00080     /** Set servo position, raw data, range 200..2000?
00081     *
00082     * &param raw data 0..2048
00083     */
00084     void servo(uint8_t ch, int16_t position);
00085 
00086     /** Set digital channel, 0..1
00087     *
00088     * &param range 0..1
00089     */
00090     void digiservo(uint8_t ch, uint8_t position);
00091 
00092     /** Read failsafe condition
00093     *
00094     * &param 0=no failsafe 1=lost signal 3=failsafe
00095     */
00096     uint8_t failsafe(void);
00097 
00098     /** Set logical data passtrough - servo values are ignored, using received data
00099     *
00100     * &param bool
00101     */
00102     void passthrough(bool mode);
00103 
00104     /** Read logical data passtrough
00105     *
00106     * &param bool
00107     */
00108     bool passthrough(void);
00109 
00110 private:
00111     MODSERIAL sbus_;
00112     Ticker  rxSBUS;
00113     void SBUS_irq_rx(MODSERIAL_IRQ_INFO *q);
00114     void rx_ticker_500us(void);
00115     void update_channels(void);
00116     void update_servos(void);
00117     volatile int rx_timeout;
00118     volatile int tx_timeout;
00119 };
00120 
00121 #endif