SBUSEncoder
Python class that encodes the sbus protocol serial signal. The sbus protocol is an UART protocol that specifies the way to send a receive multiple channel information by using a conventional UART channel.
Each channel carries information about a control value. It is mainly used by RC receivers to control the angular movement of an object along its different axis.
Among the control values in an sbus protocol message are the known throttle, aileron, rudder and elevation.
Each manufacturer can have a slightly different implementation of the sbus protocol.
The sbus implementation for the Gremsy H16 requires that the idle, stop and parity bits are inverted w.r.t to their voltage value in a conventional UART communication. That is why a hardware inverter (i.e. 74HCN04) is used on the signal.
The following is the standard convention for the channels in Gremsy H16 (although it can be changed):
-
Channel 2 is assumed to be elevation (pitch)
-
Channel 4 is assumed to be pan (yaw)
-
Channel 5 is assumed to be mode (lock, follow, off)
Source code in a2gmeasurements.py
3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 | |
__init__()
Initializes the channels.
Establishes a linear mapping between the RC control value interval (-100, 100) and the actual values seen at the osciloscoppe of a given channel (i.e rudder).
Define some attributes of the class. The atributes related with drifting are defined in the section Gremsy H16 Gimbal of the "Manual A2GMeasurements".
Source code in a2gmeasurements.py
change_mode(mode='LOCK')
Changes the mode of all gimbal motors. According to manufacturers H16 manual the choices are: FOLLOW and LOCK.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode |
str
|
either "FOLLOW" or "LOCK". A description of each mode is on the manufacturer H16 manual. Defaults to |
'LOCK'
|
Source code in a2gmeasurements.py
encode_data()
Encodes the values on the channels according to the sbus protocol.
Returns:
| Name | Type | Description |
|---|---|---|
packet |
list of int
|
a 24-fields packet/msg that encodes the channel values according to sbus protocol. |
Source code in a2gmeasurements.py
move_gimbal(ele, rud, mov_time)
Moves the gimbal a certain angle. The angle to be moved is determined by the RC control value for yaw, the RC control value for pitch, and the time those values are hold before realeasing them.
The RC control values for yaw (rud) and pitch (ele) are values in the range (-100, 100) that behave as speed values: speed the gimbal will move in that particular axis (i.e. speed it will move in the yaw axis).
Angle = Angular Speed x time
Angular Speed = function of the RC control value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ele |
int
|
RC control value for pitch. Can be thought as the pitch axis velocity. Between -100, 100. |
required |
rud |
int
|
RC control value for yaw. Can be thought as the yaw axis velocity. Between -100, 100. |
required |
mov_time |
int
|
time (seconds) to hold the velocity in a particular axis. Must be a positive value. |
required |
Source code in a2gmeasurements.py
not_move_command()
Updates the channel so that it does not continue moving.
Source code in a2gmeasurements.py
send_sbus_msg()
Calls the channels encoder to write the message on the serial port.
Since there is a known drifting in the yaw axis, this method sets a different 0 value (no movement) to counter the drifting behaviour. This is explained in "Manual A2GMeasurements" (Gremsy H16 Gimbal section), but a kind of equivalent way to understand this, is that we try to counter drift by changing the effective duty cycle of the yaw channel value.
Source code in a2gmeasurements.py
set_channel(channel, data)
Sets a value on a channel. This a "setter function".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel |
int
|
channel to be set. |
required |
data |
int
|
value to be set at the channel. |
required |
Source code in a2gmeasurements.py
start_sbus(serial_interface='/dev/ttyUSB', period_packet=0.009)
Creates the serial connection between the host computer and the gimbal.
Starts the repeating thread (RepeatTimer class instance) to send data each period_packet seconds. This mimics the behaviour between the RC transmitter and receiver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serial_interface |
str
|
serial port. Defaults to |
'/dev/ttyUSB'
|
period_packet |
float
|
time between calls of |
0.009
|
Source code in a2gmeasurements.py
stop_updating()
Stops the thread to repeatedly call self.send_sbus_msg. Closes the opened serial port for this communication.
turn_off_motors()
Turns off all gimbal motors.
Source code in a2gmeasurements.py
turn_on_motors()
update_channel(channel, value)
Update a channel given by "channel" with the value provided in "value".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel |
int
|
number of the channel: 1-16 |
required |
value |
int
|
a number between -100 and 100 representing the value of the channel |
required |
Source code in a2gmeasurements.py
update_rest_state_channel()
Sets the no movement value (0) of the yaw channel (i.e. channel 4) to the experimentally found value that counters the drift.
Change parameter to change the "effective duty cycle" of the yaw channel value. More explanation is found in "Manual A2GMeasurements" (Gremsy H16 Gimbal section)