Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "Sabertooth.h"
00020
00021 Sabertooth::Sabertooth(byte address)
00022 : _address(address), _port(SabertoothTXPinSerial)
00023 {
00024
00025 }
00026
00027 Sabertooth::Sabertooth(byte address, SabertoothStream& port)
00028 : _address(address), _port(port)
00029 {
00030
00031 }
00032
00033 void Sabertooth::autobaud(boolean dontWait) const
00034 {
00035 autobaud(port(), dontWait);
00036 }
00037
00038 void Sabertooth::autobaud(SabertoothStream& port, boolean dontWait)
00039 {
00040 if (!dontWait) { delay(1500); }
00041 port.write(0xAA);
00042 #if defined(ARDUINO) && ARDUINO >= 100
00043 port.flush();
00044 #endif
00045 if (!dontWait) { delay(500); }
00046 }
00047
00048 void Sabertooth::command(byte command, byte value) const
00049 {
00050 port().write(address());
00051 port().write(command);
00052 port().write(value);
00053 port().write((address() + command + value) & B01111111);
00054 }
00055
00056 void Sabertooth::throttleCommand(byte command, int power) const
00057 {
00058 power = constrain(power, -126, 126);
00059 this->command(command, (byte)abs(power));
00060 }
00061
00062 void Sabertooth::motor(int power) const
00063 {
00064 motor(1, power);
00065 }
00066
00067 void Sabertooth::motor(byte motor, int power) const
00068 {
00069 if (motor < 1 || motor > 2) { return; }
00070 throttleCommand((motor == 2 ? 4 : 0) + (power < 0 ? 1 : 0), power);
00071 }
00072
00073 void Sabertooth::drive(int power) const
00074 {
00075 throttleCommand(power < 0 ? 9 : 8, power);
00076 }
00077
00078 void Sabertooth::turn(int power) const
00079 {
00080 throttleCommand(power < 0 ? 11 : 10, power);
00081 }
00082
00083 void Sabertooth::stop() const
00084 {
00085 motor(1, 0);
00086 motor(2, 0);
00087 }
00088
00089 void Sabertooth::setMinVoltage(byte value) const
00090 {
00091 command(2, (byte)min(value, 120));
00092 }
00093
00094 void Sabertooth::setMaxVoltage(byte value) const
00095 {
00096 command(3, (byte)min(value, 127));
00097 }
00098
00099 void Sabertooth::setBaudRate(long baudRate) const
00100 {
00101 #if defined(ARDUINO) && ARDUINO >= 100
00102 port().flush();
00103 #endif
00104
00105 byte value;
00106 switch (baudRate)
00107 {
00108 case 2400: value = 1; break;
00109 case 9600: default: value = 2; break;
00110 case 19200: value = 3; break;
00111 case 38400: value = 4; break;
00112 case 115200: value = 5; break;
00113 }
00114 command(15, value);
00115
00116 #if defined(ARDUINO) && ARDUINO >= 100
00117 port().flush();
00118 #endif
00119
00120
00121
00122
00123
00124
00125
00126 delay(500);
00127 }
00128
00129 void Sabertooth::setDeadband(byte value) const
00130 {
00131 command(17, (byte)min(value, 127));
00132 }
00133
00134 void Sabertooth::setRamping(byte value) const
00135 {
00136 command(16, (byte)constrain(value, 0, 80));
00137 }
00138
00139 void Sabertooth::setTimeout(int milliseconds) const
00140 {
00141 command(14, (byte)((constrain(milliseconds, 0, 12700) + 99) / 100));
00142 }