[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Running a motor using Interactive C (full question)

Posted on 2005-05-12
6
Medium Priority
?
271 Views
Last Modified: 2016-05-20
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              
              }  
          }
    }    
}
0
Comment
Question by:jasoncuevas
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13992360
Hi jasoncuevas,
Exactly what is your question?

Cheers!
0
 

Author Comment

by:jasoncuevas
ID: 13992380
How do I make it know when it is at the end of the tape without it interfering with the while loop.
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 2000 total points
ID: 13994001
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13995987
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question