RC Car color tracking

Added by Ilir Gruda over 7 years ago

Hi,

I know that there are some similar question in forum but I cant find them very helpful,
Earlier i have built a car which is with anti-crash system with Arduino Uno and IR distance sensor.
Now I am trying to track colors, but i have some question:
First of all can i use only arduino software with arduino library for programing or I need Serial Terminal,
Second do I need to use tilt and pan to move CMUcam on the car to find the color or I can do it by moving the car completely somehow,
Thirdly when the CMUcam get the values for tracking do I need to tell the car to move in which way (manually like with IR distance sensor) or CMUcam has some specific way to do that(tracking).

and last is the logic almost the same as in this code.

int enable12=7;
int input1=6;
int input2=5;
int enable34=4;
int input3=3;
int input4=2;
// variables used for button
int buttonPin=12;
int val = 0;
int oldVal = 0;
int state = 0;
void setup(){
  Serial.begin(9600);
  //Set up the necessary pins as outputs or inputs
  pinMode(enable12,OUTPUT);
  pinMode(input1,OUTPUT);
  pinMode(input2,OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(enable34,OUTPUT);
  pinMode(input3,OUTPUT);
  pinMode(input4,OUTPUT);
  //Enable pins value HIGH
  digitalWrite(enable12,HIGH);
  digitalWrite(enable34,HIGH);
}
void loop(){ 
  //button configuration
  /*val = digitalRead(buttonPin);
  if((val == HIGH) && (oldVal == LOW)){
    state = 1 - state; //change state
  }
  oldVal = val; // old value gat the new value 
  if (state == 1)
  {
    digitalWrite(enable12,HIGH); // enable energy to flow in first DC
    digitalWrite(enable34,HIGH); // enable energy to flow in second DC
  }
  else{
    digitalWrite(enable12,LOW); //disable energy to flow in first DC
    digitalWrite(enable34,LOW); //disable energy to flow in senond DC
  }*/
  int sensorValue=analogRead(A2); //read in the sensor value
  Serial.println(analogRead(A2)); //allow us to see the
  //delay(1); //small delay for stability
  if (sensorValue < 400){ // measure the distanc
    goBack(); // go Forward becouse wires are mixed
    stopTurning();
  }
  else{
    goForward(); // go Back becouse wires are mixed
    goRight();
  }
}
//car's movement
void goForward(){
  digitalWrite(input1,HIGH);
  digitalWrite(input2,LOW);
}
void goBack(){
  digitalWrite(input1,LOW);
  digitalWrite(input2,HIGH);
}
void stopMovement(){
  digitalWrite(input1,LOW);
  digitalWrite(input2,LOW);
}
void goLeft(){
  digitalWrite(input3,HIGH);
  digitalWrite(input4,LOW);
}
void goRight(){
  digitalWrite(input3,LOW);
  digitalWrite(input4,HIGH); 
}
void stopTurning(){
  digitalWrite(input3,LOW);
  digitalWrite(input4,LOW);
}

I know that it maybe be a bit confusing and sorry about that


Replies (73)

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Hi, please check out this forum thread:

http://www.cmucam.org/boards/5/topics/1914?r=2129#message-2129

If you read through the whole thing you should get an idea of what to do.

Thanks,

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

OK thnx very much Kwabena :)

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

Hi,

Sorry for disturbing you,
I have another question that I think you can help me, I can try to fix it but I am afraid that I can burn my Arduino or my CMUcam4.
Until now i have finished with GUI and now I need to connect Arduino with CMUcam4.
My Arduino Uno has an additional power supply of 7.5V because I need to move the car, CMUcam is connected with FTDI cable with laptop, it has a servo motor for pan which is supplied with 6V power from 2 pin servo power connector.
Now should I share the grounds, connect 5V power from breadboard to VIN in CMUam4, and than from 4pin Prop/Clip connect RXI and TXO in both ways.
Also do I need to use still my FTDI cable or not.

And also can you help me a bit with code, in the link that u have send me earlier there is without pan and tilt, can u just give an idea about the code because i am a bit confused with that code.

Best wishes,

