VEX V5 Text Robot Movement Code is Not Working Properly. Any Help Would Be Greatly Appreciated

Our team is attempting to code our mecanum wheels but we are finding that our code does not seem to be function properly.

The left joystick when turned up makes the robot turn left.
The left joystick when turned down makes the robot turn right.
The right joystick when turned right moves the robot backwards.
The right joystick when turned left moves the robot forward.

The side to side commands should be the right joystick, but the robot isn’t reading this code.

Here is our current code:

#include "vex.h"

using namespace vex;


void vexcodeInit( void ) {
  task rc_auto_loop_task_Controller1(rc_auto_loop_callback_Controller1);
}



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

vex::motor LeftFrontMotor = vex::motor( vex::PORT1, true);
vex::motor LeftBackMotor = vex::motor( vex::PORT2, true);
vex::motor RightFrontMotor = vex::motor( vex::PORT3);
vex::motor RightBackMotor = vex::motor( vex::PORT4);
vex::motor LeftIntakeMotor = vex::motor( vex::PORT5, true);
vex::motor RightIntakeMotor = vex::motor( vex::PORT6);
vex::controller Controller1 = vex::controller();

Controller1.Screen.print("ZOOMER ATTACK!!");
  
   //Forward Movement 
  RightFrontMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct );
  LeftFrontMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct );
  RightBackMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct );
  LeftBackMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct );


  //Backward Movement
  RightFrontMotor.spin(vex::directionType::rev, Controller1.Axis3.position(), vex::velocityUnits::pct );
  LeftFrontMotor.spin(vex::directionType::rev, Controller1.Axis3.position(), vex::velocityUnits::pct );
  RightBackMotor.spin(vex::directionType::rev, Controller1.Axis3.position(), vex::velocityUnits::pct );
  LeftBackMotor.spin(vex::directionType::rev, Controller1.Axis3.position(), vex::velocityUnits::pct );
  

  //Left Movement
  LeftFrontMotor.spin(vex::directionType::rev, Controller1.Axis1.position(), vex::velocityUnits::pct );
  LeftBackMotor.spin(vex::directionType::rev, Controller1.Axis1.position(), vex::velocityUnits::pct );
  RightFrontMotor.spin(vex::directionType::rev, Controller1.Axis1.position(), vex::velocityUnits::pct );
  RightBackMotor.spin(vex::directionType::rev, Controller1.Axis1.position(), vex::velocityUnits::pct );
    

  //Right Movement
  LeftFrontMotor.spin(vex::directionType::fwd, Controller1.Axis1.position(), vex::velocityUnits::pct );
  LeftBackMotor.spin(vex::directionType::rev, Controller1.Axis1.position(), vex::velocityUnits::pct );
  RightFrontMotor.spin(vex::directionType::rev, Controller1.Axis1.position(), vex::velocityUnits::pct );
  RightBackMotor.spin(vex::directionType::fwd, Controller1.Axis1.position(), vex::velocityUnits::pct );
      
//Turn Left
if(Controller1.ButtonL1.pressing()) {
  LeftFrontMotor.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);
  LeftBackMotor.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);
  RightFrontMotor.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
  RightBackMotor.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
} else {
  Drivetrain.stop();
}
//}

//Turn Right
if(Controller1.ButtonR1.pressing()) {
  LeftFrontMotor.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
  LeftBackMotor.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
  RightFrontMotor.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);
  RightBackMotor.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);
} else {
  Drivetrain.stop();
}
//

}

You have several problems with your code.

First, you are missing usercontrol() with its while() loop.
Second, you are mixing buttons and joystick controls.

Please, refer to these topics for examples on how to implement usercontrol()

and then mecanum controls inside its while loop

Also, if you haven’t seen it this is very helpful explanation of the drivetrain class

https://api.vexcode.cloud/v5/html/classvex_1_1drivetrain.html

4 Likes

Ok so I have multiple questions:

First, where would I be putting the usercontrol() statements? And are they required in a competition setting? It seems easier to just have the program be able to obey the joysticks and simply have 1 button that if pressed activate the autonomous period or the steps we have in mind for the autonomous period.

I see you also linked code to set up the driving. Is this code better than the code I have already implemented?

Would you be able to directly edit parts of the code to incorporate these while loops, as we are unsure how to incorporate the advice you have given us?

Thanks for your help,
BCC Robotics

Yes, it is important to have proper competition template with separate autonomous() and usercontrol() function. Otherwise your robot is not going to work with field control system and will be DQed from competition.

Please, follow this tutorial for setting up your code in a proper way.

Then just cut and paste mecanum control code that I quoted into while(1) loop of your user control function.

You can still call autonomous() function from usercontol() function when some button is pressed.

Once you get it going, you may want to read some more advanced stuff like the avoiding deadzone

4 Likes

Will simply copy and pasting this code into the while statement allow our robot to function or will we need to add other code (not including the autonomous code and the intake motor code)?

Also, how do I choose when to activate the automous code vs the regular code?

Yes, drive motor commands are copy and paste, but if you have additional manual controls tied to various buttons you will need to add them.

For example, when user presses certain button on the controller you can call autonomous function. This way you don’t need competition switch to test your autonomous.

Only the side to side motion is working with the code you have given us. Oddly, the forward, backward, and turn motions are not functioning. They are giving us this error. Any help would be greatly appreciated as to how to resolve this error and finish our code. 10 PM

