Jump to content


Photo

Trouble with Robotics kit

kit robotics funduino how to make your first robot

  • Please log in to reply
82 replies to this topic

#61 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 30 March 2014 - 12:13 AM

That actually makes a lot of sense. It would have to block so that other sensors and stuff wouldn't interfere with the data from the ping. I'll fix that and give it a go, hopefully it works! :D


  • 0

#62 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 31 March 2014 - 09:13 PM

So, I changed the code to what you said (I believe anyways) and this is it currently:

// Version 4.0.3
// Major:      4 - An entire re-write of the code. Much is the same,
//                  but written so that the triple set of sensors are used
// Minor:      0 - Minor change is the addition of side sensors
// Micro:      3 - Micro change is change in detection of sides

#include <Servo.h>

// define the states
#define DRIVE_FORWARD  0
#define TURN_LEFT      1
#define TURN_RIGHT     2
#define SCAN           3

// create servo objects
Servo leftMotor;
Servo rightMotor;

int state = DRIVE_FORWARD;

const int serialPeriod = 100;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 100;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;

const int ultrasonic1TrigPin = 10;
const int ultrasonic1EchoPin = 11;

const int ultrasonic3TrigPin = 12;
const int ultrasonic3EchoPin = 13;

int ultrasonic2Distance;
int ultrasonic2Duration;

int ultrasonic1Distance;
int ultrasonic1Duration;
int ultrasonic3Distance;
int ultrasonic3Duration;

void setup()
{
    Serial.begin(9600);
  
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(ultrasonic2EchoPin, INPUT);
    
    pinMode(ultrasonic1TrigPin, OUTPUT);
    pinMode(ultrasonic1EchoPin, INPUT);
    
    pinMode(ultrasonic3TrigPin, OUTPUT);
    pinMode(ultrasonic3EchoPin, INPUT);
    
    leftMotor.attach(6);
    rightMotor.attach(7);
}


void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay >= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distance
        timeLoopDelay = millis();
    }
     
    stateMachine();
    
}


void readUltrasonicSensors()
{
    // Left Sensor
    digitalWrite(ultrasonic1TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic1TrigPin, LOW);
    ultrasonic1Duration = pulseIn(ultrasonic1EchoPin, HIGH);
    ultrasonic1Distance = (ultrasonic1Duration/2)/29;
    
    // Frontal Sensor            
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
    
    // Right Sensor
    digitalWrite(ultrasonic3TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic3TrigPin, LOW);
    ultrasonic3Duration = pulseIn(ultrasonic3EchoPin, HIGH);
    ultrasonic3Distance = (ultrasonic3Duration/2)/29;
}

void stateMachine()
{    
    if(state == DRIVE_FORWARD) // no obstacles detected
    {
        
        if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // if there's nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
        {
            // drive forward
//            rightMotor.write(180);
//            leftMotor.write(0);
            Serial.println("Driving Forward");
            
        }
        else // there's an object in front of us
        {
          state = SCAN;
      }
    }
    else if(state == TURN_LEFT) // obstacle detected -- turn left
    {
        unsigned long timeToTurnLeft = 1100; // it takes around 1.1 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurnLeft) // stay in this loop until timeToTurnLeft (1.1 seconds) has elapsed
        {
            // turn left
            rightMotor.write(180);
            leftMotor.write(180);
        }
        
        state = DRIVE_FORWARD;
    }
    else if(state == TURN_RIGHT) // obstacle detected -- turn right
    {
        unsigned long timeToTurnRight = 1100; // it takes around 1.1 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurnRight) // stay in this loop until timeToTurnRight (1.1 seconds) has elapsed
        {
            // turn left
            rightMotor.write(0);
            leftMotor.write(0);
        }
        
        state = DRIVE_FORWARD;
    }
    else if(state == SCAN) // obstacle detected -- start scanning
    {
       
        Serial.print("ultrasonic1Distance: ");
        Serial.print(ultrasonic1Distance);
        Serial.println();
  
        Serial.print("ultrasonic3Distance: ");
        Serial.print(ultrasonic3Distance);
        Serial.println();
  
        if(ultrasonic1Distance < ultrasonic3Distance)
        {
          
          Serial.println("Turning Right");
        }
        
        else if(ultrasonic1Distance > ultrasonic3Distance)
        {
          //state = TURN_LEFT;
          Serial.println("Turning Left");
        }
        state = DRIVE_FORWARD;
      
    }

}

