We are experimenting with different methods of programming in driver control and would like to use a button on the controller to toggle the functionality of the joystick. For example, when the button is not pressed the joysticks do one thing (such as moving the robot forward/backward). But then if the button is pressed, and held the joysticks cause the robot to do something else (such as strafing).
In addition to the switch statement (see code below), we’ve also tried if statements and while loops - but so far they all seem to have the same problem. Although the condition is met (tested by printing values to the controller), the joysticks only work as intended before the button state changes (in the initial mode). After the button is pressed (or released), the functions then stop depending on the state of the button.
So far we have tried putting sleep commands at the end of each section in case things are just processing too fast. We also tried using rotateFor commands instead of spin commands (with waitForCompletion set to true to make sure the commands were completing and not overlapping). None of this made any difference.
Would anyone have any other suggestions or ideas on this? Here is our most recent attempt using the switch statement.
void leftDriveForwardAndBackward() {
LeftFrontDriveMotor.setVelocity(100,velocityUnits::rpm);
LeftBackDriveMotor.setVelocity(100,velocityUnits::rpm);
LeftFrontDriveMotor.spin(vex::directionType::fwd, Controller1.Axis3.value(),vex::velocityUnits::pct);
LeftBackDriveMotor.spin(vex::directionType::fwd, Controller1.Axis3.value(),vex::velocityUnits::pct);
}
void rightDriveForwardAndBackward() {
RightFrontDriveMotor.setVelocity(100,velocityUnits::rpm);
RightBackDriveMotor.setVelocity(100,velocityUnits::rpm);
RightFrontDriveMotor.spin(vex::directionType::fwd, Controller1.Axis2.value(),vex::velocityUnits::pct);
RightBackDriveMotor.spin(vex::directionType::fwd, Controller1.Axis2.value(),vex::velocityUnits::pct);
}
void driveSideways() {
LeftFrontDriveMotor.setVelocity(100,velocityUnits::rpm);
LeftBackDriveMotor.setVelocity(100,velocityUnits::rpm);
RightFrontDriveMotor.setVelocity(100,velocityUnits::rpm);
RightBackDriveMotor.setVelocity(100,velocityUnits::rpm);
LeftFrontDriveMotor.spin(vex::directionType::fwd, Controller1.Axis1.value(),vex::velocityUnits::pct);
LeftBackDriveMotor.spin(vex::directionType::rev, Controller1.Axis1.value(),vex::velocityUnits::pct);
RightFrontDriveMotor.spin(vex::directionType::rev, Controller1.Axis1.value(),vex::velocityUnits::pct);
RightBackDriveMotor.spin(vex::directionType::fwd, Controller1.Axis1.value(),vex::velocityUnits::pct);
}
void usercontrol( void ) {
int allowStrafing = Controller1.ButtonLeft.pressing();
while(true){
allowStrafing = Controller1.ButtonLeft.pressing();
vex::task::sleep(5);
switch(allowStrafing) {
case 0:
Controller1.Axis3.changed(leftDriveForwardAndBackward);
Controller1.Axis2.changed(rightDriveForwardAndBackward);
break;
case 1:
Controller1.Axis1.changed(driveSideways);
break;
}
}
}