/
roboclaw.go
1503 lines (1380 loc) · 46.5 KB
/
roboclaw.go
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
//Created by Michael Dysart
package roboclaw
import (
"fmt"
"github.com/tarm/serial"
"io"
"time"
)
// A structure for describing the roboclaw interface
type Roboclaw struct {
port *serial.Port
retries uint8
writeSleep bool
}
type Config struct {
Name string //the name of the serial port
Baud int // the baud rate for the serial port
Retries uint8 // the number of attempted retries for sending a command to the roboclaws
WriteSleep bool // The timeout for the roboclaws is 10 milliseconds
// However, some operating systems have a minimum timeout above this value
// For certain applications, it is inefficient to have to wait for a reply to
// a write command (such as setting motor speed) if no remedial action is
// possible or planned if the write fails. For example, in the situation
// where the tx line to the roboclaw is functional but the rx line has failed,
// it is often desired to still be able to write to the roboclaws
// but innefficient to wait for the non-existent reply. The WriteSleep
// flag causes the system to wait 10 milliseconds after writing the command to the rover
// instead of the full time necessary to wait for the reply (which is not expected to come).
// This flag does not affect any commands that involve reading from the roboclaws or expected
// any reply other than the acknoweldgement 0xFF.
}
/*
* Initialize the roboclaw with the desired serial port
* @param rc (&Config) the roboclaw config struct
* @returns (*Roboclaw, error) the reference to the roboclaw and any errors that occur when opening the file
*/
func Init(rc *Config) (*Roboclaw, error) {
// The expected timeout for a roboclaw to reply to a message is 10 milliseconds
// However, some operating systems have a minimum timeout above this value
// For example, on a linux system the minimum timeout for a serial port is 100 milliseconds
c := &serial.Config{Name: rc.Name, Baud: rc.Baud, ReadTimeout: time.Millisecond * 10}
if rc.Retries == 0 {
return nil, fmt.Errorf("Number of retries must be larger than zero")
} else if port, err := serial.OpenPort(c); err == nil {
return &Roboclaw{port: port, retries: rc.Retries, writeSleep: rc.WriteSleep}, err
} else {
return nil, err
}
}
/*
* Close the roboclaw's serial port
* @return {error}
*/
func (r *Roboclaw) Close() error {
return r.port.Close()
}
// See the roboclaw user manual
// at http://downloads.ionmc.com/docs/roboclaw_user_manual.pdf
// for more details
/*
* Drive motor 1 forwards
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127
* @return {error}
*/
func (r *Roboclaw) ForwardM1(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 0, speed)
}
/*
* Drive motor 1 backwards
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127
* @return {error}
*/
func (r *Roboclaw) BackwardM1(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 1, speed)
}
/*
* Drive motor 2 forwards
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127
* @return {error}
*/
func (r *Roboclaw) ForwardM2(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 4, speed)
}
/*
* Drive motor 2 backwards
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127
* @return {error}
*/
func (r *Roboclaw) BackwardM2(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 5, speed)
}
/*
* Sets minimum main voltage (command 57 preferred)
* Roboclaw shuts down if voltage drops below this point
* Volts are (Desired volts - 6) * 5
* @param address {uint8}
* @param voltage {uint8} valid data is 0 to 140 (6V to 34 V)
* @return {error}
*/
func (r *Roboclaw) SetMinVoltageMainBattery(address uint8, voltage uint8) error {
if voltage > 140 {
return fmt.Errorf("Voltage above maximum of 140")
}
return r.write_n(address, 2, voltage)
}
/*
* Sets maximum main voltage (command 57 preferred)
* Roboclaw shuts down if voltage drops below this point
* Volts are Desired volts * 5.12
* @param address {uint8}
* @param voltage {uint8} valid data is 30 to 175 (6V to 34 V)
* @return {error}
*/
func (r *Roboclaw) SetMaxVoltageMainBattery(address uint8, voltage uint8) error {
if voltage > 175 {
return fmt.Errorf("Voltage above maximum of 175")
} else if voltage < 30 {
return fmt.Errorf("Voltage below minimum of 30")
}
return r.write_n(address, 3, voltage)
}
/*
* Drive motor 1 forward or backward
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 full reverse, 64 stop, 127 full forward)
* @return {error}
*/
func (r *Roboclaw) ForwardBackwardM1(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 6, speed)
}
/*
* Drive motor 2 forward or backward
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 full reverse, 64 stop, 127 full forward)
* @return {error}
*/
func (r *Roboclaw) ForwardBackwardM2(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 7, speed)
}
/*
* Drive motors forward in mixed mode
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 stop, 127 full speed)
* @return {error}
*/
func (r *Roboclaw) ForwardMixed(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 8, speed)
}
/*
* Drive motors backward in mixed mode
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 stop, 127 full speed)
* @return {error}
*/
func (r *Roboclaw) BackwardMixed(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 9, speed)
}
/*
* Turn right in mixed mode
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 stop, 127 full speed)
* @return {error}
*/
func (r *Roboclaw) TurnRightMixed(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 10, speed)
}
/*
* Turn left in mixed mode
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 stop, 127 full speed)
* @return {error}
*/
func (r *Roboclaw) TurnLeftMixed(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 11, speed)
}
/*
* Drive forward or backward in mixed mode
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 full speed backward, 0 stop, 127 full forward)
* @return {error}
*/
func (r *Roboclaw) ForwardBackwardMixed(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 12, speed)
}
/*
* Turn in mixed mode
* @param address {uint8}
* @param speed {uint8} valid data is 0 to 127 (0 full speed left, 0 stop, 127 full right)
* @return {error}
*/
func (r *Roboclaw) LeftRightMixed(address uint8, speed uint8) error {
if speed > 127 {
return fmt.Errorf("Speed above maximum of 127")
}
return r.write_n(address, 13, speed)
}
/*
* Read the encoder count for m1
* @param address {uint8}
* @return {uint32, uint8, error} encoder count, encoder status, error
*/
func (r *Roboclaw) ReadEncM1(address uint8) (uint32, uint8, error) {
return r.read4_1(address, 16)
}
/*
* Read the encoder count for m2
* @param address {uint8}
* @return {uint32, uint8, error} encoder count, encoder status, error
*/
func (r *Roboclaw) ReadEncM2(address uint8) (uint32, uint8, error) {
return r.read4_1(address, 17)
}
/*
* Read the encoder speed for m1
* Speed is in pulses per second
* Direction is 0 for forward and 1 for backward
* @param address {uint8}
* @return {uint32, uint8, error} encoder speed, direction, error
*/
func (r *Roboclaw) ReadSpeedM1(address uint8) (uint32, uint8, error) {
return r.read4_1(address, 18)
}
/*
* Read the encoder speed for m2
* Speed is in pulses per second
* Direction is 0 for forward and 1 for backward
* @param address {uint8}
* @return {uint32, uint8, error} encoder speed, direction, error
*/
func (r *Roboclaw) ReadSpeedM2(address uint8) (uint32, uint8, error) {
return r.read4_1(address, 19)
}
/*
* Reset encoder counters to 0 (for quadrature encoders)
* @param address {uint8}
* @return {error}
*/
func (r *Roboclaw) ResetEncoders(address uint8) error {
return r.write_n(address, 20)
}
/*
* Read the roboclaw version
* @param address {uint8}
* @return {string, error}
*/
func (r *Roboclaw) ReadVersion(address uint8) (string, error) {
var (
version []uint8 = make([]uint8, 0)
crc crcType
ccrc uint16
buf [2]uint8
err error
)
loop:
for trys := r.retries; trys > 0; trys-- {
// Empty error (this should never be returned to user, but is employed in case of error in control flow logic)
// That way the only way nil is returned is if there is success
err = fmt.Errorf("Assert: This error should be replaced by other errors or nil")
if err = r.port.Flush(); err != nil {
continue
}
crc = crcType(0)
crc.update(address, 21)
if _, err = r.port.Write([]uint8{address, 21}); err != nil {
continue
}
// Reads up to 48 bytes
for i := 0; i < 48; i++ {
if _, err = io.ReadFull(r.port, buf[:1]); err == nil {
//Use append to ensure that version is no longer than it
//needs to be
version = append(version, buf[0])
crc.update(buf[0])
if buf[0] == 0 {
if _, err = io.ReadFull(r.port, buf[:]); err == nil {
ccrc = uint16(buf[0]) << 8
ccrc |= uint16(buf[1])
if ccrc == uint16(crc) {
return string(version), nil
} else {
err = fmt.Errorf("Mismatched checksum values")
continue loop
}
} else {
continue loop
}
}
} else {
continue loop
}
}
err = fmt.Errorf("No end of version delimiter received")
}
return "", err
}
/*
* Set encoder 1 counter to 0 (for quadrature encoders)
* @param address {uint8}
* @param val {int32}
* @return {error}
*/
func (r *Roboclaw) SetEncM1(address uint8, val int32) error {
return r.write_n(address, 22, setDWORDval(uint32(val))...)
}
/*
* Set encoder 2 counter to 0 (for quadrature encoders)
* @param address {uint8}
* @param val {int32}
* @return {error}
*/
func (r *Roboclaw) SetEncM2(address uint8, val int32) error {
return r.write_n(address, 23, setDWORDval(uint32(val))...)
}
/*
* Read the main battery voltage (connected to B+ and B- terminals)
* @param address {uint8}
* @return {uint16, error} voltage is returned in 10ths of a volt
*/
func (r *Roboclaw) ReadMainBatteryVoltage(address uint8) (uint16, error) {
return r.read2(address, 24)
}
/*
* Read the logic battery voltage (connected to LB+ and LB- terminals)
* @param address {uint8}
* @return {uint16, error} voltage is returned in 10ths of a volt
*/
func (r *Roboclaw) ReadLogicBatteryVoltage(address uint8) (uint16, error) {
return r.read2(address, 25)
}
/*
* Sets the minimum logic battery voltage
* Volts are (Desired volts - 6) * 5
* @param address {uint8}
* @param voltage {uint8} valid data from 0 - 140 (6V to 34V)
* @return {error}
*/
func (r *Roboclaw) SetMinVoltageLogicBattery(address uint8, voltage uint8) error {
if voltage > 140 {
return fmt.Errorf("Voltage above maximum of 140")
}
return r.write_n(address, 26, voltage)
}
/*
* Sets the maximum logic battery voltage
* Volts are Desired volts * 5.12
* @param address {uint8}
* @param voltage {uint8} valid data from 30 - 175 (6V to 34V)
* @return {error}
*/
func (r *Roboclaw) SetMaxVoltageLogicBattery(address uint8, voltage uint8) error {
if voltage > 175 {
return fmt.Errorf("Voltage above maximum of 175")
} else if voltage < 30 {
return fmt.Errorf("Speed below minimum of 30")
}
return r.write_n(address, 27, voltage)
}
/*
* Set velocity PID constant for motor 1
* @param address {uint8}
* @param kp_fp {float32} proportional constant. valid data from 0 to 65536
* @param ki_fp {float32} integral constant. valid data from 0 to 65536
* @param kd_fp {float32} derivative constant. valid data from 0 to 65536
* @param qpps {uint32} speed of encoder when motor is at 100 %
* @return {error}
*/
func (r *Roboclaw) SetM1VelocityPID(address uint8, kp_fp float32, ki_fp float32, kd_fp float32, qpps uint32) error {
// Although the arguments to the motor controller is a uint32, the arguments
// to the function are floats to match basic micro libraries
// 65536 is the maximum value because it is multiplied by 65536 in the function,
// the product of which is the maximum uint32
if kp_fp > 65536 || kp_fp < 0 {
return fmt.Errorf("Proportional contant outside of acceptable range (0 - 65536)")
} else if ki_fp > 65536 || kp_fp < 0 {
return fmt.Errorf("Integral contant outside of acceptable range (0 - 65536)")
} else if kd_fp > 65536 || kp_fp < 0 {
return fmt.Errorf("Derivative contant outside of acceptable range (0 - 65536)")
}
array := append(setDWORDval(uint32(kp_fp*65536)), setDWORDval(uint32(ki_fp*65536))...)
array = append(array, setDWORDval(uint32(kd_fp*65536))...)
array = append(array, setDWORDval(qpps)...)
return r.write_n(address, 28, array...)
}
/*
* Set velocity PID constant for motor 2
* @param address {uint8}
* @param kp_fp {float32} proportional constant. valid data from 0 to 65536
* @param ki_fp {float32} integral constant. valid data from 0 to 65536
* @param kd_fp {float32} derivative constant. valid data from 0 to 65536
* @param qpps {uint32} speed of encoder when motor is at 100 %
* @return {error}
*/
func (r *Roboclaw) SetM2VelocityPID(address uint8, kp_fp float32, ki_fp float32, kd_fp float32, qpps uint32) error {
// Although the arguments to the motor controller is a uint32, the arguments
// to the function are floats to match basic micro libraries
// 65536 is the maximum value because it is multiplied by 65536 in the function,
// the product of which is the maximum uint32
if kp_fp > 65536 || kp_fp < 0 {
return fmt.Errorf("Proportional contant outside of acceptable range (0 - 65536)")
} else if ki_fp > 65536 || kp_fp < 0 {
return fmt.Errorf("Integral contant outside of acceptable range (0 - 65536)")
} else if kd_fp > 65536 || kp_fp < 0 {
return fmt.Errorf("Derivative contant outside of acceptable range (0 - 65536)")
}
array := append(setDWORDval(uint32(kp_fp*65536)), setDWORDval(uint32(ki_fp*65536))...)
array = append(array, setDWORDval(uint32(kd_fp*65536))...)
array = append(array, setDWORDval(qpps)...)
return r.write_n(address, 29, array...)
}
/*
* Read raw speed for motor 1
* Pulses counted in last 300th of a second, but returned as encoder counts per second
* Direction is 0 for forward and 1 for backward
* @param address {uint8}
* @return {uint32, uint8, error} speed, direction, error
*/
func (r *Roboclaw) ReadISpeedM1(address uint8) (uint32, uint8, error) {
return r.read4_1(address, 30)
}
/*
* Read raw speed for motor 2
* Pulses counted in last 300th of a second, but returned as encoder counts per second
* Direction is 0 for forward and 1 for backward
* @param address {uint8}
* @return {uint32, uint8, error} speed, direction, error
*/
func (r *Roboclaw) ReadISpeedM2(address uint8) (uint32, uint8, error) {
return r.read4_1(address, 31)
}
/*
* Drive motor 1 in duty cycle mode
* Values represent +/- 100% duty
* @param address {uint8}
* @param duty {int16}
* @return {error}
*/
func (r *Roboclaw) DutyM1(address uint8, duty int16) error {
return r.write_n(address, 32, setWORDval(uint16(duty))...)
}
/*
* Drive motor 2 in duty cycle mode
* Values represent +/- 100% duty
* @param address {uint8}
* @param duty {int16}
* @return {error}
*/
func (r *Roboclaw) DutyM2(address uint8, duty int16) error {
return r.write_n(address, 33, setWORDval(uint16(duty))...)
}
/*
* Drive motor 1 and 2 in duty cycle mode
* Values represent +/- 100% duty
* @param address {uint8}
* @param duty1 {int16}
* @param duty2 {int16}
* @return {error}
*/
func (r *Roboclaw) DutyM1M2(address uint8, duty1 int16, duty2 int16) error {
return r.write_n(address, 34, append(setWORDval(uint16(duty1)), setWORDval(uint16(duty2))...)...)
}
/*
* Drive motor 1 at given speed. Sign indicates direction.
* @param address {uint8}
* @param speed {int32}
* @return {error}
*/
func (r *Roboclaw) SpeedM1(address uint8, speed int32) error {
return r.write_n(address, 35, setDWORDval(uint32(speed))...)
}
/*
* Drive motor 2 at given speed. Sign indicates direction.
* @param address {uint8}
* @param speed {int32}
* @return {error}
*/
func (r *Roboclaw) SpeedM2(address uint8, speed int32) error {
return r.write_n(address, 36, setDWORDval(uint32(speed))...)
}
/*
* Drive motors 1 and 2 at the given speeds. Sign indicates direction.
* @param address {uint8}
* @param speed1 {int32}
* @param speed2 {int32}
* @return {error}
*/
func (r *Roboclaw) SpeedM1M2(address uint8, speed1 int32, speed2 int32) error {
return r.write_n(address, 37, append(setDWORDval(uint32(speed1)), setDWORDval(uint32(speed2))...)...)
}
/*
* Drive motor 1 with speed and acceleration
* @param address {uint8}
* @param accel {uint32} acceleration is unsigned
* @param speed {int32}
* @return {error}
*/
func (r *Roboclaw) SpeedAccelM1(address uint8, accel uint32, speed int32) error {
return r.write_n(address, 38, append(setDWORDval(accel), setDWORDval(uint32(speed))...)...)
}
/*
* Drive motor 2 with speed and acceleration
* @param address {uint8}
* @param accel {uint32} acceleration is unsigned
* @param speed {int32}
* @return {error}
*/
func (r *Roboclaw) SpeedAccelM2(address uint8, accel uint32, speed int32) error {
return r.write_n(address, 39, append(setDWORDval(accel), setDWORDval(uint32(speed))...)...)
}
/*
* Drive motors 1 and 2 with speed and acceleration
* @param address {uint8}
* @param accel {uint32} acceleration is unsigned
* @param speed1 {int32}
* @param speed2 {int32}
* @return {error}
*/
func (r *Roboclaw) SpeedAccelM1M2(address uint8, accel uint32, speed1 int32, speed2 int32) error {
array := append(setDWORDval(accel), setDWORDval(uint32(speed1))...)
return r.write_n(address, 40, append(array, setDWORDval(uint32(speed2))...)...)
}
/*
* Drive motors 1 with speed to distance
* @param address {uint8}
* @param speed {int32}
* @param distance {uint32} distance is unsigned
* @param buffer {bool} true to override the previous command, false to buffer
* @return {error}
*/
func (r *Roboclaw) SpeedDistanceM1(address uint8, speed int32, distance uint32, buffer bool) error {
var flag uint8
if buffer {
flag = 1
} else {
flag = 0
}
array := append(setDWORDval(uint32(speed)), setDWORDval(distance)...)
return r.write_n(address, 41, append(array, flag)...)
}
/*
* Drive motors 2 with speed to distance
* @param address {uint8}
* @param speed {int32}
* @param distance {uint32} distance is unsigned
* @param buffer {bool} true to override the previous command, false to buffer
* @return {error}
*/
func (r *Roboclaw) SpeedDistanceM2(address uint8, speed int32, distance uint32, buffer bool) error {
var flag uint8
if buffer {
flag = 1
} else {
flag = 0
}
array := append(setDWORDval(uint32(speed)), setDWORDval(distance)...)
return r.write_n(address, 42, append(array, flag)...)
}
/*
* Drive motors 1 and 2 with speed to distance
* @param address {uint8}
* @param speed1 {int32}
* @param distance1 {uint32} distance is unsigned
* @param speed2 {int32}
* @param distance2 {uint32} distance is unsigned
* @param buffer {bool} true to override the previous command, false to buffer
* @return {error}
*/
func (r *Roboclaw) SpeedDistanceM1M2(address uint8, speed1 int32, distance1 uint32, speed2 int32, distance2 uint32, buffer bool) error {
var flag uint8
if buffer {
flag = 1
} else {
flag = 0
}
array := append(setDWORDval(uint32(speed1)), setDWORDval(distance1)...)
array = append(array, setDWORDval(uint32(speed2))...)
array = append(array, setDWORDval(distance2)...)
return r.write_n(address, 43, append(array, flag)...)
}
/*
* Drive motor 1 with acceleration and speed to distance
* @param address {uint8}
* @param accel {uint32} acceleration is unsigned
* @param speed {int32}
* @param distance {uint32} distance is unsigned
* @param buffer {bool} true to override the previous command, false to buffer
* @return {error}
*/
func (r *Roboclaw) SpeedAccelDistanceM1(address uint8, accel uint32, speed int32, distance uint32, buffer bool) error {
var flag uint8
if buffer {
flag = 1
} else {
flag = 0
}
array := append(setDWORDval(accel), setDWORDval(uint32(speed))...)
array = append(array, setDWORDval(distance)...)
return r.write_n(address, 44, append(array, flag)...)
}
/*
* Drive motor 2 with acceleration and speed to distance
* @param address {uint8}
* @param accel {uint32} acceleration is unsigned
* @param speed {int32}
* @param distance {uint32} distance is unsigned
* @param buffer {bool} true to override the previous command, false to buffer
* @return {error}
*/
func (r *Roboclaw) SpeedAccelDistanceM2(address uint8, accel uint32, speed int32, distance uint32, buffer bool) error {
var flag uint8
if buffer {
flag = 1
} else {
flag = 0
}
array := append(setDWORDval(accel), setDWORDval(uint32(speed))...)
array = append(array, setDWORDval(distance)...)
return r.write_n(address, 45, append(array, flag)...)
}
/*
* Drive motors 1 and 2 with acceleration and speed to distance
* @param address {uint8}
* @param accel {uint32} acceleration is unsigned
* @param speed1 {int32}
* @param distance1 {uint32} distance is unsigned
* @param speed2 {int32}
* @param distance2 {uint32} distance is unsigned
* @param buffer {bool} true to override the previous command, false to buffer
* @return {error}
*/
func (r *Roboclaw) SpeedAccelDistanceM1M2(address uint8, accel uint32, speed1 int32, distance1 uint32, speed2 int32, distance2 uint32, buffer bool) error {
var flag uint8
if buffer {
flag = 1
} else {
flag = 0
}
array := append(setDWORDval(accel), setDWORDval(uint32(speed1))...)
array = append(array, setDWORDval(distance1)...)
array = append(array, setDWORDval(uint32(speed2))...)
array = append(array, setDWORDval(distance2)...)
return r.write_n(address, 46, append(array, flag)...)
}
/*
* Read buffer lengths
* Maximum length is 64,
* A value of 128 means that the buffer is empty and all commands are finished
* A value of 0 means that the last command is executing
* @param address {uint8}
* @return {uint8, uint8, error} buffer 1 length, buffer 2 length, error
*/
func (r *Roboclaw) ReadBuffers(address uint8) (uint8, uint8, error) {
value, valid := r.read2(address, 47)
return uint8(value >> 8), uint8(value), valid
}
/*
* Read pwm values
* PWM values are +/- 32767
* Divide by 327.67 to get duty cycle percent
* @param address {uint8}
* @return {int16, int16, error} m1 pwm, m2 pwm, error
*/
func (r *Roboclaw) ReadPWMs(address uint8) (int16, int16, error) {
value, valid := r.read4(address, 48)
return int16(value >> 16), int16(value), valid
}
/*
* Read currents
* Values in 10 mA increments
* Divide by 100 to get A
* @param address {uint8}
* @return {int16, int16, error} m1 current, m2 current, error
*/
func (r *Roboclaw) ReadCurrents(address uint8) (int16, int16, error) {
value, valid := r.read4(address, 49)
return int16(value >> 16), int16(value), valid
}
/*
* Drive motors 1 and 2 with acceleration and speed
* @param address {uint8}
* @param accel1 {uint32} acceleration is unsigned
* @param speed1 {int32}
* @param accel2 {uint32} acceleration is unsigned
* @param speed2 {int32}
* @return {error}
*/
func (r *Roboclaw) SpeedAccelM1M2_2(address uint8, accel1 uint32, speed1 int32, accel2 uint32, speed2 int32) error {
array := append(setDWORDval(accel1), setDWORDval(uint32(speed1))...)
array = append(array, setDWORDval(accel2)...)
array = append(array, setDWORDval(uint32(speed2))...)
return r.write_n(address, 50, array...)
}
/*
* Drive motors 1 and 2 with acceleration and speed
* @param address {uint8}
* @param accel1 {uint32} acceleration is unsigned
* @param speed1 {int32}
* @param distance1 {uint32} distance is unsigned
* @param accel2 {uint32} acceleration is unsigned
* @param speed2 {int32}
* @param distance2 {uint32} distance is unsigned
* @param buffer {bool} true to override the previous command, false to buffer
* @return {error}
*/
func (r *Roboclaw) SpeedAccelDistanceM1M2_2(address uint8, accel1 uint32, speed1 int32, distance1 uint32, accel2 uint32, speed2 int32, distance2 uint32, buffer bool) error {
var flag uint8
if buffer {
flag = 1
} else {
flag = 0
}
array := append(setDWORDval(accel1), setDWORDval(uint32(speed1))...)
array = append(array, setDWORDval(distance1)...)
array = append(array, setDWORDval(accel2)...)
array = append(array, setDWORDval(uint32(speed2))...)
array = append(array, setDWORDval(distance2)...)
return r.write_n(address, 51, append(array, flag)...)
}
/*
* Drive motor 1 with duty and acceleration
* @param address {uint8}
* @param duty {int16}
* @param accel {uint32} the acceleration. Valid data from 0 to 655359 (-100% to 100% in 100ms)
* @return {error}
*/
func (r *Roboclaw) DutyAccelM1(address uint8, duty int16, accel uint32) error {
if accel > 655359 {
return fmt.Errorf("Acceleration above maximum value of 655359")
}
return r.write_n(address, 52, append(setWORDval(uint16(duty)), setDWORDval(accel)...)...)
}
/*
* Drive motor 2 with duty and acceleration
* @param address {uint8}
* @param duty {int16}
* @param accel {uint32} the acceleration. Valid data from 0 to 655359 (-100% to 100% in 100ms)
* @return {error}
*/
func (r *Roboclaw) DutyAccelM2(address uint8, duty int16, accel uint32) error {
if accel > 655359 {
return fmt.Errorf("Acceleration above maximum value of 655359")
}
return r.write_n(address, 53, append(setWORDval(uint16(duty)), setDWORDval(accel)...)...)
}
/*
* Drive motors 1 and 2 with duty and acceleration
* @param address {uint8}
* @param duty1 {int16}
* @param accel1 {uint32} the acceleration. Valid data from 0 to 655359 (-100% to 100% in 100ms)
* @param duty2 {int16}
* @param accel2 {uint32} the acceleration. Valid data from 0 to 655359 (-100% to 100% in 100ms)
* @return {error}
*/
func (r *Roboclaw) DutyAccelM1M2(address uint8, duty1 int16, accel1 uint32, duty2 int16, accel2 uint32) error {
if accel1 > 655359 {
return fmt.Errorf("Acceleration for m1 above maximum value of 655359")
}
if accel2 > 655359 {
return fmt.Errorf("Acceleration for m2 above maximum value of 655359")
}
array := append(setWORDval(uint16(duty1)), setDWORDval(accel1)...)
array = append(array, setWORDval(uint16(duty2))...)
array = append(array, setDWORDval(accel2)...)
return r.write_n(address, 54, array...)
}
/*
* Read motor 1 PID and QPPS
* @param address {uint8}
* @return {float32, float32, float32, uint32, error} proportional, integral, derivative, qpps, error
*/
func (r *Roboclaw) ReadM1VelocityPID(address uint8) (float32, float32, float32, uint32, error) {
var Kp, Ki, Kd, qpps uint32
err := r.read_n(address, 55, &Kp, &Ki, &Kd, &qpps)
return float32(Kp / 65536), float32(Ki / 65536), float32(Kd / 65536), qpps, err
}
/*
* Read motor 2 PID and QPPS
* @param address {uint8}
* @return {float32, float32, float32, uint32, error} proportional, integral, derivative, qpps, error
*/
func (r *Roboclaw) ReadM2VelocityPID(address uint8) (float32, float32, float32, uint32, error) {
var Kp, Ki, Kd, qpps uint32
err := r.read_n(address, 56, &Kp, &Ki, &Kd, &qpps)
return float32(Kp / 65536), float32(Ki / 65536), float32(Kd / 65536), qpps, err
}
/*
* Sets the minimum and maximum main battery voltage
* @param address {uint8}
* @param min {uint16} Values in 10th of a volt
* @param max {uint16} Values in 10th of a volt
* @return {error}
*/
func (r *Roboclaw) SetMainVoltages(address uint8, min uint16, max uint16) error {
return r.write_n(address, 57, append(setWORDval(min), setWORDval(max)...)...)
}
/*
* Sets the minimum and maximum logic battery voltage
* @param address {uint8}
* @param min {uint16} Values in 10th of a volt
* @param max {uint16} Values in 10th of a volt
* @return {error}
*/
func (r *Roboclaw) SetLogicVoltages(address uint8, min uint16, max uint16) error {
return r.write_n(address, 58, append(setWORDval(min), setWORDval(max)...)...)
}
/*
* Reads min and max main battery voltage
* Voltages are in mV
* @param address {uint8}
* @return {uint16, uint16, error} min, max, error
*/
func (r *Roboclaw) ReadMinMaxMainVoltages(address uint8) (uint16, uint16, error) {
value, valid := r.read4(address, 59)
return uint16(value >> 16), uint16(value), valid
}
/*
* Reads min and max logic battery voltage
* Voltages are in mV
* @param address {uint8}
* @return {uint16, uint16, error} min, max, error
*/
func (r *Roboclaw) ReadMinMaxLogicVoltages(address uint8) (uint16, uint16, error) {
value, valid := r.read4(address, 60)
return uint16(value >> 16), uint16(value), valid
}
/*
* Set position PID constants for motor 1
* @param address {uint8}
* @param kp_fp {float32} proportional constant. valid data from 0 to 4194304
* @param ki_fp {float32} integral constant. valid data from 0 to 4194304
* @param kd_fp {float32} derivative constant. valid data from 0 to 4194304
* @param kiMax {uint32} maximum integral windup
* @param deadzone {uint32} encoder counts deadzone
* @param min {uint32} minimum position
* @param max {uint32} maximum position
* @return {error}
*/
func (r *Roboclaw) SetM1PositionPID(address uint8, kp_fp float32, ki_fp float32, kd_fp float32, kiMax uint32, deadzone uint32, min uint32, max uint32) error {
// Although the arguments to the motor controller is a uint32, the arguments
// to the function are floats to match basic micro libraries
// 4194304 is the maximum value because it is multiplied by 1024 in the function,
// the product of which is the maximum uint32
if kp_fp > 4194304 || kp_fp < 0 {
return fmt.Errorf("Proportional contant outside of acceptable range (0 - 4194304)")
} else if ki_fp > 4194304 || kp_fp < 0 {
return fmt.Errorf("Integral contant outside of acceptable range (0 - 4194304)")
} else if kd_fp > 4194304 || kp_fp < 0 {
return fmt.Errorf("Derivative contant outside of acceptable range (0 - 4194304)")
}
array := append(setDWORDval(uint32(kp_fp*1024)), setDWORDval(uint32(ki_fp*1024))...)
array = append(array, setDWORDval(uint32(kd_fp*1024))...)
array = append(array, setDWORDval(kiMax)...)
array = append(array, setDWORDval(deadzone)...)
array = append(array, setDWORDval(min)...)
array = append(array, setDWORDval(max)...)
return r.write_n(address, 61, array...)
}
/*
* Set position PID constants for motor 2
* @param address {uint8}
* @param kp_fp {float32} proportional constant. valid data from 0 to 4194304
* @param ki_fp {float32} integral constant. valid data from 0 to 4194304
* @param kd_fp {float32} derivative constant. valid data from 0 to 4194304
* @param kiMax {uint32} maximum integral windup
* @param deadzone {uint32} encoder counts deadzone
* @param min {uint32} minimum position
* @param max {uint32} maximum position
* @return {error}
*/
func (r *Roboclaw) SetM2PositionPID(address uint8, kp_fp float32, ki_fp float32, kd_fp float32, kiMax uint32, deadzone uint32, min uint32, max uint32) error {
// Although the arguments to the motor controller is a uint32, the arguments
// to the function are floats to match basic micro libraries
// 4194304 is the maximum value because it is multiplied by 1024 in the function,
// the product of which is the maximum uint32
if kp_fp > 4194304 || kp_fp < 0 {
return fmt.Errorf("Proportional contant outside of acceptable range (0 - 4194304)")
} else if ki_fp > 4194304 || kp_fp < 0 {
return fmt.Errorf("Integral contant outside of acceptable range (0 - 4194304)")
} else if kd_fp > 4194304 || kp_fp < 0 {
return fmt.Errorf("Derivative contant outside of acceptable range (0 - 4194304)")
}
array := append(setDWORDval(uint32(kp_fp*1024)), setDWORDval(uint32(ki_fp*1024))...)
array = append(array, setDWORDval(uint32(kd_fp*1024))...)
array = append(array, setDWORDval(kiMax)...)
array = append(array, setDWORDval(deadzone)...)