IMG_0750.JPG (947.7 kB)

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Sorry about not getting back to you, I was on vacation.

Okay, so let's tackle on question at a time. You'll want to share the grounds of everything. Additionally, you only need the CMUcam4GUI and the FTDI cable to figure out what settings you want to use for the camera. Once you've gotten all the settings down then you put them in your code.

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

Firstly thank you for your response,

I have finished connections with Arduino, I am sheering the ground, taking power from breadboard for CMUcam4, and connecting RXI and TXO, so i think connection is good, cause neither Arduino or CMUcam4 where burned :)

But i am having some problems with the code, I am understanding most of the code that you have send me in that link, but there is without pan and tilt, so if u can give me an idea how to use also pan and tilt and how to combine with the code that I already have (the code which is moving the car), I have send u the code earlier.

Thank you again, and sorry for late response from me but we have time differences and I had also lectures today.

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Hi,

For the pan and tilting functionality, what do you want to do? Please explain to me very clearly what functionality you have, and what is missing and that you want.

Thanks,

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

Hi,

I have a car which need to track a color, I haven't got yet my pan and tilt bracket but I have order one.
Until now my car is can move in all directions, but it dose not track from CMUcam4, I have understand the code that u have told me but I saw there is without pan and tilt so robot there is turning completely to search for specific color, I need to find where is color which I am tracking with pan and tilt and than to make the car moving in that direction.
I hope that I am clear.

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

here is the code that I could manage to write with your help, I think this is the idea correct me if I am wrong.
in comments I was trying to explain what do I need to do.

#include <CMUcam4.h>
#include <CMUcom4.h

#define Y_MIN 182
#define Y_MAX 231
#define U_MIN 94
#define U_MAX 116
#define V_MIN 221
#define V_MAX 239

#define NOISE_FILTER 2

#define PIXELS_THRESHOLD 1
#define CONFIDENCE_THRESHOLD 50

#define enable12 7;
#define input1 6;
#define input2 5;

#define enable34 4;
#define input3 3;
#define input4 2;

void setup(){
  Serial.begin(9600); //Set baud rate

  //Set up the necessary pins as outputs
  pinMode(enable12,OUTPUT);
  pinMode(input1,OUTPUT);
  pinMode(input2,OUTPUT);

  pinMode(enable34,OUTPUT);
  pinMode(input3,OUTPUT);
  pinMode(input4,OUTPUT);

  //Set the Enable pins to HIGH
  digitalWrite(enable12,HIGH);
  digitalWrite(enable34,HIGH);

  Serial.print("Starting camera: ");
   Serial.println(cam.begin()); // Should print out "Starting Camera: 0" on success

   cam.LEDOn(5);
   delay(5000);

   cam.autoGainControl(false);
   cam.autoWhiteBalance(false);

   cam.LEDOn(CMUCAM4_LED_ON);

   cam.pollMode(true);
   cam.colorTracking(true);
   cam.setTrackingParameters(V_MIN, V_MAX, Y_MIN, Y_MAX, U_MIN, U_MAX);
   cam.noiseFilter(NOISE_FILTER);
}

void loop(){

  CMUcam4_tracking_data_t data;

  for(;;){

    if ((cam.trackColor() == 0) && (cam.getTypeTDataPacket(&data) == 0)&&
    (cam.pixels >= PIXELS_THRESHOLD) && (data.confidence >= CONFIDENCE_THRESHOLD)){
      // Pan and Tilt functions to find the color
    }//if 

   else // if we find the color how to turn the car in that exact direction, cause if i turn left for example baybe it will turn to much or les, so how to define how much and in which direction to turn 
  }// for(;;)

}// void loop

//Functions used to control the car's movement
void goForward(){
  digitalWrite(input1,HIGH);
  digitalWrite(input2,LOW);
}

void goBack(){
  digitalWrite(input1,LOW);
  digitalWrite(input2,HIGH);
}

void stopMovement(){
  digitalWrite(input1,LOW);
  digitalWrite(input2,LOW);
}

void goLeft(){
  digitalWrite(input3,HIGH);
  digitalWrite(input4,LOW);
}

void goRight(){
  digitalWrite(input3,LOW);
  digitalWrite(input4,HIGH);
}

