Sabertooth.cpp
Go to the documentation of this file.
00001 /*
00002 Arduino Library for SyRen/Sabertooth Packet Serial
00003 Copyright (c) 2012-2013 Dimension Engineering LLC
00004 http://www.dimensionengineering.com/arduino
00005 
00006 Permission to use, copy, modify, and/or distribute this software for any
00007 purpose with or without fee is hereby granted, provided that the above
00008 copyright notice and this permission notice appear in all copies.
00009 
00010 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00013 SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
00014 RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
00015 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
00016 USE OR PERFORMANCE OF THIS SOFTWARE.
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   // (1) flush() does not seem to wait until transmission is complete.
00121   //     As a result, a Serial.end() directly after this appears to
00122   //     not always transmit completely. So, we manually add a delay.
00123   // (2) Sabertooth takes about 200 ms after setting the baud rate to
00124   //     respond to commands again (it restarts).
00125   // So, this 500 ms delay should deal with this.
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 }


ric_mc
Author(s): RoboTiCan
autogenerated on Fri May 20 2016 03:48:56