I had a question with threading, when I tried this:
void servos_level() { thread tid1 = thread_start(first_servo_level); thread tid2 = thread_start(second_servo_level); thread_wait(tid1); thread_wait(tid2); thread_destroy(tid1); thread_destroy(tid2); } void first_servo_level() { position_servo(0, 100, 500); } void second_servo_level() { position_servo(1, 100, 1548); }
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?
I also tried int tid1 instead of thread tid1, this is what I got:
http://imgur.com/8BPXqud
-amehta (Andrew Mehta)
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
Oh, I was assuming that had to do with the iRobot Create...
Thanks
-amehta (Andrew Mehta)
So like this?
I still get these errors:
http://imgur.com/NyGbwZ4
-amehta (Andrew Mehta)
I replaced thread_start(first_servo_level); [and the second one too] with thread_start(tid1);, still didn't work
-amehta (Andrew Mehta)
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
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
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
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
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)