-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbetalink.c
1964 lines (1692 loc) · 55.6 KB
/
betalink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Communication with a betaflight FC using multiwii protocol
* JG, June 2021
* To compile : gcc -Wall -pthread -o betalink betalink.c
*/
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#if defined(__linux__)
#include <linux/serial.h>
#include <linux/input.h>
#endif
#include "betalink.h"
#include "msp_codes.h"
// Flags
#define BLK_STANDALONE // main is added for testing
//#define BLK_THREADED_UPDATE // activate threaded update
//#define BLK_TWO_FC // Test with rwo FCs
//#define BLK_SINGLE_THREAD // Only one thread per device at a time
//#define BLK_DEBUG // Display additional debug infos
// Defines
#define BLK_DEV_SERIALNB 2810339146 // Serial number of the test FC
#define BLK_DEV2_SERIALNB 707372631 // Serial number of the second test FC
#define BLK_DEV_SERIALLG 16 // Max length of a serial number
#define BLK_BAUDRATE B230400 // Serial baudrate
#define BLK_READ_TIMEOUT 5 // Tenth of second
#define BLK_NB_PING 1000 // Nb roundtrip communication
#define BLK_PERIOD 30000 // Period of serial exchange (us)
#define BLK_STEP_PERIOD 200 // Duration of a step (itertions)
#define BLK_MIN_VEL 3000 // Min velocity in rpm
#define BLK_STEP_VEL 200 // Step amplitude in rpm
#if defined(__linux__)
#define BLK_SERIAL_DEV_DIR "/dev/serial/by-id/"
#elif defined(__APPLE__)
#define BLK_SERIAL_DEV_DIR "/dev/"
#else
#define BLK_SERIAL_DEV_DIR "/dev/"
#endif
#define MSP_PROTOCOL_MAX_BUF_SZ 5000
#define MSP_SYM_BEGIN '$'
#define MSP_SYM_PROTO_V1 'M'
#define MSP_SYM_PROTO_V2 'X'
#define MSP_SYM_FROM_MWC '>'
#define MSP_SYM_TO_MWC '<'
#define MSP_SYM_UNSUPPORTED '!'
#define MSP_PROTOCOL_V1 1
#define MSP_PROTOCOL_V2 2
#define MSP_JUMBO_FRAME_MIN_SIZE 255
#define MSP_IDLE 0
#define MSP_PROTO_IDENTIFIER 1
#define MSP_DIRECTION_V1 2
#define MSP_DIRECTION_V2 3
#define MSP_FLAG_V2 4
#define MSP_PAYLOAD_LENGTH_V1 5
#define MSP_PAYLOAD_LENGTH_JUMBO_LOW 6
#define MSP_PAYLOAD_LENGTH_JUMBO_HIGH 7
#define MSP_PAYLOAD_LENGTH_V2_LOW 8
#define MSP_PAYLOAD_LENGTH_V2_HIGH 9
#define MSP_CODE_V1 10
#define MSP_CODE_JUMBO_V1 11
#define MSP_CODE_V2_LOW 12
#define MSP_CODE_V2_HIGH 13
#define MSP_PAYLOAD_V1 14
#define MSP_PAYLOAD_V2 15
#define MSP_CHECKSUM_V1 16
#define MSP_CHECKSUM_V2 17
#define MSP_JUMBO_FRAME_SIZE_LIMIT 255
#define MSP_V1_LENGTH_OFFSET 3
#define MSP_V2_LENGTH_OFFSET 6
#define MSP_V1_OVERHEAD 6
#define MSP_V2_OVERHEAD 9
// Typedefs
typedef struct blk_msp_s {
uint8_t msp_state;
uint8_t msp_message_direction;
uint8_t msp_code;
uint16_t msp_message_length_expected;
uint16_t msp_message_length_received;
uint8_t msp_message_checksum;
uint8_t msp_messageIsJumboFrame;
uint8_t msp_crcError;
uint8_t msp_packet_error;
uint8_t msp_unsupported;
uint8_t msp_buf_out[MSP_PROTOCOL_MAX_BUF_SZ];
uint8_t msp_buf_in[MSP_PROTOCOL_MAX_BUF_SZ];
} blk_msp_t;
// Globals
int blk_fd[BLK_MAX_DEVICES] =
{ BLK_ERROR_FD,
BLK_ERROR_FD,
BLK_ERROR_FD,
BLK_ERROR_FD,
BLK_ERROR_FD }; // Serial port file descriptor
uint32_t blk_devname[BLK_MAX_DEVICES] = { 0 }; // Serial port crc32 number used to get fd with open
struct termios blk_oldtio[BLK_MAX_DEVICES]; // Backup of initial tty configuration
blk_state_t blk_state[BLK_MAX_DEVICES] = { 0 }; // Main betalink state structure
blk_msp_t blk_msp[BLK_MAX_DEVICES] = { 0 }; // MSP state
//
// betalink API
//
//
// crc32 helper function
//
uint32_t blk_crc32b( char *message ) {
int i, j;
uint32_t byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
byte = message[i]; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
//
// Get the device name from the device serial number
//
char *blk_name_from_serial( uint32_t serial_nb ) {
DIR *d;
struct dirent *dir;
static char portname[PATH_MAX];
uint32_t crc;
// Open directory where serial devices can be found
d = opendir( BLK_SERIAL_DEV_DIR );
// Look for a device name contining teensy serial number
if ( d ) {
printf( "BLK: scanning of connected UART devices...\n" );
// Scan each file in the directory
while ( ( dir = readdir( d ) ) != NULL ) {
if ( ( !strcmp( dir->d_name, "." ) ) || ( !strcmp( dir->d_name, ".." ) ) )
continue;
crc = blk_crc32b( dir->d_name );
printf( " %s: serial number=%u\n", dir->d_name, crc );
if ( crc == serial_nb ) {
// A match is a device name containing the serial number
snprintf( portname, PATH_MAX, "%s%s", BLK_SERIAL_DEV_DIR, dir->d_name );
printf( "BLK: portname found=%s\n", portname );
closedir( d );
return portname;
}
}
closedir( d );
}
return NULL;
}
//
// Get the file descriptor index which device name contains
// specified serial number.
// Returns -1 if no matching fd is found.
//
int blk_get_fd( uint32_t serial_nb ) {
int i;
for ( i = 0; i < BLK_MAX_DEVICES; i++ )
if ( blk_fd[i] != BLK_ERROR_FD )
if ( blk_devname[i] == serial_nb )
return i;
return BLK_ERROR_FD;
}
//
// Initialize serial port
//
int blk_init_port( uint32_t serial_nb ) {
struct termios newtio;
int check_fd;
int fd_idx;
char *portname;
int ret;
// Check if device plugged in
portname = blk_name_from_serial( serial_nb );
if ( !portname )
return BLK_ERROR_DEV;
// Open device
check_fd = open( portname, O_RDWR | O_NOCTTY | O_NONBLOCK );
if ( check_fd < 0 ) {
perror( portname );
return BLK_ERROR_DEV;
}
// Look for an empty slot to store the fd
for ( fd_idx = 0; fd_idx < BLK_MAX_DEVICES; fd_idx++ )
if ( blk_fd[fd_idx] == BLK_ERROR_FD )
break;
// Close fd and throw an error if all slots are used
if ( fd_idx == BLK_MAX_DEVICES ) {
close( check_fd );
return BLK_ERROR_MAX_DEV;
}
// Store the fd and the corresponding devname
blk_fd[fd_idx] = check_fd;
blk_devname[fd_idx] = serial_nb;
/* Save current port settings */
tcgetattr( check_fd, &blk_oldtio[fd_idx] );
/* Define new settings */
bzero( &newtio, sizeof(newtio) );
cfmakeraw( &newtio );
newtio.c_cflag = BLK_BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
#if defined(__APPLE__)
cfsetispeed( &newtio, BLK_BAUDRATE );
cfsetospeed( &newtio, BLK_BAUDRATE );
#endif
/* Apply the settings */
tcflush( check_fd, TCIFLUSH );
tcsetattr( check_fd, TCSANOW, &newtio );
// Initialize main data structure
if ( blk_get_battery_state( serial_nb ) ||
blk_get_motor_throttle( serial_nb ) ||
blk_get_motor_telemetry( serial_nb ) ||
blk_get_imu( serial_nb ) ) {
return BLK_ERROR_INIT_STATE;
}
// Initialize mutex
pthread_mutex_init( &blk_state[fd_idx].read_mutex, NULL );
pthread_mutex_init( &blk_state[fd_idx].update_mutex, NULL );
// Check if betalink firmware is available
ret = blk_detect( serial_nb );
if ( ret ) {
fprintf( stderr, "BLK: error %d in blk_detect\n", ret );
}
return 0;
}
//
// Release serial port
//
void blk_release_port( uint32_t serial_nb ) {
int fd_idx;
// Get fd index from serial number
fd_idx = blk_get_fd( serial_nb );
if ( fd_idx != BLK_ERROR_FD ) {
// Wait for pending threads to finish before proceeding
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
// Restore initial settings if needed
tcsetattr( blk_fd[fd_idx], TCSANOW, &blk_oldtio[fd_idx] );
close( blk_fd[fd_idx] );
// Clear fd and corresponding devname
blk_fd[fd_idx] = BLK_ERROR_FD;
blk_devname[fd_idx] = 0;
// Destroy update mutex
if ( pthread_mutex_destroy( &blk_state[fd_idx].update_mutex ) ) {
perror( "BLK: pthread_mutex_destroy update_mutex" );
}
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
// Destroy read mutex
if ( pthread_mutex_destroy( &blk_state[fd_idx].read_mutex ) ) {
perror( "BLK: pthread_mutex_destroy read_mutex" );
}
}
}
//
// serial write
//
int blk_write( uint32_t serial_nb, uint8_t* buf, uint16_t size ) {
int res = 0, fd_idx;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
// Send output buffer data
res = write( blk_fd[fd_idx], buf, size );
if ( res < 0 ) {
perror( "BLK: write blk_comm" );
return BLK_ERROR_WRITE_SER;
}
// Flush output buffer
fsync( blk_fd[fd_idx] );
return res;
}
//
// serial read
//
int blk_read( uint32_t serial_nb, uint8_t* buf, uint16_t max_size ) {
int ret, res = 0, fd_idx;
uint8_t data_in;
uint16_t size = max_size;
struct timespec start, cur;
unsigned long long elapsed_us;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
// Wait for response
// Get current time
clock_gettime( CLOCK_MONOTONIC, &start );
// Reset byte counter
res = 0;
#ifdef BLK_DEBUG
fprintf( stderr, "FC->BLK:\n" );
#endif
do {
ret = read( blk_fd[fd_idx], &buf[res], 1 );
// Data received
if ( ret > 0 ) {
#ifdef BLK_DEBUG
fprintf( stderr, "%d:%d\t\t[%c]\n", res, buf[res], buf[res] );
#endif
res += ret;
}
// Read error
if ( ret < 0 )
break;
// Read expected size
if ( size == max_size ) {
if ( res > 2 ) {
if ( ( buf[0] == MSP_SYM_BEGIN ) && ( buf[1] == MSP_SYM_PROTO_V1 ) ) {
if ( res > MSP_V1_LENGTH_OFFSET + 1 ) {
size = buf[MSP_V1_LENGTH_OFFSET] + MSP_V1_OVERHEAD;
#ifdef BLK_DEBUG
fprintf( stderr, "BLK: v1 packet size=%d\n", size );
#endif
}
}
if ( ( buf[0] == MSP_SYM_BEGIN ) && ( buf[1] == MSP_SYM_PROTO_V2 ) ) {
if ( res > MSP_V2_LENGTH_OFFSET + 2 ) {
size = buf[MSP_V2_LENGTH_OFFSET] + ( buf[MSP_V2_LENGTH_OFFSET+1] << 8 ) + MSP_V2_OVERHEAD;
#ifdef BLK_DEBUG
fprintf( stderr, "BLK: v2 packet size=%d\n", size );
#endif
}
}
}
if ( size > max_size ) {
fprintf( stderr, "BLK: Packet too large for buffer size.\n" );
size = max_size;
break;
}
}
// Compute time elapsed
clock_gettime( CLOCK_MONOTONIC, &cur );
elapsed_us = ( cur.tv_sec * 1e6 + cur.tv_nsec / 1e3 ) -
( start.tv_sec * 1e6 + start.tv_nsec / 1e3 );
// Timeout
if ( elapsed_us / 100000 > BLK_READ_TIMEOUT )
break;
} while ( res < size );
#ifdef BLK_DEBUG
fprintf( stderr, "BLK: read duration = %d us.\n", elapsed_us );
#endif
// Check response size
if ( res != size ) {
fprintf( stderr, "BLK: Packet with unexpected size received.\n" );
// Flush input buffer
while ( ( ret = read( blk_fd[fd_idx], &data_in, 1 ) ) )
if ( ret <= 0 )
break;
return BLK_ERROR_BAD_PK_SZ;
}
return res;
}
//
// Get motor telemetry data
//
int blk_get_motor_telemetry( uint32_t serial_nb ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
int i;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
ret = msp_encode( fd_idx, MSP_MOTOR_TELEMETRY, NULL, 0 );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret > 0 ) {
// Packet is sccessfully decoded
sbufInit( &sbuf, blk_msp[fd_idx].msp_buf_in, (uint8_t*)(blk_msp[fd_idx].msp_buf_in+ret) );
pthread_mutex_lock( &blk_state[fd_idx].update_mutex );
// Read motor count
blk_state[fd_idx].motor_count = sbufReadU8( &sbuf );
if ( blk_state[fd_idx].motor_count > BLK_MAX_MOTORS )
blk_state[fd_idx].motor_count = BLK_MAX_MOTORS;
// Read motor data
for ( i = 0; i < blk_state[fd_idx].motor_count; i++ ) {
blk_state[fd_idx].rpm[i] = sbufReadU32( &sbuf );
blk_state[fd_idx].inv[i] = sbufReadU16( &sbuf );
blk_state[fd_idx].temp[i] = sbufReadU8( &sbuf );
blk_state[fd_idx].volt[i] = sbufReadU16( &sbuf );
blk_state[fd_idx].amp[i] = sbufReadU16( &sbuf );
blk_state[fd_idx].mah[i] = sbufReadU16( &sbuf );
}
pthread_mutex_unlock( &blk_state[fd_idx].update_mutex );
}
else {
return BLK_MSP_ERROR_DEC;
}
}
else {
return BLK_MSP_ERROR_READ;
}
}
else {
return BLK_MSP_ERROR_WRITE;
}
}
else {
return ret;
}
return 0;
}
//
// Get motor current throttle
//
int blk_get_motor_throttle( uint32_t serial_nb ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
int i;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
ret = msp_encode( fd_idx, MSP_MOTOR, NULL, 0 );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret > 0 ) {
// Packet is sccessfully decoded
sbufInit( &sbuf, blk_msp[fd_idx].msp_buf_in, (uint8_t*)(blk_msp[fd_idx].msp_buf_in+ret) );
// Read motor throttle
pthread_mutex_lock( &blk_state[fd_idx].update_mutex );
for ( i = 0; i < blk_state[fd_idx].motor_count; i++ ) {
blk_state[fd_idx].throttle[i] = sbufReadU16( &sbuf );
}
pthread_mutex_unlock( &blk_state[fd_idx].update_mutex );
}
else {
return BLK_MSP_ERROR_DEC;
}
}
else {
return BLK_MSP_ERROR_READ;
}
}
else {
return BLK_MSP_ERROR_WRITE;
}
}
else {
return ret;
}
return 0;
}
//
// Get battery state
//
int blk_get_battery_state( uint32_t serial_nb ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
ret = msp_encode( fd_idx, MSP_BATTERY_STATE, NULL, 0 );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret > 0 ) {
// Packet is sccessfully decoded
sbufInit( &sbuf, blk_msp[fd_idx].msp_buf_in, (uint8_t*)(blk_msp[fd_idx].msp_buf_in+ret) );
pthread_mutex_lock( &blk_state[fd_idx].update_mutex );
// Read cell count
blk_state[fd_idx].bat_cell_cnt = sbufReadU8( &sbuf );
// Read capacity
blk_state[fd_idx].bat_capa = sbufReadU16( &sbuf );
// Read legacy voltage
sbufReadU8( &sbuf );
// Read mAh drawn
blk_state[fd_idx].bat_mah = sbufReadU16( &sbuf );
// Read current
blk_state[fd_idx].bat_amp = sbufReadU16( &sbuf );
// Read alerts
sbufReadU8( &sbuf );
// Read voltage
blk_state[fd_idx].bat_volt = sbufReadU16( &sbuf );
pthread_mutex_unlock( &blk_state[fd_idx].update_mutex );
}
else {
return BLK_MSP_ERROR_DEC;
}
}
else {
return BLK_MSP_ERROR_READ;
}
}
else {
return BLK_MSP_ERROR_WRITE;
}
}
else {
return ret;
}
return 0;
}
//
// Get IMU raw data
//
int blk_get_imu( uint32_t serial_nb ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
int i;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
ret = msp_encode( fd_idx, MSP_RAW_IMU, NULL, 0 );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret > 0 ) {
// Packet is sccessfully decoded
sbufInit( &sbuf, blk_msp[fd_idx].msp_buf_in, (uint8_t*)(blk_msp[fd_idx].msp_buf_in+ret) );
pthread_mutex_lock( &blk_state[fd_idx].update_mutex );
// Read accelerometers
for ( i = 0; i < 3; i++ )
blk_state[fd_idx].acc[i] = sbufReadU16( &sbuf );
// Read gyros
for ( i = 0; i < 3; i++ )
blk_state[fd_idx].gyr[i] = sbufReadU16( &sbuf );
// Read magnetometers
for ( i = 0; i < 3; i++ )
blk_state[fd_idx].mag[i] = sbufReadU16( &sbuf );
pthread_mutex_unlock( &blk_state[fd_idx].update_mutex );
}
else {
return BLK_MSP_ERROR_DEC;
}
}
else {
return BLK_MSP_ERROR_READ;
}
}
else {
return BLK_MSP_ERROR_WRITE;
}
}
else {
return ret;
}
return 0;
}
//
// Motor arming enable control
//
int blk_enable_motor( uint32_t serial_nb, uint8_t flag ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
// Define MSP data
sbufInit( &sbuf, buf, (uint8_t*)(buf+MSP_PROTOCOL_MAX_BUF_SZ) );
if ( flag ) {
sbufWriteU8( &sbuf, false ); // ArmingDisabled
sbufWriteU8( &sbuf, true ); // disableRunawayTakeoff
}
else {
sbufWriteU8( &sbuf, true ); // ArmingDisabled
sbufWriteU8( &sbuf, false ); // disableRunawayTakeoff
}
// Start communication
ret = msp_encode( fd_idx, MSP_SET_ARMING_DISABLED, buf, sbufIndex(&sbuf) );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret == 0 ) {
// Packet is sccessfully decoded
return 0;
}
else {
return BLK_MSP_ERROR_DEC;
}
}
else {
return BLK_MSP_ERROR_READ;
}
}
else {
return BLK_MSP_ERROR_WRITE;
}
}
else {
return ret;
}
}
//
// Motor throttle_control
// BLK_PWM_RANGE_MIN : motor stop
//
int blk_set_motor( uint32_t serial_nb, uint16_t *throttle ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
int i;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
// Consistency check
if ( ( blk_state[fd_idx].motor_count == 0 ) ||
( blk_state[fd_idx].motor_count > BLK_MAX_MOTORS ) )
return BLK_ERROR_INT;
// Define MSP data
sbufInit( &sbuf, buf, (uint8_t*)(buf+MSP_PROTOCOL_MAX_BUF_SZ) );
for ( i = 0; i < blk_state[fd_idx].motor_count; i++ ) {
if ( throttle[i] < BLK_PWM_RANGE_MIN )
sbufWriteU16( &sbuf, BLK_PWM_RANGE_MIN );
else if ( throttle[i] > BLK_PWM_RANGE_MAX )
sbufWriteU16( &sbuf, BLK_PWM_RANGE_MAX );
else
sbufWriteU16( &sbuf, throttle[i] );
}
// Start communication
ret = msp_encode( fd_idx, MSP_SET_MOTOR, buf, sbufIndex(&sbuf) );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret == 0 ) {
// Packet is sccessfully decoded
return 0;
}
else {
return BLK_MSP_ERROR_DEC;
}
}
else {
return BLK_MSP_ERROR_READ;
}
}
else {
return BLK_MSP_ERROR_WRITE;
}
}
else {
return ret;
}
}
//
// Detect if betalink firmware is available
//
int blk_detect( uint32_t serial_nb ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
int i;
uint16_t vel_ref[BLK_MAX_MOTORS] = { 0 };
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
// By default, no betalink firmware
blk_state[fd_idx].betalink_detected = false;
// Consistency check
if ( ( blk_state[fd_idx].motor_count == 0 ) ||
( blk_state[fd_idx].motor_count > BLK_MAX_MOTORS ) )
return BLK_ERROR_INT;
// Define MSP data
sbufInit( &sbuf, buf, (uint8_t*)(buf+MSP_PROTOCOL_MAX_BUF_SZ) );
for ( i = 0; i < blk_state[fd_idx].motor_count; i++ ) {
sbufWriteU16( &sbuf, vel_ref[i] );
}
// Start communication
ret = msp_encode( fd_idx, MSP_BETALINK, buf, sbufIndex(&sbuf) );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret > 0 ) {
// Packet is sccessfully decoded
sbufInit( &sbuf, blk_msp[fd_idx].msp_buf_in, (uint8_t*)(blk_msp[fd_idx].msp_buf_in+ret) );
pthread_mutex_lock( &blk_state[fd_idx].update_mutex );
// Read timestamp
blk_state[fd_idx].timestamp = sbufReadU32( &sbuf );
// Read voltage
blk_state[fd_idx].bat_volt = sbufReadU16( &sbuf );
// Read current
blk_state[fd_idx].bat_amp = sbufReadU16( &sbuf );
// Read mAh drawn
blk_state[fd_idx].bat_mah = sbufReadU16( &sbuf );
// Read accelerometers
for ( i = 0; i < 3; i++ )
blk_state[fd_idx].acc[i] = sbufReadU16( &sbuf );
// Read gyros
for ( i = 0; i < 3; i++ )
blk_state[fd_idx].gyr[i] = sbufReadU16( &sbuf );
// Read magnetometers
for ( i = 0; i < 3; i++ )
blk_state[fd_idx].mag[i] = sbufReadU16( &sbuf );
// Read Euler angles
blk_state[fd_idx].roll = sbufReadU16( &sbuf );
blk_state[fd_idx].pitch = sbufReadU16( &sbuf );
blk_state[fd_idx].yaw = sbufReadU16( &sbuf );
// Read motor data
for ( i = 0; i < blk_state[fd_idx].motor_count; i++ ) {
blk_state[fd_idx].rpm[i] = sbufReadU32( &sbuf );
blk_state[fd_idx].inv[i] = sbufReadU16( &sbuf );
}
blk_state[fd_idx].betalink_detected = true;
pthread_mutex_unlock( &blk_state[fd_idx].update_mutex );
fprintf( stderr, "BLK: betalink firmware detected!\n" );
return 0;
}
else {
return BLK_MSP_ERROR_DEC;
}
}
else {
return BLK_MSP_ERROR_READ;
}
}
else {
return BLK_MSP_ERROR_WRITE;
}
}
else {
return ret;
}
}
//
// Send motor velocity reference and refresh sensor values
// If 1000 < vel_ref <= 2000 => throttle signal
// If 2000 < vel_ref => velocity reference
// If vel_ref <= 1000 => stop motor
//
int blk_update( uint32_t serial_nb, uint16_t *vel_ref ) {
uint8_t buf[MSP_PROTOCOL_MAX_BUF_SZ];
int ret, ret2;
sbuf_t sbuf;
int fd_idx;
int i;
// Get fd index
fd_idx = blk_get_fd( serial_nb );
// Check if fd index is valid
if ( fd_idx == BLK_ERROR_FD )
return BLK_ERROR_FD;
// Consistency check
if ( ( blk_state[fd_idx].motor_count == 0 ) ||
( blk_state[fd_idx].motor_count > BLK_MAX_MOTORS ) )
return BLK_ERROR_INT;
// Multiple transactions when no betalink firmware is detected
if ( blk_state[fd_idx].betalink_detected == false ) {
ret = blk_set_motor( serial_nb, vel_ref );
if ( ret )
return ret;
ret = blk_get_imu( serial_nb );
if ( ret )
return ret;
ret = blk_get_battery_state( serial_nb );
if ( ret )
return ret;
ret = blk_get_motor_telemetry( serial_nb );
return ret;
}
// Define MSP data
sbufInit( &sbuf, buf, (uint8_t*)(buf+MSP_PROTOCOL_MAX_BUF_SZ) );
for ( i = 0; i < blk_state[fd_idx].motor_count; i++ ) {
sbufWriteU16( &sbuf, vel_ref[i] );
}
// Start communication
ret = msp_encode( fd_idx, MSP_BETALINK, buf, sbufIndex(&sbuf) );
if ( ret > 0 ) {
ret2 = blk_write( serial_nb, blk_msp[fd_idx].msp_buf_out, ret );
if ( ret2 == ret ) {
pthread_mutex_lock( &blk_state[fd_idx].read_mutex );
ret = blk_read( serial_nb, buf, MSP_PROTOCOL_MAX_BUF_SZ );
pthread_mutex_unlock( &blk_state[fd_idx].read_mutex );
if ( ret > 0 ) {
ret = msp_decode( fd_idx, buf, ret );
if ( ret > 0 ) {