Threading and servos

1 reply [Last post]
cannonh
Title: NooBot
Joined: 07/09/2012
Posts:
BotPoints: 19
User offline. Last seen 9 years 46 weeks ago.

Our sorting robot has two threads so it can sort while moving. The threads access different servos, but sometimes when both threads are moving a servo it does this weird glitch. Both servos go into this state where they iterate to one value and then slam back to a low value. Currently we stop the sorting while doing iteration of our arm, but we would like to solve this.

Currently I'm looking around in the source code trying to find the cause and a solution. Aside from throughly confusing myself, I have accomplished absolutely nothing doing that.

Marty Rand
Marty Rand's picture
Title: Botball Youth Advisory Council
Joined: 07/04/2009
Posts:
BotPoints: 253
User offline. Last seen 8 years 41 weeks ago.

Try putting:

  1. #include "/usr/include/pthread.h"
  2. pthread_mutex_t k_mem = PTHREAD_MUTEX_INITIALIZER;
  3. #define set_servo_position(servo,position) pthread_mutex_lock(&k_mem);set_servo_position(servo,position);pthread_mutex_unlock(&k_mem);

at the top of your program (VERY top). Also, be sure that you are not including pthread twice after doing this. I wrote this on the fly and I'm not sure it will work. The Link is very picky about threads. By any chance are you using create_read_block() anywhere while the thread is running? Is it even a create? I've been meaning to submit a but report about that function not liking threads. I'm not sure why really.

-Marty Rand
{
Senior programmer at Norman Advanced Robotics

Former senior programmer at Whittier Middle School

Youth Advisory Council

All around nerd
}