Running a motor using Interactive C (full question)

The project is to design a robot out of legos which, by using infrared and breakbeam sensors, will follow a path of black tape on a white floor, pick up a block at the end of the tape, turn around, follow the tape back, and stop at the end. The break beam sensor is broken when the block enters two arms, at which point it activates muscle wire which drops a bar, trapping the block. The break beam sensor deals only with the block, not with the tape. The infrared sensors are place near the middle of the bot, facing straight down. They are placed a little closer together than the width of the tape, so that if it hits a straightaway, both sensors are over the tape. My issue is trying to figure out how to get the sensors to recognize that it's at the end of the tape. At the moment, it backs up if it gets off the tape (which it does 15-20 times while actually travelling the track), so when it gets to the end, it just spazzes by going alternatingly forward/backward on and off the tape.
Any help would be appreciated, here's the code.

void main()
{
    int left_motor = 1, right_motor = 3, left_sensor = 5,
      right_sensor = 6, break_sensor = 2, back_count = 0,
      break_count = 0, motor_sensor = 0;
   
    while(1)
      {
        if(analog(break_sensor) > 200 && break_count == 0)  //if break beam sensor is broken, it will do this
          {
            motor(left_motor,100);
            motor(right_motor,100);   //moves bot forward
            msleep(300L);
            off(left_motor);
            off(right_motor);
            motor(motor_sensor,100);  //activates muscle wire, traps box
            sleep(5.0);  
            off(motor_sensor); //turns motor_0 motors off
            motor(left_motor, 100);
            msleep(500L);
            motor(right_motor,-100);    //this makes robot pull a 180
            msleep(500L);
            break_count++;   //exits the loop permanently
        }
        else
          {  
            if(analog(left_sensor)<120 && analog(right_sensor)>120) //left sensor off tape
              {                                                     //right sensor on tap
                motor(left_motor,100);
                motor(right_motor,25);  //turns left motor on high, right motor 25%, turns left
              }
            if(analog(left_sensor)>120 && analog(right_sensor)>120) //both on tape
              {
                motor(left_motor,100);
                motor(right_motor,100);  //both motors full speed
              }
            if(analog(left_sensor)>120 && analog(right_sensor)<120) //left on, right off
              {    
                motor(left_motor,25);
                motor(right_motor,100);   //makes a right turn to get back on tape
              }
            if(analog(left_sensor)<120 && analog(right_sensor)<120) //both sensors off tape
              {
                motor(left_motor,-50);
                motor(right_motor,-50);   //back up both motors              
              }  
          }
    }    
}
jasoncuevasAsked:
Who is Participating?
 
PaulCaswellConnect With a Mentor Commented:
Jason,

It looks like the problem is, if both sensors are reading off-tape, are you at the end of the tape or off to one side. The best way to get a better idea would be to recode the loop as a state-machine. This allows you to decide from the current state what the next state should be. Assume the bot isnt moving so fast that it could step right off the tape in one step then both sensors reading off-tape can mean something different depending on your previous state. E.G. If last time around you were off to the left then there's a good chance you are now just further off. If last time you were fully on tape then there's a good chance you are at the end of the tape.

Something like:

while (1)
{
 enum { on, offLeft, wayOffLeft, offRight, wayOffRight, offEnd, offStart, off } state = off;
 int left = analog(left_sensor);
 int right = analog(right_sensor);
 // Check the state.
 switch ( state )
 {
   case off:
           // Check sensors to see if we're getting back on.
           if(left<120 && right>120) state = offLeft;
           ...
           break;
   case offLeft:
          if(left<120 && right<120) state = wayOffLeft;
          ...
          break;
 }
 // Now act on the state.
 switch ( state )
 {
   case offLeft:
           // Make adjustments to turn slightly right.
           break;
 }
}

I think this recoding would make your decisions much easier to make by separating the detection from the action. Let me know if you dont understand and I'll give you some more details.

Paul
0
 
AxterCommented:
Hi jasoncuevas,
Exactly what is your question?

Cheers!
0
 
jasoncuevasAuthor Commented:
How do I make it know when it is at the end of the tape without it interfering with the while loop.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Jason,

How many sensors do you have looking for the black tape?  I suggest three, positioned close enough that at least two of the sensors can seen the tape simultaneously, and possibly close enough that all three can see the tape.

Here's layout 1:

S  S  S   (The three sensors)
  TTT     (The tape)
  TTT
  TTT
  TTT

Nominally, only the middle sensor is reading the tape.  However, as the craft veers off-center, both the center sensor and one of the side sensors both detect the tape.  The robot has strayed off center toward the side sensor that is detecting tape and must be moved toward that sensor.

If the center sensor loses tape but a side sensor still detects it, you stop the robot and reverse until the center sensor again detects tape.  You make a steering correction and resume forward.

When all three sensors lose the tape, you've reached the end of the tape.


The variation where all three sensors can detect the tape simultaneously works similarly.

Kent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.