19 #ifndef USBSabertooth_h
20 #define USBSabertooth_h
27 #if defined(ARDUINO) && ARDUINO < 100
28 #error "This library requires Arduino 1.0 or newer."
34 #define SabertoothTXPinSerial Serial1 // Arduino Leonardo has TX->1 on Serial1, not Serial.
36 #define SabertoothTXPinSerial Serial
38 #define SyRenTXPinSerial SabertoothTXPinSerial
40 #define SABERTOOTH_COMMAND_MAX_BUFFER_LENGTH 10
41 #define SABERTOOTH_COMMAND_MAX_DATA_LENGTH 5
42 #define SABERTOOTH_DEFAULT_GET_RETRY_INTERVAL 100
43 #define SABERTOOTH_DEFAULT_GET_TIMEOUT SABERTOOTH_INFINITE_TIMEOUT
44 #define SABERTOOTH_GET_TIMED_OUT -32768
45 #define SABERTOOTH_INFINITE_TIMEOUT -1
46 #define SABERTOOTH_MAX_VALUE 16383
48 enum USBSabertoothCommand
50 SABERTOOTH_CMD_SET = 40,
51 SABERTOOTH_CMD_GET = 41,
54 enum USBSabertoothReplyCode
56 SABERTOOTH_RC_GET = 73
59 enum USBSabertoothGetType
61 SABERTOOTH_GET_VALUE = 0x00,
62 SABERTOOTH_GET_BATTERY = 0x10,
63 SABERTOOTH_GET_CURRENT = 0x20,
64 SABERTOOTH_GET_TEMPERATURE = 0x40
67 enum USBSabertoothSetType
69 SABERTOOTH_SET_VALUE = 0x00,
70 SABERTOOTH_SET_KEEPALIVE = 0x10,
71 SABERTOOTH_SET_SHUTDOWN = 0x20,
72 SABERTOOTH_SET_TIMEOUT = 0x40
75 class USBSabertoothCommandWriter
78 static size_t writeToBuffer(byte* buffer, byte address, USBSabertoothCommand command,
boolean useCRC,
const byte* data,
size_t lengthOfData);
79 static void writeToStream(Stream& port, byte address, USBSabertoothCommand command,
boolean useCRC,
const byte* data,
size_t lengthOfData);
82 class USBSabertoothChecksum
85 static byte value(
const byte* data,
size_t lengthOfData);
88 class USBSabertoothCRC7
92 void write(byte data);
93 void write(
const byte* data,
size_t lengthOfData);
97 inline byte value()
const {
return _crc; }
98 void value(byte crc) { _crc = crc; }
100 static byte value(
const byte* data,
size_t lengthOfData);
106 class USBSabertoothCRC14
110 void write(byte data);
111 void write(
const byte* data,
size_t lengthOfData);
115 inline uint16_t value()
const {
return _crc; }
116 void value(uint16_t crc) { _crc = crc; }
118 static uint16_t value(
const byte* data,
size_t lengthOfData);
124 class USBSabertoothReplyReceiver
127 USBSabertoothReplyReceiver();
130 inline byte address ()
const {
return _data[0]; }
131 inline USBSabertoothReplyCode command ()
const {
return (USBSabertoothReplyCode)_data[1]; }
132 inline const byte* data ()
const {
return _data; }
133 inline boolean usingCRC()
const {
return _usingCRC; }
136 inline boolean ready()
const {
return _ready; }
137 void read (byte data);
141 byte _data[SABERTOOTH_COMMAND_MAX_BUFFER_LENGTH];
142 size_t _length;
boolean _ready, _usingCRC;
145 class USBSabertoothTimeout
148 USBSabertoothTimeout(int32_t timeoutMS);
151 boolean canExpire()
const;
153 boolean expired()
const;
186 inline Stream&
port() {
return _port; }
189 boolean tryReceivePacket();
196 USBSabertoothReplyReceiver _receiver;
220 inline byte
address()
const {
return _address; }
243 void motor(
int value);
253 void motor(byte motorOutputNumber,
int value);
260 void power(
int value);
270 void power(byte powerOutputNumber,
int value);
277 void drive(
int value);
284 void turn(
int value);
303 void freewheel(byte motorOutputNumber,
int value = 2048);
316 void shutDown(byte type, byte number,
boolean value =
true);
332 void set(byte type, byte number,
int value);
349 void setRamping(byte motorOutputNumber,
int value);
381 inline int get(byte type, byte number)
383 return get(type, number, SABERTOOTH_GET_VALUE,
false);
394 inline int getBattery(byte motorOutputNumber,
boolean unscaled =
false)
396 return get(
'M', motorOutputNumber, SABERTOOTH_GET_BATTERY, unscaled);
407 inline int getCurrent(byte motorOutputNumber,
boolean unscaled =
false)
409 return get(
'M', motorOutputNumber, SABERTOOTH_GET_CURRENT, unscaled);
422 return get(
'M', motorOutputNumber, SABERTOOTH_GET_TEMPERATURE, unscaled);
466 int get(byte type, byte number,
467 USBSabertoothGetType getType,
boolean raw);
469 void set(byte type, byte number,
int value,
470 USBSabertoothSetType setType);
478 int32_t _getRetryInterval;