I have a sabertooth 2x25 and I want it to control my 2pcs 24 volt scooter motors.
I found this working code used in a balancing segway http://sites.google.com/site/onewheeledselfbalancing/Home/easy-build-low-cost-arduino-self-balancer
I tweaked the code for the balancing segway to fit my need, but it had an error in compiling..HELP
My application: Control ( forward, reverse) 2 pcs of scooter motor upon keyboard press by using the Arduino and sabertooth motor controller
My code:
#include <SoftwareSerial.h>
/*****************************************************
- DIP Switches as per the wized:
-
- NiMh Battery ( Our Case: Lead Acid)
-
- TTL RS232
-
- Simplified Serial Mode
-
- Just one Sabertooth conected
-
- 9600 baudrate
- Pin 1 - ON
- Pin 2 - OFF
- Pin 3 - ON
- Pin 4 - OFF
- Pin 5 - ON
- Pin 6 - ON
****************************************************/
// Labels for use with the Sabertooth 2x5 motor controller
// Digital pin 13 is the serial transmit pin to the
// Sabertooth 2x5
#define SABER_TX_PIN 13
// NOT USED (but still init'd)
// Digital pin 12 is the serial receive pin from the
// Sabertooth 2x5
#define SABER_RX_PIN 12
// Set to 9600 through Sabertooth dip switches
#define SABER_BAUDRATE 9600
// Simplified serial Limits for each motor
#define SABER_MOTOR1_FULL_FORWARD 127
#define SABER_MOTOR1_FULL_REVERSE 1
#define SABER_MOTOR1_FULL_STOP 64
#define SABER_MOTOR2_FULL_FORWARD 255
#define SABER_MOTOR2_FULL_REVERSE 128
#define SABER_MOTOR2_FULL_STOP 192
// Motor level to send when issuing the full stop command
#define SABER_ALL_STOP 0
SoftwareSerial SaberSerial = SoftwareSerial( SABER_RX_PIN,
SABER_TX_PIN );
void initSabertooth( void )
{
// Init software UART to communicate
// with the Sabertooth 2x5
pinMode( SABER_TX_PIN, OUTPUT );
SaberSerial.begin( SABER_BAUDRATE );
// 2 second time delay for the Sabertooth to init
delay( 2000 );
// Send full stop command
setEngineSpeed( SABER_ALL_STOP );
}
/*****************************************************
- setEngineSpeed
- Inputs - cSpeed_Motor1 - Input a percentage of full
- speed, from -100 to +100
*****************************************************/
void setEngineSpeed( signed char cNewMotorSpeed )
{
initSabertooth( );
unsigned char cSpeedVal_Motor1 = 0;
unsigned char cSpeedVal_Motor2 = 0;
// Check for full stop command
if( cNewMotorSpeed == 0 )
{
// Send full stop command for both motors
SaberSerial.print( 0, BYTE );
return;
}
// Calculate the speed value for motor 1
if( cNewMotorSpeed >= 100 )
{
cSpeedVal_Motor1 = SABER_MOTOR1_FULL_FORWARD;
cSpeedVal_Motor2 = SABER_MOTOR2_FULL_FORWARD;
}
else if( cNewMotorSpeed <= -100 )
{
cSpeedVal_Motor1 = SABER_MOTOR1_FULL_REVERSE;
cSpeedVal_Motor2 = SABER_MOTOR2_FULL_REVERSE;
}
else
{
// Calc motor 1 speed (Final value ranges from 1 to 127)
cSpeedVal_Motor1 = map( cNewMotorSpeed,
-100,
100,
SABER_MOTOR1_FULL_REVERSE,
SABER_MOTOR1_FULL_FORWARD );
// Calc motor 2 speed (Final value ranges from 128 to 255)
cSpeedVal_Motor2 = map( cNewMotorSpeed,
-100,
100,
SABER_MOTOR2_FULL_REVERSE,
SABER_MOTOR2_FULL_FORWARD );
}
// Fire the values off to the Sabertooth motor controller
SaberSerial.print( cSpeedVal_Motor1, BYTE );
SaberSerial.print( cSpeedVal_Motor2, BYTE );
}
void move( )
{
initSabertooth( );
}
void control ( )
{
// Full stop
setEngineSpeed( 0 );
// Half reverse
setEngineSpeed( -50 );
// Full reverse
setEngineSpeed( -100 );
// Half forward
setEngineSpeed( 50 );
// Full forward
setEngineSpeed( 100 );
}
DIFFERENTIAL STEERING: Motor 1: Left side Motor 2: Right side
void setEngineSpeedDir( signed char cNewMotorSpeedDir )
{
unsigned char cSpeedValDir_Motor1 = 0;
unsigned char cSpeedValDir_Motor2 = 0;
if( cNewMotorSpeedDir >= 100 )
{
cSpeedValDir_Motor1 = SABER_MOTOR1_FULL_FORWARD; // GO RIGHT
cSpeedValDir_Motor2 = SABER_MOTOR2_FULL_STOP;
}
else if( cNewMotorSpeedDir <= -100 )
{
cSpeedValDir_Motor1 = SABER_MOTOR1_FULL_STOP; // GO LEFT
cSpeedValDir_Motor2 = SABER_MOTOR2_FULL_FORWARD;
}
else
{
// Calc motor 1 speed (Final value ranges from 64 to 127)
cSpeedValDir_Motor1 = map( cNewMotorSpeedDir,
-100,
100,
SABER_MOTOR1_FULL_STOP,
SABER_MOTOR1_FULL_FORWARD );
// Calc motor 2 speed (Final value ranges from 192 to 255)
cSpeedValDir_Motor2 = map( cNewMotorSpeedDir,
-100,
100,
SABER_MOTOR2_FULL_FORWARD,
SABER_MOTOR2_FULL_STOP);
}
// Fire the values off to the Sabertooth motor controller
SaberSerial.print( cSpeedValDir_Motor1, BYTE );
SaberSerial.print( cSpeedValDir_Motor2, BYTE );
void moveit( )
{
initSabertooth( );
}
void turn ( )
{
// Turn left
setEngineSpeedDir( -100 );
// Turn right
setEngineSpeedDir( 100 );
}
//KEY PRESS: (10/12/10)
void key() {
If(Serial.available()>0) {
Int data = Serial.read();
digitalWrite(buttonPin,LOW);
switch(data)
{
case'w':traverse = 100;break; //full forward
case's':traverse = -50;break; // half reverse
case'q':traverse = 0;break; // Stop
case'a':negotiate = -100;break; // left
case'd':negotiate = 100;break; // right
}
setEngineSpeed( traverse );
setEngineSpeedDir( negotiate );
}
{