V5 X-drive need help

Here is my code this is my first year in c++ so i don’t fully know everything but i think i understand the basics

if(Controller1.Axis3.position(percent)>30)

Motor1.spin(forward);Motor3.spin(forward);Motor2.spin(reverse);Motor4.spin(reverse);

if(Controller1.Axis1.position(percent)>30)

Motor1.spin(forward);Motor3.spin(reverse);Motor2.spin(forward);Motor4.spin(reverse);

}

The program works but instead of turning on the right stick on axis one it is all on the left stick on axis 3 and 4
The program would work but one of the motors will not move its powered and will move freely
I want to know if that is because of the program or the motor

Pls help

20200924_122149

the back right motor isnt working thats Motor 1 in the program

1 Like

send a pic or vid of the bot

open the device page on your brain and try to spin the motor there. if it works fine then you code is causing the problem

3 Likes

The first problem to fix is this.

if(Controller1.Axis3.position(percent)>30)
   Motor1.spin(forward);Motor3.spin(forward);Motor2.spin(reverse);Motor4.spin(reverse);

C does not care about whitespace( spaces, tabs, newline etc.) so just by putting everything on one line does mean it will be part of the if statement, you could have written it like this (still wrong).

if(Controller1.Axis3.position(percent)>30)
    Motor1.spin(forward);
    Motor3.spin(forward);
    Motor2.spin(reverse);
    Motor4.spin(reverse);

only the first line will run when the if condition is true, all other motors will spin at the default velocity.
so change that code so all motors are part of the conditional by adding braces.

if(Controller1.Axis3.position(percent)>30) {
    Motor1.spin(forward);
    Motor3.spin(forward);
    Motor2.spin(reverse);
    Motor4.spin(reverse);
}
7 Likes

i have changed the code to that with the braces on it still only powers one motor like you said here is the knew code

if(Controller1.Axis3.position(percent)>30){

Motor1.spin(forward);

Motor3.spin(forward);

Motor2.spin(reverse);

Motor4.spin(reverse);

}

if(Controller1.Axis1.position(percent)>30){

Motor1.spin(forward);

Motor3.spin(reverse);

Motor2.spin(forward);

Motor4.spin(reverse);

}

}

oddly its not powering motor 1 First in line of code which is the same motor that wouldn’t move the first time i posted Its moving motor 3
Wondering if its just random or if its something different???

You should keep in mind that since you’ve used 2 if statements that both power the motors, the second loop will override the first (so whatever values get set to the motors in the first if statement will get changed in the second if statement, rendering the first one useless). Instead, you should add the values of the joystick values. Here’s some pseudocode:

while(true) {
  front left power = axis3 + axis1;
  front right power = axis3 - axis1;
  back left power = -axis3 + axis1;
  back right power = -axis3 - axis1;
}

This should set the motor power based off of both axes, instead of just Axis1, which is what was happening in the code you posted.

As for why some motors aren’t moving, first make sure all of your wires are in the right ports. Then try to power each motor directly from the brain (you can do this by clicking on the motor under “devices”). There might be an issue with your axles, port, wire, or motor so check everything and see if you can locate the problem.

1 Like