Threading

23 replies [Last post]
amehta
Title: NooBot
Joined: 07/18/2011
Posts:
BotPoints: 136
User offline. Last seen 5 years 32 weeks ago.

I had a question with threading, when I tried this:

  1. void servos_level()
  2. {
  3. thread tid1 = thread_start(first_servo_level);
  4. thread tid2 = thread_start(second_servo_level);
  5. thread_wait(tid1);
  6. thread_wait(tid2);
  7. thread_destroy(tid1);
  8. thread_destroy(tid2);
  9. }
  10.  
  11. void first_servo_level()
  12. {
  13. position_servo(0, 100, 500);
  14. }
  15.  
  16. void second_servo_level()
  17. {
  18. position_servo(1, 100, 1548);
  19. }

I got these errors:
http://imgur.com/0r7TnQb

I'm just trying to run these two functions (as an example, I might be doing other things) at the same time, what am I doing wrong?

-amehta (Andrew Mehta)

amehta
Title: NooBot
Joined: 07/18/2011
Posts:
BotPoints: 136
User offline. Last seen 5 years 32 weeks ago.

I also tried int tid1 instead of thread tid1, this is what I got:
http://imgur.com/8BPXqud

-amehta (Andrew Mehta)

edmyers
Title: NooBot
Joined: 06/01/2009
Posts:
BotPoints: 28
User offline. Last seen 9 years 22 weeks ago.

You first need to create the thread using - thread_create

There is an example in the KISS programmers manual (in the KIPR Link Library section) that shows what you need to do.

Eugene Myers
Cedarhouse Robotics

amehta
Title: NooBot
Joined: 07/18/2011
Posts:
BotPoints: 136
User offline. Last seen 5 years 32 weeks ago.

Oh, I was assuming that had to do with the iRobot Create...

Thanks

-amehta (Andrew Mehta)

amehta
Title: NooBot
Joined: 07/18/2011
Posts:
BotPoints: 136
User offline. Last seen 5 years 32 weeks ago.

So like this?

  1. void servos_level()
  2. {
  3. thread tid1 = thread_create(first_servo_level);
  4. thread tid2 = thread_create(second_servo_level);
  5. thread_start(first_servo_level);
  6. thread_start(second_servo_level);
  7. thread_wait(tid1);
  8. thread_wait(tid2);
  9. thread_destroy(tid1);
  10. thread_destroy(tid2);
  11. }

I still get these errors:
http://imgur.com/NyGbwZ4

-amehta (Andrew Mehta)

amehta
Title: NooBot
Joined: 07/18/2011
Posts:
BotPoints: 136
User offline. Last seen 5 years 32 weeks ago.

I replaced thread_start(first_servo_level); [and the second one too] with thread_start(tid1);, still didn't work

-amehta (Andrew Mehta)

edmyers
Title: NooBot
Joined: 06/01/2009
Posts:
BotPoints: 28
User offline. Last seen 9 years 22 weeks ago.

What did it do?

from looking at your code it probably didn't do anything. Here' s possibly why

(1) The servos need to be enabled otherwise the position_servo call does nothing.

(2) Also, depending upon your code, you may be exiting your program before the servos would have a chance to move. (you only show a function so hard to tell without some context)

Eugene Myers
Cedarhouse Robotics

edmyers
Title: NooBot
Joined: 06/01/2009
Posts:
BotPoints: 28
User offline. Last seen 9 years 22 weeks ago.

Could be either one of two things:

(1) servos need to be enabled, otherwise position_servo is ignored

(2) your program could be completing before the servos have had a chance to move. If this is the case, use a sleep at the proper place.

Eugene Myers
Cedarhouse Robotics

edmyers
Title: NooBot
Joined: 06/01/2009
Posts:
BotPoints: 28
User offline. Last seen 9 years 22 weeks ago.

Most likely your program completed before your servos had a chance to move...

Even though you have a thread_wait the threads will complete before the servos have moved. All position_servo does is to load a hardware register and returns. The servos themselves will then start to move to the position loaded in the register. Their final position will not be attained until some time after the function has completed.

To test this, place a sleep after position_servo and you should see the servos move. Use a second at first and adjust downward.

Also, make sure that you have enabled servos.

Eugene Myers
Cedarhouse Robotics

edmyers
Title: NooBot
Joined: 06/01/2009
Posts:
BotPoints: 28
User offline. Last seen 9 years 22 weeks ago.

Your program is probably completing before the servos have had a chance to move.

All position_servo does is to load a hardware register and returns. The servo starts to move, but takes a relatively long time to move.

Since position_servo return immediately, the thread completes and the function terminates.

Place a sleep after each of the position_servo calls. For now make the sleep at least .5 sec (you can adjust this based on experience). You should see the servos move.

Also, make sure you enable servos.

Eugene Myers
Cedarhouse Robotics

amehta
Title: NooBot
Joined: 07/18/2011
Posts:
BotPoints: 136
User offline. Last seen 5 years 32 weeks ago.

Thanks, but it just doesn't compile in the first place. That's the main problem here.

And don't worry, I enabled the servos in the main. And my position_servo function has its own sleep inside of it. Should I still do one outside too?

-amehta (Andrew Mehta)