void stopTurning(){
  digitalWrite(input3,LOW);
  digitalWrite(input4,LOW);
}

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Hi,

I'll just write up some code tonight to do everything you need to do. Give me the following info however:

1 - All the relevant pin connections and what they do. Please give me as much information about this as you can.
2 - All the CMUcam4 settings
3 - How to drive your motors and pan/tilt bracket. For example, what happens when I drive the left motor forward... does the robot turn right if the right motor is not moving, etc. What values for the servo pulse length make the pan and tilt system move left, right, up down, etc.

Thanks,

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

thank you very much,

1)
#define enable12 7 // power motor (F/B)
#define input1 6 // forward
#define input2 5 // backward

#define enable34 4 // power motor (L/R)
#define input3 3 // left
#define input4 2 //right

2)
I haven't find yet the object that i will track but in attachment is an examples that I made before.

3)
My car has 2 motors, one of them is for the back-wheels and control if car should go forward or backward. The second motor is in front-wheels and control if car should go left or right.
For example if input 1 is high and input 2 is low car would go forward,
if input 3 is high and input 4 low car would go left etc.
I have create them in functions so i just call for example goForward(); goLeft(); and the car will go in the left side

In the code that code that I send u before i think are my ideas and how i mange to move my car before.

and this question "What values for the servo pulse length make the pan and tilt system move left, right, up down, etc." i don't understand it or i don't know. Sorry!

I don't know if I am clear.

Untitled.png (146.6 kB)

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Okay,

So your car is not a differential type drive robot...? Its an RC style car right? I've never written code for a non-diff drive robot. Mmm...

This is a diff drive

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

yes exactly is rc car, there are two motors one for direction, and the other one for forward or backward.
and yes i have seen those for diff robot, but is a bit different.
but in the first code that i have send u, you can see that there are not much differences and is not that hard to be controlled.

And I am really thankful for your time.

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Okay, I see why you need the pan and tilt bracket now. A diff drive kinda removes the RC car problem.

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

yes so with pan and tilt I will find firstly where is the object that I am tracking, and than tell the car to go in that direction.

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Do you have a mega or a uno?

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Okay, try this code out... it should get the robot driving towards an object

#include <CMUcam4.h>
#include <CMUcom4.h>

#include <Servo.h>

/*
  This code is for driving a RC car type robot with a pan and tilt bracket.
*/

// BEGIN SETTINGS YOU MIGHT NEED TO TWEAK //

#define Y_MIN 182
#define Y_MAX 231
#define U_MIN 94
#define U_MAX 116
#define V_MIN 221
#define V_MAX 239

#define NOISE_FILTER 2

#define PIXELS_THRESHOLD 1
#define CONFIDENCE_THRESHOLD 50

// END SETTINGS YOU MIGHT NEED TO TWEAK //

#define TILT_PIN 9
#define PAN_PIN 8

// Forward/Backward Motor
#define ENABLE_12 7
#define INPUT_1 6
#define INPUT_2 5

// Left/Right Motor
#define ENABLE_34 4
#define INPUT_3 3
#define INPUT_4 2

CMUcam4 cam(CMUCOM4_SERIAL);

Servo tiltServo;
Servo panServo;

void driveInit()
{
  pinMode(ENABLE_12, OUTPUT);
  pinMode(INPUT_1, OUTPUT);
  pinMode(INPUT_2, OUTPUT);

  pinMode(ENABLE_34, OUTPUT);
  pinMode(INPUT_3, OUTPUT);
  pinMode(INPUT_4, OUTPUT);
}

// power > 0 = foward
// power < 0 = backward
// turn > 0 = left
// turn < 0 = right

void drive(int power, int turn)
{
  analogWrite(ENABLE_12, min(abs(power), 255));
  digitalWrite(INPUT_1, power > 0);
  digitalWrite(INPUT_2, power < 0);

  analogWrite(ENABLE_34, min(abs(turn), 255));
  digitalWrite(INPUT_3, turn > 0);
  digitalWrite(INPUT_4, turn < 0);  
}

