Monday, September 16, 2013

Moving around a room

This assignment was to teach us about the Parallax Ultrasonic Sensor and how to use it to navigate. We also used a positional servo motor to spin the ultrasonic sensor so the robot is able to sweep its surroundings for objects and then move in a clear path. I was not able to make a video of it moving because of some external factors but the robot does do all of the necessary movements and controls.


This assignment took me about 4 hours to complete. I unplugged the line followers so I didn't use too much power from them and so the wiring wasn't so cluttered. The ultrasonic sensor and additional servo were easily installed because they were exactly the same as the other servos. I only had to use four PWM slots for all of the devices so that also freed up my wires.




It was easier to program and troubleshoot by taking a couple of lines of code at a time. This let me isolate my problems and fine-tune my turns and sensing. The Serial.println also helped with troubleshooting so I knew what the robot was seeing. I was able to use some previous functions in this program but I had to come up with new code for the loop. This program showed me how important indenting and whitespace while programming is. (The indenting never looks the same on my blog.)  This is the code that I came up with after it was all working.

/*
Brian Swanson, Assignment 3
Using the Ultrasonic sensor to move about a room
Using two continuous servos and one positional
Using one ultrasonic sensor
*/
#include <SoftwareSerial.h>    //Including the necessary libraries
#include <Servo.h>
Servo rightServo;              //Defining the servos
Servo leftServo;
Servo pingServ;
long pingTime = 0;            //setting constants and variables
const int Ping = 6;
int pos = 180;
void setup()
{
  Serial.begin(115200);
  rightServo.attach(12);      //Sets the right servo to I/O 12
  leftServo.attach(13);       //Sets left servo to I/O 13
  pingServ.attach(5);         //Setsthe ping servo to I/O 5
}
void straight()                //Function for robot to go straight
{
  leftServo.write(pos);
  rightServo.write(-pos);
}
void leftTurn()                //Function for robot to go left
{
  leftServo.write(-pos);
  rightServo.write(-pos);
  delay(250);
}
void rightTurn()              //Function for robot to go right
{
  leftServo.write(pos);
  rightServo.write(pos);
  delay(250);
}
void halt()                   //Function for robot to stop
{
  leftServo.write(90);
  rightServo.write(90);
  delay(250);
}
void turnAround()            //Function to turn around
{
  leftServo.write(pos);
  rightServo.write(pos);
  delay(450);
}
void sensStraight()          //Function to turn the ultrasonic straight and then read a distance 
{
  pingServ.write(83);
  delay(500);
  sensDist();
}
void sensLeft()              //Function to turn the ultrasonic left and then read a distance
{
  pingServ.write(165);
  delay(500);
  sensDist();
}
void sensRight()            //Function to turn the ultrasonic right and then read a distance
{
  pingServ.write(0);
  delay(500);
  sensDist();
}
void sensDist()            //Function to read a distance from the ultrasonic
{
  pinMode(Ping, OUTPUT);
  digitalWrite(Ping, LOW);
  delayMicroseconds(2);
  digitalWrite(Ping, HIGH);
  delayMicroseconds(5);   
  digitalWrite(Ping, LOW);
  delayMicroseconds(2);
  pinMode(Ping, INPUT);
 
  pingTime = pulseIn(Ping, HIGH, 2800);
  if (pingTime == 0)
  {
    Serial.println("Nothing");
  }
  else
  {
    pingTime = pingTime/2/74;
    Serial.println(pingTime);
  }
  delay(10);
}
void loop ()
{ 
  sensStraight();
 
  if(pingTime > 7 || pingTime == 0)       //looks straight until object is 7 or less inches away
  {
    sensStraight();
    straight();
  }
  else                  //if straight is obstructed, looks left
  {
    halt();
   
    sensLeft();
   
    delay(100);
   
    if(pingTime > 7 || pingTime == 0)    //if left is clear, turn left and loop back to top
    {
      leftTurn();
      sensStraight();
      straight();
    }
    else          //if left is obstructed, look right
    {
      halt();
     
      sensRight();
     
      delay(500);
     
      if(pingTime > 7 || pingTime == 0)      //if right is clear, turn right and loop back to top
      {
         rightTurn();
         sensStraight();
         straight();
      }
      else          //if straight, left, and right are obstructed, turn around and loop back to top
      {
         turnAround();
      }
    }
  }
}

Monday, September 9, 2013

Following the Dark Side (Dark Side = Black Line)

Our objective in this assignment was to have our robot wait for a switch to be hit and then follow a black line. I forgot to include the part where I hit the switch but it works and this is the video of it following the  line.(There was a problem uploading the video so its on YouTube)

Line following

This took about 3-4 hours to complete and most of it was programming. The wiring was easy except for having to put power to white and information to red on the line followers. The wiring for the first program was simple because there weren't many wires but I can see where wire management could play a role in this one (I probably could have done better with mine).
 
 
 
I kept the two servos hooked up because we needed these to make the robot move. I used an additional five PWM's for the line followers and one for the switch. I also had to use a 10kohm resistor for the switch.


 
 
