SabertoothSimplified.cpp
Go to the documentation of this file.
00001 /*
00002 Arduino Library for Sabertooth Simplified 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 "SabertoothSimplified.h"
00020 
00021 SabertoothSimplified::SabertoothSimplified()
00022   : _port(SabertoothTXPinSerial)
00023 {
00024   
00025 }
00026 
00027 SabertoothSimplified::SabertoothSimplified(Print& port)
00028   : _port(port)
00029 {
00030   
00031 }
00032 
00033 void SabertoothSimplified::motor(int power)
00034 {
00035   motor(1, power);
00036 }
00037 
00038 void SabertoothSimplified::motor(byte motor, int power)
00039 {
00040   mixedMode(false);
00041   raw(motor, power);  
00042 }
00043 
00044 void SabertoothSimplified::drive(int power)
00045 {
00046   mixedMode(true);
00047   _mixedDrive = constrain(power, -127, 127);
00048   _mixedDriveSet = true;
00049   mixedUpdate();
00050 }
00051 
00052 void SabertoothSimplified::turn(int power)
00053 {
00054   mixedMode(true);
00055   _mixedTurn = constrain(power, -127, 127);
00056   _mixedTurnSet = true;
00057   mixedUpdate();
00058 }
00059 
00060 void SabertoothSimplified::stop()
00061 {
00062   _port.write((uint8_t)0);
00063   _mixedDriveSet = false;
00064   _mixedTurnSet = false;
00065 }
00066 
00067 void SabertoothSimplified::mixedMode(boolean enable)
00068 {
00069   if (_mixed == enable) { return; }
00070   
00071   stop();
00072   _mixed = enable;
00073 }
00074 
00075 void SabertoothSimplified::mixedUpdate()
00076 {
00077   if (!_mixedDriveSet || !_mixedTurnSet) { return; }
00078   raw(1, _mixedDrive - _mixedTurn);
00079   raw(2, _mixedDrive + _mixedTurn);
00080 }
00081 
00082 void SabertoothSimplified::raw(byte motor, int power)
00083 {
00084   byte command, magnitude;
00085   power = constrain(power, -127, 127);
00086   magnitude = abs(power) >> 1;
00087   
00088   if (motor == 1)
00089   {
00090     command = power < 0 ? 63 - magnitude : 64 + magnitude;
00091   }
00092   else if (motor == 2)
00093   {
00094     command = power < 0 ? 191 - magnitude : 192 + magnitude;
00095   }
00096   
00097   command = constrain(command, 1, 254);
00098   _port.write(command);
00099 }


ric_mc
Author(s): RoboTiCan
autogenerated on Thu Aug 27 2015 14:39:50