X Drive Movement

I am using X Drive currently, I have it so the controller axis 3 is going front and back and that works, and controller axis 1 spins the bot and that works, but axis 4 also spins the bot for some reason instead of driving sideways. Here is my code

#include "vex.h"

using namespace vex;
using signature = vision::signature;
using code = vision::code;

// A global instance of brain used for printing to the V5 Brain screen
brain  Brain;

// VEXcode device constructors
controller Controller1 = controller(primary);
motor leftFront = motor(PORT1, ratio18_1, false);
motor leftBack = motor(PORT2, ratio18_1, false);
motor rightBack = motor(PORT9, ratio18_1, true);
motor rightFront = motor(PORT10, ratio18_1, true);

// VEXcode generated functions
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
// define variables used for controlling motors based on controller inputs
bool DrivetrainNeedsToBeStopped_Controller1 = true;

// define a task that will handle monitoring inputs from Controller1
int rc_auto_loop_function_Controller1() {
  // process the controller input every 20 milliseconds
  // update the motors based on the input values
  while(true) {
    if(RemoteControlCodeEnabled) {
      // calculate the drivetrain motor velocities from the controller joystick axies
      // left = Axis3 + Axis4
      // right = Axis3 - Axis4
      int leftFrontSpeed = Controller1.Axis3.position() + Controller1.Axis4.position() + Controller1.Axis1.position();
      int leftBackSpeed = Controller1.Axis3.position() + Controller1.Axis4.position() + Controller1.Axis1.position();
      int rightFrontSpeed = Controller1.Axis3.position() - Controller1.Axis4.position() - Controller1.Axis1.position();
      int rightBackSpeed = Controller1.Axis3.position() - Controller1.Axis4.position() - Controller1.Axis1.position();
      
      // check if the values are inside of the deadband range
      if (abs(leftFrontSpeed) < 5 && abs(rightFrontSpeed) < 5 && abs(leftBackSpeed) < 5 && abs(rightBackSpeed) < 5) {
        // check if the motors have already been stopped
        if (DrivetrainNeedsToBeStopped_Controller1) {
          // stop the drive motors
          leftFront.stop();
          leftBack.stop();
          rightFront.stop();
          rightBack.stop();
          // tell the code that the motors have been stopped
          DrivetrainNeedsToBeStopped_Controller1 = false;
        }
      } else {
        // reset the toggle so that the deadband code knows to stop the motors next time the input is in the deadband range
        DrivetrainNeedsToBeStopped_Controller1 = true;
      }
      
      // only tell the right drive motor to spin if the values are not in the deadband range
      if (DrivetrainNeedsToBeStopped_Controller1) {
       leftFront.setVelocity(leftFrontSpeed, percent);
        leftFront.spin(forward);
        rightFront.setVelocity(rightFrontSpeed, percent);
        rightFront.spin(forward);
        leftBack.setVelocity(leftBackSpeed, percent);
        leftBack.spin(forward);
        rightBack.setVelocity(rightBackSpeed, percent);
        rightBack.spin(forward);
      }
    }
    // wait before repeating the process
    wait(20, msec);
  }
  return 0;
}

/**
 * Used to initialize code/tasks/devices added using tools in VEXcode Pro.
 * 
 * This should be called at the start of your int main function.
 */
void vexcodeInit( void ) {
  task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
}

Check your signs when calculating your velocities.

You have:

leftFront=Axis3+Axis4+Axis1
leftBack=Axis3+Axis4+Axis1
rightFront=Axis3-Axis4-Axis1
rightBack=Axis3-Axis4-Axis1

I believe it should be

leftFront=Axis3+Axis4+Axis1
leftBack=Axis3+Axis4-Axis1
rightFront=Axis3-Axis4-Axis1
rightBack=Axis3-Axis4+Axis1

Try that and see if it works!

E: Mixed around Axis 1 and Axis 4, see below response

4 Likes

Ok so now axis 1 makes it go side to side but its a little off, like it doesnt go straight side to side it kinda curves, axis 4 still spins it.

Edit: Changing it to the code below fixed it but going side to side still isnt curves instead of going straight.

      int leftFrontSpeed = Controller1.Axis3.position() + Controller1.Axis4.position() + Controller1.Axis1.position();
      int leftBackSpeed = Controller1.Axis3.position() - Controller1.Axis4.position() + Controller1.Axis1.position();
      int rightFrontSpeed = Controller1.Axis3.position() - Controller1.Axis4.position() - Controller1.Axis1.position();
      int rightBackSpeed = Controller1.Axis3.position() + Controller1.Axis4.position() - Controller1.Axis1.position();

Yes, sorry about that. I mixed around your intentions for Axis 1 and Axis 4, my bad.

If it’s curving slightly that means it is probably a physical issue. Perhaps one or more of your wheels has more friction than the others on your base.

For example, when my team built an X drive a few years ago, we had a similar issue with the drive curving slightly. Turns out one of the screws was loose and a wheel was rubbing up against the metal, causing it to go a little slower than the rest of the drive…

Does the robot go perfectly straight when going forward?

2 Likes

Yes it goes perfectly straight going forward

edit: Centered weight, everything works now, thank you!

1 Like