The programming was again the hardest part. It took a while to learn how to use the RCTime function and how it works. After looking on the arduino website and having someone help me with it, I was able to understand how it all works and why it works. The logic was fairly easy because we did a very similar program in another robotics course. Being able to keep some of the same functions from the first program saved a little time. The Serial Monitor was helpful for debugging the program because I was able to look at exactly what each line follower was reading. This is what I came up with.
 
 
#include <Servo.h>              //adds servo library
Servo rightServo;
Servo leftServo;
int pos = 180;                 //Sets position to 180. Used to move motor forward or backwards 
int color =15;
int leftMost = 11;             //Sets variable corresponding to PWM slot
int rightMost = 10;
int innerRight = 9;
int innerLeft = 8;
int buttonSlot = 7;
int buttonValue = 0;
void straight()                //Function for robot to go straight
{
  leftServo.write(pos);
  rightServo.write(-pos);
}
void leftTurn()                //Function for robot to go left
{
  leftServo.write(-pos);
  rightServo.write(-pos);
}
void rightTurn()              //Function for robot to go right
{
  leftServo.write(pos);
  rightServo.write(pos);
}
void halt()                   //Function to stop robot
{
  rightServo.write(90);
  leftServo.write(90);
}
void setup()                 
{
  Serial.begin(115200);
  rightServo.attach(12);      //Sets the right servo to I/O 12
  leftServo.attach(13);       //Sets the left servo to I/O 13
}
long RCTime (int sensIN)      //This is used to read the line followers. Similar to charging and discharging a capacitor
{
  long result = 0;
  pinMode(sensIN, OUTPUT);
  digitalWrite(sensIN, HIGH);
  delay(1);
 
  pinMode (sensIN, INPUT);
  digitalWrite(sensIN, LOW);
  while (digitalRead(sensIN))
  {
    result++;
  }
  return result;
}
void loop()
{
  halt();
 
  buttonValue = digitalRead(buttonSlot);      //Reads the value of the button

  while (buttonValue == 1)                    //Doesn't start until the button is on
  {
    delay(1000);
   
    while(true)                               //Loops forever
    {
      Serial.print("leftMost");          //Prints the values of all four line followers for easy troubleshooting
      Serial.println(RCTime(leftMost));
     
      Serial.print("rightMost");
      Serial.println(RCTime(rightMost));
     
      Serial.print("innerLeft");
      Serial.println(RCTime(innerLeft));
     
      Serial.print("innerRight");
      Serial.println(RCTime(innerRight));
     
      Serial.println();
     
      delay(10);
     
      if(RCTime(innerRight) > color || RCTime(innerLeft) > color && (RCTime(leftMost) < color && RCTime(rightMost) < color))     //if the robot is moving anong like it should
      {
        straight();
      }
     
      if((RCTime(innerLeft) < color && RCTime(innerRight) < color) && (RCTime(leftMost) > color))        //if the robot goes too far right
      {
        leftTurn();
      }
     
      if((RCTime(innerLeft) < color && RCTime(innerRight) < color) && (RCTime(rightMost) > color))       //if the robot goes too far left
      {
        rightTurn();
      }     
    }
  }
}

Monday, September 2, 2013

Assignment 1: Moving and Blinking

Our objective of this assignment was to familiarize ourself with the Arduino Megaboard and its language. We had to make our robot move forward, backward, turn left and turn right. After it went through these motions we had to make an LED blink several times. This is my final product.
 
The assignment took about 4 hours to complete everything. The assembly was fairly easy but I did run into a few problems. I would have liked to have a little more room on top of the robot. Part of the breadboard is hanging over the edge and I had to tilt the Arduino to make everything fit but I got it all to work out.
 
The wiring was a little tough but it was one of the easiest parts of the whole project. Each servo had three wires going to it: ground, power, and information. After hooking up all the wires for the servos and testing them I still had to hook up an LED. I started by putting the negative end in ground. I used a 470 ohm resistor on the other end to limit the current. The other end of the resistor went to a PWM along with the wires from the servos. This is what it looked like.
 
 

 
The programming was by far the hardest part. The Arduino language is different from anything I've used before so I had to learn a few keywords. I spent a maybe an hour looking up keywords and watching tutorials on how to write programs in Arduino. I also had to fiddle with the screw on the servos before  could get the robot to move. I took the programming step by step and tried to make it do one motion at a time before adding another one. This was very helpful because I could see my programming errors and it was easier to debug. This is what my program looked like when it was complete.
 
#include <Servo.h>              //adds servo library
Servo rightServo;
Servo leftServo;
int pos = 180;                 //Sets position to 180. Used to move motor forward or backwards 
int led = 11;                  //LED is in I/O 11
void straight()                //Function for robot to go straight
{
  leftServo.write(pos);
  rightServo.write(-pos);
}
void leftTurn()                //Function for robot to go left
{
  leftServo.write(-pos);
  rightServo.write(-pos);
}
void rightTurn()              //Function for robot to go right
{
  leftServo.write(pos);
  rightServo.write(pos);
}
void reverse()                //Function for robot to go backwards
{
  leftServo.write(-pos);
  rightServo.write(pos);
}
void halt()                   //Function for robot to stop
{
  leftServo.write(90);
  rightServo.write(90);
}
void setup()                 
{
  rightServo.attach(12);      //Sets the right servo to I/O 12
  leftServo.attach(13);       //Sets left servo to I/O 13
  pinMode(led,OUTPUT);        //Stes LED to I/O 11
}
void loop()
{
  halt();
  delay(1000);
 
  straight();
  delay(2000);
 
  halt();
  delay(1000);
 
  reverse();
  delay(2000);
 
  halt();
  delay(1000);
 
  leftTurn();
  delay(2000);
 
  halt();
  delay(1000);
 
  rightTurn();
  delay(2000);
 
  halt();
  delay(1000);
 
  digitalWrite(led,HIGH);        //Lines 75 thru 91 blink LED on and off three times
  delay(250);
 
  digitalWrite(led,LOW);
  delay(250);
 
  digitalWrite(led,HIGH);
  delay(250);
 
  digitalWrite(led,LOW);
  delay(250);
 
  digitalWrite(led,HIGH);
  delay(250);
 
  digitalWrite(led,LOW);
  delay(250);
}