Search code examples
multithreadingrobotc

RobotC: Multithreading in TeleOp (controlling mode)


I am making a program for a robot in a competition, and need to multithread.

When I make a second task (task two()) and try to start (startTask) it with a button press from a controller, it just executes the first statement of the task and only as long as the button is pressed, instead of the whole block. I've tried many things including putting a loop in the second task also, using a function instead of a task and sleeping for 200 milliseconds before, and after the startTask(two); function, but the same thing happens every time.

I can't post my program because I don't want other people to steal it, sorry.

What edits will make it run the whole block? Any help would be appreciated.


Solution

  • Since this is Controller Mode, I'm assuming that you are setting the motors to stop when the corresponding button is not pressed.

    if([...])
    [...]
    else
    {
       setMotorSpeed(motor10, 0);
    }
    

    This is the cause for the stopping of the motors when you release. All of the other methods that you tried had nothing to do with this, so they shouldn't have worked.

    You need to put something like this:

    int  Motor10Speed;
    [...]
    if([...])
    [...]
    else
    {
       setMotorSpeed(motor10, Motor10Speed);
    }
    

    This will control an individual motor. Repeat this for all other motors being used.

    After that is done, make the function look something like this:

    task mini_function();
    
    task main()
    {
    [...]
    }
    
    task mini_function()
    {
    Motor10Speed = 50;
    sleep(1000);
    Motor10Speed = 0;
    }
    

    Expand the above program so it matches your current function, while using the MotorSpeed variables as setMotorSpeed variables.

    This should make you able to drive and run a function at the same time without them interrupting each other.