Tuesday, October 8, 2013

Basic Movements DC Motors (Sabertooth and Parallax motor controllers)

Tasked to build a robot, using a Mega 2560 Arduino board, that went forwards, backwards, turned left, turned right, and stopped utilizing dc motors. I came up with the specimen in the gifs below. A total of about 3 hours was spent wiring, coding, and debugging.



Parallax
Forwards, Forward Slow, Backwards, Backward Slow


Parallax
Left Turn, Right Turn, Left Veer, Right Veer


Sabertooth
Forwards, Forward Slow, Backwards, Backward Slow, Left Turn


Sabertooth
Backwards, Backward Slow, Left Turn, Right Turn, Left Veer, Right Veer


Parallax LCD Display
Shows writeMicroseconds values for each movement


Wiring:

For more information on the motor ports please see the previous blog entry.


Parallax
This photo shows the correct wiring scheme (that I found) that works with this setup.


Parallax
This photo shows the wiring scheme from the Parallax Motor Controllers to the breadboard and to the Arduino including the Parallax LCD Display (topmost set of connections)


Sabertooth
This photo shows the correct wiring scheme (that I found) for the Sabertooth motor controller to the Arduino and breadboard.


Sabertooth
This photo shows the correct wiring scheme (that I found) for the Sabertooth motor controller to the motors and power supply (12 Volt Battery).


Sabertooth
Here is another view of the breadboard and Arduino connections.

Code:

/* In this program the robot will demonstrate basic movement utilizing DC motors. This program is designed
   to make to robot move forwards, backwards, turn left, turn right, veer left, veer right, and stop.
   
   Program written by Korbin Barker on 9/27/13
   
   Robotic Platform: Undetermined
   Microcontroller:  Arduino MEGA 2560
   Motors:           4 DC Motors
                        2 Parallax Motor Controllers
                        1 Sabertooth Motor Controller
   Inputs:           1 Flip Switch
   Power Supply:     12 Volts DC
*/

#include <Servo.h>
#include <SoftwareSerial.h>

/* ================= INITIALIZATION ===================== */

/* initializes the servos */
Servo left;
Servo right;

/* initializes all necessary inputs for movement */
const int leftMotor = 2;  //left motor is in port 2
const int rightMotor = 7; // right motor is in port 7
const int LCDDisplay = 14; //LCD Display is in TX 3 (port 14)

const int stopValue = 1500; //neutral value
const int forwardValue = 2000; //full forward
const int forwardSlow = 1600; //slow forward
const int backwardValue = 1000; //full backward
const int backwardSlow =  1300; //slow backward
int leftValue = 0; //intializes left value for LCD Display
int rightValue = 0; //initializes right value for LCD Display

SoftwareSerial mySerial = SoftwareSerial(255, LCDDisplay);

/* =================== FUNCTION DECLARATION ==================== */

/* clears LCD Display */
void clearScreen()
{
  mySerial.write(12);  //clears screen
  mySerial.write(17);  //turns backlight on
  delay(5);
}

void writeDisplay()
{
  mySerial.print("Left Value:");
  mySerial.print(leftValue);
  mySerial.write(13);
  mySerial.write(17);
  mySerial.print("Right Value:");
  mySerial.print(rightValue);
  delay(5);
}

/* stops robot's movement */
void motorStop()
{
  rightValue = leftValue = stopValue; //sets values to be printed on LCD Display
  left.writeMicroseconds(stopValue);  //writes pulse (neutral / stopped) for 1.5 ms
  right.writeMicroseconds(stopValue); //writes pulse (neutral / stopped) for 1.5 ms
  writeDisplay();
  delay(2000);
}

/* moves robot forward */
void motorForward()
{
  rightValue = leftValue = forwardValue;
  left.writeMicroseconds(forwardValue);   //writes pulse (full positive rotation) for 2 ms
  right.writeMicroseconds(forwardValue);  //writes pulse (full positive rotation) for 2 ms
  writeDisplay();
  delay(2000);
  motorStop();
}

/* moves robot backward */
void motorBackward()
{
  rightValue = leftValue = backwardValue;
  left.writeMicroseconds(backwardValue);   //writes pulse (full negative rotation) for 1 ms
  right.writeMicroseconds(backwardValue);  //writes pulse (full negative rotation) for 1 ms
  writeDisplay();
  delay(2000);
  motorStop();
}

/* moves robot slow forward */
void slowForward()
{
  rightValue = leftValue = forwardSlow;
  left.writeMicroseconds(forwardSlow);
  right.writeMicroseconds(forwardSlow);
  writeDisplay();
  delay(2000);
  motorStop();
}

/* moves robot slow backward */
void slowBackward()
{
  rightValue = leftValue = backwardSlow;
  left.writeMicroseconds(backwardSlow);
  right.writeMicroseconds(backwardSlow);
  writeDisplay();
  delay(2000);
  motorStop();
}

/* turns robot left */
void rightTurn()
{
  leftValue = forwardValue;
  rightValue = backwardValue;
  left.writeMicroseconds(forwardValue);   //writes pulse (full positive rotation) for 2 ms
  right.writeMicroseconds(backwardValue);  //writes pulse (full negative rotation) for 1 ms
  writeDisplay();
  delay(1000);
  motorStop();
}

/* turns robot right */
void leftTurn()
{
  leftValue = backwardValue;
  rightValue = forwardValue;
  left.writeMicroseconds(backwardValue);   //writes pulse (full negative rotation) for 1 ms
  right.writeMicroseconds(forwardValue);  //writes pulse (full positive rotation) for 2 ms
  writeDisplay();
  delay(1000);
  motorStop();
}

/* veers robot left */
void leftVeer()
{
  leftValue = forwardSlow;
  rightValue = forwardValue;
  left.writeMicroseconds(forwardSlow);   //writes a very low positive pulse for 1.55 ms
  right.writeMicroseconds(forwardValue);  //writes pulse (full positive rotation) for 2 ms
  writeDisplay();
  delay(1000);
  motorStop();
}

/* veers robot right */
void rightVeer()
{
  leftValue = forwardValue;
  rightValue = forwardSlow;
  left.writeMicroseconds(forwardValue);   //writes pulse (full positive rotation) for 2 ms
  right.writeMicroseconds(forwardSlow);  //writes a very low positive pulse for 1.55 ms
  writeDisplay();
  delay(1000);
  motorStop();
}

/* ================= SETUP STEP ======================*/
void setup()
{
  left.attach(leftMotor);
  right.attach(rightMotor);
  pinMode(LCDDisplay, OUTPUT);
  digitalWrite(LCDDisplay, HIGH);
  mySerial.begin(9600);
  delay(100);
  Serial.begin(9600);  
}

/* ================ LOOP STEP =======================*/
void loop()
{
  /* see comments under the FUNCTION DECLARATION to see what each of the functions does */
  motorStop();
  motorForward();
  slowForward();
  motorBackward();
  slowBackward();
  leftTurn();
  rightTurn();
  leftVeer();
  rightVeer();  
}


Issues:
No significant issues arose from this assignment that couldn't easily be remedied. One of my original Parallax controllers didn't work so I borrowed one from a friend.

No comments:

Post a Comment