Best Robot Run

This is the robot’s best run. It uses a sonar sensor to measure the distance from the wall in front of it and optical encoders to measure the turn. Unfortunately the encoders were unable to give consistent readings for each turn.

The Source Code

#include “URMSerial.h”

// The measurement we’re taking
#define DISTANCE 1
#define TEMPERATURE 2
#define ERROR 3
#define NOTREADY 4
#define TIMEOUT 5

URMSerial urm;

const int enablePinL = 3; //H-Bridge Left Side
const int motorLeftPinA = 4;
const int motorLeftPinB = 5;

const int enablePinR = 9; //H-Bridge Right Side
const int motorRightPinA = 6;
const int motorRightPinB = 7;

const int encoderPinL = 2;
const int encoderPinR = 13;

volatile int ticks = 0;
volatile boolean spinState = false;

void encInterrupt()
{
ticks++;
}

void Stop()
{
detachInterrupt(0);

analogWrite(enablePinL, 0);
analogWrite(enablePinR, 0);
digitalWrite(motorLeftPinA, LOW);
digitalWrite(motorLeftPinB, LOW);
digitalWrite(motorRightPinA, LOW);
digitalWrite(motorRightPinB, LOW);
}

void Forward(int speed)
{
analogWrite(enablePinL, speed);
analogWrite(enablePinR, speed);
digitalWrite(motorLeftPinA, LOW);
digitalWrite(motorLeftPinB, HIGH);
digitalWrite(motorRightPinA, LOW);
digitalWrite(motorRightPinB, HIGH);
}

void Reverse(int speed)
{
analogWrite(enablePinL, speed);
analogWrite(enablePinR, speed);
digitalWrite(motorLeftPinA, HIGH);
digitalWrite(motorLeftPinB, LOW);
digitalWrite(motorRightPinA, HIGH);
digitalWrite(motorRightPinB, LOW);
}

void TurnRight(int speed)
{
analogWrite(enablePinL, speed);
analogWrite(enablePinR, speed);
digitalWrite(motorLeftPinA, LOW);
digitalWrite(motorLeftPinB, HIGH);
digitalWrite(motorRightPinA, HIGH);
digitalWrite(motorRightPinB, LOW);
}

void TurnLeft(int speed)
{
analogWrite(enablePinL, speed);
analogWrite(enablePinR, speed);
digitalWrite(motorLeftPinA, HIGH);
digitalWrite(motorLeftPinB, LOW);
digitalWrite(motorRightPinA, LOW);
digitalWrite(motorRightPinB, HIGH);
}

void setup()
{
Serial.begin(9600);                  // Sets the baud rate to 9600
urm.begin(11,12,9600);                 // RX Pin, TX Pin, Baud Rate
Serial.println(“URM37 Library by Miles Burton – Distance. Version 2.0″);   // Shameless plug

pinMode(encoderPinL, INPUT);
pinMode(enablePinL, OUTPUT);
pinMode(enablePinR, OUTPUT);
pinMode(motorLeftPinA, OUTPUT);
pinMode(motorLeftPinB, OUTPUT);
pinMode(motorRightPinA, OUTPUT);
pinMode(motorRightPinB, OUTPUT);

delay(2500);
}

void loop()
{
Serial.print(“Measurement: “);
Serial.println(getMeasurement(DISTANCE));  // Output measurement

Serial.print(“Ticks: “);
Serial.println(ticks);

if(!spinState)
{
Forward(200);

if(getMeasurement(DISTANCE) < 20)
{
Stop();
spinState = true;
attachInterrupt(0, encInterrupt, CHANGE);
}

}
else if(spinState)
{
//if(ticks < 20)
//{
TurnRight(255);
//}
if(ticks >= 33)
{
ticks = 0;
spinState = false;
detachInterrupt(0);
}
}

delay(100);
}

int value; // This value will be populated
int getMeasurement(int mode)
{
// Request a distance reading from the URM37
switch(urm.requestMeasurementOrTimeout(mode, value)) // Find out the type of request
{
case DISTANCE: // Double check the reading we recieve is of DISTANCE type
//    Serial.println(value); // Fetch the distance in centimeters from the URM37
return value;
break;
case TEMPERATURE:
return value;
break;
case ERROR:
Serial.println(“Error”);
break;
case NOTREADY:
Serial.println(“Not Ready”);
break;
case TIMEOUT:
Serial.println(“Timeout”);
break;
}

return -1;
}

Advertisements

Entry 9

Robot drives at reduced power, then switches to full power, rotates, then drives again. Source Code is below video.

 

Source Code:

//Ashani TEst program

const int motorRightaPin = 7;    // H-bridge motor+ 1 (pin 2, 1A)
const int motorRightbPin = 6;    // H-bridge motor- 1 (pin 7, 2A)
const int motorLeftaPin = 5;    // H-bridge motor+ 2 (pin 15, 1A)
const int motorLeftbPin = 4;    // H-bridge motor- 2 (pin 10, 2A)
const int enablePin = 3;  // H-bridge enable pin Left side of robot

//const int motor3aPin = 10;    // H-bridge motor+ 2 (pin 15, 1A)
//const int motor3bPin = 11;    // H-bridge motor- 2 (pin 10, 2A)

//const int motor4aPin = 13;    // H-bridge motor+ 2 (pin 15, 1A)
//const int motor4bPin = 12;    // H-bridge motor- 2 (pin 10, 2A)

const int enable2Pin = 9;    // H-bridge enable pin for right side of robot

const int ultraSoundPin = 8;

int time = 0;

void goForward(int speed)
{

analogWrite(enablePin, speed); //enable motors 50%duty cycle
analogWrite(enable2Pin, speed); //enable motors 50%duty cycle

digitalWrite(motorRightaPin, HIGH);
digitalWrite(motorRightbPin, LOW);

digitalWrite(motorLeftaPin, HIGH);
digitalWrite(motorLeftbPin, LOW);
}

void goTurnLeft()
{
analogWrite(enablePin, 255); //enable motors 50%duty cycle
analogWrite(enable2Pin, 255); //enable motors 50%duty cycle

digitalWrite(motorRightaPin, HIGH);
digitalWrite(motorRightbPin, LOW);

digitalWrite(motorLeftaPin, LOW);
digitalWrite(motorLeftbPin, HIGH);
}

void goTurnRight()
{
analogWrite(enablePin, 255); //enable motors 50%duty cycle
analogWrite(enable2Pin, 255); //enable motors 50%duty cycle

digitalWrite(motorRightaPin, LOW);
digitalWrite(motorRightbPin, HIGH);

digitalWrite(motorLeftaPin, HIGH);
digitalWrite(motorLeftbPin, LOW);
}

void goBackWard(int speed)
{

analogWrite(enablePin, speed); //enable motors 50%duty cycle
analogWrite(enable2Pin, speed); //enable motors 50%duty cycle

analogWrite(enablePin, 255); //enable motors 50%duty cycle
analogWrite(enable2Pin, 255); //enable motors 50%duty cycle
digitalWrite(motorRightaPin, LOW);
digitalWrite(motorRightbPin, HIGH);

digitalWrite(motorLeftaPin, LOW);
digitalWrite(motorLeftbPin, HIGH);
}

void setup() {

// beginSerial(9600);

// set all the other pins you’re using as outputs:
pinMode(motorRightaPin, OUTPUT);
pinMode(motorRightbPin, OUTPUT);
pinMode(motorLeftaPin, OUTPUT);
pinMode(motorLeftbPin, OUTPUT);

pinMode(enablePin, OUTPUT);
pinMode(enable2Pin, OUTPUT);

}

void loop()
{
time = millis();

if ( time < 10000)
{
goForward(127);
}
else if(time > 10000 && time < 20000)
{
goForward (255);
}

else if (time >20000 & time < 30000)
{
goTurnRight();
}

else if (time > 30000)
{
goBackWard(255);
}

delay(5000);

}


Entry 8

First successful attempt at getting the motors to work with the arduino and h-bridges. Source code is below video.

Source Code:

//Test Program written for Arduino Robot v1.0
//Marcorel

const int motor1aPin = 7;    // H-bridge motor+ 1 (pin 2, 1A)
const int motor1bPin = 6;    // H-bridge motor- 1 (pin 7, 2A)
const int motor2aPin = 5;    // H-bridge motor+ 2 (pin 15, 1A)
const int motor2bPin = 4;    // H-bridge motor- 2 (pin 10, 2A)
const int enablePin = 3;  // H-bridge enable pin

const int motor3aPin = 10;    // H-bridge motor+ 2 (pin 15, 1A)
const int motor3bPin = 11;    // H-bridge motor- 2 (pin 10, 2A)

const int motor4aPin = 13;    // H-bridge motor+ 2 (pin 15, 1A)
const int motor4bPin = 12;    // H-bridge motor- 2 (pin 10, 2A)

const int enable2Pin = 9;    // H-bridge enable pin

void setup() {

// set all the other pins you’re using as outputs:
pinMode(motor1aPin, OUTPUT);
pinMode(motor1bPin, OUTPUT);
pinMode(motor2aPin, OUTPUT);
pinMode(motor2bPin, OUTPUT);

pinMode(motor3aPin, OUTPUT);
pinMode(motor3bPin, OUTPUT);

pinMode(motor4aPin, OUTPUT);
pinMode(motor4bPin, OUTPUT);

pinMode(enablePin, OUTPUT);
pinMode(enable2Pin, OUTPUT);

}

void loop() {

analogWrite(enablePin, 150); //enable motors 50%duty cycle
analogWrite(enable2Pin, 150); //enable motors 50%duty cycle
//Back engines
digitalWrite(motor1aPin, HIGH);
digitalWrite(motor1bPin, LOW);

digitalWrite(motor2aPin, HIGH);
digitalWrite(motor2bPin, LOW);

// Front Engines
digitalWrite(motor3aPin, HIGH);
digitalWrite(motor3bPin, LOW);

digitalWrite(motor4aPin, HIGH);
digitalWrite(motor4bPin, LOW);
delay(300);

}


Entry 7

SN7544 10NE H-Bridge Schematic.

These are the H-bridges wired to the protoshield and connected to the arduino board using the above schematic.


Entry 6

Charging circuit picture and diagram.

 


Entry 5

Protoshield assembly.

Arudiuno protoshield tutorial:

http://www.arduino.cc/playground/Tutorials/Protoshield


Entry 4

The Arduino Uno, protoshield, and the breadboard with H-bridges (SN 7544 10NE) that we are using for this project.