We are having issues getting tasks working in VexCode C++. We read the forum posts and VEX API reference but just can’t seem to get it to work. We were able to get the sample multi-thread example working with 2 motor functions we wanted if we preset the value. Once we tried to set it up as parameters, we just couldn’t get it to work. Here’s the main code we have, not sure if we are even on the right track.

Basically, we are trying to control 2 motors so they can move simultaneously. As we have several positions we need to set with enums, we need to pass parameters when we call it. The parameters we need to set each time we call it are speed, position.

Part of our utils.cpp

``````// Function to calculate wheel diameter in centimeters and wheel circumference
void getWheelProperties(double &wheelCircumferenceCm, double &gearRatio) {
double wheelDiameterInches = 3.25; // in inches
double wheelDiameterCm = wheelDiameterInches * 2.54; // convert to cm
wheelCircumferenceCm = wheelDiameterCm * M_PI; // in cm
gearRatio = 6.0; // 6:1 gear ratio
}

// Implementation of the spinToPosition function for armMotor
void spinArmToPosition(int position, int power) {
armMotor.spinToPosition(position, rotationUnits::deg, power, velocityUnits::pct, false);
}

// Implementation of the spinToPosition function for elbowMotor
void spinElbowToPosition(int position, int power) {
elbowMotor.spinToPosition(position, rotationUnits::deg, power, velocityUnits::pct, false);
}

// Create a function that will be used as a thread to control the arm motor.
MotorParams* motorParams = static_cast<MotorParams*>(params);
spinArmToPosition(motorParams->position, motorParams->power);
}

// Create a function that will be used as a thread to control the elbow motor.
MotorParams* motorParams = static_cast<MotorParams*>(params);
spinElbowToPosition(motorParams->position, motorParams->power);
}

Utils.h (just the relevant code)

struct MotorParams {
int position;
int power;
};

driver.cpp (just the relevant code)
// Example control structure
if (Controller.ButtonR1.pressing()) {
if (!wasR1Pressed) {
MotorParams* params = new MotorParams{-200, 100};
// Use params as needed, e.g., control motor speed
wasR1Pressed = true;
}
} else {
wasR1Pressed = false;
}

// Declaration of the thread functions
``````

So where do you actually create and start the thread ?

I also recommend against using “new” unless you are prepared to handle using “delete” to free the memory.

it also seems that if you are then going to call functions like this

``````elbowMotor.spinToPosition(position, rotationUnits::deg, power, velocityUnits::pct, false);
``````

which you have set to be non blocking there’s no point calling that from a thread at all.

1 Like

Honestly, we’re really lost on this. We’ve been told by others that we should use task instead and don’t use void, use int instead. I don’t know if there’s an example somewhere of a simple spinToPosition task for driver control. I couldn’t find it. It also seems more finicky in driver control. We had it lock the entire controller once or arm just moves super slow.

Here’s an example code with passing params (if I made a mistake let me know):

``````#include "vex.h"

using namespace vex;

// Define a struct to hold various types of parameters
struct Params {
int a;
float b;
bool c;
double d;
char e;
std::string f;  // Note: You may need to include <string> for this
};

int exampleFunction(void* params) {
Params* p = static_cast<Params*>(params);
// Function implementation
Brain.Screen.print("Integer: %d, Float: %.2f, Boolean: %s, Double: %.3f, Char: %c, String: %s\n",
p->a, p->b, p->c ? "true" : "false", p->d, p->e, p->f.c_str());
return 0; // VEXCode requires an int return type
}

int main() {
// Initialize Robot Configuration
vexcodeInit();

// Initialize the struct with parameters
Params myParams = {42, 3.14f, true, 2.718, 'A', "Hello"};

// Create and start the task, passing the struct as a parameter