void setup()
{
  // The CMUcam4 is using the only serial port so print/println statements don't work and will break things

  driveInit();

  tiltServo.attach(TILT_PIN);
  panServo.attach(PAN_PIN);

  if(cam.begin() == CMUCAM4_RETURN_SUCCESS) // Init success
  {
    // Wait for autogain/autowhite balance to run

    cam.LEDOn(10);
    delay(5000); 

    // Turn autogain/autowhite balance off

    cam.autoGainControl(false);
    cam.autoWhiteBalance(false);
    cam.LEDOn(CMUCAM4_LED_ON);

    cam.pollMode(true);

    cam.colorTracking(true);
    cam.setTrackingParameters(V_MIN, V_MAX, Y_MIN, Y_MAX, U_MIN, U_MAX);
    cam.noiseFilter(NOISE_FILTER);   
  }
  else
  { 
    for(;;); // Error... maybe indicate this to the user in some way
  }
}

void loop()
{
  CMUcam4_tracking_data_t data;

  for(;;)
  {
    if((cam.trackColor() == 0) // Data request ok?
    && (cam.getTypeTDataPacket(&data) == 0) // Data received ok?
    && (data.pixels >= PIXELS_THRESHOLD) // Tracked pixels found?
    && (data.confidence >= CONFIDENCE_THRESHOLD)) // Dense blob of pixels found?
    {
      int xError = data.mx - (CMUCAM4_NATIVE_H_RES/2); // positive = object to the right, negative = object to the left
      int yError = data.my - (CMUCAM4_NATIVE_V_RES/2); // positive = object above, negative = object below  

      if(abs(xError) > 10) // The play/hysteresis is 10 pixels...
      {
        drive(100, (-xError) * 2); // Drive towards object...    
      }
      else
      {
        drive(0, 0); // Do nothing...  
      } 
    }
    else
    {
      drive(0, 0); // Halt the robot... 
    }
  }
}

There might be mistakes in the code. Please try to understand what is going on.

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

sorry i was out to eat,
I will try it now, and do u know can i use only pan not tilt cause i need to search for tracking object only left and right?

and thnx again

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Hi, yeah, one step at a time. Let's get the basics working first.

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

Actually is not working i was expecting at least servo motor will try to find the object but not,
and do u think that analogWrite(ENABLE_12, min(abs(power), 255)); should be digitalWrite(ENABLE_12, min(abs(power), 255)); because is connected in digital pins not analog (I tried this but was the same).
And by the way I have only one servo for panning which is provided with 6V from 2 pin connector, I have a wire from TXO of CMUcam4 to RX1 in Arduino and the opposite. Do I need anything else to do.

IMG_0752.JPG (814.6 kB)

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Hi,

AnalogWrite does PWM on a digital i/o pin. It isn't actually an analog voltage. Using analogWrite gives speed control. Just settings pins high or low in the code you posted works very poorly.

As for it not working. There are a lot of things that can go wrong. You need to figure each one out at a time. The first thing to do is make sure everything is physically built correctly. You might want to mount the CMUcam4 on an arm for example so it looks forwards.

Next, verify all your wire connections are good and don't have any shorts or are not connected.

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

I tried that and still the same,
After I tried again firstly only CMUcam4 it was ok (i took another target), than i tried only Arduino it was also ok.
Than i connect them and change the target and it just give me a sign just 2 times the wheels turn right shortly, and nothing more.

And i am really sorry for disturbing you.

RE: RC Car color tracking - Added by Kwabena Agyeman over 7 years ago

Okay, so, what do you want to debug first?

I can answer any questions you have. But, you have to work through each part of the system at a time.

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

I am trying to see where could be the problem but I can not find it, i cant use serial print!
i don't know if camera is tracking or not now i don't know where to start my debugging!

RE: RC Car color tracking - Added by Ilir Gruda over 7 years ago

I have 2 questions that can help me maybe even though i am skeptic:

1.Do u think that there can be problem with power that CMUcam is getting from Arduino, I am connecting arduino with loptop and than i have a wire from arduino 5V to VIN in CMUcam ?
2.Do i need to set Automatic Pan or to set the position of Pan?

1 2 3 Next » (1-25/73)