Robot is spinning in circles when I'm moving forward

My robot keeps spinning in circles when I’m moving foward, but when I’m moving back it goes just fine.

I can probably help you with this issue, but please publish your code. If it is in blocks take a screenshot of that, and if is in vexcode V5 text or vexcode Pro paste it in. Remember to put three backticks (```) before if you are pasting in text so it is in a legible format. :grinning:

1 Like

the main.cpp is:

#include "vex.h"

using namespace vex;

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

//drives foward for 300 millimeters. 
 Drivetrain.driveFor(300, mm);

 //drives reverse for 300 millimeters. 
 Drivetrain.driveFor(-300, mm);
}

and the robot-config.cpp is:

#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
motor LeftDriveSmart = motor(PORT1, ratio18_1, false);
motor RightDriveSmart = motor(PORT10, ratio18_1, true);
drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 319.19, 295, 40, mm, 1);
controller Controller1 = controller(primary);

// VEXcode generated functions
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
// define variables used for controlling motors based on controller inputs
bool DrivetrainLNeedsToBeStopped_Controller1 = true;
bool DrivetrainRNeedsToBeStopped_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 + Axis1
      // right = Axis3 - Axis1
      int drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
      int drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
      
      // check if the value is inside of the deadband range
      if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
        // check if the left motor has already been stopped
        if (DrivetrainLNeedsToBeStopped_Controller1) {
          // stop the left drive motor
          LeftDriveSmart.stop();
          // tell the code that the left motor has been stopped
          DrivetrainLNeedsToBeStopped_Controller1 = false;
        }
      } else {
        // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
        DrivetrainLNeedsToBeStopped_Controller1 = true;
      }
      // check if the value is inside of the deadband range
      if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
        // check if the right motor has already been stopped
        if (DrivetrainRNeedsToBeStopped_Controller1) {
          // stop the right drive motor
          RightDriveSmart.stop();
          // tell the code that the right motor has been stopped
          DrivetrainRNeedsToBeStopped_Controller1 = false;
        }
      } else {
        // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
        DrivetrainRNeedsToBeStopped_Controller1 = true;
      }
      
      // only tell the left drive motor to spin if the values are not in the deadband range
      if (DrivetrainLNeedsToBeStopped_Controller1) {
        LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
        LeftDriveSmart.spin(forward);
      }
      // only tell the right drive motor to spin if the values are not in the deadband range
      if (DrivetrainRNeedsToBeStopped_Controller1) {
        RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
        RightDriveSmart.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);
}

edit: code tags added by mods, guess you missed that request,

I can’t really tell what is wrong with your code. However, I will have another look at it and check again. If you can tell a bit more about the way your robot is behaving, especially which way it is spinning and if both motors are moving when you try to go forward, it would be easier to pinpoint the error. Maybe someone else with more expertise will have a look at your code in the meantime. :grinning:

1 Like

your left/right motors are probably not reversed correctly

1 Like

It would also spin in circles going backward then.

I see what you’re going for with your dead band, but I think the ‘toggling’ motor stops could be the source of the problem here. When I code a dead band, it usually looks like this:

int Axis1 = 0;
int Axis3 = 0;

while (true) {
   if (absolutevalue(ControllerAxis1)>5) {
      Axis1 = ControllerAxis1;
   } else {
      Axis1 = 0;
   }
   
   if (absolutevalue(ControllerAxis3)>5) {
         Axis3 = ControllerAxis3;
      } else {
         Axis3 = 0;
      }

   set right motor velocity = Axis1 - Axis3;
   set left motor velocity = Axis1 + Axis3;
}

I’m typing this from memory on a mobile phone, so syntax and formatting won’t be accurate. But the main points are, using the absolute value function instead of an && will make it easier to get right. Also, this removes the need for a stop toggle.

1 Like