Those are warnings. Warnings are not errors and do not prevent your code from compiling, though you may still have an error.

If you’d like help debugging code that won’t compile, please copy and paste your entire program into a reply, remembering to wrap it in [code]...[/code] tags for formatting. Please also include the entire contents of the “output” tab that are generated when you try to compile the program.

1 Like

Our code:

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       BCC RoboBarons                                                       */
/*    Created:      Fri Jan 24 2020                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Drivetrain           drivetrain    1, 2, 3, 4      
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here
vex::motor frontLeft = vex::motor( vex::PORT1, true);
vex::motor backLeft = vex::motor( vex::PORT2, true);
vex::motor frontRight = vex::motor( vex::PORT3);
vex::motor backRight = vex::motor( vex::PORT4);
vex::motor LeftIntakeMotor = vex::motor( vex::PORT5, true);
vex::motor RightIntakeMotor = vex::motor( vex::PORT6);
vex::controller Controller = vex::controller();
/*---------------------------------------------------------------------------*/
/*                          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) {
  // ..........................................................................
  // 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) {
    // 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.

    int left = Controller.Axis3.position(vex::percent);
    int right = Controller.Axis2.position(vex::percent);
    int sideways = Controller.Axis4.position(vex::percent);

    //you can also calculate this...
    int turn =    (right - left) * 0.5;
    int forward = (right + left) * 0.5;

    frontRight.spin(vex::forward, right - sideways, vex::percent);
    frontLeft.spin(vex::forward,  left  + sideways, vex::percent);
    backRight.spin(vex::forward,  right + sideways, vex::percent);
    backLeft.spin(vex::forward,   left  - sideways, vex::percent);
    // ........................................................................
    // 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);
  }
}

Our output:

[info]: Saving Project ...
[info]: Project saved!
unix build for platform vexv5
CXX src/main.cpp
src/main.cpp:87:9: warning: unused variable 'turn' [-Wunused-variable]
    int turn =    (right - left) * 0.5;
        ^
src/main.cpp:88:9: warning: unused variable 'forward' [-Wunused-variable]
    int forward = (right + left) * 0.5;
        ^
2 warnings generated.
CXX src/robot-config.cpp
LINK build/CompetitionCode.elf
   text	   data	    bss	    dec	    hex	filename
  11952	   1092	1064968	1078012	 1072fc	build/CompetitionCode.elf

Based on the contents of the output tab, that code appears to compile fine, and I just verified that by compiling it on my system.

What exactly is the problem you’re experiencing with this code?

1 Like

Our code’s goal is to allow the robot to perform all movements with its mecum wheels. It works when we attempt to move the robot side to side without turning (courtesy of our mecum wheels). But unfortunately, the forward, backward, and turning movements are not working. Only some of the wheels move when we attempt to move the joysticks in the direction of these functions.

You may find it helpful to read through this thread for info about mecanum wheel control schemes, particularly this post:

2 Likes

Could you please explain to me why this problem is occurring as I am almost certain this is the cause of the problem (since the forward and turn are not working and these are the ones popping up with the error)? And potentially how to fix it.

The warnings you receive

src/main.cpp:87:9: warning: unused variable 'turn' [-Wunused-variable]
    int turn =    (right - left) * 0.5;
        ^
src/main.cpp:88:9: warning: unused variable 'forward' [-Wunused-variable]
    int forward = (right + left) * 0.5;
        ^

just mean that you are declaring two variables called turn and forward, but not actually referring to them anywhere in the program (hence “unused variable”).

I would suggest that you read the post I linked above and implement the described control scheme.

2 Likes

@BCC_Robo_Barons, your code looks fine at a glance. You have to give more detailed debugging information, before people could help you further.

You first need to confirm that two left wheels spin forward if you lift your robot up in the air and push Axis3 control stick forward. Similarly, when you move Axis2, you should expect both right wheels to move in the correct direction.

If both of those check out and you still don’t have correct movement when you put robot on the ground, you need to ensure that mecanum wheels are installed correctly.

6 Likes

I can confirm that the wheels are composed properly along with the wheels you mentioned spinning when I move Axis 2 & 3

Well, if you can confirm that, while robot is lifted in the air, all individual wheels spin in proper direction when you push on control sticks and that the wheels are installed correctly, then what happens when you place the robot on the ground?

If it doesn’t move forward as expected, then the next thing to check is that all 4 mecanum wheels are contacting the ground evenly and that they all have identically geared (colored) cartridge inserts.

Other than that, if none of the wheels somehow have a ton of extra friction, I don’t really know what else it could be. You would need to provide a video footage of the robot not driving correctly with some detailed views of the chassis underside.

4 Likes

Ok I will compile this ASAP. I will likely have it by 5pm tomorrow.

Hi, thank your for your help with this issue. Fortunately, we were able to fix it ourselves.

Are you able to hit a button to run your autonomous period in a competition, or is it not allowed to have contact with the remote even to run the autonomous?

Using the controller in autonomous period is prohibited. Hence the wording autonomous period.
A fifteen second (0:15) time period during which Robots operate and react only to sensor inputs and to commands pre-programmed by the Students into the Robot control system.
To make a program that runs during autonomous, you can use the competition template and place the code in the autonomous function.
Side note, You can make autonomous movements to use in driver control.

2 Likes