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.
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.
, 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:
stage = 1;
while(stage < 3)
if(stage == 1)
//these two functions operate simultaneously as long as they aren’t composed of while loops
//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;
if(stage == 2)
if(grabValue != grabValueP) //while loop ends when the grabValue changes
stage = 3;
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!