Tuesday, October 1, 2013

Basic Movements (DC Motors)


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 1 hour was spent wiring, coding, and debugging. In the gifs I neglected to do the slow forward and slow backwards movements.



Forwards, Backwards, Left Turn


Left Turn, Right Turn, Right Veer, Left Veer


Wiring:

In this we can see that both motors are connected to ground (via the black leads) and to the arduino (via the blue leads).


In this we can see that PWM ports 2 and 7 are occupied by the leads from/to the left and right motors, respectively,.


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 BaneBot Motor Controllers
   Inputs:           1 Flip Switch
   Power Supply:     12 Volts DC
*/

#include <Servo.h>

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

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

/* initializes all necessary inputs for movement */
int leftMotor = 2;  //left motor is in port 2
int rightMotor = 7; // right motor is in port 7

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

/* stops robot's movement */
void motorStop()
{
  left.writeMicroseconds(1500);  //writes pulse (neutral / stopped) for 1.5 ms
  right.writeMicroseconds(1500); //writes pulse (neutral / stopped) for 1.5 ms
  delay(2000);
}

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

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

/* moves robot slow forward */
void slowForward()
{
  left.writeMicroseconds(1600);
  right.writeMicroseconds(1600);
  delay(2000);
  motorStop();
}

/* moves robot slow backward */
void slowBackward()
{
  left.writeMicroseconds(1400);
  right.writeMicroseconds(1450);
  delay(2000);
  motorStop();
}

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

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

/* veers robot left */
void leftVeer()
{
  left.writeMicroseconds(1550);   //writes a very low positive pulse for 1.55 ms
  right.writeMicroseconds(2000);  //writes pulse (full positive rotation) for 2 ms
  delay(1000);
  motorStop();
}

/* veers robot right */
void rightVeer()
{
  left.writeMicroseconds(2000);   //writes pulse (full positive rotation) for 2 ms
  right.writeMicroseconds(1600);  //writes a very low positive pulse for 1.55 ms
  delay(1000);
  motorStop();
}

/* ================= SETUP STEP ======================*/
void setup()
{
  left.attach(leftMotor);
  right.attach(rightMotor);
  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 pressing issues were found during this project's completion

No comments:

Post a Comment