Pages: 1 2 3 [4]   Go Down
Author Topic: i dont get it  (Read 3661 times)
0 Members and 1 Guest are viewing this topic.
ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 990
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, in case 3 when right is > left you output a message but you don't set the state to 4.  similarly for the other conditions there.

then there's no break; before the case 4 label so you fall into it.  this sets the left motor running.

in state 1 you read the distance before you center the servo and you don't delay for the servo to move.

also in case 2 you output "in state 4".

in the read ping right left you read the left and right but your serial print prints the left distance twice.

fix those, run it again and READ the output, trace through your code by hand tracking the states and variables.

I would get rid of all the prints except those at the beginning of loop and i would put spaces between the individual variables to make it more readable.

Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok, in case 3 when right is > left you output a message but you don't set the state to 4.  similarly for the other conditions there.

Code:
case 3:
    if(distance_right > distance_left )
    {
      state =4;
      Serial.print("switching to state 4 ");  
    }
i thought the above is setting it to state 4 isin't it?   :-?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i thought the above is setting it to state 4 isin't it?
It is.

I wonder if your issue is that you expect that changing the state to 4 in the case 3 block will cause the case 4 block to be executed. It will not. Only the initial value of state is used to select the block to execute. If you need to execute additional blocks of code when the state changes, a switch statement is not the way to go.

Anyway, there have been several suggestions concerning changes that need to be made to your code, and it isn't clear whether you've made those changes, and if you have that you have made them correctly.

It's time to post your code again, along with a refresh of what works, what doesn't, what you need help with, and what you are trying to do.
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

[size=20]NEW CODE [/size]

Code:
///setting up pin no's ////


#include <Servo.h>            
#define ledpin_right 13                  
#define ledpin_left 12
#define motor_ONE_pin_ONE 3          
#define motor_ONE_pin_TWO 4    
#define motor_TWO_pin_ONE 5      
#define motor_TWO_pin_TWO 6
const int pingPin = 10;
int pos = 0 ;
Servo myservo;
long duration,cm;
long distance_right = 0 ;
long distance_left = 0 ;
int state =1;
 
 
/////setting pinmode//////


void setup()
{
  myservo.attach(9);
  Serial.begin(9600);                            
  pinMode(motor_ONE_pin_ONE,OUTPUT);    
  pinMode(motor_ONE_pin_TWO,OUTPUT);    
  pinMode(motor_TWO_pin_ONE,OUTPUT);    
  pinMode(motor_TWO_pin_TWO,OUTPUT);    
}


///looping functions////


void loop()
{
  default_action();
  if (cm < 10)
  {
    switch(state)
  {
    case 1:
    drive_stop();
    Serial.print("()stopped driving gointo to state 2()");
    state = 2;
    break;
    case 2:
    Serial.print("()scanning left and right distances()");
    ping_read_right_left();
    state = 3;
    break;
    case 3:
    Serial.print("()deciding action()");
    decide_action();
    break;
    default:;
  }
  }
  else
  {
    default_action();
  }
}

void ping_read()
{
  pinMode(pingPin, OUTPUT);            
  digitalWrite(pingPin, LOW);          
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);          
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);        
  Serial.print("()cm()");      
  Serial.println();
  return;
}
void ping_read_right_left()
{
  myservo.write(10);
  delay(350);
  pinMode(pingPin, OUTPUT);            
  digitalWrite(pingPin, LOW);          
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);          
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_left = microsecondsToCentimeters(duration);
  Serial.print( distance_left);      
  Serial.print("cm");    
  Serial.println();
  myservo.write(180);
  delay(350);
  pinMode(pingPin, OUTPUT);            
  digitalWrite(pingPin, LOW);          
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);          
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_right = microsecondsToCentimeters(duration);
  Serial.print( distance_right );      
  Serial.print("cm");    
  Serial.println();
  return;

}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}


//action functions////


void default_action()
{
  myservo.write(80);
  delay(150);
  drive_forward();
  ping_read();
}

void decide_action()
{
  Serial.print(distance_right);
  Serial.print("()distance_right()");
  Serial.print(distance_left);
  Serial.print("()distance_left()");
  
  if(distance_right > distance_left )
    {
      drive_right();
      Serial.print("driving to the right");  
    }
    else if (distance_right < distance_left )
    {
      drive_left();      
      Serial.print("driving to the left");
    }
    else if ( (distance_right<10) && (distance_left<10) )
    {
      drive_360();
      Serial.print("driving 360 ");
    }
}

////drive_functions/./////

void drive_forward()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}


void drive_stop()
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_right()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  delay(1000);
  return;
}

void drive_left()
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  delay(1000);
  return;
  
}

void drive_360()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  delay(2000);
  return;
}


the values on serial monitor
Quote
()stopped driving gointo to state 2()6()cm()
()scanning left and right distances()36cm
90cm
9()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right132()cm()
132()cm()
131()cm()
132()cm()
132()cm()
132()cm()
132()cm()
131()cm()
132()cm()
132()cm()
131()cm()
132()cm()
132()cm()
132()cm()
9()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right11()cm()
6()cm()
4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right5()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right4()cm()
()deciding action()90()distance_right()36()distance_left()driving to the right5()cm()
()deciding action()90()distance_right()36()distance_left()

