Show Posts
Pages: [1] 2
1  Community / Exhibition / Gallery / SRF01 Ultrasonic Range Finder Working Code on: November 09, 2012, 11:58:43 am
I searched the internet and the forums trying to find code that worked for this range finder.  I only found a couple of examples and neither worked.  So a friend and myself set down and figured it out.  We used the old code as our template and modified it. 

All you have to do to make this work with your device is hook to 5v, ground, and digital pin 10.

Enjoy.

Code:
// Code modified and developed by Will Myers and Justin Miller

#include <SoftwareSerial.h>

#define txrxPin 10                                           // Defines Pin 10 to be used as both rx and tx for the SRF01
#define srfAddress 0x01                                      // Address of the SFR01
#define getSoft 0x5D                                         // Byte to tell SRF01 we wish to read software version
#define getRange 0x53                                        // Byte used to get range from SRF01 in inches
#define getStatus 0x5F                                       // Byte used to get the status of the transducer

SoftwareSerial UltrasonicBus(txrxPin, txrxPin);              // Sets up the SoftwareSerial with Digital Pin 10 and sets the name to "UltrasonicBus"

void setup(){
  Serial.begin(19200);
 
  UltrasonicBus.begin(9600);                                   
  delay(200);                                                // Waits some time to make sure that SRF01 is powered up
  Serial.println("SRF01 Test");                           
  SRF01_Cmd(srfAddress, getSoft);                            // Calls a function to get the SRF01 software version
  while (UltrasonicBus.available() < 1);                     // Waits to get good data
  int softVer = UltrasonicBus.read();                        // Read software version from SRF01
  Serial.print("V:");                                       
  Serial.println(softVer);                                   // Prints the software version to LCD03
  delay(200);
 
  Serial.println("Initialization Complete");                 // After inititalization is complete you should see the correct version of your device printed to the serial monitor
}


void loop(){
 
  int max = 1;                                               // Setup so that you can averaqe readings
  int sum = 0;                                               // Currently I am not taking an average.. Max = 1
  for (int count = 0; count < max; count++) {
    sum = sum + doRange();                                   // Calls a function to get range from SRF01 and sums the ranges up so that you can take an avg
  }
 
  int range = sum / max;
 
  Serial.print("Range avg = ");                               
  Serial.print(range);                                       // Print range result to the screen
  Serial.println("  ");                                      // Print some spaces to the screen to make sure space direcly after the result is clear
  checkLock();                                               // Calls a function to check if the transducer is locked or unlocked
}


void SRF01_Cmd(byte Address, byte cmd){                      // Function to send commands to the SRF01
  pinMode(txrxPin, OUTPUT);                                  // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
  digitalWrite(txrxPin, LOW);                             
  delay(2);                                               
  digitalWrite(txrxPin, HIGH);                           
  delay(1);                                               
  UltrasonicBus.write(Address);                              // Send the address of the SRF01
  UltrasonicBus.write(cmd);                                  // Send commnd byte to SRF01
  pinMode(txrxPin, INPUT);                                   // Make input ready for Rx
  int availableJunk = UltrasonicBus.available();             // Filter out the junk data
  for(int x = 0; x < availableJunk; x++){
    byte junk = UltrasonicBus.read();
  }
}



void checkLock() {
  SRF01_Cmd(srfAddress, getStatus);                          // Call to a function that checks if the trancducer is locked or unlocked
  byte statusByte = UltrasonicBus.read();                    // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
  int status = statusByte & 0x01;                            // Get status of lease significan bit
  if(status == 0)
  {                                     
    Serial.println("Unlocked");                              // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
  }
   else
  {                                     
    Serial.println("Locked   ");                             // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in
  }
}

