Add sabertooth lib, start enabling all motor drive

This commit is contained in:
Cole Deck
2023-09-13 11:30:59 -05:00
parent 8f59f0fb15
commit 109b2c92c4
19 changed files with 1158 additions and 54 deletions

View File

@ -0,0 +1,46 @@
// Jolty Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
#include <Sabertooth.h>
Sabertooth ST(128); // The Sabertooth is on address 128. We'll name its object ST.
// If you've set up your Sabertooth on a different address, of course change
// that here. For how to configure address, etc. see the DIP Switch Wizard for
// Sabertooth - http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
// SyRen - http://www.dimensionengineering.com/datasheets/SyrenDIPWizard/start.htm
// Be sure to select Packetized Serial Mode for use with this library.
//
// On that note, you can use this library for SyRen just as easily.
// The diff-drive commands (drive, turn) do not work on a SyRen, of course, but it will respond correctly
// if you command motor 1 to do something (ST.motor(1, ...)), just like a Sabertooth.
//
// In this sample, hardware serial TX connects to S1.
// See the SoftwareSerial example in 3.Advanced for how to use other pins.
void setup()
{
SabertoothTXPinSerial.begin(9600); // 9600 is the default baud rate for Sabertooth packet serial.
ST.autobaud(); // Send the autobaud command to the Sabertooth controller(s).
// NOTE: *Not all* Sabertooth controllers need this command.
// It doesn't hurt anything, but V2 controllers use an
// EEPROM setting (changeable with the function setBaudRate) to set
// the baud rate instead of detecting with autobaud.
//
// If you have a 2x12, 2x25 V2, 2x60 or SyRen 50, you can remove
// the autobaud line and save yourself two seconds of startup delay.
}
void loop()
{
ST.motor(1, 127); // Go forward at full power.
delay(2000); // Wait 2 seconds.
ST.motor(1, 0); // Stop.
delay(2000); // Wait 2 seconds.
ST.motor(1, -127); // Reverse at full power.
delay(2000); // Wait 2 seconds.
ST.motor(1, 0); // Stop.
delay(2000);
}

View File

@ -0,0 +1,53 @@
// Sweep Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
#include <Sabertooth.h>
Sabertooth ST(128); // The Sabertooth is on address 128. We'll name its object ST.
// If you've set up your Sabertooth on a different address, of course change
// that here. For how to configure address, etc. see the DIP Switch Wizard for
// Sabertooth - http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
// SyRen - http://www.dimensionengineering.com/datasheets/SyrenDIPWizard/start.htm
// Be sure to select Packetized Serial Mode for use with this library.
//
// On that note, you can use this library for SyRen just as easily.
// The diff-drive commands (drive, turn) do not work on a SyRen, of course, but it will respond correctly
// if you command motor 1 to do something (ST.motor(1, ...)), just like a Sabertooth.
//
// In this sample, hardware serial TX connects to S1.
// See the SoftwareSerial example in 3.Advanced for how to use other pins.
void setup()
{
SabertoothTXPinSerial.begin(9600); // 9600 is the default baud rate for Sabertooth packet serial.
ST.autobaud(); // Send the autobaud command to the Sabertooth controller(s).
// NOTE: *Not all* Sabertooth controllers need this command.
// It doesn't hurt anything, but V2 controllers use an
// EEPROM setting (changeable with the function setBaudRate) to set
// the baud rate instead of detecting with autobaud.
//
// If you have a 2x12, 2x25 V2, 2x60 or SyRen 50, you can remove
// the autobaud line and save yourself two seconds of startup delay.
}
void loop()
{
int power;
// Ramp motor 1 from -127 to 127 (full reverse to full forward),
// waiting 20 ms (1/50th of a second) per value.
for (power = -127; power <= 127; power ++)
{
ST.motor(1, power);
delay(20);
}
// Now go back the way we came.
for (power = 127; power >= -127; power --)
{
ST.motor(1, power); // Tip for SyRen users: Typing ST.motor(power) does the same thing as ST.motor(1, power).
delay(20); // Since SyRen doesn't have a motor 2, this alternative can save you typing.
}
}

View File

@ -0,0 +1,69 @@
// Tank-Style Sweep Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
#include <Sabertooth.h>
// Mixed mode is for tank-style diff-drive robots.
Sabertooth ST(128); // The Sabertooth is on address 128. We'll name its object ST.
// If you've set up your Sabertooth on a different address, of course change
// that here. For how to configure address, etc. see the DIP Switch Wizard for
// Sabertooth - http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
// SyRen - http://www.dimensionengineering.com/datasheets/SyrenDIPWizard/start.htm
// Be sure to select Packetized Serial Mode for use with this library.
//
// This sample uses the mixed mode (diff-drive) commands, which require two motors
// and hence do not work on a SyRen.
//
// In this sample, hardware serial TX connects to S1.
// See the SoftwareSerial example in 3.Advanced for how to use other pins.
void setup()
{
SabertoothTXPinSerial.begin(9600); // 9600 is the default baud rate for Sabertooth packet serial.
ST.autobaud(); // Send the autobaud command to the Sabertooth controller(s).
// NOTE: *Not all* Sabertooth controllers need this command.
// It doesn't hurt anything, but V2 controllers use an
// EEPROM setting (changeable with the function setBaudRate) to set
// the baud rate instead of detecting with autobaud.
//
// If you have a 2x12, 2x25 V2, 2x60 or SyRen 50, you can remove
// the autobaud line and save yourself two seconds of startup delay.
ST.drive(0); // The Sabertooth won't act on mixed mode packet serial commands until
ST.turn(0); // it has received power levels for BOTH throttle and turning, since it
// mixes the two together to get diff-drive power levels for both motors.
}
// The SLOW ramp here is turning, and the FAST ramp is throttle.
// If that's the opposite of what you're seeing, swap M2A and M2B.
void loop()
{
int power;
// Don't turn. Ramp from going backwards to going forwards, waiting 20 ms (1/50th of a second) per value.
for (power = -127; power <= 127; power ++)
{
ST.drive(power);
delay(20);
}
// Now, let's use a power level of 20 (out of 127) forward.
// This way, our turning will have a radius.
ST.drive(20);
// Ramp turning from full left to full right SLOWLY by waiting 50 ms (1/20th of a second) per value.
for (power = -127; power <= 127; power ++)
{
ST.turn(power);
delay(50);
}
// Now stop turning, and stop driving.
ST.turn(0);
ST.drive(0);
// Wait a bit. This is so you can catch your robot if you want to. :-)
delay(5000);
}

View File

@ -0,0 +1,34 @@
// Set Minimum Voltage Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
// The values in this sample are specifically for the Sabertooth 2x25, and may
// not have the same effect on other models.
#include <Sabertooth.h>
Sabertooth ST(128);
void setup()
{
SabertoothTXPinSerial.begin(9600);
ST.autobaud();
// This setting does not persist between power cycles.
// See the Packet Serial section of the documentation for what values to use
// for the minimum voltage command. It may vary between Sabertooth models
// (2x25, 2x60, etc.).
//
// On a Sabertooth 2x25, the value is (Desired Volts - 6) X 5.
// So, in this sample, we'll make the low battery cutoff 12V: (12 - 6) X 5 = 30.
ST.setMinVoltage(30);
}
void loop()
{
ST.motor(1, 50);
delay(5000);
ST.motor(1, -50);
delay(5000);
}

View File

@ -0,0 +1,48 @@
// Set Baud Rate Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
// WARNING: This sample makes changes that will persist between restarts.
// NOTE: The setBaudRate function will only have an effect on V2 controllers (2x12, 2x25 V2, 2x60, SyRen 50).
// Earlier controllers automatically detect the baud rate you choose in Serial.begin
// when you call the autobaud function. Autobaud was replaced in V2 controllers for reliability
// in the event that the Sabertooth lost power.
#include <Sabertooth.h>
Sabertooth ST(128);
void setup()
{
// This sample will tell the Sabertooth *at 9600 baud* to *switch to 2400 baud*.
// Keep in mind you must send the command to change the baud rate *at the baud rate
// the Sabertooth is listening at* (factory default is 9600). After that, if it works,
// you will be able to communicate using the new baud rate.
//
// Options are:
// 2400
// 9600
// 19200
// 38400
// 115200 (only supported by some devices such as 2X60 -- check the device's datasheet)
//
// WARNING: The Sabertooth remembers this command between restarts.
// To change your Sabertooth back to its default, you must *be at the baud rate you've
// set the Sabertooth to*, and then call ST.setBaudRate(9600)
SabertoothTXPinSerial.begin(9600);
ST.setBaudRate(2400);
SabertoothTXPinSerial.end();
// OK, we're at 2400. Let's talk to the Sabertooth at that speed.
SabertoothTXPinSerial.begin(2400);
}
void loop()
{
ST.drive(0);
ST.turn(20);
delay(2000);
ST.turn(-20);
delay(2000);
}

View File

@ -0,0 +1,33 @@
// Set Deadband Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
// WARNING: This sample makes changes that will persist between restarts AND in all modes.
#include <Sabertooth.h>
Sabertooth ST(128);
void setup()
{
SabertoothTXPinSerial.begin(9600);
ST.autobaud();
// This makes the deadband from -20 to 20 (of 127).
// If your commands for a motor stay entirely within the deadband for more than
// a second, the motor driver will stop the motor.
// WARNING: The Sabertooth remembers this command between restarts AND in all modes.
// To change your Sabertooth back to its default, call ST.setDeadband(0)
ST.setDeadband(20);
}
void loop()
{
// 50 is greater than 20, so the motor moves.
ST.motor(1, 50);
delay(5000);
// 10 is NOT, so the motor does not move.
ST.motor(1, 10);
delay(5000);
}

View File

@ -0,0 +1,41 @@
// Set Maximum Voltage Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
// WARNING: This sample makes changes that will persist between restarts AND in all modes.
// The values in this sample are specifically for the Sabertooth 2x25, and may
// not have the same effect on other models.
#include <Sabertooth.h>
Sabertooth ST(128);
void setup()
{
SabertoothTXPinSerial.begin(9600);
ST.autobaud();
// See the Packet Serial section of the documentation for what values to use
// for the maximum voltage command. It may vary between Sabertooth models
// (2x25, 2x60, etc.).
//
// On a Sabertooth 2x25, the value is (Desired Volts) X 5.12.
// In this sample, we'll cap the max voltage before the motor driver does
// a hard brake at 14V. For a 12V ATX power supply this might be reasonable --
// at 16V they tend to shut off. Here, if the voltage climbs above
// 14V due to regenerative braking, the Sabertooth will go into hard brake instead.
// While this is occuring, the red Error LED will turn on.
//
// 14 X 5.12 = 71.68, so we'll go with 71, cutting off slightly below 14V.
//
// WARNING: This setting persists between power cycles.
ST.setMaxVoltage(71);
}
void loop()
{
ST.motor(1, 50);
delay(5000);
ST.motor(1, -50);
delay(5000);
}

View File

@ -0,0 +1,39 @@
// Set Ramping Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
// WARNING: This sample makes changes that will persist between restarts AND in all modes.
#include <Sabertooth.h>
Sabertooth ST(128);
void setup()
{
SabertoothTXPinSerial.begin(9600);
ST.autobaud();
// See the Sabertooth 2x60 documentation for information on ramping values.
// There are three ranges: 1-10 (Fast), 11-20 (Slow), and 21-80 (Intermediate).
// The ramping value 14 used here sets a ramp time of 4 seconds for full
// forward-to-full reverse.
//
// 0 turns off ramping. Turning off ramping requires a power cycle.
//
// WARNING: The Sabertooth remembers this command between restarts AND in all modes.
// To change your Sabertooth back to its default, call ST.setRamping(0)
ST.setRamping(14);
}
void loop()
{
// Full forward, both motors.
ST.motor(1, 127);
ST.motor(2, 127);
delay(5000);
// Full reverse
ST.motor(1, -127);
ST.motor(2, -127);
delay(5000);
}

View File

@ -0,0 +1,33 @@
// Set Serial Timeout Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
#include <Sabertooth.h>
Sabertooth ST(128);
void setup()
{
SabertoothTXPinSerial.begin(9600);
ST.autobaud();
// setTimeout rounds up to the nearest 100 milliseconds, so this 950 will actually be 1 second.
// A value of 0 disables the serial timeout.
ST.setTimeout(950);
}
void loop()
{
// Set motor 1 to reverse 20 (out of 127), and sleep for 5 seconds.
// Notice how it cuts out after 1 second -- this is the serial timeout in action.
// Since we configured it in setup() for 1 second, 1 second without any new
// commands will cause the motors to stop.
ST.motor(1, -20);
delay(5000);
// Why do this?
// If the S1 wire gets cut for some reason, or if your program crashes,
// the Sabertooth will stop receiving commands from the Arduino.
// With a timeout, your robot will stop. So, it's a safety feature mostly.
}

View File

@ -0,0 +1,43 @@
// Shared Line Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
#include <Sabertooth.h>
// Up to 8 Sabertooth/SyRen motor drivers can share the same S1 line.
// This sample uses three: address 128 and 129 on ST1[0] and ST1[2],
// and address 130 on ST2.
Sabertooth ST1[2] = { Sabertooth(128), Sabertooth(129) };
Sabertooth ST2(130);
void setup()
{
SabertoothTXPinSerial.begin(9600);
Sabertooth::autobaud(SabertoothTXPinSerial); // Autobaud is for the whole serial line -- you don't need to do
// it for each individual motor driver. This is the version of
// the autobaud command that is not tied to a particular
// Sabertooth object.
// See the examples in 1.Basics for information on whether you
// need this line at all.
}
void loop()
{
// ST1[0] (address 128) has power 50 (of 127 max) on M1,
// ST1[1] (address 129) has power 60 (of 127 max) on M2, and
// ST2 (address 130) we'll do tank-style and have it drive 20 and turn right 50.
// Do this for 5 seconds.
ST1[0].motor(1, 50);
ST1[1].motor(2, 60);
ST2.drive(20);
ST2.turn(50);
delay(5000);
// And now let's stop for 5 seconds, except address 130 -- we'll let it stop and turn left...
ST1[0].motor(1, 0);
ST1[1].motor(2, 0);
ST2.drive(0);
ST2.turn(-40);
delay(5000);
}

View File

@ -0,0 +1,35 @@
// Software Serial Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
#include <SoftwareSerial.h>
#include <Sabertooth.h>
SoftwareSerial SWSerial(NOT_A_PIN, 11); // RX on no pin (unused), TX on pin 11 (to S1).
Sabertooth ST(128, SWSerial); // Address 128, and use SWSerial as the serial port.
void setup()
{
SWSerial.begin(9600);
ST.autobaud();
}
void loop()
{
int power;
// Ramp from -127 to 127 (full reverse to full forward), waiting 20 ms (1/50th of a second) per value.
for (power = -127; power <= 127; power ++)
{
ST.motor(1, power);
delay(20);
}
// Now go back the way we came.
for (power = 127; power >= -127; power --)
{
ST.motor(1, power);
delay(20);
}
}