void debugOutput()
{
    
    if((millis() - timeSerialDelay) > serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm: ");
        Serial.println();
        
        timeSerialDelay = millis();
    }
}

Except, its printing the debug messages to the console WAY too fast, so they are useless. What should I change so I can actually get debug info? Beyond that, I can't say if anything has changed because I can't read the debug info :P


  • 0

#63 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 31 March 2014 - 09:18 PM

This line controls how fast the debug output is printed to the serial monitor:

    if((millis() - timeSerialDelay) > serialPeriod)

If the amount of time that has elapsed since the last time stuff was printed out is greater than serialPeriod, then the IF statement becomes true and the stuff gets printed out again. Looking at the top of your program, it looks like you changed serialPeriod from 250 to 100. If you change it back to 250, then the debug output will print out four times a second instead of ten times a second (which I agree would be pretty hard to read!).

 

Are the ultrasonic sensors working now?


  • 0

#64 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 31 March 2014 - 09:39 PM

I was messing about with those variables, guess I forgot to change it back :P

 

Anyways, it is more readable, but it changes once the robot sees something in front the first time. From what I can see, it looks like it may be reading correctly, but, like I said, I can't totally read it. I'm thinking we're close, but not having the debug at a readable pace is really killing it :P I don't see why this code spits it out so much faster than the tutorial, no changes one...


  • 0

#65 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 31 March 2014 - 09:50 PM

I just noticed you have this:

        Serial.print("ultrasonic1Distance: ");
        Serial.print(ultrasonic1Distance);
        Serial.println();
  
        Serial.print("ultrasonic3Distance: ");
        Serial.print(ultrasonic3Distance);
        Serial.println();

Inside of stateMachine(). Put that in debugOutput() and change serialPeriod to 250 and it should print out at a reasonable rate. You still have a few other println() statements inside stateMachine(), though, so those will continue to print out fast (unless you devise a way to make them print out more slowly).


  • 0

#66 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 01 April 2014 - 12:11 AM

I think it is working! Side detectors do seem to be judging range, though I'm not sure if it is entirely correct, I've mentioned that a little bit later. The distance comparison is working fine and I believe everything else is a go!

 

One side note, the side detectors never seem to get above 4cm....do you have any clue why that is? If we can nail down this final thing, I think I might have a complete robot, and just in time too :)


  • 0

#67 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 01 April 2014 - 01:06 AM

Great, glad to hear you got it (mostly :P) working!

 

So.. neither of the side sensors reports over 4cm? The first thing I would do is try swapping one of them out with the front sensor to make sure it's not a problem with the sensor (probably isn't, but it would be a good idea to rule it out first).


  • 0

#68 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 07 April 2014 - 10:22 PM

Sorry about the late reply. I was excessively busy and then apparently I decided to get sick with a real bugger of a cold/cough. Lovely stuff that :P Anyways, tried what you said with swapping out the sensors, no change.

 

However, I did have one of my own theories, and it was that the wheels were getting in the way. So, I removed the wheels and what-da-ya-know :D it was the wheels. So, all I have to do is raise the sensors up a tad and then add in the code for the servos and hopefully (I said this a bajillion times already) it will be a working robot :)


  • 0

#69 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 07 April 2014 - 10:25 PM

That would make sense! I probably should mention in the tutorial that the ultrasonic sensors emit a somewhat wide "beam.."


  • 0

#70 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 08 April 2014 - 12:40 AM

Yes, they scatter once emitted, so they have to be right on the edge of the robot with like, nothing, in front of them. That's where my issue is coming from actually. The sensors are too far back from the wheels, and so they are bouncing off the wheels and then returning pre-maturely. :P

 

Methinks I shall be purchasing a few cables sometime soon because adding height under the breadboard does nothing :P


  • 0

#71 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 09 April 2014 - 09:07 PM

Huzzah! I do believe we have almost conquered this beast! I raised the sensors up again and brought the servos in closer to the base of the robot and if I make sure the sensors aren't pointing down too far, it works!

 