int doRange() {
  SRF01_Cmd(srfAddress, getRange);                           // Calls a function to get range from SRF01
  while (UltrasonicBus.available() < 2);                     // Waits to get good data
  byte highByte = UltrasonicBus.read();                      // Get high byte
  byte lowByte = UltrasonicBus.read();                       // Get low byte
  int dist = ((highByte<<8)+lowByte);                        // Put them together
  return dist;
}
2  Using Arduino / Installation & Troubleshooting / Unable to install drivers on: May 06, 2012, 04:19:17 pm
Well I decided to reformat my computer to fix the issues with arduino loading.  It now loads but I am unable to use it because I cant sync the board to the comp.  I try to install the drivers from my arduino file but it wont work.  Im running windows vista 64 bit.  Any ideas?   Oh and im using and arduino mega 2560 rev3.  I have used this board on other computers just fine. 
3  Using Arduino / Installation & Troubleshooting / Re: Problem Opening Arduino on: May 05, 2012, 07:13:33 pm
I have still not been able to use arduino on my laptop.  Anyone else have any ideas?
4  Topics / Robotics / Re: Balancing robot for dummies on: April 03, 2012, 06:39:27 pm
So I am really struggling to pick out the right motor.  I really want to stick with my motor shield but it has a max of 2 amps.  Most motors that I am finding have low free running current but the stall current exceeds my motor shield.  Do these type of bots push the motors to their stall current?  I do not see my design pushing the motor anywhere close to its stall current?
5  Topics / Robotics / Re: Balancing robot for dummies on: April 02, 2012, 10:52:25 pm
What do you guys think of this motor and encoder setup??

 http://www.robotshop.com/cytron-12v-430rpm-15-57oz-gear-motor-encoder-2.html
6  Topics / Robotics / Re: Balancing robot for dummies on: March 30, 2012, 10:48:50 pm
I ordered the 6dof IMU  board from sparkfun tonight!
7  Topics / Robotics / Re: Balancing robot for dummies on: March 29, 2012, 07:06:31 pm
Yeah I guess that would be true because of the moment of inertia.  So ill move the battery toward the top and mount the IMU close to it.
8  Topics / Robotics / Re: Balancing robot for dummies on: March 28, 2012, 10:19:58 pm
Sorry here is the link http://www.sparkfun.com/products/10121

I feel pretty comfortable with this IMU.  I have been reading a lot about it and have already found several examples of code to get the data that I need.  I have been using arduino for about a year now so I feel pretty confident with my ability to get the board working.  I am not as familiar with I^2C  as I should be but I am reading and working on it now. 

My main concern is the motors.  I know that selecting the proper motors and wheels will make or break the project.  Ultimately I am thinking I need something with a lot of torque that can respond quickly.  I don't think I need high rpm, but it must spin fast enough to catch up with itself when falling. 

Possibly this type of motor with the encoders.  http://www.robotshop.com/12v-gear-motor-encoder.html

Also correct me if I am wrong, but I am thinking something around a foot to foot and a half tall with the center of mass being around the motors.  From what I am reading the IMU needs to be near the top?
9  Topics / Robotics / Re: Balancing robot for dummies on: March 26, 2012, 07:49:02 pm
Also what do you guys think of this IMU.  The goal is to use this on a balancing robot first and if I have success I will move onto a quad copter.
10  Topics / Robotics / Re: Balancing robot for dummies on: March 26, 2012, 07:37:47 pm
Hey guys I am thinking about trying to build one of these self balancing robots.  I have an arduino r3 motor shield.  Im curious if I can find good enough motors that I can control with my shield and not push the 2 amp limit.
11  Using Arduino / Motors, Mechanics, and Power / Re: Arduino Motorshield R3 Help on: March 25, 2012, 05:07:27 pm
The R3 motorshield is very easy to use.  If you go to the main site and look up the board it will tell you exactly which pins you need to use to control the board.  Also you can go to sparkfun and look up their ardumoto.  They have some sample code that would be a good starting point.
12  Using Arduino / Installation & Troubleshooting / Re: Arduino Mega 2560 REV3 on: March 25, 2012, 05:04:27 pm
I took the board back to radioshack.  They opened up another box and it had the real rev 3 in it.  They swapped the board with the one i purchased yesterday.  Crazy thing is I watched the lady put the old box with the wrong chip right back up on the shelf.  Pathetic...
13  Using Arduino / Installation & Troubleshooting / Re: Arduino Mega 2560 REV3 on: March 25, 2012, 09:00:24 am
Thats what I figured.  Well im definitely going to take it back.  I guess I will order one.
14  Using Arduino / Installation & Troubleshooting / Re: Problem Opening Arduino on: March 25, 2012, 08:59:35 am
That is ok I appreciate all of your help.  Yeah once i ran that command the command line never went back to normal it was like it was stuck.  I had to close it out to be able to use it again.   Something seriously bugged it out.
15  Using Arduino / Installation & Troubleshooting / Re: Problem Opening Arduino on: March 25, 2012, 08:30:22 am
I did have  bluetooth on so I disabled anything I could find bluetooth related and unfortunately I had the same problem as  before.
Pages: [1] 2