GimbalRS2 module
Bases: object
Python Class that works as the driver for the gimbal DJI RS2.
The gimbal should be connected to the host computer through an USB-to-PCAN bridge (PCAN System).
It creates a thread (called here a gimbal thread) to handle the communication between the gimbal and this host computer.
More info on "Manual A2GMeasurements".
Gimbal control modified and extended from (based as well on DJI R SDK demo software):
Source code in a2gmeasurements.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | |
__init__(speed_yaw=40, speed_pitch=40, speed_roll=40, DBG_LVL_1=False, DBG_LVL_0=False)
Initialize properties of the class, like the DJI SDK frame header, sequence number and others.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
speed_yaw |
int
|
speed of yaw axis in deg/s. Defaults to 40. |
40
|
speed_pitch |
int
|
speed of pitch axis in deg/s. Defaults to 40. |
40
|
speed_roll |
int
|
speed of roll axis in deg/s. Defaults to 40. |
40
|
DBG_LVL_1 |
bool
|
level of verbose to show at the command line (beta). This shows less verbose than the 0 level. Defaults to False. |
False
|
DBG_LVL_0 |
bool
|
level of verbose to show at the command line (beta). Defaults to False. |
False
|
Source code in a2gmeasurements.py
assemble_can_msg(cmd_type, cmd_set, cmd_id, data)
Builds a DJI RS2 message frame based on the command to be sent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_type |
int
|
see DJI R SDK Protocol and User Interface document for a description. |
required |
cmd_set |
int
|
see DJI R SDK Protocol and User Interface document for a description. |
required |
cmd_id |
int
|
see DJI R SDK Protocol and User Interface document for a description. |
required |
data |
int
|
see DJI R SDK Protocol and User Interface document for a description. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
whole_can_frame |
str
|
parsed can frame whose fields are separated by ":". |
Source code in a2gmeasurements.py
can_buffer_to_full_frame()
Parse the full DJI R frame message from the can buffer.
Its fields are explained in the DJI R SDK Protocol and User Interface.
Returns:
| Name | Type | Description |
|---|---|---|
full_msg_frames |
list
|
a full message frame |
Source code in a2gmeasurements.py
can_callback(data)
Callback for the thread in charge of checking the USB-to-CAN input (receive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
list
|
DJI RS2 frame message |
required |
Source code in a2gmeasurements.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
parse_position_response(data_frame)
Retrieve the position from the full DJI frame message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_frame |
list
|
DJI RS2 frame message |
required |
Source code in a2gmeasurements.py
receive(bus, stop_event)
Threading callback function. Defined when the thread is created. This thread listens for coming (received) can messages on a USB port. Reads 1 entry of the rx bus buffer at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bus |
can.Bus object
|
object pointing to the type of bus (from 'can' python package). |
required |
stop_event |
Event
|
works as a flag to stop receiving messages. |
required |
Source code in a2gmeasurements.py
request_current_position()
Sends command to request the current position of the gimbal.
Blocks thread execution for the time given by attribute TIME_POS_REQ to allow the response to be received.
Source code in a2gmeasurements.py
send_cmd(cmd)
Wrapper to send_data method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd |
str
|
command fields separated by ':'. |
required |
send_data(can_id, data)
Sends a command through the can bus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
can_id |
int
|
static can id. |
required |
data |
list
|
fields of the frame. |
required |
Source code in a2gmeasurements.py
seq_num()
Updates the sequence number of the gimbal data.
Returns:
| Name | Type | Description |
|---|---|---|
seq_str |
str
|
number in hexadecimal split by a |
Source code in a2gmeasurements.py
setPosControl(yaw, roll, pitch, ctrl_byte=1, time_for_action=20)
Set the gimbal position by providing the yaw, roll and pitch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaw |
int
|
yaw angle. Value should be between -1800 and 1800. |
required |
roll |
int
|
roll angle. value should be betweeen -1800 and 1800. However, gimbal might stop if it reachs its maximum/minimum (this) axis value. |
required |
pitch |
int
|
pitch angle. value should be betweeen -1800 and 1800. However, gimbal might stop if it reachs its maximum/minimum (this) axis value. |
required |
ctrl_byte |
hexadecimal
|
Absolute or relative movement. For absolute use 0x01, while for relative use 0x00. Defaults to 0x01. |
1
|
time_for_action |
hexadecimal
|
Time it takes for the gimbal to move to desired position. Implicitly, this command controls the speed of gimbal. It is given in units of 0.1 s. For example: a value of 0x14 is 20, which means that the gimbal will take 2s (20*0.1) to reach its destination. Defaults to 0x14. |
20
|
Returns:
| Name | Type | Description |
|---|---|---|
True |
bool
|
always returns the same, unless there is a raising error. |
Source code in a2gmeasurements.py
setSpeedControl(yaw, roll, pitch, ctrl_byte=128)
Sets speed for each axis of the gimbal.
Always after seting the speed the gimbal roll is moved (strange behaviour).
Developer has to send a setPosControl to set again the position of the gimbal where it was previously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaw |
int
|
yaw speed in units of 0.1 deg/s. |
required |
roll |
int
|
roll speed in units of 0.1 deg/s. |
required |
pitch |
int
|
pitch speed in units of 0.1 deg/s. |
required |
ctrl_byte |
hexadecimal
|
check DJI SDK manual. Defaults to 0x80. |
128
|
Returns:
| Name | Type | Description |
|---|---|---|
True |
bool
|
True if provided arguments are within acceptable range. False otherwise. |
Source code in a2gmeasurements.py
start_thread_gimbal(bitrate=1000000)
Starts the thread for listening the incoming data (if any) from the gimbal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bitrate |
int
|
Bitrate for the usb-to-can interface. This is a parameter inherited from can.Bus. Defaults to 1000000. |
1000000
|
Source code in a2gmeasurements.py
stop_thread_gimbal()
Stops the gimbal thread by setting the threading. Event attribute created in start_thread_gimbal.
validate_api_call(data_frame)
CRC error check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_frame |
list
|
DJI RS2 frame message |
required |
Returns:
| Name | Type | Description |
|---|---|---|
validated |
bool
|
passed or not the crc check |