it is not driving forward
and it is not scanning using the servo everytime only the first time !
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
()stopped driving gointo to state 2()123()cm()
124()cm()
4()cm()
()scanning left and right distances()49cm
7cm
93()cm()
124()cm()
124()cm()
124()cm()
124()cm()
123()cm()
124()cm()
123()cm()
123()cm()
124()cm()
123()cm()
124()cm()
124()cm()
123()cm()
124()cm()
123()cm()
123()cm()
3()cm()
4()cm()
()deciding action()7()distance_right()49()distance_left()driving to the left124()cm()
in this case  the distance right is less than distance left it is driving left  
« Last Edit: December 03, 2010, 12:03:21 pm by newman » Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
7()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right5()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right5()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right5()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right5()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right3()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right4()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right4()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right4()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right4()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right4()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right5()cm()
()deciding action()88()distance_right()6()distance_left()driving to the right4()cm()
()deciding action()88()distance_right()6()distance_left()driving to the righ
in this case it is driving to the right (displaying only) but running the left wheel  >smiley-sad
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
7()cm()
6()cm()
()stopped driving gointo to state 2()3()cm()
()scanning left and right distances()5cm
4cm
4()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left5()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left4()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left4()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left5()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left5()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left6()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left4()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left4()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left4()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left4()cm()
()deciding action()4()distance_right()5()distance_left()driving to the left4()cm()
()deciding action()4()distance_right()5()distance_left()driving to
this is for drive 360 but again it dosent occur
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5557
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Something you should probably do -first-, is to define your states. Offhand, I don't know what they would necessarily be in your case, but probably something like (lets call this an example):

1. Moving forward (while checking distance)
2. Scanning Left/Right distance
3. Backing up to the left
4. Backing up to the right

So - you would have code that would look something like (this is pseudocode here):

Code:
int state=1;

loop () {
  switch (state) {
    case 1: // Moving forward (while checking distance)
       forward();
       distanceC = getDistanceCenter();
       if (distanceC < 20) state = 2;
       break;
    case 2: // Scanning Left/Right distance
       distanceL=getDistanceLeft();
       distanceR=getDistanceRight();

       if (distanceL < distanceR) {
         // object to the left, so we want to backup to the left
         state = 3;
       }
       else {
         // object to the right, so we want to backup to the right
         state = 4;
       }
       break;
    case 3; // Backing up to the left
       backupToLeft();
       state = 1;
       break;
    case 4; // Backing up to the right
       backupToRight();
       state = 2;
       break;
    default: // should never get here!
       state = 1;
       break;
  }
}

int getDistanceCenter() {
  moveServoCenter();
  return (getDistance());
}

int getDistanceLeft() {
  moveServoLeft();
  return (getDistance());
}

int getDistanceRight() {
  moveServoRight();
  return (getDistance());
}

int getDistance() {
  // activate PING here, get distance, and return it
  return (distance);
}

Ok - I am not going to claim the above is perfect, or even right for your situation - but it should give you an idea on how to proceed. If you look at the link I gave earlier to the electro-tech forums post, you will see that the guy I am having a discussion with on state machines posts a state-diagram detailing the operations of a simple elevator controller with 7 states. My first implementation used only 3 states (well, 4, actually - but it could be simplified to 3), but managed to replicate his version with 7 states; he wanted the 7 state version, which I admit I didn't replicate properly, so I re-did the code, and it seemed to come out cleaner.

What this all means is that first, you -must- determine your states, and draw a flow diagram if you can. Show each state, show what causes the transition between states, and try to simplify things to as few states as seems clear, needed, possible (note, in my example above, I reduced the number of states to 3 for the elevator example, yet it turned out to have a clearer representation with 7 states - so what to take away from that is, if in the coding of a state machine, it seems overly complex - perhaps you need -more- states, not fewer!).

Something -not- to take away from his example, though, is the temptation to use if-then constructs and gotos for the implementation of the state machine. While I admit, it looks very tempting, that may be a path towards frustration, especially if your state machine needs to change mid-project, or in the future. But ultimately, its your call.

Hope this helps. Good luck!

 smiley

Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have a function called ping_read(). The code in that function is replicated 3 times in the ping_read_right_left() function. Why? Why don't you call ping_read() from ping_read_right_left()?

Have you written a sketch that confirms that each function - ping_read(), ping_read_right_left(), drive_forward(), etc. - are all functioning correctly?

A return statement is not needed at the end of a function whose return type is void. The end of a function triggers a return, regardless of whether you have one, or not.

Why do drive_left() and drive_right() have delays? The motor states are not reset after the delay, so a left turn will continue until some other condition is met. If a left turn is needed, it should be needed until it is no longer needed, not for some fixed amount of time.
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You have a function called ping_read(). The code in that function is replicated 3 times in the ping_read_right_left() function. Why? Why don't you call ping_read() from ping_read_right_left()?
it just didnt occur to me  ;D

Quote
Have you written a sketch that confirms that each function - ping_read(), ping_read_right_left(), drive_forward(), etc. - are all functioning correctly?



YEAH!
they work correctly the main common problem in all ,my codes is that the drive forward dosent occur  >smiley-sad
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hope this helps. Good luck!
Working on it hpoing to finish it tonight  :smiley
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: