Skip to main content

Multi-Tasking (Parallel Computing)

Now here's where coding gets a lot more complicated. Typically once you start being very competitive, you want to increase the speed of your robot. The easiest way that this can be done by having the robot now do multiple things at once. In computing terms this is refered to as Asynchrony or Parallel Computing (Often used interchangably).

Up to this point you are likely used to code running line-by-line, where a line must complete before moving onto the next one - since most functions operate in a similar way, often times when programmed with this methodology, this means that a drive function must wait for an arm raising function to complete or vise-versa. That super simple example can be illustrated below:

void AutoControl(void) {
driveFunction(700, 60); // The robot will rotate wheels forward by 700 degrees at 60 pct motor speed.
armMotor.spintoPosition(135); // Waiting for driveFunction to finish, The arm will then rotate 135 degrees
}

// --- OR ---

void AutoControl(void) {
armMotor.spintoPosition(135); // The arm will rotate 135 degrees
driveFunction(700, 60); // Waiting for spintoPosition to finish, The robot will then rotate wheels forward by 700 degrees at 60 pct motor speed.
}

Built-in Motor Functions

To ease you into the concepts, we can start with some of the built-in commands. Luckily for us, the Motor class actually comes with some built-in parallezation. This can be seen in the spintoPosition() function and the spinFor() function.

Blocking/Non-Blocking Functions

When functions are required to wait for another to finish, they are called Blocking Functions. Similarly, when they do not have to wait for another function to finish they are called Non-Blocking.

Motor.spinFor(fwd, 45);        // This command is Blocking by default
Motor.spinFor(fwd, 45, true); // This command is now Blocking
Motor.spinFor(fwd, 45, false); // This command is now non-Blocking

Motor.spintoPosition(45); // This command is Blocking by default
Motor.spintoPosition(45, true); // This command is now Blocking
Motor.spintoPosition(45, false); // This command is now non-Blocking

Now that we know that Non-Blocking functions exist for motors (and motor groups), we now can implement some easy parallazation. Here is that example before, but now the arm will lift, while the robot is driving forward.

void AutoControl(void) {
armMotor.spintoPosition(135, false); // Now it is non-blocking, The arm will rotate 135 degrees
driveFunction(700, 60); // No waiting, The robot will then rotate wheels forward by 700 degrees at 60 pct motor speed.
}

While this can be very helpful, it is important to note some of the downfalls!

  • More difficulty in programming autonomous
  • Could instigate consistency issues
  • Motors may not behave as you want them to while en-route.

Threads/Tasks