-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSabertoothMod.cpp
158 lines (132 loc) · 3.94 KB
/
SabertoothMod.cpp
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
/*
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.
*/
#include "SabertoothMod.h"
Sabertooth::Sabertooth(byte address, const int s2)
: _address(address), _port(SabertoothTXPinSerial), _s2(s2)
{
}
Sabertooth::Sabertooth(byte address, SabertoothStream& port, const int s2)
: _address(address), _port(port), _s2(s2)
{
}
void Sabertooth::setupEmergencyStop() const
{
if (_s2 != NOT_A_PIN) {
pinMode(_s2, OUTPUT);
emergencyStop();
}
}
void Sabertooth::autobaud(boolean dontWait) const
{
autobaud(port(), dontWait);
}
void Sabertooth::autobaud(SabertoothStream& port, boolean dontWait)
{
if (!dontWait) { delay(1500); }
port.write(0xAA);
#if defined(ARDUINO) && ARDUINO >= 100
port.flush();
#endif
if (!dontWait) { delay(500); }
}
void Sabertooth::command(byte command, byte value) const
{
port().write(address());
port().write(command);
port().write(value);
port().write((address() + command + value) & B01111111);
}
void Sabertooth::throttleCommand(byte command, signed char power) const
{
power = constrain(power, _minPower, _maxPower);
this->command(command, (byte)abs(power));
}
void Sabertooth::motor(signed char power) const
{
motor(1, power);
}
void Sabertooth::motor(byte motor, signed char power) const
{
if (motor < 1 || motor > 2) { return; }
throttleCommand((motor == 2 ? 4 : 0) + (power < 0 ? 1 : 0), power);
}
void Sabertooth::drive(signed char power) const
{
throttleCommand(power < 0 ? 9 : 8, power);
}
void Sabertooth::turn(signed char power) const
{
throttleCommand(power < 0 ? 11 : 10, power);
}
void Sabertooth::stop() const
{
motor(1, 0);
motor(2, 0);
}
void Sabertooth::setMinVoltage(byte value) const
{
command(2, (byte)min(value, 120));
}
void Sabertooth::setMaxVoltage(byte value) const
{
command(3, (byte)min(value, 127));
}
void Sabertooth::setBaudRate(long baudRate) const
{
#if defined(ARDUINO) && ARDUINO >= 100
port().flush();
#endif
byte value;
switch (baudRate)
{
case 2400: value = 1; break;
case 9600: default: value = 2; break;
case 19200: value = 3; break;
case 38400: value = 4; break;
case 115200: value = 5; break;
}
command(15, value);
#if defined(ARDUINO) && ARDUINO >= 100
port().flush();
#endif
// (1) flush() does not seem to wait until transmission is complete.
// As a result, a Serial.end() directly after this appears to
// not always transmit completely. So, we manually add a delay.
// (2) Sabertooth takes about 200 ms after setting the baud rate to
// respond to commands again (it restarts).
// So, this 500 ms delay should deal with this.
delay(500);
}
void Sabertooth::setDeadband(byte value) const
{
command(17, (byte)min(value, 127));
}
void Sabertooth::setRamping(byte value) const
{
command(16, (byte)constrain(value, 0, 80));
}
void Sabertooth::setTimeout(int milliseconds) const
{
command(14, (byte)((constrain(milliseconds, 0, 12700) + 99) / 100));
}
void Sabertooth::setPowerLimits(signed char minPower, signed char maxPower)
{
if (minPower > maxPower) return;
_minPower = max(minPower, -127);
_maxPower = min(maxPower, 127);
}