Robot Control Library
dsm.h
Go to the documentation of this file.
1 /**
2  * <rc/dsm.h>
3  *
4  * @brief DSM2 and DSMX radio interface
5  *
6  * DSM2 and DSMX are common standards for remote control plane and car radios.
7  * Typically, a receiver outputs pulse width modulated signals to individual
8  * servos or ESCs over separate servo connectors. Commonly a satellite-receiver
9  * is mounted on the outside of an RC plane to provide better signal strength
10  * and sends the same information as a serial packet over a 3-pin wire. These
11  * receivers have nothing to do with satellites but instead are named due to
12  * their remote mounting away from the standard receiver.
13  *
14  * The Robotics Cape supports direct connection of satellite receivers as well
15  * as binding to transmitters without the need for a standard receiver and bind
16  * plug as is traditionally used. The software has been tested with Orange brand
17  * DSM2 receivers, as well as Spektrum and JR branded DSMX receivers.
18  *
19  * See rc_balance, rc_test_dsm, rc_bind_dsm, rc_calibrate_dsm, and
20  * rc_dsm_passthroguh examples.
21  *
22  * @addtogroup DSM
23  * @{
24  */
25 
26 #ifndef RC_DSM_H
27 #define RC_DSM_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <stdint.h> // for int64_t
34 
35 #define RC_MAX_DSM_CHANNELS 9
36 
37 /**
38  * @brief Starts the DSM background service
39  *
40  * @return 0 on success, -1 on failure
41  */
42 int rc_dsm_init(void);
43 
44 
45 /**
46  * @brief stops the DSM background service
47  *
48  * @return 0 on success, -1 on failure. 1 if there was a timeout due to user
49  * callback function not returning.
50  */
51 int rc_dsm_cleanup(void);
52 
53 
54 /**
55  * @brief Returns the pulse width in microseconds commanded by the
56  * transmitter for a particular channel.
57  *
58  * The user can specify channels 1 through 9 but non-zero values will only be
59  * returned for channels the transmitter is actually using. The raw values in
60  * microseconds typically range from 900-2100us for a standard radio with
61  * default settings.
62  *
63  * @param[in] ch channel (1-9)
64  *
65  * @return pulse width in microseconds if data is being transmitted, 0 if
66  * data is not being transmitted on that channel, -1 on error
67  */
68 int rc_dsm_ch_raw(int ch);
69 
70 
71 /**
72  * @brief Returns a scaled value from -1 to 1 corresponding to the min and
73  * max values recorded during calibration.
74  *
75  * The user MUST run the rc_calibrate_dsm example to ensure the normalized
76  * values returned by this function are correct. It is possible that values
77  * outside of the range from -1 to 1 are returned if the calibration is not
78  * perfect.
79  *
80  * @param[in] ch channel (1-9)
81  *
82  * @return normalized input from -1.0 to 1.0 if that channel has data, 0 if
83  * that channel has no data, -1 on error.
84  */
85 double rc_dsm_ch_normalized(int ch);
86 
87 
88 /**
89  * @brief This is a check to see if new data is available.
90  *
91  * After new data is received this will return 1. It will return 0 as soon as
92  * any channel has been read by either rc_dsm_ch_raw or rc_dsm_ch_normalized.
93  *
94  * @return returns 1 if new data is ready to be read by the user. otherwise
95  * returns 0
96  */
97 int rc_dsm_is_new_data(void);
98 
99 
100 /**
101  * @brief Set your own callback function to be called when new DSM data is
102  * ready.
103  *
104  * @param[in] func callback function
105  */
106 void rc_dsm_set_callback(void (*func)(void));
107 
108 
109 /**
110  * @brief Set your own callback function to be called when DSM loses
111  * connection.
112  *
113  * @param[in] func callback function
114  */
115 void rc_dsm_set_disconnect_callback(void (*func)(void));
116 
117 
118 /**
119  * @brief Easily check on the state of the DSM radio packets without
120  * checking timeouts yourself.
121  *
122  * @return returns 1 if packets are arriving in good health without
123  * timeouts. returns 0 otherwise.
124  */
126 
127 
128 /**
129  * @brief Measures time since the last DSM packet was received.
130  *
131  * @return Returns the number of nanoseconds since the last dsm packet was
132  * received. Return -1 on error or if no packet has ever been received.
133  */
134 int64_t rc_dsm_nanos_since_last_packet(void);
135 
136 
137 /**
138  * @brief Used to determine if DSM packets are arriving with 10 or 11-bit
139  * resolution
140  *
141  * @return returns 10 or 11 indicating 10-bit or 11-bit resolution returns a
142  * 0 if no packet has been received yet or -1 on error
143  */
144 int rc_dsm_resolution(void);
145 
146 
147 /**
148  * @brief fetches number of DSM channels currently being received.
149  *
150  * @return Returns number of channels being received, 0 if no packet has
151  * been received yet or -1 on error.
152  */
153 int rc_dsm_channels(void);
154 
155 
156 /**
157  * @brief Begins the binding routine and prints instructions to the screen
158  * along the way.
159  *
160  * The user doesn't need to call this function unless they really want to. Use
161  * the rc_bind_dsm example program instead.
162  *
163  * DSM satellite receivers are put into bind mode by sending them a sequence of
164  * pulses right after it receives power and starts up. This program puts the
165  * normally UART signal pin into GPIO pulldown mode temporarily, detects when
166  * the user unplugs and plugs back in the receiver, then sends the binding
167  * pulses.
168  *
169  * The number of pulses dictates the mode the satellite receiver will request
170  * the transmitter to use. The transmitter may bind but use a different mode. I
171  * suggest configuring your radio to use DSMX 11ms fast mode if it allows that.
172  *
173  * 2048 & 1024 indicates 10 or 11 bit resolution. 11ms & 22ms indicates the time
174  * period between the transmitter sending frames. 11ms is required for
175  * transmitters with 8 or more channels.
176  *
177  * Testing done with DX7s, DX6i, DX8, and Orange T-SIX
178  *
179  * Table of Bind Modes
180  *
181  * | pulses | mode |
182  * |:------:|:---------------:|
183  * | 3 | DSM 1024/22ms |
184  * | 5 | DSM 2048/11ms |
185  * | 7 | DSMX 1024/22ms |
186  * | 9 | DSMX 2048/11ms |
187  *
188  * This is a bit of a finicky process and may require a few attempts to work.
189  *
190  * @return 0 on success, -1 on failure
191  */
192 int rc_dsm_bind_routine(void);
193 
194 
195 /**
196  * @brief routine for measuring the min and max values from a transmitter
197  * on each channel and save to disk for future use.
198  *
199  * If a channel isn't used by the transmitter then default values are saved. if
200  * the user forgot to move one of the channels during the calibration process
201  * then defualt values are also saved.
202  *
203  * @return 0 on success, -1 on failure
204  */
205 int rc_dsm_calibrate_routine(void);
206 
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 
212 #endif // RC_DSM_H
213 
214 /** @} end group DSM */
int64_t rc_dsm_nanos_since_last_packet(void)
Measures time since the last DSM packet was received.
int rc_dsm_bind_routine(void)
Begins the binding routine and prints instructions to the screen along the way.
int rc_dsm_is_new_data(void)
This is a check to see if new data is available.
void rc_dsm_set_callback(void(*func)(void))
Set your own callback function to be called when new DSM data is ready.
double rc_dsm_ch_normalized(int ch)
Returns a scaled value from -1 to 1 corresponding to the min and max values recorded during calibrati...
int rc_dsm_ch_raw(int ch)
Returns the pulse width in microseconds commanded by the transmitter for a particular channel...
int rc_dsm_init(void)
Starts the DSM background service.
int rc_dsm_channels(void)
fetches number of DSM channels currently being received.
int rc_dsm_cleanup(void)
stops the DSM background service
void rc_dsm_set_disconnect_callback(void(*func)(void))
Set your own callback function to be called when DSM loses connection.
int rc_dsm_is_connection_active(void)
Easily check on the state of the DSM radio packets without checking timeouts yourself.
int rc_dsm_calibrate_routine(void)
routine for measuring the min and max values from a transmitter on each channel and save to disk for ...
int rc_dsm_resolution(void)
Used to determine if DSM packets are arriving with 10 or 11-bit resolution.