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 "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 }