USB Sabertooth Packet Serial Library for Arduino
Control your USB-enabled Sabertooth with reliable Packet Serial.
USBSabertooth.h
Go to the documentation of this file.
1 /*
2 Arduino Library for USB Sabertooth Packet Serial
3 Copyright (c) 2013 Dimension Engineering LLC
4 http://www.dimensionengineering.com/arduino
5 
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9 
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
14 RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
16 USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 
19 #ifndef USBSabertooth_h
20 #define USBSabertooth_h
21 
27 #if defined(ARDUINO) && ARDUINO < 100
28 #error "This library requires Arduino 1.0 or newer."
29 #endif
30 
31 #include <Arduino.h>
32 
33 #if defined(USBCON)
34 #define SabertoothTXPinSerial Serial1 // Arduino Leonardo has TX->1 on Serial1, not Serial.
35 #else
36 #define SabertoothTXPinSerial Serial
37 #endif
38 #define SyRenTXPinSerial SabertoothTXPinSerial
39 
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
47 
48 enum USBSabertoothCommand
49 {
50  SABERTOOTH_CMD_SET = 40,
51  SABERTOOTH_CMD_GET = 41,
52 };
53 
54 enum USBSabertoothReplyCode
55 {
56  SABERTOOTH_RC_GET = 73
57 };
58 
59 enum USBSabertoothGetType
60 {
61  SABERTOOTH_GET_VALUE = 0x00,
62  SABERTOOTH_GET_BATTERY = 0x10,
63  SABERTOOTH_GET_CURRENT = 0x20,
64  SABERTOOTH_GET_TEMPERATURE = 0x40
65 };
66 
67 enum USBSabertoothSetType
68 {
69  SABERTOOTH_SET_VALUE = 0x00,
70  SABERTOOTH_SET_KEEPALIVE = 0x10,
71  SABERTOOTH_SET_SHUTDOWN = 0x20,
72  SABERTOOTH_SET_TIMEOUT = 0x40
73 };
74 
75 class USBSabertoothCommandWriter
76 {
77 public:
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);
80 };
81 
82 class USBSabertoothChecksum
83 {
84 public:
85  static byte value(const byte* data, size_t lengthOfData);
86 };
87 
88 class USBSabertoothCRC7
89 {
90 public:
91  void begin();
92  void write(byte data);
93  void write(const byte* data, size_t lengthOfData);
94  void end ();
95 
96 public:
97  inline byte value() const { return _crc; }
98  void value(byte crc) { _crc = crc; }
99 
100  static byte value(const byte* data, size_t lengthOfData);
101 
102 private:
103  byte _crc;
104 };
105 
106 class USBSabertoothCRC14
107 {
108 public:
109  void begin();
110  void write(byte data);
111  void write(const byte* data, size_t lengthOfData);
112  void end ();
113 
114 public:
115  inline uint16_t value() const { return _crc; }
116  void value(uint16_t crc) { _crc = crc; }
117 
118  static uint16_t value(const byte* data, size_t lengthOfData);
119 
120 private:
121  uint16_t _crc;
122 };
123 
124 class USBSabertoothReplyReceiver
125 {
126 public:
127  USBSabertoothReplyReceiver();
128 
129 public:
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; }
134 
135 public:
136  inline boolean ready() const { return _ready; }
137  void read (byte data);
138  void reset();
139 
140 private:
141  byte _data[SABERTOOTH_COMMAND_MAX_BUFFER_LENGTH];
142  size_t _length; boolean _ready, _usingCRC;
143 };
144 
145 class USBSabertoothTimeout
146 {
147 public:
148  USBSabertoothTimeout(int32_t timeoutMS);
149 
150 public:
151  boolean canExpire() const;
152 
153  boolean expired() const;
154 
155  void expire();
156 
157  void reset();
158 
159 private:
160  uint32_t _start;
161  int32_t _timeoutMS;
162 };
163 
170 {
171  friend class USBSabertooth;
172 
173 public:
179  USBSabertoothSerial(Stream& port = SabertoothTXPinSerial);
180 
181 public:
186  inline Stream& port() { return _port; }
187 
188 private:
189  boolean tryReceivePacket();
190 
191 private:
192  USBSabertoothSerial(USBSabertoothSerial& serial); // no copy
193  void operator = (USBSabertoothSerial& serial);
194 
195 private:
196  USBSabertoothReplyReceiver _receiver;
197  Stream& _port;
198 };
199 
205 {
206 public:
214 
215 public:
220  inline byte address() const { return _address; }
221 
227  void command(byte command, byte value);
228 
235  void command(byte command, const byte* value, size_t bytes);
236 
237 public:
243  void motor(int value);
244 
253  void motor(byte motorOutputNumber, int value);
254 
260  void power(int value);
261 
270  void power(byte powerOutputNumber, int value);
271 
277  void drive(int value);
278 
284  void turn(int value);
285 
292  void freewheel(int value = 2048);
293 
303  void freewheel(byte motorOutputNumber, int value = 2048);
304 
316  void shutDown(byte type, byte number, boolean value = true);
317 
318 public:
332  void set(byte type, byte number, int value);
333 
339  void setRamping(int value);
340 
349  void setRamping(byte motorOutputNumber, int value);
350 
359  void setTimeout(int milliseconds);
360 
366  void keepAlive();
367 
368 public:
381  inline int get(byte type, byte number)
382  {
383  return get(type, number, SABERTOOTH_GET_VALUE, false);
384  }
385 
394  inline int getBattery(byte motorOutputNumber, boolean unscaled = false)
395  {
396  return get('M', motorOutputNumber, SABERTOOTH_GET_BATTERY, unscaled);
397  }
398 
407  inline int getCurrent(byte motorOutputNumber, boolean unscaled = false)
408  {
409  return get('M', motorOutputNumber, SABERTOOTH_GET_CURRENT, unscaled);
410  }
411 
420  inline int getTemperature(byte motorOutputNumber, boolean unscaled = false)
421  {
422  return get('M', motorOutputNumber, SABERTOOTH_GET_TEMPERATURE, unscaled);
423  }
424 
425 public:
429  inline int32_t getGetRetryInterval() const { return _getRetryInterval; }
430 
435  inline void setGetRetryInterval(int32_t intervalMS) { _getRetryInterval = intervalMS; }
436 
441  inline int32_t getGetTimeout() const { return _getTimeout; }
442 
447  inline void setGetTimeout(int32_t timeoutMS) { _getTimeout = timeoutMS; }
448 
453  inline boolean usingCRC() const { return _crc; }
454 
458  inline void useChecksum() { _crc = false; }
459 
463  inline void useCRC() { _crc = true ; }
464 
465 private:
466  int get(byte type, byte number,
467  USBSabertoothGetType getType, boolean raw);
468 
469  void set(byte type, byte number, int value,
470  USBSabertoothSetType setType);
471 
472 private:
473  void init();
474 
475 private:
476  const byte _address;
477  boolean _crc;
478  int32_t _getRetryInterval;
479  int32_t _getTimeout;
480  USBSabertoothSerial& _serial;
481 };
482 
483 #endif