V5 Changing Driver Control Speed

I need help changing driver control speed in VCS. My driver wants to be able to change how fast the drive train moves (Change it’s max speed) with a simple button press. I was wondering if this was possible with a variable. I tried setting Axis Values to a variable but that didn’t seem to work.

If you have question I’ll get back to you as soon as possible. I’m sorry if this has been posted before.

I think this post should have everything you need

Yes, it is very possible with a variable. Here is an example:

(in driver loop)

        int rate = 1;

        if(Controller1.WhateverButton.pressing()){rate = 2;} //sets drive to half speed
        else if(Controller1.OtherWhateverButton.pressing()){rate = 1;} //sets drive to full speed

        FrontRight.spin(vex::directionType::fwd, (Controller1.Axis2.value()/rate), vex::velocityUnits::pct);
        FrontLeft.spin(vex::directionType::fwd, (Controller1.Axis3.value()/rate), vex::velocityUnits::pct);
        BackRight.spin(vex::directionType::fwd, (Controller1.Axis2.value()/rate), vex::velocityUnits::pct);
        BackLeft.spin(vex::directionType::fwd, (Controller1.Axis3.value()/rate), vex::velocityUnits::pct); 

And if you wanted to, for example, have one button to toggle between the two, you could do something like this:

if(Controller1.ButtonR1.pressing()){
        if(rate == 1){ 
        rate = 0;
        task::sleep(100); ///to keep from switching too quickly
        }

        else if(rate == 0){
        rate = 1;
        task::sleep(100); ///to keep from toggling too quickly
        }
}

(note with this example, unless you run it as a separate task, for the 100 ms wait time you will not be able to do anything else in the driver loop.)

4 Likes

When i try to do this for our team, when i have the value of the controller axis divided by the rate it turns it to arcade drive and everything is on the left joystick. then when i get rid of the dividing by rate it works normally but i cannot change speeds. please help.

Hooray reviving threads. @99750A Please try to make your own thread when you have problems like this. You can put the link to this thread in your new one and people will be able to help a little more. I am not sure what your problem is without your code, so if you could post it that would be great. You could have forgotten to change what axis is controlling the motors or you could have the wrong axis.

after further inspection of code i was able to find the problem. thanks for the help though

yea but would this work for people who are already using an arcade drive code?

*----------------------------------------------------------------------------*/

/* /
/
Module: main.cpp /
/
Author: VEX /
/
Created: Thu Sep 26 2019 /
/
Description: Competition Template /
/
/
/
----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----

#include “vex.h”

using namespace vex;

// A global instance of competition
competition Competition;

vex::motor motor_right= vex::motor(vex::PORT19);
vex::motor motor_left=vex::motor(vex::PORT20);
vex::motor Hand1 = vex::motor(vex::PORT11);
vex::motor Hand2 = vex::motor(vex::PORT12);
vex::motor tail =vex::motor(vex::PORT13);
vex::controller con(vex::controllerType::primary);

// define your global instances of motors and other devices here

/---------------------------------------------------------------------------/
/* Pre-Autonomous Functions /
/
/
/
You may want to perform some actions before the competition starts. /
/
Do them in the following function. You must return from this function /
/
or the autonomous and usercontrol tasks will not be started. This /
/
function is only called once after the V5 has been powered on and /
/
not every time that the robot is disabled. /
/
---------------------------------------------------------------------------*/

void pre_auton(void) {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, …
}

/---------------------------------------------------------------------------/
/* /
/
Autonomous Task /
/
/
/
This task is used to control your robot during the autonomous phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
---------------------------------------------------------------------------*/

void autonomous(void) {

motor_right.rotateFor(+1300, rotationUnits::deg, 30, velocityUnits::pct, false);
motor_left.rotateFor(-1300, rotationUnits::deg, 30, velocityUnits::pct,false);
Hand1.rotateFor(+6500,rotationUnits::deg,127,velocityUnits::pct,false);
Hand2.rotateFor(-6500,rotationUnits::deg,127,velocityUnits::pct);
motor_right.rotateFor(-900, rotationUnits::deg, 100, velocityUnits::pct, false);
motor_left.rotateFor(-900, rotationUnits::deg, 100, velocityUnits::pct);
motor_right.rotateFor(+1464, rotationUnits::deg, 40, velocityUnits::pct, false);
motor_left.rotateFor(-1464, rotationUnits::deg, 40, velocityUnits::pct);
tail.rotateFor(-1000,rotationUnits::deg,30,velocityUnits::pct);
wait(1000,msec);
tail.rotateFor(+500,rotationUnits::deg,10,velocityUnits::pct,false);
Hand1.rotateFor(-1000,rotationUnits::deg,40,velocityUnits::pct,false);
Hand2.rotateFor(+1000,rotationUnits::deg,40,velocityUnits::pct);
motor_right.rotateFor(-1000, rotationUnits::deg, 50, velocityUnits::pct, false);
motor_left.rotateFor(+1000, rotationUnits::deg, 50, velocityUnits::pct);

// Insert autonomous user code here.
// …
}

/---------------------------------------------------------------------------/
/* /
/
User Control Task /
/
/
/
This task is used to control your robot during the user control phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
---------------------------------------------------------------------------*/

void usercontrol(void) {
// User control code here, inside the loop
while (1) {
while(true){

vexcodeInit();
//Left motor, vertical axis of left joystick plus horizontal axis of left joystick
motor_left.spin(vex::directionType::fwd,con
.Axis4.position(vex::percentUnits::pct) -con
.Axis3.position(vex::percentUnits::pct),
vex::velocityUnits::pct);
//Right motor, vertical axis of left joystick minus horizontal axis of left joystick
motor_right.spin(vex::directionType::fwd,con
.Axis4.position(vex::percentUnits::pct) +con
.Axis3.position(vex::percentUnits::pct),
vex::velocityUnits::pct);

// This is the main execution loop for the user control program.

    //Elbow Motor

//Hand Motors
if (con.ButtonR1.pressing())
{
Hand1.spin(vex::directionType::fwd,127,vex::velocityUnits::rpm);
Hand2.spin(vex::directionType::rev,127,vex::velocityUnits::rpm);
}
else if (con.ButtonR2.pressing())
{
Hand1.spin(vex::directionType::rev,127,vex::velocityUnits::rpm);
Hand2.spin(vex::directionType::fwd,127,vex::velocityUnits::rpm);
}
else
{
Hand1.stop(vex::brakeType::coast);
Hand2.stop(vex::brakeType::coast);
}
if( con.ButtonA.pressing() )
{
motor_right.stop(vex::brakeType::hold );
motor_left.stop(vex::brakeType::hold );
}
else if( con.ButtonY.pressing())
{
motor_right.stop(vex::brakeType::coast );
motor_left.stop(vex::brakeType::coast );
}

if (con.ButtonB.pressing())
{
tail.spin(vex::directionType::fwd,60,vex::velocityUnits::rpm);
}
else if (con.ButtonX.pressing())
{
tail.spin(vex::directionType::rev,30,vex::velocityUnits::rpm);
}

else
{
tail.stop(vex::brakeType::hold );
}

}
} // This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo
// values based on feedback from the joysticks.

// ........................................................................
// Insert user code here. This is where you use the joystick values to
// update your motors, etc.
// ........................................................................

wait(20, msec); // Sleep the task for a short amount of time to
                // prevent wasted resources.

}

//
// Main will set up the competition functions and callbacks.
//
int main() {
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);

// Run the pre-autonomous function.
pre_auton();

// Prevent main from exiting with an infinite loop.
while (true) {
wait(100, msec);
}
}