Drive Speed Joysticks

So I have a portion of my code using the joysticks below:

  FrontLeftDriveMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct);
  BackLeftDriveMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct);
  FrontRightDriveMotor.spin(vex::directionType::fwd, Controller1.Axis2.position(), vex::velocityUnits::pct);
  BackRightDriveMotor.spin(vex::directionType::fwd, Controller1.Axis2.position(), vex::velocityUnits::pct);

I am trying to change the arm speed. I have the code below for the value I want it to be:

int armSpeedPCT = 50;

How do I get the armSpeedPCT into the function. I want my drive motors to drive at 50% and I can’t figure out the syntax to do it.

You can multiply the joystick value by a number. If you want it to go at half speed multiply by 0.5

To put armSpeedPCT in a function you would put it like this

Motor.spin(forward, armSpeedPCT, pct);

1 Like

Would that still work with the joystick through, I am trying to do it with a joystick. I know how to change speeds with a button, its when I use the joystick that I can’t get it to work.

Yes if you multiply a joystick value by a number, it will work. It will make all joystick values half of what they actually are

I am a little confused where this part of the code would go:


Doesn’t it need that for the joystick to work? I’m pretty much confused on the actual syntax of it and what it would exactly look like.

So if your making a motor spin with joysticks it would be:

Motor.spin(vex::directionType::fwd, Controller2.Axis3.position() *0.5, vex::velocityUnits::pct);

In that line of code you can see the motor is set to spin forward. The speed of the motor goes at is half of the joystick. If the joystick is at the value a 100, the motor will spin at 50.


Quick question: Why would you not just set velocity of motors directly?

Included is a short program based on your response, using your method, where I have commented out the method of directly setting the velocity of the motors.

#include "robot-config.h"

using namespace vex;
vex::brain Brain;
vex::motor LeftMotorFront  (vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor LeftMotorBack  (vex::PORT2, vex::gearSetting::ratio18_1, false);
vex::motor RightMotorFront (vex::PORT9, vex::gearSetting::ratio18_1, true);
vex::motor RightMotorBack (vex::PORT10, vex::gearSetting::ratio18_1, true);
vex::controller Controller1 = vex::controller();

Robot Configuration:
[Smart Port]    [Name]        		[Type]           [Description]       		[Reversed]
Motor Port 1    LeftMotorFront     	V5 Smart Motor   Left side Front motor     	false
Motor Port 2	LeftMotorBack		V5 Smart Motor	 Left side Back motor		false
Motor Port 9    RightMotorFront    	V5 Smart Motor   Right side Front motor    	true
Motor Port 10	RightMotorBack		V5 Smart Motor	 Right side Back motor

/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++++
Topic: 		 Tank Drive Control for Joysticks with a Power Reducer Percentage
Description: This program will instruct your robot to move forward at a reduced power percentage of
			 the user choosing based on power reducer variable (PRV).
			 Program does not set velocity of motors directly.
			 There is a two second pause at the beginning of the program.

float PRV = 0.70; // Power Reduction Factor. Power set to 70% of MAX.

int main() {
	// Wait 2 seconds or 2000 milliseconds before starting the program.
	// Print to the screen that the program has started.	
    Brain.Screen.print("Drive Power Reducer Program has Started.");        

	/*---------- THIS PROGRAM DOES NOT USE THIS METHOD ----------*\
   	// Set the velocity of the left and right motor to 50% power. 
	// This command will not make the motor spin.
    LeftMotorFront.setVelocity(50, vex::velocityUnits::pct);
	LeftMotorBack.setVelocity(50, vex::velocityUnits::pct);
    RightMotorFront.setVelocity(50, vex::velocityUnits::pct);
	RightMotorBack.setVelocity(50, vex::velocityUnits::pct);

    while(true) {
	// Commands from Joysticks to controll motors	
    LeftMotorFront.spin(vex::directionType::fwd, Controller1.Axis3.value() *PRV, vex::velocityUnits::pct); 	// (Axis3+Axis4)/2
    LeftMotorBack.spin(vex::directionType::fwd, Controller1.Axis3.value() *PRV, vex::velocityUnits::pct); 		// (Axis3+Axis4)/2
	RightMotorFront.spin(vex::directionType::fwd, Controller1.Axis2.value() *PRV, vex::velocityUnits::pct);	// (Axis3-Axis4)/2
	RightMotorBack.spin(vex::directionType::fwd, Controller1.Axis2.value() *PRV, vex::velocityUnits::pct);		// (Axis3-Axis4)/2
    // Print to the brain's screen that the program has ended.
    Brain.Screen.newLine();	// Move the cursor to a new line on the screen.
    Brain.Screen.print("Drive Power Reducer Program has Ended.");

    // Prevent main from exiting with an infinite loop.
    while(1) {
      vex::task::sleep(100);	// Sleep the task for a short amount of time to prevent wasted resources.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.