How to follow the line for a certain amount of time

11 replies [Last post]
Arlington robot town
Title: NooBot
Joined: 03/25/2013
Posts:
BotPoints: 3
User offline. Last seen 4 years 30 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
Posts:
BotPoints: 1168
User offline. Last seen 2 years 22 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
Beta's picture
Title: The Magnificent
Joined: 02/24/2012
Posts:
BotPoints: 266
User offline. Last seen 3 years 36 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.

FrankEggers
Title: NooBot
Joined: 02/23/2017
Posts:
BotPoints: 12
User offline. Last seen 25 weeks 2 days 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.

AeroHead
Title: NooBot
Joined: 03/04/2017
Posts:
BotPoints: 10
User offline. Last seen 32 weeks 6 days 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. {
  4. <<< INSERT LINE FOLLOWING CODE >>>
  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;
  2.  
  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
  9.  
  10. target_MS = 100000; // 100000 --> 100 seconds
  11.  
  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());
  19.  
  20.  
  21. return 0;
  22. }

panistefanin
Title: NooBot
Joined: 05/29/2017
Posts:
BotPoints: 22
User offline. Last seen 20 weeks 6 days ago.

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

enough
Title: NooBot
Joined: 06/07/2017
Posts:
BotPoints: 12
User offline. Last seen 16 weeks 4 days 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

rojana
Title: NooBot
Joined: 07/24/2017
Posts:
BotPoints: 8
User offline. Last seen 12 weeks 5 days 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

mb22
Title: NooBot
Joined: 07/28/2017
Posts:
BotPoints: 16
User offline. Last seen 12 weeks 1 day 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

mb22
Title: NooBot
Joined: 07/28/2017
Posts:
BotPoints: 16
User offline. Last seen 12 weeks 1 day 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

annetrose
Title: NooBot
Joined: 07/06/2017
Posts:
BotPoints: 24
User offline. Last seen 22 hours 28 min 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