I added in the code for the servos and it seems to be working.  However, I seem to have run into a bit of an issue. 1100 seconds is making the robot do 180s :P Kind of an issue, when it should only be going 90 degrees. I'm guessing that this is because I just replaced the batteries, it has happened before. I'll try messing with the code, but otherwise, I think it's done (at least the basic version) :D

 

Next up, the testing :)


  • 0

#72 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 09 April 2014 - 09:49 PM

Great job!

 

Yeah, turning the robot for a set amount of time will result in different angles depending on the charge of the batteries. To get it to turn exactly 90 degrees every time, some sort of sensor would be needed (such as wheel encoders or a gyroscope).


  • 0

#73 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 10 April 2014 - 01:39 AM

And I would like to try out gyroscopes, just I don't think I want to risk messing up the entire project this close to the fair :P I will definitely look into it as a side project though :D Also, wheel encoders? Haven't heard of those, they sound interesting. Do you have any links to tutorials regarding them?

 

Thanks :D


  • 0

#74 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 10 April 2014 - 03:10 PM

And I would like to try out gyroscopes, just I don't think I want to risk messing up the entire project this close to the fair :P I will definitely look into it as a side project though :D Also, wheel encoders? Haven't heard of those, they sound interesting. Do you have any links to tutorials regarding them?

 

Thanks :D

 

I don't have any tutorials on encoders yet, and I don't have any in the store right now either.. I should try to do that.. In the meantime, if you're interested in learning about them, you can look up "rotary encoder."


  • 0

#75 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 11 April 2014 - 01:23 AM

Will do, except the le bugs have struck again. Servo issue this time. Don't worry, sensors are working just fine :P but when I tell the servos to turn left, nuhuh.

 

Here's the code for it, that isn't working. It's an exact duplicate (aside from the servo values) of the turn right if statement, which works just dandy. But does turning left, no sir.

 else if(state == TURN_RIGHT) // obstacle detected -- turn right
    {
        Serial.println("Turning Right");
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurn90) // stay in this loop until timeToTurnRight (1.1 seconds) has elapsed
        {
            // turn left
            rightMotor.write(0);
            leftMotor.write(0);
        }
        
        state = DRIVE_FORWARD;
    }

Data is coming through from the sensors right, timing's as good as it has been, comparison is going just fine. But ask the robot to turn left, and it decides to just go very slowly for the set time, and then keep going forward. I know it's not a power issue, as it turns right and drives forward just fine. Funny thing is, this just started, but like a minute before, it was fine.

 

I'm like ... 48402294.jpg

 

Ugh, and it was all going so well.

 

:ph34r: <-- We need one of them to come in and ninja-scare the bot so it works right


  • 0

#76 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 11 April 2014 - 01:52 AM

Ha ha, nice meme..

 

Do you have a println() statement inside of the turn left state to see if the program is going into that state as it should?


  • 0

#77 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 11 April 2014 - 01:58 PM

Your welcome. I tend to get nutty when upset.

 

As for the println() statement, no, but I can try that. I think it is, because the robot does sort of try to drift toward the left, but then it corrects back.

 

Sigh :P

 

one-does-not-simply-meme-generator-one-d


  • 0

#78 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 11 April 2014 - 02:14 PM

The key to debugging is tracing the execution of the program, which in the case of Arduino means putting println() statements in key places. So try using println() statements to follow the execution of the program to make sure it's actually going into the TURN_LEFT state.


  • 0

#79 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 06 May 2014 - 04:13 PM

I heard you won first place with your science fair project! Congrats!!


  • 0

#80 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 05 July 2014 - 05:06 PM

Yeah! I've been really busy and totally forgot about this thread until a couple of days ago. I won first place (really surprised I have to say) even though I fried the servo the day of the fair. :P

 

I had to change the design (again) because it was too big (changed it to your new design) and managed to short-circuit wire the servo :blink:. My judge was actually a lawyer, so that was intense, but I think the reason I won was because I understood almost everything about my project/the technology behind it.

 

I'm going to be entering the project in the County Fair as one of my 4H projects and I've got to order a new servo. Thanks for all your help!


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users