RTOS End Thread

02 May 2012

I am using the RTOS library for servo movement (servo's need to move simultaneous to other actions). I have the main thread, a thread running sensor updates and calculations, and a third constantly updating servo positions based on any servo movement. My problem is that the following thread will only get initialized 4 times before it stops. After that it will not get hung up on thread creation, but the thread will not start. I found that the thread max is 7, so I am wondering if that is the issue? Do threads not automatically end once they reach completion? And if they don't how can I delete on completion?

#include <stdlib.h>
#include "rtos.h"
#include "threading.h"
#include "mbed.h"
#include "DCM_AHRS/minimu9.h"
#include "constants.h"

void increment( void const *args ) { //float *joint, float inc, float target, int speed )
    move_args* moveArgs = (move_args*)( args );
    float *joint        = moveArgs->joint;
    float *angle        = moveArgs->angle;
    float inc           = moveArgs->inc;
    float target        = moveArgs->target;
    int   speed         = moveArgs->speed;
    float start         = *joint;

    // Move to a certain position
    if ( NULL == angle ) {
        int num_reps = int ( ( abs( *joint - target ) / inc ) + 0.5 );

        for ( int i = 0; i < num_reps; *joint+=inc, i+=1 ) {
            Thread::wait( ( 0.1 / speed ) * 1000 );
        }
    }
    // Move to a certain angle
    else {
        Thread::wait( ( 0.1 / speed ) * 1000 );
        if ( *angle < target ) {
            for ( *joint = start; *angle < target; *joint+=inc ) {
                Thread::wait( ( 0.1 / speed ) * 1000 );
            }
        } else {
            for ( *joint = start; *angle > target; *joint+=inc ) {
                Thread::wait( ( 0.1 / speed ) * 1000 );
            }
        }
    }
    delete moveArgs;

    return;
}


void thread_inc_ang( float *joint, float inc, float target, int speed, float *angle ) {
    move_args* moveArgs = new move_args;
    moveArgs->joint  = joint;
    moveArgs->inc    = inc;
    moveArgs->angle  = angle;
    moveArgs->target = target;
    moveArgs->speed  = speed;

    Thread move( increment, moveArgs );
    move.terminate();  // Will not work, terminates before it actually starts
    return;
}

Update:

I make thread_inc_ang return a pointer to the <<code>>Thread move<</code>> in which the process it returns to eventually calls move->terminate, then delete move, to no effect. I still run into the same problem where it is not initializing any threads after the first 4 times it calls this.