So, i want to make a simpler way to make basic controls through a function, i want to make strings which you input into the function like group which you put in a motor group, or button where you put in a controller button.
How would i go about doing this, i tried std::string for each one but that didn’t work because i couldn’t substitute them into the actual commands.
Yes you can use strings as an argument to a function but it is not very efficient.
This is an example of what you are asking:
void drive(std::string action, int value)
{
if( action=="forward" )
{
motorL.spin(fwd, value, pct);
motorR.spin(fwd, value, pct);
}
else if( action=="reverse" )
{
motorL.spin(rev, value, pct);
motorR.spin(rev, value, pct);
}
else if( action=="turnLeft" )
{
motorL.spin(rev, value, pct);
motorR.spin(fwd, value, pct);
}
else if( action=="turnRight" )
{
motorL.spin(fwd, value, pct);
motorR.spin(rev, value, pct);
}
else if( action=="stop" )
{
motorL.stop(brake);
motorR.stop(brake);
}
}
void autonomous()
{
drive("forward",50);
vex::task::sleep(1000);
drive("turnLeft",30);
vex::task::sleep(500);
drive("stop");
}
Better way of passing commands is enum:
enum driveEnum {
eStop = 0,
eDriveTime = 1,
eTurnTime = 2,
eDriveDistance = 3,
eTurnDistance = 4
};
void drive(driveEnum action, int value, int howLong)
{
if( action==eStop )
{
motorL.stop(brake);
motorR.stop(brake);
}
else if( action==eDriveTime ) // positive value drives forward, negarive backward
{
motorL.spin(fwd, value, pct);
motorR.spin(fwd, value, pct);
if( howLong != 0 )
vex::task::sleep(howLong);
}
else if( action==eTurnTime ) // negative value turns to the left, positive to the right
{
motorL.spin(fwd, value, pct);
motorR.spin(rev, value, pct);
if( howLong != 0 )
vex::task::sleep(howLong);
}
else if( action==eDriveDistance ) // positive howLong drives forward with value speed, negarive backward
{
motorL.spinFor( howLong, deg, value, pct, false); // don't wait
motorR.spinFor( howLong, deg, value, pct, true); // wait for completion
}
else if( action==eTurnDistance ) // positive howLong turns to the right with value speed, negarive to the left
{
motorL.spinFor( howLong, deg, value, pct, false); // don't wait
motorR.spinFor( -howLong, deg, value, pct, true); // wait for completion
}
}
void autonomous()
{
drive(eDriveDistance, 50, 500); // vel=50 pct, distance=500 deg
drive(eTurnDistance, 30, -200); // vel=30 pct, turn left=200 deg in wheel units, not robot units
drive(eDriveTime, -50, 1000); // drive back with vel = 50 for 1 sec
drive(eStop);
}
Motor command reference: https://api.vexcode.cloud/v5/html/classvex_1_1motor.html
7 Likes