-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboClawFS.h
2424 lines (2079 loc) · 93.4 KB
/
RoboClawFS.h
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
/*****************************************************************************
******************************* RoboClawFS.h ******************************
*****************************************************************************/
/*
Interface for the IonMC Robo Claw Motor Controllers using the
Packet Serial Communication Mode.
This driver is intended to work on any platform where the USB port can be
accessed via standard file system calls [open(), read(), write()].
Initial testing was done on Windows 8.1 with IonMC's driver installed which
makes the link to the RoboClaw show up as one of the COMx ports.
The Boost endian library is used to automatically deal with the RoboClaw
using big endian integers and whatever the host processor happens to use.
Little endian on Intel and ARM.
One of the features is that the order of the function parameters is
more consistent and regular (in terms of how at least I think of them).
For example, I think speed then acceleration instead of acceleration and then
speed. The PID constant functions aren't consistent in the order they're called
out so that has been made consistent using PID as the natural order.
There are two forms to the motor control functions.
The first specifies M1 or M2 in the function name and uses internally the ECommandCode enumerations.
The second form allows programmatically selection the motor by specifying the motor number as a parameter.
WARNING: Users should specify using the provided constants M1 and M2 as these are valued 0 and 1 (NOT 1 and 2) for
efficiency. This will also make YOUR code more readable.
Author: Tim Craig (Druai Robotics) 2017
*/
#pragma once
#if !defined(__ROBOCLAWFS_H__)
#define __ROBOCLAWFS_H__
/*****************************************************************************
****************************** I N C L U D E *******************************
*****************************************************************************/
#include <cstdint>
#include <string>
//#include <stdio.h>
#include <io.h>
#include <iostream>
#include <fcntl.h>
#include <boost/endian/conversion.hpp>
/*****************************************************************************
**************************** class DRoboClawFS ****************************
*****************************************************************************/
// Uncomment the following line to debug the RoboClaw command responses
//#define DEBUG_RESPONSE
// Comment out the following line to prevent string name functions from being
// implemented. Save space on systems with limited memory.
#define IMPLEMENT_STRING_FUNTIONS
class DRoboClawFS
{
public :
// As of when this was written in 2017, RoboClaws return 16 bit CRC!!
using CRC16 = uint16_t;
// Motor constants for those who prefer to specify by motor number
// This makes it easy to programmatically switch motors
// No checking will be done in the version of functions taking this form
// so users going off the reservation will pay the price!!
static constexpr uint8_t M1 = 0;
static constexpr uint8_t M2 = 1;
// Direction values used by several commands
enum EDirection : uint8_t { eFwd, eRev };
// Get a readable string for the direction
static std::string GetDirectionString(EDirection eDirection) noexcept;
// Current Unit Status Bit Flags
// Returned by Read Status (Command 90)
enum class EStatus : uint16_t
{
eNormal = 0x0000,
eM1OverCurrentWarning = 0x0001,
eM2OverCurrentWarning = 0x0002,
eEStop = 0x0004,
eTempError = 0x0008,
eTemp2Error = 0x0010,
eMainBatteryHighError = 0x0020,
eLogicBatteryHighError = 0x0040,
eLogicBatteryLowError = 0x0080,
eM1DriveFault = 0x0100,
eM2DriveFault = 0x0200,
eMainBatteryHighWarning = 0x0400,
eMainBatteryLowWarning = 0x0800,
eTempWarning = 0x1000,
eTemp2Warning = 0x2000,
eM1Home = 0x4000,
eM2Home = 0x8000,
};
// Return a readable string for the status
static std::string GetStatusString(EStatus eStatus) noexcept;
std::string GetStatusString() const noexcept
{
EStatus eStatus;
std::string strStatus;
bool bRet = ReadStatus(eStatus);
if (bRet)
{
strStatus = GetStatusString(eStatus);
} // end if
return(strStatus);
}
// Commands 98 & 99
enum EStdConfigSettings
{
eRCMode = 0x0000,
eAnalog = 0x0001,
eSimpleSerial = 0x0002,
ePacketSerial = 0x0003,
eBatteryModeOff = 0x0000,
eBatteryModeAuto = 0x0004,
eBatteryMode2Cell = 0x0008,
eBatteryMode3Cell = 0x000C,
eBatteryMode4Cell = 0x0010,
eBatteryMode5Cell = 0x0014,
eBatteryMode6Cell = 0x0018,
eBatteryMode7Cell = 0x001C,
eMixing = 0x0020,
eExponential = 0x0040,
eMCU = 0x0080,
eBaudRate2400 = 0x0000,
eBaudRate9600 = 0x0002,
eBaudRate19200 = 0x0004,
eBaudRate38400 = 0x0006,
eBaudRate57600 = 0x000,
eBaudRate115200 = 0x00A0,
eBaudRate230400 = 0x00C0,
eBaudRate460800 = 0x00E0,
eFlipSwitch = 0x0100,
ePackAddr0x80 = 0x0000,
ePackAddr0x81 = 0x0100,
ePackAddr0x82 = 0x0200,
ePackAddr0x83 = 0x0300,
ePackAddr0x84 = 0x0400,
ePackAddr0x85 = 0x0500,
ePackAddr0x86 = 0x0600,
ePackAddr0x87 = 0x0700,
eSlaveMode = 0x0800,
eRelayMode = 0x1000,
eSwapEncoders = 0x2000,
eSwapButtons = 0x4000,
eMultiUnitMode = 0x8000,
};
// CTRL1 and CTRL2 output pins (Commands 100 & 101)
enum ECTRLMode : uint8_t
{
eDisable, eUser, eVoltageClamp, eBrake
};
// PWM Mode (Commands 148 & 149)
enum EPWMMode : uint8_t { eLockedAntiphase, eSignMagnitude };
// Return a readable string for the PWM Mode
static std::string GetPWMModeString(EPWMMode ePWMMode) noexcept;
// Used with Commands 91, 92, & 93. Strongly types the Encoder Modes
class EncoderMode
{
public:
enum EType { eQuadrature = 0, eAbsolute = 1 };
bool m_bRC_AnalogEnabed;
EType m_eType;
EncoderMode(bool bRC_AEnabled = false, EType eType = eQuadrature) noexcept
: m_bRC_AnalogEnabed(bRC_AEnabled), m_eType(eType)
{
return;
}
~EncoderMode() = default;
void Decode(uint8_t uMode) noexcept
{
m_bRC_AnalogEnabed = ((uMode & eEnableRC_AnalogEncoder) != 0);
m_eType = ((uMode & eType) != 0) ? eAbsolute : eQuadrature;
return;
}
uint8_t Encode() const noexcept
{
uint8_t uMode = m_bRC_AnalogEnabed ? eEnableRC_AnalogEncoder : 0;
uMode |= m_eType;
return (uMode);
}
protected:
enum EEncoderModeBitsMask : uint8_t
{
eEnableRC_AnalogEncoder = 0x80,
eType = 0x01, // Quadrature(0)/Absolute(1)
};
};
enum class ES3Mode : uint8_t
{
eDefault = 0, eEStopLatching = 1, eEStop = 2, eVoltageClamp = 3,
};
// Get a readable string for the S3 Mode
static std::string GetS3ModeString(ES3Mode eMode) noexcept;
enum class ES4Mode : uint8_t
{
eDisabled = 0, eEStopLatching = 1, eEStop = 2, eVoltageClamp = 3, eM1Home = 4,
};
// Get a readable string for the S4 Mode
static std::string GetS4ModeString(ES4Mode eMode) noexcept;
enum class ES5Mode : uint8_t
{
eDisabled = 0, eEStopLatching = 1, eEStop = 2, eVoltageClamp = 3, eM2Home = 4,
};
// Get a readable string for the S5 Mode
static std::string GetS5ModeString(ES5Mode eMode) noexcept;
// Encapsulate the encoder status
class EncoderStatus
{
public:
bool m_bUnderflow;
bool m_bOverflow;
EDirection m_eDirection;
EncoderStatus() noexcept
{
m_bUnderflow = false;
m_bOverflow = false;
m_eDirection = EDirection::eFwd;
return;
}
void DecodeByte(uint8_t uStatus) noexcept
{
m_bUnderflow = ((uStatus & eEncoderUnderflow) != 0);
m_bOverflow = ((uStatus & eEncoderOverflow) != 0);
m_eDirection = ((uStatus & eEncoderDirection)) ? EDirection::eRev : EDirection::eFwd;
return;
}
};
enum EEncoderStatusMask : uint8_t
{
eEncoderUnderflow = 0x01,
eEncoderDirection = 0x02,
eEncoderOverflow = 0x04
};
DRoboClawFS(const std::string strTTYDevice = "/dev/roboclaw", uint8_t uAddr = 0x80,
uint32_t uReadTimeout = 100) noexcept;
DRoboClawFS(const DRoboClawFS& src) = delete;
DRoboClawFS(const DRoboClawFS&& src) = delete;
virtual ~DRoboClawFS();
DRoboClawFS& operator=(const DRoboClawFS& rhs) = delete;
DRoboClawFS& operator=(const DRoboClawFS&& rhs) = delete;
// RoboClaw address handling
void SetAddr(uint8_t nAddr) noexcept
{
m_nAddr = nAddr;
return;
}
uint8_t GetAddr() const noexcept
{
return (m_nAddr);
}
// Device handling
void SetDevice(std::string strTTYDevice) noexcept
{
m_strTTYDevice = strTTYDevice;
return;
}
std::string GetDevice() const noexcept
{
return (m_strTTYDevice);
}
// Read timeout
uint32_t GetReadTimeout() const noexcept
{
return (m_uReadTimeout);
}
void SetReadTimeout(uint32_t uTimeout) noexcept
{
m_uReadTimeout = uTimeout;
return;
}
// Port handling
bool OpenPort() noexcept;
void ClosePort() noexcept
{
if (m_nPort != -1)
{
close(m_nPort);
m_nPort = -1;
} // end if
return;
}
bool IsPortOpen() const noexcept
{
return (m_nPort >= 0);
}
// Flush the receive buffer
void FlushBuffer() noexcept
{
uint8_t uByte;
while (read(m_nPort, &uByte, 1) == 1);
return;
}
/***********************************************************************
************************** RoboClaw Commands ***************************
***********************************************************************/
/***********************************************************************
********************* Compatibility Mode Commands **********************
***********************************************************************/
//** Cmd 0 (0 <= nSpeed <= 127)
// Verified TTC
bool M1Forward(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eM1Forward, nSpeed));
}
//** Cmd 1 (0 <= nSpeed <= 127)
bool M1Backward(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eM1Backward, nSpeed));
}
//** Cmd 2 Sets the minimum allowable main battery voltage
// 0 <= nV <= 120 (6V-30V) nV = 5.0 * (Volts - 6.0)
//!! Deprecated. Used Command 57
bool SetMinMainVoltage(uint8_t nVolts) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eSetMinMainVolt, nVolts));
}
// Convenience function taking actual voltage as a float (6V-30V)
bool SetMinMainVoltage(float fVolts) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eSetMaxMainVolt,
static_cast<uint8_t>((fVolts - 6.0f) * 5.0f)));
}
//** Cmd 3 Sets the maximum allowable main battery voltage
// 0 <= nV <= 154 (0V-30V) nV = Volts * 5.12
//!! Deprecated. Used Command 57
bool SetMaxMainVoltage(uint8_t nV) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eSetMaxMainVolt, nV));
}
// Convenience function taking actual voltage as a float (0V-30V)
bool SetMaxMainVoltage(float fVolts) const noexcept
{
return (SetMaxMainVoltage(static_cast<uint8_t>(fVolts * 5.12f)));
}
//** Cmd 4 (0 <= nSpeed <= 127)
bool M2Forward(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eM2Forward, nSpeed));
}
//** Cmd 5 (0 <= nSpeed <= 127)
bool M2Backward(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eM2Backward, nSpeed));
}
//** Cmd 6 (0 [Full Reverse] <= nSpeed <= 127 [Full Forward])
bool M1Drive7Bit(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eM1Drive7Bit, nSpeed));
}
//** Cmd 7 (0 [Full Reverse] <= nSpeed <= 127 [Full Forward])
bool M2Drive7Bit(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eM2Drive7Bit, nSpeed));
}
//** Cmd 6 & Cmd 7 (0 [Full Reverse] <= nSpeed <= 127 [Full Forward])
bool MotorDrive7Bit(uint8_t uMotor, uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(MotorCmd(ECommandCode::eM1Drive7Bit, uMotor), nSpeed));
}
/***********************************************************************
****************** Mixed Mode Compatibility Commands *******************
***********************************************************************/
/* The following commands are mix mode compatibility commands used to
* control speed and turn using differential steering. Before a command
* is executed valid drive and turn data is required. You only need to
* send both data packets once. After receiving both valid drive and turn
* data RoboClaw will begin to operate the motors. At this point you only
* need to update turn or drive data as needed.
*/
//** Cmd 8 (0 <= nSpeed <= 127)
bool DriveFwd(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eMixedDriveFwd, nSpeed));
}
//** Cmd 9 (0 <= nSpeed <= 127)
bool DriveBck(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eMixedDriveBck, nSpeed));
}
//** Cmd 10 (0 <= nSpeed <= 127)
bool TurnRight(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eMixedTurnRight, nSpeed));
}
//** Cmd 11 (0 <= nSpeed <= 127)
bool TurnLeft(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eMixedTurnLeft, nSpeed));
}
//**Cmd 12 (0 [Full Backward] <= nSpeed <= 127 [Full Forward])
bool DriveMixed7Bit(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eMixedDrive7Bit, nSpeed));
}
//**Cmd 13 (0 [Full Backward] <= nSpeed <= 127 [Full Forward])
bool TurnMixed7Bit(uint8_t nSpeed) const noexcept
{
return (SendCompatibilityCmd(ECommandCode::eMixedTurn7Bit, nSpeed));
}
/***********************************************************************
*************************** Encoder Commands ***************************
***********************************************************************/
//** Cmd 16 Read Encoder 1
bool ReadEncoder1(uint32_t& uCount, EncoderStatus& Status) const noexcept
{
return (ReadEncoder(ECommandCode::eReadEncoder1, uCount, Status));
}
//** Cmd 17 Read Encoder 2
bool ReadEncoder2(uint32_t& uCount, EncoderStatus& Status) const noexcept
{
return (ReadEncoder(ECommandCode::eReadEncoder2, uCount, Status));
}
//** Cmd 16 & Cmd 17 Read Encoder
bool ReadMotorEncoder(uint8_t uMotor, uint32_t& uCount, EncoderStatus& Status) const noexcept
{
return (ReadEncoder(MotorCmd(ECommandCode::eReadEncoder1, uMotor), uCount, Status));
}
//** Cmd 16 Read Encoder 1
bool ReadEncoder1(int32_t& nCount, EncoderStatus& Status) const noexcept
{
return (ReadEncoder(ECommandCode::eReadEncoder1, nCount, Status));
}
//** Cmd 17 Read Encoder 2
bool ReadEncoder2(int32_t& nCount, EncoderStatus& Status) const noexcept
{
return (ReadEncoder(ECommandCode::eReadEncoder2, nCount, Status));
}
//** Cmd 16 & Cmd 17 Read Encoder
bool ReadMotorEncoder(uint8_t uMotor, int32_t& nCount, EncoderStatus& Status) const noexcept
{
return (ReadEncoder(MotorCmd(ECommandCode::eReadEncoder1, uMotor), nCount, Status));
}
//** Cmd 18 Read Encoder 1 Speed
// Value is in counts/second.
//Manual says filtered so I assume that means a running average
// The direction parameter seemed to imply the speed is unsigned. In
// practice it is NOT. Need to treat it as signed. Then why direction?
// Verified TTC
bool ReadEncoder1Speed(int32_t& nSpeed, EDirection& eDirection) const noexcept
{
return (ReadEncoderSpeed(ECommandCode::eReadEncoder1Speed, nSpeed, eDirection));
}
// Since Speed is signed, no real need for direction
bool ReadEncoder1Speed(int32_t& nSpeed) const noexcept
{
EDirection eDirection;
return (ReadEncoder1Speed(nSpeed, eDirection));
}
//** Cmd 19 Read Encoder 2 Speed
// Value is in counts/second.
//Manual says filtered so I assume that means a running average
bool ReadEncoder2Speed(int32_t& nSpeed, EDirection& eDirection) const noexcept
{
return (ReadEncoderSpeed(ECommandCode::eReadEncoder2Speed, nSpeed, eDirection));
}
// Since Speed is signed, no real need for direction
bool ReadEncoder2Speed(int32_t& nSpeed) const noexcept
{
EDirection eDirection;
return (ReadEncoder2Speed(nSpeed, eDirection));
}
//** Cmd 18 & Cmd 19
bool ReadMotorEncoderSpeed(uint8_t uMotor, int32_t& nSpeed, EDirection& eDirection) const noexcept
{
return (ReadEncoderSpeed(MotorCmd(ECommandCode::eReadEncoder1Speed, uMotor), nSpeed, eDirection));
}
// Since Speed is signed, no real need for direction
bool ReadMotorEncoderSpeed(uint8_t uMotor, int32_t& nSpeed) const noexcept
{
EDirection eDirection;
return (ReadMotorEncoderSpeed(uMotor, nSpeed, eDirection));
}
//** Cmd 20 Reset the Encoder Counters
// Verified TTC
bool ResetEncoders() const noexcept
{
return (SendHeaderCRC0xFF(ECommandCode::eResetEncoders));
}
//** Cmd 22 Set Encoder 1 Value
// Verified TTC
bool SetEncoder1(uint32_t uCounts) const noexcept
{
return (SetEncoder(ECommandCode::eSetEncoder1, uCounts));
}
bool SetEncoder1(int32_t nCounts) const noexcept
{
return (SetEncoder(ECommandCode::eSetEncoder1, nCounts));
}
//** Cmd 23 Set Encoder 2 Value
// Verified TTC
bool SetEncoder2(uint32_t uCounts) const noexcept
{
return (SetEncoder(ECommandCode::eSetEncoder2, uCounts));
}
bool SetEncoder2(int32_t nCounts) const noexcept
{
return (SetEncoder(ECommandCode::eSetEncoder2, nCounts));
}
//** Cmd 22 & Cmd 23 Set Encoder Value
// Verified TTC
bool SetMotorEncoder(uint8_t uMotor, uint32_t uCounts) const noexcept
{
return (SetEncoder(MotorCmd(ECommandCode::eSetEncoder1, uMotor), uCounts));
}
bool SetMotorEncoder(uint8_t uMotor, int32_t nCounts) const noexcept
{
return (SetEncoder(MotorCmd(ECommandCode::eSetEncoder1, uMotor), nCounts));
}
// Convenience functions to reset individual Encoders
bool ResetEncoder1() const noexcept
{
return (SetEncoder1(0));
}
bool ResetEncoder2() const noexcept
{
return (SetEncoder2(0));
}
bool ResetMotorEncoder(uint8_t uMotor) const noexcept
{
return (SetMotorEncoder(uMotor, 0));
}
//** Cmd 30 Read Encoder 1 Raw Speed
// Encoder counts per second based on the last 1/300th second
// The direction parameter seemed to imply the speed is unsigned. In
// practice it is NOT. Need to treat it as signed. Then why direction?
// Verified TTC
bool ReadEncoder1RawSpeed(int32_t& nSpeed, EDirection& eDirection) const noexcept
{
return (ReadEncoderRawSpeed(ECommandCode::eReadEncoder1RawSpeed, nSpeed, eDirection));
}
// Since Speed is signed, no real need for direction
bool ReadEncoder1RawSpeed(int32_t& nSpeed) const noexcept
{
EDirection eDirection;
return (ReadEncoder1RawSpeed(nSpeed, eDirection));
}
//** Cmd 31 Read Encoder 2 Raw Speed
// Encoder counts per second based on the last 1/300th second
// The direction parameter seemed to imply the speed is unsigned. In
// practice it is NOT. Need to treat it as signed. Then why direction?
bool ReadEncoder2RawSpeed(int32_t& nSpeed, EDirection& eDirection) const noexcept
{
return (ReadEncoderRawSpeed(ECommandCode::eReadEncoder2RawSpeed, nSpeed, eDirection));
}
// Since Speed is signed, no real need for direction
bool ReadEncoder2RawSpeed(int32_t& nSpeed) const noexcept
{
EDirection eDirection;
return (ReadEncoder2RawSpeed(nSpeed, eDirection));
}
//** Cmd 30 & Cmd 31 Read Encoder Raw Speed
// Encoder counts per second based on the last 1/300th second
// The direction parameter seemed to imply the speed is unsigned. In
// practice it is NOT. Need to treat it as signed. Then why direction?
// Verified TTC
bool ReadMotorEncoderRawSpeed(uint8_t uMotor, int32_t& nSpeed, EDirection& eDirection) const noexcept
{
return (ReadEncoderRawSpeed(MotorCmd(ECommandCode::eReadEncoder1RawSpeed, uMotor), nSpeed, eDirection));
}
// Since Speed is signed, no real need for direction
bool ReadMotorEncoderRawSpeed(uint8_t uMotor, int32_t& nSpeed) const noexcept
{
EDirection eDirection;
return (ReadMotorEncoderRawSpeed(uMotor, nSpeed, eDirection));
}
//** Cmd 78 Read Both Encoders
// Verified TTC
bool ReadEncodersCounts(uint32_t& uCounts1, uint32_t& uCounts2) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadEncodersCounts, uCounts1, uCounts2));
}
// Read Both Encoders with counts as signed values
// Good for controlling position where it's centered around zero.
bool ReadEncodersCounts(int32_t& nCounts1, int32_t& nCounts2) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadEncodersCounts, nCounts1, nCounts2));
}
//** Cmd 79 Read Both Encoder Speeds (Counts/Second based on last 1/300th second)
bool ReadEncodersSpeeds(int32_t& nSpeed1, int32_t& nSpeed2) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadEncodersSpeeds, nSpeed1, nSpeed2));
}
/***********************************************************************
************************ Advanced Packet Serial ************************
***********************************************************************/
//** Cmd 21 Read the firmware version (Max 48 characters)
// Verified TTC
bool ReadFirmwareVersion(std::string& strVersion) const noexcept;
//** Cmd 24 Read the main battery voltage in tenths of a volt
// Verified TTC
bool ReadMainBatteryVoltage(uint16_t& uVolts) const noexcept
{
return (Read1IntegralTypeCRC(ECommandCode::eReadMainBatteryVoltage, uVolts));
}
// Get the main battery voltage in volts
bool ReadMainBatteryVoltage(float& fVolts) const noexcept
{
uint16_t uVolts;
bool bRet = ReadMainBatteryVoltage(uVolts);
if (bRet)
{
fVolts = uVolts * 0.1f;
} // end if
return (bRet);
}
//** Cmd 25 Read the logic battery voltage in tenths of a volt
// Verified TTC
bool ReadLogicBatteryVoltage(uint16_t& uVolts) const noexcept
{
return (Read1IntegralTypeCRC(ECommandCode::eReadLogicLogicBatteryVoltage, uVolts));
}
// Get the logic battery voltage in volts
bool ReadLogicBatteryVoltage(float& fVolts) const noexcept
{
uint16_t uVolts;
bool bRet = ReadLogicBatteryVoltage(uVolts);
if (bRet)
{
fVolts = uVolts * 0.1f;
} // end if
return (bRet);
}
//** Cmd 26 Set the Minimum Logic Battery Voltage
// Valid range 0-140 in 0.2V increments. [0=6V, 140 = 34V]
// Deprecated!! Use Cmd 58
bool SetMinLogicBatteryVoltage(uint8_t uVolts) const noexcept
{
return (SendHeaderByteCRC0xFF(ECommandCode::eSetMinLogicVolt, uVolts));
}
// Set Minimum Logic Batter Voltage in volts (6.0V-34.0V)
bool SetMinLogicBatteryVoltage(float fVolts) const noexcept
{
return (SetMinLogicBatteryVoltage(static_cast<uint8_t>((fVolts - 6.0f) * 5.0f)));
}
//** Cmd 27 Set the Maximum Logic Battery Voltage
// Valid range 30-175. Value = 5.12 * Voltage.
// Deprecated!! Use Cmd 58
bool SetMaxLogicBatteryVoltage(uint8_t uVolts) const noexcept
{
return (SendHeaderByteCRC0xFF(ECommandCode::eSetMinLogicVolt, uVolts));
}
// Set Maximum Logic Batter Voltage in volts (6.0V-34.0V)
bool SetMaxLogicBatteryVoltage(float fVolts) const noexcept
{
return (SetMaxLogicBatteryVoltage(static_cast<uint8_t>(fVolts * 5.12f)));
}
//** Cmd 48 Read Both Motor PWM Values. Values +-32,767.
// Percent duty cycle divide by 327.67
// Verified TTC
bool ReadMotorPWMValues(int16_t& nPWM1, int16_t& nPWM2) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadMotorPWMs, nPWM1, nPWM2));
}
// Read Motor PWM Values in percent
bool ReadMotorPWMValues(float& fPWM1, float& fPWM2) const noexcept
{
int16_t nPWM1, nPWM2;
bool bRet = ReadMotorPWMValues(nPWM1, nPWM2);
if (bRet)
{
fPWM1 = nPWM1 / 327.67f;
fPWM2 = nPWM2 / 327.67f;
} // end if
return (bRet);
}
//** Cmd 49 Read Both Motor Currents
// Values are in 10ma increments (divide by 100 get get amps)
// Verified TTC
bool ReadMotorCurrents(uint16_t& uCur1, uint16_t& uCur2) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadMotorCurrents, uCur1, uCur2));
}
// Read the motor currents in Amps.
bool ReadMotorCurrents(float& fCur1, float& fCur2) const noexcept
{
uint16_t uCur1, uCur2;
bool bRet = ReadMotorCurrents(uCur1, uCur2);
if (bRet)
{
fCur1 = uCur1 / 100.0f;
fCur2 = uCur2 / 100.0f;
} // end if
return (bRet);
}
//** Cmd 57 Set Main Battery Voltages
// Set the Min and Max Main Battery Voltages in 10ths of a volt
// Verified TTC
bool SetMainBatteryVoltageMinMax(uint16_t uMin, uint16_t uMax) const noexcept
{
return (Send2IntegralTypes0xFF(ECommandCode::eSetMainBatteryVoltages, uMin, uMax));
}
// Set the Main Batter Voltages in Volts
bool SetMainBatteryVoltageMinMax(float fMin, float fMax) const noexcept
{
return (SetMainBatteryVoltageMinMax(static_cast<uint16_t>(fMin * 10.0f),
static_cast<uint16_t>(fMax * 10.0f)));
}
//** Cmd 58 Set Logic Battery Voltages
// Set the Min and Max Logic Battery Voltages in 10ths of a volt
// Verified TTC
bool SetLogicBatteryVoltageMinMax(uint16_t uMin, uint16_t uMax) const noexcept
{
return (Send2IntegralTypes0xFF(ECommandCode::eSetLogicBatteryVoltages, uMin, uMax));
}
// Set the Logic Batter Voltages in Volts
bool SetLogicBatteryVoltageMinMax(float fMin, float fMax) const noexcept
{
return (SetLogicBatteryVoltageMinMax(static_cast<uint16_t>(fMin * 10.0f),
static_cast<uint16_t>(fMax * 10.0f)));
}
//** Cmd 59 Read Main Battery Voltage Settings
// Read the Min and Max Main Battery Voltage Settings in 10th of a volt
// Verified TTC
bool ReadMainBatteryVoltageMinMax(uint16_t& uMin, uint16_t& uMax) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadMainBatteryVoltageSettings, uMin, uMax));
}
// Read the Min and Max Main Battery Voltage Settings in Volts
bool ReadMainBatteryVoltageMinMax(float& fMin, float& fMax) const noexcept
{
uint16_t uMin, uMax;
bool bRet = ReadMainBatteryVoltageMinMax(uMin, uMax);
if (bRet)
{
fMin = uMin / 10.0f;
fMax = uMax / 10.0f;
} // end if
return (bRet);
}
//** Cmd 60 Read Logic Battery Voltage Settings
// Read the Min and Max Main Battery Voltage Settings in 10th of a volt
// Verified TTC
bool ReadLogicBatteryVoltageMinMax(uint16_t& uMin, uint16_t& uMax) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadLogicBatterVoltageSettings, uMin, uMax));
}
// Read the Min and Max Logic Battery Voltage Settings in Volts
bool ReadLogicBatteryVoltageMinMax(float& fMin, float& fMax) const noexcept
{
uint16_t uMin, uMax;
bool bRet = ReadLogicBatteryVoltageMinMax(uMin, uMax);
if (bRet)
{
fMin = uMin / 10.0f;
fMax = uMax / 10.0f;
} // end if
return (bRet);
}
// Cmd 68 Set M1 Default Duty Acceleration
// Set default Acceleration for M1 when using duty cycle commands
// (Cmds 32, 33, & 34) or Standard Serial, RC and Analog PWM modes.
// Verified TTC
bool SetM1DefaultDutyAccel(uint32_t uAccel) const noexcept
{
return (Send1IntegralType0xFF(ECommandCode::eSetM1DefaultDutyCycleAccel, uAccel));
}
// Cmd 69 Set M2 Default Duty Acceleration
// Set default Acceleration for M2 when using duty cycle commands
// (Cmds 32, 33, & 34) or Standard Serial, RC and Analog PWM modes.
// Verified TTC
bool SetM2DefaultDutyAccel(uint32_t uAccel) const noexcept
{
return (Send1IntegralType0xFF(ECommandCode::eSetM2DefaultDutyCycleAccel, uAccel));
}
//** Cmd 74 Set S3, S4, S5 Modes
bool SetS3_S4_S5Modes(ES3Mode eS3, ES4Mode eS4, ES5Mode eS5) const noexcept
{
return (Send3IntegralTypes0xFF(ECommandCode::eSetS3_S4_S5Modes,
static_cast<uint8_t>(eS3), static_cast<uint8_t>(eS4),
static_cast<uint8_t>(eS5)));
}
//** Cmd 75 Get S3, S4, S5 Modes
bool GetS3_S4_S5Modes(ES3Mode& eS3, ES4Mode& eS4, ES5Mode& eS5) const noexcept
{
uint8_t uS3, uS4, uS5;
bool bRet = Read3IntegralTypesCRC(ECommandCode::eSetS3_S4_S5Modes, uS3, uS4, uS5);
if (bRet)
{
eS3 = static_cast<ES3Mode>(uS3);
eS4 = static_cast<ES4Mode>(uS4);
eS5 = static_cast<ES5Mode>(uS5);
} // end if
return (bRet);
}
//** Cmd 80 Set Factory Defaults
bool RestoreFactoryDefaults() const noexcept
{
return (SendSimpleCommand(ECommandCode::eRestoreDefaults));
}
//** Cmd 81 Read Default Duty Acceleration Settings Both Motors
// Verified TTC
bool ReadDefaultDutyAccels(uint32_t& uAccel1, uint32_t& uAccel2) const noexcept
{
return (Read2IntegralTypesCRC(ECommandCode::eReadDefaultDutyCycleAccels, uAccel1, uAccel2));
}
//** Cmd 82 Read Temperature (10ths of a degree (C or F?)
// Verified TTC
bool ReadTemperature(uint16_t& uTemp) const noexcept
{
return (Read1IntegralTypeCRC(ECommandCode::eReadTemperature, uTemp));
}
// Read Temperature in Degrees
bool ReadTemperature(float& fTemp) const noexcept
{
uint16_t uTemp;
bool bRet = ReadTemperature(uTemp);
if (bRet)
{
fTemp = uTemp / 10.0f;
} // end if
return (bRet);
}
//** Cmd 83 Read Temperature 2 (10ths of a degree (C or F?)
// Available on supported units only
// Verified TTC (Not supported on my test unit. Returned 0)
bool ReadTemperature2(uint16_t& uTemp) const noexcept
{
return (Read1IntegralTypeCRC(ECommandCode::eReadTemperature2, uTemp));
}
// Read Temperature 2 in Degrees
bool ReadTemperature2(float& fTemp) const noexcept
{
uint16_t uTemp;
bool bRet = ReadTemperature2(uTemp);
if (bRet)
{
fTemp = uTemp / 10.0f;
} // end if
return (bRet);
}
//** Cmd 90 Read Status
// Verified TTC
bool ReadStatus(EStatus& eStatus) const noexcept
{
uint16_t uStatus;
bool bRet = Read1IntegralTypeCRC(ECommandCode::eReadStatus, uStatus);
if (bRet)
{
eStatus = static_cast<EStatus>(uStatus);
} // end if
return (bRet);
}
//** Cmd 91 Read Encoder Mode for both encoders
// Verified TTC
bool ReadEncoderMode(EncoderMode& Mode1, EncoderMode& Mode2) const noexcept
{
uint8_t uMode1, uMode2;
bool bRet = Read2IntegralTypesCRC(ECommandCode::eReadEncoderModes, uMode1, uMode2);
if (bRet)
{
Mode1.Decode(uMode1);
Mode2.Decode(uMode2);
} // end if