How to follow the line for a certain amount of time

27 replies [Last post]
Arlington robot town
Title: NooBot
Joined: 03/25/2013
BotPoints: 3
User offline. Last seen 9 years 35 weeks ago.

This is our program for follow the line. We are wondering how we make it go a certain distance or time before turning around. Can you help us?

  1. int main()
  2. {
  3. int rport=0, leftmtr=0, rghtmtr=3; // identify port and motors
  4. int threshold=400; // set threshold for light conditions
  5. int high=40,low=-10; // set wheel powers for arc radius
  6. printf("Line following: position robot on tape\n");
  7. printf("Press B button when ready\n\nPress side button to stop\n");
  8. while(b_button()==0) {} // wait for button press
  9. while(side_button()==0){ // stop if button is pressed
  10. while (analog10(0) > threshold) { // continue until not dark
  11. motor(leftmtr,low); motor(rghtmtr,high); // arc left
  12. if (side_button()!=0) break; } // or button pressed
  13. while (analog10(rport) <= threshold){ // continue until dark
  14. motor(leftmtr,high); motor(rghtmtr,low); // arc right
  15. if (side_button()!=0) break; } // or button pressed
  16. }
  17. ao(); // stop because button pressed
  18. printf("done\n");
  19. return 0;
  20. }

[Edited by Jeremy to fix markup]

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
BotPoints: 1168
User offline. Last seen 7 years 28 weeks ago.

Hi, welcome to the Botball Community!

For reference, you can enclose C code in < c > ... < / c > (remove the spaces), and it will do syntax highlighting and preserve the spaces. I've done this for you in your post.

As for your question, there's a function called seconds() on the CBC, I assume it's on the Link too. It returns the current system time in seconds (as a float). You might try storing what seconds() returns in a variable prior to starting the loop, and then use a break; inside the loop if seconds() is greater than the variable by more than a certain amount.

There's also a function called get_motor_position_counter which works similarly based on motor distance instead of timing.

Hope this helps.

-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS

Beta's picture
Title: The Magnificent
Joined: 02/24/2012
BotPoints: 266
User offline. Last seen 8 years 41 weeks ago.

seconds() now returns a double. For example:

  1. double start = seconds();
  2. // While the current time minus the starting time is less than 5 seconds
  3. while(seconds() - start < 5.00) {
  4. // do something
  5. }

Braden McDorman

Developer of the KIPR Link, KISS IDE, KIPR's 2D Simulator, and CBCJVM.

Reach me at bmcdorman(cat)kipr(dog)org where (cat)=@ and (dog)=. if you need assistance of any kind.

Title: NooBot
Joined: 02/23/2017
BotPoints: 46
User offline. Last seen 4 years 20 weeks ago.

I dont have any idea of such things but can suggest you many sources. I remember some australianwritings firm has posted similar things in their blogs. Do surf them when needed and find the appropriate answers.

Title: NooBot
Joined: 03/04/2017
BotPoints: 10
User offline. Last seen 5 years 38 weeks ago.

There are actually two different questions here. The first is "how do I follow a line for a specific distance?" The second is "How to I loop for a specific amount of time?" I will attempt to answer both. Please note that we have problems with repeatability (for numerous reasons), so you must understand that each of these methods has limitations.

Following a line for a specific distance

Looping for a specific distance is fairly easy. Typically this is done by tracking wheel movement. On the iCreate robot you can use the set_create_distance() and get_create_distance() functions. I believe these functions give results in MM. The finished code would look something like this

  1. final_distance = get_create_distance() + desired_distance_of_travel
  2. while (get_create_distance() <= desired_distance_of_travel)
  3. {
  5. }

On the little robot, you would measure travel using motor rotations. This is done with the gmpc() or get_motor_position_counter() function. The logic is very similar to that given above, but instead of measuring travel in MM, you will need to measure motor rotation in ticks. One rotation is about 1100 ticks.You will have to choose which motor you are using (the left or the right) to track movement.

Looping for a specific amount of time

I have not used the seconds() function noted by the earlier post. Our team uses the clock() function in C, which is based on the number of clock cycles that are run on the CPU. You must include the time.h library for this to work. I have included working code (below), but it is off by about 3-5%.

  1. #include &lt;time.h&gt;
  3. int main()
  4. {
  5. int start_time;
  6. int end_time;
  7. int target_MS; // desired duration of loop in milliseconds
  8. int CPU_CPMS = 800; //estimated CPU cycles per millisecond
  10. target_MS = 100000; // 100000 --> 100 seconds
  12. start_time = clock();
  13. end_time = start_time + target_MS * CPU_CPMS;
  14. printf("Start Loop. Clock time %d \n",clock());
  15. while(clock()<end_time)
  16. {
  17. }
  18. printf("Timer Finished. Clock time %d \n",clock());
  21. return 0;
  22. }

Title: NooBot
Joined: 05/29/2017
BotPoints: 64
User offline. Last seen 4 years 24 weeks ago.

Hello! I'm sorry, that is not the topic! I really like your forum! Thank you, I'm with you)
sprint corporate

Title: NooBot
Joined: 06/07/2017
BotPoints: 12
User offline. Last seen 5 years 22 weeks ago.

Interesting topic shown here, i am now working on it regularly here and would say keep the future posts like this continuously.
Engineering college in punjab
Best Engineering College in North India
Fashion technology college in Punjab

Title: NooBot
Joined: 07/24/2017
BotPoints: 8
User offline. Last seen 5 years 18 weeks ago.

DPS has a very good hostel for the students where they get homely environment. Students of various age groups and different places live in the hostel.
Best School in rajasthan
CBSE Boarding School in NCR

Title: NooBot
Joined: 07/28/2017
BotPoints: 16
User offline. Last seen 5 years 18 weeks ago.

The T-Series StageWorks Academy is an initiative designed to identify, nurture and create a pool of immensely skillful and passionate professionals who can match national and global demands.
Best Dance school in noida
Best singing school in noida
Acting classes in noida

Title: NooBot
Joined: 07/28/2017
BotPoints: 16
User offline. Last seen 5 years 18 weeks ago.

The Fairdeal Group of Companies is a fast-growing group with the ambition of helping people across Kenya build their dream homes and offices.
Furniture manufacture in Kenya

Title: NooBot
Joined: 07/06/2017
BotPoints: 30
User offline. Last seen 4 years 43 weeks ago.

Actually, I have been searching for this code for past few days for my lab. I have gone through the code which you have shared here. It is very simple to follow and easy to understand. Keep continuing to share more articles like this.Martin Enclosures