Mecanum diagonal driving code help

So i’m making a mecanum base with my team and i’m trying to do a diagonal angle like when the left controller is going in the top left angle it would make the base go into diagonal in that angle

like that:

but my code is not working somewhy here is the code:

while (wa == 1) {
   // ColorChanging();
    FL.setVelocity(Controller1.Axis1.position() + Controller1.Axis2.position(), percent);
    FL.spin(forward);
    RR.setVelocity(Controller1.Axis1.position() + Controller1.Axis2.position(), percent);
    RR.spin(forward);
    FR.setVelocity(Controller1.Axis1.position() - Controller1.Axis2.position(),percent);
    FR.spin(forward);
    RL.setVelocity(Controller1.Axis1.position() - Controller1.Axis2.position(),percent);
    RL.spin(forward);
    FL.setVelocity(Controller1.Axis3.position() + Controller1.Axis1.position() + Controller1.Axis4.position(), percent);
    FL.spin(forward);
    RL.setVelocity(Controller1.Axis3.position() - Controller1.Axis1.position() + Controller1.Axis4.position(), percent);
    RL.spin(forward);
    FR.setVelocity(Controller1.Axis3.position() - Controller1.Axis1.position() - Controller1.Axis4.position(), percent);
    FR.spin(forward);
    RR.setVelocity(Controller1.Axis3.position() + Controller1.Axis1.position() - Controller1.Axis4.position(), percent);
    RR.spin(forward);

  }

before i had thing to move the base in diagonal it was working but now it’s not

so how would i do it?

Can you describe exactly how it is not working?

like it break all the code and make it go like weird for exemple when it’s going foward it’s like trying to turn and trying to go foward and backward

You are telling each motor to do two things. The statements where you are summing three vectors per motor are (likely) correct. The other statements are not needed.

If it turns or strafes oddly after commenting/deleting the unneeded lines, check the signs in your controller signal statements, and/or the reversal of motors in their definitions.

1 Like

You will need to scale the values.

This is because the joystick values at diagonal add up to 180 because it is 90 from each side. The motors can only read up to 100. (I am talking in percents).

So you will need to do 100/180 to find what it needs to be scaled by.

I am not going to give you a straight-up answer, even though I have found it. I will help you work through it though.

2 Likes

i can try that but i just don’t understand the controller axis

i knew that too but it’s the first time im using c++

The controller axis can go from 127 to -127 on just one axis.

Let’s use axis 3 and 4 for example.

Axis 3 has the max values of 127 and -127
Axis 4 and the max values of 127 and -127

If you go at a 45-degree angle to the right, and up, the value will be 180.
This is because it makes a right triangle.
image

1 Like

i still don’t fully understand but basically it mean that if i want to go in diagonal i need to do something like Axis2 + Axis1 - Axis3 - Axis4

There is only two axis on a joystick. Axis 1 & 2 are on the left one, 3 & 4 are on the right I believe.

Do you want strafing to be on the right joystick and forwards & reverse on the left?

I put the strafing and forwards and reverse on the left joystick, and pivoting on the right.

yea also i found how to do it and it’s working properly thank you!:

    RL.spin(forward);
    FL.setVelocity(Controller1.Axis3.position() + Controller1.Axis1.position() + Controller1.Axis4.position() + Controller1.Axis2.position(), percent);
    FL.spin(forward);
    RL.setVelocity(Controller1.Axis3.position() - Controller1.Axis1.position() + Controller1.Axis4.position() + Controller1.Axis2.position(), percent);
    RL.spin(forward);
    FR.setVelocity(Controller1.Axis3.position() - Controller1.Axis1.position() - Controller1.Axis4.position() + Controller1.Axis2.position(), percent);
    FR.spin(forward);
    RR.setVelocity(Controller1.Axis3.position() + Controller1.Axis1.position() - Controller1.Axis4.position() + Controller1.Axis2.position() , percent);
    RR.spin(forward);

all i did was adding the Axis2 position to all the motor

This is what i did kinda. I added pivoting, which is axis 2 for me. I have forwards and reverse on 3, and strafing on axis 4


mmmm i see but does it do the same thing has what i did?

yes, but I can pivot while strafing while moving forwards. It is a very complex movement, but it is fun. I can walk you through the pivoting if you want me too, but it is the same concept, just add or subtract the value depending on the motor setup.

alright but what exaclty is pivoting?

Robot go spinny spin around its center. This slide explains how I did it for my robot.

oh yea like turning?

kinda, the technical term is pivot, turn is a gradual movement where you drive forwards or reverse while turning. Pivot is turning in a single spot, or during movement. But they are very alike yes.

is it something like that?

or like that: