arduino mega and sabertooth 2x25

Hi, I'm now to the whole programming and arduino stuff. I'm trying to make an object avoidance code that can control the sabertooth. I'm having trouble with working out the commands for forward, back, left and right, would anyone have any ideas of what I can do or where to look? So far I'm working off two examples, one was for a robot with object avoidance and the code for the tank sweep for the sabertooth.

This is what i have so far

#include <NewPing.h> //Include NewPing Library
#include <SabertoothSimplified.h>
SabertoothSimplified ST; // We'll name the Sabertooth object ST.
// For how to configure the Sabertooth, see the DIP Switch Wizard for
// Sabertooth 2X25, 2X12 and 2X5 DIP switch configuration wizard
// Be sure to select Simplified Serial Mode for use with this library.
// This sample uses a baud rate of 9600.
//
// Connections to make:
// Arduino TX->1 -> Sabertooth S1
// Arduino GND -> Sabertooth 0V
// Arduino VIN -> Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)
//
// If you want to use a pin other than TX->1, see the SoftwareSerial example.

#define TRIGGER_PIN 6 //Trigger pin of Ultrasonic sensor connected to pin 6
#define ECHO_PIN 7 //Echo pin of Ultrasonic sensor connected to pin 7
#define MAX_DISTANCE 100 //The maximum distance we want the sensor to look for is 1m

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //Create Ultrasonic sensor object

unsigned int time; //Variable to store how long it takes for the ultrasonic wave to come back
int distance; //Variable to store the distance calculated from the sensor
int triggerDistance = 30; //The distance we want the robot to look for a new path
int fDistance; //Variable to store the distance in front of the robot
int lDistance; //Variable to store the distance on the left side of the robot
int rDistance; //Variable to store the distance on the right side of the robot

void setup() {
SabertoothTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.

ST.drive(0); // The Sabertooth won't act on mixed mode 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.
// So, we set both to zero initially.
}

void loop() {
int power;
scan(); //Get the distance retrieved
fDistance = distance; //Set that distance to the front distance
if(fDistance < triggerDistance){ //If there is something closer than 30cm in front of us
moveBackward(); //Move Backward for a second
delay(1000);
moveRight(); //Turn Right for half a second
delay(500);
moveStop(); //Stop
scan(); //Take a reading
rDistance = distance; //Store that to the distance on the right side
moveLeft();
delay(1000); //Turn left for a second
moveStop(); //Stop
scan(); //Take a reading
lDistance = distance; //Store that to the distance on the left side
if(lDistance < rDistance){ //If the distance on the left is smaller than that of the right
moveRight(); //Move right for 200 milliseconds
delay(200);
moveForward(); //Then move forward
}
else{
moveForward(); //If the left side is larger than the right side move forward
}
}
else{
moveForward(); //If there is nothing infront of the robot move forward
}
}

void scan(){
time = sonar.ping(); //Send out a ping and store the time it took for it to come back in the time variable
distance = time / US_ROUNDTRIP_CM; //Convert that time into a distance
if(distance == 0){ //If no ping was recieved
distance = 100; //Set the distance to max
}
delay(10);
}

void moveBackward(){
ST.drive(power = -100);
}

void moveForward(){
ST.drive(power = 100)
}

void moveRight(){
(power = 100; power <= 100; power ++)
ST.turn(power);
delay(50);
}

void moveLeft(){
(power = 100; power = -100; power ++)
ST.turn(power);
delay(50);
}

void moveStop(){
// Now stop turning, and stop driving.
ST.turn(0);
ST.drive(0);

}

void moveRight(){
  (power = 100; power <= 100; power ++)
   ST.turn(power);
    delay(50);
}

void moveLeft(){
  (power = 100; power = -100; power ++)
  ST.turn(power);
    delay(50);
}

Did you write these functions or are they taken from an example ?
Either way they are rubbish.

I got them from the tank style sweep code for the sabertooth.

Did you type them in or copy/paste them ?
Can you please post the original functions here.

This is the original be for i played around with it to slow it down.

// Tank-Style Sweep Sample
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.

#include <SabertoothSimplified.h>

// Mixed mode is for tank-style diff-drive robots.
// Only Packet Serial actually has mixed mode, so this Simplified Serial library
// emulates it (to allow easy switching between the two libraries).

SabertoothSimplified ST; // We'll name the Sabertooth object ST.
// For how to configure the Sabertooth, see the DIP Switch Wizard for
// Sabertooth 2X25, 2X12 and 2X5 DIP switch configuration wizard
// Be sure to select Simplified Serial Mode for use with this library.
// This sample uses a baud rate of 9600.
//
// Connections to make:
// Arduino TX->1 -> Sabertooth S1
// Arduino GND -> Sabertooth 0V
// Arduino VIN -> Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)
//
// If you want to use a pin other than TX->1, see the SoftwareSerial example.

void setup()
{
SabertoothTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.

ST.drive(0); // The Sabertooth won't act on mixed mode 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.
// So, we set both to zero initially.
}

// Mixed mode tips:
// drive() should go forward and back, turn() should go right and left.
// If this is reversed, swap M2A and M2B.
// Positive on drive() should go forward, negative should go backward.
// If this is reversed, swap A and B on both M1 and M2.
// Positive on turn() should go right, negative should go left.
// If this is reversed, swap M1 and M2.

// In this sample, the SLOW sweep (left-to-right) here is turning,
// and the FAST sweep (backwards-to-forwards) is throttle.
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. Mostly, the command
// is just to demonstrate you can use drive() and turn() at the same time.
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. :slight_smile:
delay(5000);
}

A for loop from the sample program

for (power = -127; power <= 127; power ++)

This means assign a value of -127 to the variable named power and while it is less than or equal to 127 execute the code associated with the for loop then increment the power variable.

What I assume is an attempt at a for loop in your program

(power = 100; power = -100; power ++)

This means assign a value of 100 to the variable named power then assign a value of -100 to the variable named power then increment the power variable.

Seems like you need to read up on for loops in C

Thank you so much for your help! I spent a while playing around with the code and got it working.
But then I ran in to a new problem once I uploaded the code in to the arduino mega and hooked it all up, it still runs at 100% despite taking the power down to 50%
And the sabertooth error light comes on every so often.

Sigh.....

We cannot provide help unless you post your code as it is now.

#include <NewPing.h> //Include NewPing Library
#include <SabertoothSimplified.h>
SabertoothSimplified ST; // We'll name the Sabertooth object ST.
// For how to configure the Sabertooth, see the DIP Switch Wizard for
// Sabertooth 2X25, 2X12 and 2X5 DIP switch configuration wizard
// Be sure to select Simplified Serial Mode for use with this library.
// This sample uses a baud rate of 9600.
//
// Connections to make:
// Arduino TX->1 -> Sabertooth S1
// Arduino GND -> Sabertooth 0V
// Arduino VIN -> Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)
//
// If you want to use a pin other than TX->1, see the SoftwareSerial example.

#define TRIGGER_PIN 6 //Trigger pin of Ultrasonic sensor connected to pin 6
#define ECHO_PIN 7 //Echo pin of Ultrasonic sensor connected to pin 7
#define MAX_DISTANCE 100 //The maximum distance we want the sensor to look for is 1m

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //Create Ultrasonic sensor object

unsigned int time; //Variable to store how long it takes for the ultrasonic wave to come back
int distance; //Variable to store the distance calculated from the sensor
int triggerDistance = 30; //The distance we want the robot to look for a new path
int fDistance; //Variable to store the distance in front of the robot
int lDistance; //Variable to store the distance on the left side of the robot
int rDistance; //Variable to store the distance on the right side of the robot

void setup() {
SabertoothTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.

ST.drive(0); // The Sabertooth won't act on mixed mode 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.
// So, we set both to zero initially.
}

void loop() {

scan(); //Get the distance retrieved
fDistance = distance; //Set that distance to the front distance
if(fDistance < triggerDistance){ //If there is something closer than 30cm in front of us
moveBackward(); //Move Backward for a second
delay(1000);
moveRight(); //Turn Right for half a second
delay(500);
moveStop(); //Stop
scan(); //Take a reading
rDistance = distance; //Store that to the distance on the right side
moveLeft();
delay(1000); //Turn left for a second
moveStop(); //Stop
scan(); //Take a reading
lDistance = distance; //Store that to the distance on the left side
if(lDistance < rDistance){ //If the distance on the left is smaller than that of the right
moveRight(); //Move right for 200 milliseconds
delay(200);
moveForward(); //Then move forward
}
else{
moveForward(); //If the left side is larger than the right side move forward
}
}
else{
moveForward(); //If there is nothing infront of the robot move forward
}
}

void scan(){
time = sonar.ping(); //Send out a ping and store the time it took for it to come back in the time variable
distance = time / US_ROUNDTRIP_CM; //Convert that time into a distance
if(distance == 0){ //If no ping was recieved
distance = 100; //Set the distance to max
}
delay(10);
}

void moveBackward(){
int power;
for (power = -100; power <= 100; power ++)
ST.drive(power = -50);
}

void moveForward(){
int power;
for (power = -100; power <= 100; power ++)
ST.drive(power = 50);
}
void moveRight(){
int power;
for (power = -50; power <= 50; power ++)
ST.turn(power= 50);
delay(50);
}

void moveLeft(){
int power;
for(power = 100; power = -100; power ++)
ST.turn(power= 50);
delay(50);
}

void moveStop(){
// Now stop turning, and stop driving.
ST.turn(0);
ST.drive(0);

}

void moveBackward() {
  int power;
  for (power = -100; power <= 100; power ++)
    ST.drive(power = -50);
}

That's a better attempt but

power = -50;

evaluates to 1
so your command becomes

    ST.drive(1);

I expect you meant

    ST.drive(power);

but you may need to slow things down a little because the for loop will execute in microseconds.

That's a better attempt but

power = -50;

evaluates to 1

Shouldn't do. that's an assignment operator, not equality. That should assign -50 to power, then pass in -50 to that function.

Of course by doing this it also keeps assigning -50 to power, and as power is also the loop counter, it should never get out of that for loop.

*Edit:

What does this function take? If its values between -127 for zero power, and 127 for max power, should you be clipping the lower part?

I may be wrong here, but are you after something like:

int power;
  for (power = -127; power <= 0; power ++)
    ST.drive( power );

Shouldn't do. that's an assignment operator, not equality

Your'e right, of course. My mind must have been elsewhere.

tammytam:
Shouldn't do. that's an assignment operator, not equality. That should assign -50 to power, then pass in -50 to that function.

Of course by doing this it also keeps assigning -50 to power, and as power is also the loop counter, it should never get out of that for loop.

Ok I'm a bit lost here.
from what i can tell it looks like the robot is stuck in a loop so how would i get it out of it?

The values that it works on are 127 full power forward, 0 = stop, -1 and so on are reverse.

Post your new code.

Also, I may be naive here, but why are we calling ST.drive() in a for loop?

If this is an attempt at soft starting rather than going full force into forward backwards, I don't think it will work. You need to increment over a period of time, otherwise that entire loop will execute and it will drive in whatever state the last iteration of the for loop pushed in to it.

The code I'm using at the moment is still the same as be for.
The problem is when i start the robot up it jut gets stuck in a loop and drives off in some random direction it dosent seam to be taking any sonar readings or if it is just not responding.

So is anyone able to help me or point me in the right direction? Or does anyone know of any similar projects? I have been searching through the internet looking for anything that might help me work out the code for the sabertooth. All I need is to know what to type in the code to make it go left or right.

Have you got any idea to move left/right?

Sir,have you solve the problem ?

I doubt that you will get an answer from the OP after nearly 2 years. Why not start a new thread of your own describing your problem and including your code.