-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSabertoothMod.h
208 lines (178 loc) · 6.17 KB
/
SabertoothMod.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
/*
Arduino Library for SyRen/Sabertooth Packet Serial
Modified by Srikanth Anantharam
Copyright (c) 2016-NOW Srikanth Anantharam
Copyright (c) 2012-2013 Dimension Engineering LLC
http://www.dimensionengineering.com/arduino
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef Sabertooth_h
#ifndef SabertoothMod_h
#define SabertoothMod_h
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
typedef Stream SabertoothStream;
#else
#include <WProgram.h>
typedef Print SabertoothStream;
#endif
#if defined(USBCON)
#define SabertoothTXPinSerial Serial1 // Arduino Leonardo has TX->1 on Serial1, not Serial.
#else
#define SabertoothTXPinSerial Serial
#endif
#define SyRenTXPinSerial SabertoothTXPinSerial
/*!
\class Sabertooth
\brief Controls a %Sabertooth or %SyRen motor driver running in Packet Serial mode.
*/
class Sabertooth
{
public:
/*!
Initializes a new instance of the Sabertooth class.
The driver address is set to the value given, and the Arduino TX serial port is used.
\param address The driver address.
*/
Sabertooth(byte address, const int s2 = NOT_A_PIN);
/*!
Initializes a new instance of the Sabertooth class.
The driver address is set to the value given, and the specified serial port is used.
\param address The driver address.
\param port The port to use.
*/
Sabertooth(byte address, SabertoothStream& port, const int s2 = NOT_A_PIN);
/*!
Setups sabertooth emergency stop.
*/
void setupEmergencyStop() const;
public:
/*!
Gets the driver address.
\return The driver address.
*/
inline byte address() const { return _address; }
/*!
Gets the serial port.
\return The serial port.
*/
inline SabertoothStream& port() const { return _port; }
/*!
Sends the autobaud character.
\param dontWait If false, a delay is added to give the driver time to start up.
*/
void autobaud(boolean dontWait = false) const;
/*!
Sends the autobaud character.
\param port The port to use.
\param dontWait If false, a delay is added to give the driver time to start up.
*/
static void autobaud(SabertoothStream& port, boolean dontWait = false);
/*!
Sends a packet serial command to the motor driver.
\param command The number of the command.
\param value The command's value.
*/
void command(byte command, byte value) const;
public:
/*!
Sets the power of motor 1.
\param power The power, between -127 and 127.
*/
void motor(signed char power) const;
/*!
Sets the power of the specified motor.
\param motor The motor number, 1 or 2.
\param power The power, between -127 and 127.
*/
void motor(byte motor, signed char power) const;
/*!
Sets the driving power.
\param power The power, between -127 and 127.
*/
void drive(signed char power) const;
/*!
Sets the turning power.
\param power The power, between -127 and 127.
*/
void turn(signed char power) const;
/*!
Stops.
*/
void stop() const;
/*!
Causes the sabertooth driver to shut down.
*/
inline void emergencyStop() const { digitalWrite(_s2, LOW); }
/*!
Causes the sabertooth driver to become active.
*/
inline void removeEmergencyStop() const { digitalWrite(_s2, HIGH); }
public:
/*!
Sets the minimum voltage.
\param value The voltage. The units of this value are driver-specific and are specified in the Packet Serial chapter of the driver's user manual.
*/
void setMinVoltage(byte value) const;
/*!
Sets the maximum voltage.
Maximum voltage is stored in EEPROM, so changes persist between power cycles.
\param value The voltage. The units of this value are driver-specific and are specified in the Packet Serial chapter of the driver's user manual.
*/
void setMaxVoltage(byte value) const;
/*!
Sets the baud rate.
Baud rate is stored in EEPROM, so changes persist between power cycles.
\param baudRate The baud rate. This can be 2400, 9600, 19200, 38400, or on some drivers 115200.
*/
void setBaudRate(long baudRate) const;
/*!
Sets the deadband.
Deadband is stored in EEPROM, so changes persist between power cycles.
\param value The deadband value.
Motor powers in the range [-deadband, deadband] will be considered in the deadband, and will
not prevent the driver from entering nor cause the driver to leave an idle brake state.
0 resets to the default, which is 3.
*/
void setDeadband(byte value) const;
/*!
Sets the ramping.
Ramping is stored in EEPROM, so changes persist between power cycles.
\param value The ramping value. Consult the user manual for possible values.
*/
void setRamping(byte value) const;
/*!
Sets the serial timeout.
\param milliseconds The maximum time in milliseconds between packets. If this time is exceeded,
the driver will stop the motors. This value is rounded up to the nearest 100 milliseconds.
This library assumes the command value is in units of 100 milliseconds. This is true for
most drivers, but not all. Check the packet serial chapter of the driver's user manual
to make sure.
*/
void setTimeout(int milliseconds) const;
/*!
Sets the minimum and maximum power limits.
\param minPower
\param maxPower
*/
void setPowerLimits(signed char minPower, signed char maxPower);
private:
void throttleCommand(byte command, signed char power) const;
private:
signed char _minPower = -127;
signed char _maxPower = 127;
const int _s2;
const byte _address;
SabertoothStream& _port;
};
#endif
#endif