Help completing functions asynchronously?

I’ve written a lot of functions for my robot for it’s autonomous but I have the problem that they complete synchronously, while I need them to complete asynchronously. For example.


Open();
Drivestraight(3, 64);
Grab();
wait(1);

These functions complete all at once for one second, which Is not desireable. I want the open() and Drivestraight() functions to complete before the next one begins, then the Grab() function to stop when I change the claw’s values. If I need to, I’ll post some more code.

I don’t really see what tasks or a PID controller will do for you here.
You need to write those functions in such a way that they block until complete. Eg. put a while loop in each that sleeps until a certain condition is met (assuming that you have sensors on all your subsystems).
If you post the code for the functions, we can give me specific help.

@Highwayman I misread the question, you are correct. However, making the functions blocking does not allow for multiple actuations at the same time. For example, if you wanted to raise your arm while you were driving towards the fence, you would have to use a somewhat bizarre combination of tasks. It would be better to just sleep after you set the nonblocking function into action

As I understand it, not having multiple things happening at the same time is exactly what the OP wants, but you’re right, that is a limitation/problem with what I suggested. Using sleep doesn’t really allow for sensors, but I see what you’re saying.
In that case, A better, somewhat more complicated way of doing it is creating additional functions that wait for whatever action to complete. That way you can start multiple actions before waiting for completion.

@Highwayman You could implement a custom sleep type function which checks a sensor and sleeps until a criterion is met. For example,


waitUntil(&tickNumber, "greater than", desiredAmount)

, or similar syntax. (By the way, my automatic opposition to blocking functions is reactionary; I come from the world of web development, where blocking function means the user cannot interact with the UI, however in embedded systems, this doesn’t matter)

There are lots of ways of doing it, but what I personally like doing is putting the entire autonomous section into a while loop and creating a variable called stage. It looks like this in pseudo code:

int stage;
stage = 1;
resetEncoders();

while(stage < 3)
{
if(stage == 1)
{
//these two functions operate simultaneously as long as they aren’t composed of while loops
Open();
DriveStraight(3, 64);

  //this is where you would put the parameters for when you want the next section of your code to start
    if(drive and claw have reached their destination)
    {
        stage = 2;
        resetEncoders();
    }
 }
if(stage == 2)
{
    driveStop(); 
    grab();
    if(grabValue != grabValueP)  //while loop ends when the grabValue changes
    {
        stage = 3;
    }
}

}
allMotorsStop();

you might have to change your functions a bit, but it will allow you to do multiple actions while simultaneously referencing your sensors. Hope this helps!

Thanks for the help everyone!
I’ve fixed my problem. The autonomous code stayed the same, I made all my changes in the functions.


void Open()
{
	while(SensorValue[rightpot] > MaxOpened)
	{
		motor(port5) = 127;
		motor(port6) = 127;
		wait(0.1);
	}
	motor(port5) = 0;
	motor(port6) = 0;
}

You see there is a while loop which the cortex gets stuck on so that the bot does not move forward until it the claw has opened all the way. This fixed my problem.

void TurnLeft(int turnticksleft)
{
	SensorValue[leftencode] = 0;
  SensorValue[rightencode] = 0;
	while(SensorValue[rightencode] < turnticksleft)
	{
  	motor(port7) = -64;
		if(leftencode > (rightencode * -1) + 10)
		{
			motor(port4) = 74;
		}
		else if(leftencode < (rightencode * -1) - 10)
		{
			motor(port4) = 54;
		}
		else
		{
			motor(port4) = 64;
		}
	}
	motor(port7) = 0;
	motor(port4) = 0;
}

I did the same thing in my code for consistent turning.