Argh....
Having a small bug 
When I move my red object to the left of the kinect the motors turn right and moving the object to the right of the kinect motors turn left.
Basically the steering is reversed. Been trying everything I can think of (all night...)
Here's the main loop:
void loop()
{
while (Serial.available())
{
// all we do is construct the incoming command to be handled in the main loop
// get the incoming byte from the serial stream
char incomingByte = (char)Serial.read();
if (incomingByte == EOP)
{
// marks the end of a command
commandReceived = true;
break;
}
else if (incomingByte == SOP)
{
// marks the start of a new command
command = "";
commandReceived = false;
}
else
{
command += incomingByte;
}
}
pingCalc();
//stepperMove();
//stepper.run();
handleCommand();
if((valAxisX >= 0) && (valAxisX <= 200))
{
servoThrottle.write(neutralSpeed);
leftSteer();
}
else if((valAxisX >= 440) && (valAxisX <= 640))
{
servoThrottle.write(neutralSpeed);
rightSteer();
}
else if((valAxisX > 200) && (valAxisX < 440))
{
handleAcceleration();
servoSteering.write(neutralSteer);
}
}
Here's the left & right steering functions:
void leftSteer()
{
const int targetPos = 1500;
const int Step = 10;
int currentLPos = valAxisX;
currentLPos = map(currentLPos, 0, 640, 1000, 2000);
int newPos = currentLPos + Step;
if((currentLPos < targetPos) && (newPos > targetPos))
{
newPos = targetPos;
}
servoSteering.write(newPos);
}
void rightSteer()
{
const int targetPos = 1500;
const int Step = 10;
int currentRPos = valAxisX;
currentRPos = map(currentRPos, 0, 640, 1000, 2000);
int newPos = currentRPos - Step;
if((currentRPos > targetPos) && (newPos < targetPos))
{
newPos = targetPos;
}
servoSteering.write(newPos);
}
And the full sketch:
//Sabertooth 2X25 motor controlled robot in RC microcontroller (mixed) mode
// with differential & exponential settings.
//S1 controls throttle.
//S2 controls steering.
//Date: 7Jan14
#include <AccelStepper.h>
#include <Servo.h>
#define N_PINGS 4
const byte pingPins [N_PINGS] = {
22, 23, 24, 25}; // PING pins
const int minRanges [N_PINGS] = {
14, 14, 36, 14};
int pingRange = 0;
AccelStepper stepper(2,12,13);
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 8;
const int brakeB = 9;
String command; // a string to hold the incoming command
#define SOP '<'
#define EOP '>'
boolean commandReceived = false;
int valAxisX =0;
Servo servoThrottle; // create servo object for throttle
Servo servoSteering; // create servo object steering
int currentSpeed = 0; // variable to store the servo current speed
int targetSpeed = 0; // variable to store the servo target speed
unsigned long lastSpeedUpdateTime = 0;
unsigned long lastPosUpdateTime = 0;
const unsigned long SPEED_UPDATE_INTERVAL = 10; // ms
const unsigned long POS_UPDATE_INTERVAL = 10; // ms
const int neutralSpeed = 1500;
const int neutralSteer = 1500;
void setup()
{
Serial.begin(9600);
servoThrottle.attach(53, 1000, 2000); // attach the throttle servo
servoSteering.attach(52, 1000, 2000); // attach the steering servo
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
stepper.setMaxSpeed(100);
stepper.setSpeed(50);
stepper.setAcceleration(200);
//stepper.moveTo(40);
}
void loop()
{
while (Serial.available())
{
// all we do is construct the incoming command to be handled in the main loop
// get the incoming byte from the serial stream
char incomingByte = (char)Serial.read();
if (incomingByte == EOP)
{
// marks the end of a command
commandReceived = true;
break;
}
else if (incomingByte == SOP)
{
// marks the start of a new command
command = "";
commandReceived = false;
}
else
{
command += incomingByte;
}
}
pingCalc();
//stepperMove();
//stepper.run();
handleCommand();
if((valAxisX >= 0) && (valAxisX <= 200))
{
servoThrottle.write(neutralSpeed);
leftSteer();
}
else if((valAxisX >= 440) && (valAxisX <= 640))
{
servoThrottle.write(neutralSpeed);
rightSteer();
}
else if((valAxisX > 200) && (valAxisX < 440))
{
handleAcceleration();
servoSteering.write(neutralSteer);
}
}
void setSpeed(int requiredSpeed)
{
int range [N_PINGS];
range [2] = ping(pingPins [2]);
delay(5);
int centerThresh = range [2];
centerThresh = map(centerThresh, 14, 54, 1500, 2000);
requiredSpeed = constrain(centerThresh, 1500, 2000);
targetSpeed = requiredSpeed;
}
void handleAcceleration()
{
setSpeed(targetSpeed);
unsigned long now = millis();
if(now - lastSpeedUpdateTime > SPEED_UPDATE_INTERVAL)
{
// time to check the motor speed again
if(currentSpeed < targetSpeed)
{
// accelerating
int incr = (targetSpeed - currentSpeed) / 10;
if(incr == 0)
incr = 1;
currentSpeed += incr;
servoThrottle.write(currentSpeed);
}
else if(currentSpeed > targetSpeed)
{
// deccelerating
int incr = (currentSpeed - targetSpeed) / 10;
if(incr == 0)
incr = 1;
currentSpeed -= incr;
servoThrottle.write(currentSpeed);
}
else
{
// motor is already at the target speed - no action required
}
lastSpeedUpdateTime = now;
}
}
void leftSteer()
{
const int targetPos = 1500;
const int Step = 10;
int currentLPos = valAxisX;
currentLPos = map(currentLPos, 0, 640, 1000, 2000);
int newPos = currentLPos + Step;
if((currentLPos < targetPos) && (newPos > targetPos))
{
newPos = targetPos;
}
servoSteering.write(newPos);
}
void rightSteer()
{
const int targetPos = 1500;
const int Step = 10;
int currentRPos = valAxisX;
currentRPos = map(currentRPos, 0, 640, 1000, 2000);
int newPos = currentRPos - Step;
if((currentRPos > targetPos) && (newPos < targetPos))
{
newPos = targetPos;
}
servoSteering.write(newPos);
}
void pingCalc()
{
int range [N_PINGS];
for (int i = 0; i < N_PINGS; ++i) {
range [i] = ping(pingPins [i]);
//delay(5);
pingRange = range[i];
}
}
long ping(int pin)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(2);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
return (microsecondsToInches(pulseIn(pin, HIGH)));
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
void handleCommand()
{
if (!commandReceived) return; // no command to handle
// variables to hold the command id and the command data
int data[17]; // up to three integers of data
char cmd[command.length()+1];
command.toCharArray(cmd, command.length()+1);
char *token = strtok(cmd, ",");
// data
for (int i = 0; i < 17; i++)
{
if (token)
{
data[i] = atoi(token);
token = strtok(NULL, ",");
valAxisX = data[i];
}
}
// flag that we've handled the command
commandReceived = false;
}
void stepperWaiting()
{
while (stepper.distanceToGo() != 0)
{
stepper.run();
}
Serial.println(stepper.currentPosition());
delay(2000);
}
void stepperMove()
{
stepper.moveTo(40);
stepperWaiting();
stepper.moveTo(0);
stepperWaiting();
stepper.move(-40);
stepperWaiting();
stepper.moveTo(0);
stepperWaiting();
}