# Using joystick for all cardinal directions

Im trying to create a mecanum drive train that is controlled by the right joystick and I’m trying to figure out if theres a way to allow the joystick to recognize diagonals as well as up, down, left, and right, I am fairly new to text based coding so all help and ideas are greatly appreciated.

You can calculate the velocity of each drive motor with a little bit of math.

Python Code:

``````controller_1.axis1.position() # Returns the horizontal position of the right joystick
controller_1.axis2.position() # Returns the vertical position of the right joystick
``````

C++ Code:

``````Controller1.Axis1.position() // Returns the horizontal position of the right joystick
Controller1.Axis1.position() // Returns the vertical position of the right joystick
``````

Because those methods return a value ranging from -100 to 100, you can accurately set the motor velocity percentage based on those values.

1 Like

I implemented this and it didn’t seem to work any help or feedback would greatly appreciated

#pragma region VEXcode Generated Robot Configuration
// Make sure all required headers are included.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

#include “vex.h”

using namespace vex;

// Brain should be defined by default
brain Brain;

// START V5 MACROS
#define waitUntil(condition)
do {
wait(5, msec);
} while (!(condition))

#define repeat(iterations)
for (int iterator = 0; iterator < iterations; iterator++)
// END V5 MACROS

// Robot configuration code.
motor Claw = motor(PORT5, ratio18_1, false);

motor Arm = motor(PORT6, ratio18_1, false);

controller Controller1 = controller(primary);
motor Back_left = motor(PORT7, ratio18_1, false);

motor Back_Right = motor(PORT8, ratio18_1, true);

motor Front_left = motor(PORT11, ratio18_1, false);

motor Front_Right = motor(PORT20, ratio18_1, true);

// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
#pragma endregion VEXcode Generated Robot Configuration

/----------------------------------------------------------------------------/
/* /
/
Module: main.cpp /
/
Author: {Alex} /
/
Created: {date} /
/
Description: Mecanum drive /
/
/
/
----------------------------------------------------------------------------*/

// Include the V5 Library
#include “vex.h”
// Allows for easier use of the VEX Library
using namespace vex;
int Axis1P = Controller1.Axis1.position();
int Axis2P = Controller1.Axis2.position();
int Axis3P = Controller1.Axis3.position();
int Axis4P = Controller1.Axis4.position();

int main() {
//fowards
while (Axis1P<(20) && Axis1P>(-20) && Axis2P>(10)) {
Back_left.setVelocity(Axis2P,percent);
Back_Right.setVelocity(Axis2P,percent);
Front_left.setVelocity(Axis2P,percent);
Front_Right.setVelocity(Axis2P,percent);
Back_left.spin(forward);
Back_Right.spin(forward);
Front_left.spin(forward);
Front_Right.spin(forward);
}
//Backwards
while (Axis1P<(20) && Axis1P>(-20) && Axis2P<(-10)){
Back_left.setVelocity(Axis2P,percent);
Back_Right.setVelocity(Axis2P,percent);
Front_left.setVelocity(Axis2P,percent);
Front_Right.setVelocity(Axis2P,percent);
Back_left.spin(reverse);
Back_Right.spin(reverse);
Front_left.spin(reverse);
Front_Right.spin(reverse);
}
}

The problem with this code is that you are only getting the position when the program starts. The position is never updated whenever you move the joystick. There are two main ways that you can fix this:

The first one is to check the position in a forever loop.

``````int main {
while (true) {
int Axis1Position = Controller1.Axis1.position();
int Axis2Position = Controller1.Axis2.position();
int Axis3Position = Controller1.Axis3.position();
int Axis4Position = Controller1.Axis4.position();
// Drive Code Here
wait(5, msec);
}
}
``````

And the second one is event-callback based:

``````void updateDriveVelocity() {
int Axis1Position = Controller1.Axis1.position();
int Axis2Position = Controller1.Axis2.position();
int Axis3Position = Controller1.Axis3.position();
int Axis4Position = Controller1.Axis4.position();
// Drive Code Here
}

void controllerAxisOneChangedEvent() {
updateDriveVelocity();
}

void controllerAxisTwoChangedEvent() {
updateDriveVelocity();
}

void controllerAxisThreeChangedEvent() {
updateDriveVelocity();
}

void controllerAxisFourChangedEvent() {
updateDriveVelocity();
}

int main() {
Controller1.Axis1.changed(controllerAxisOneChangedEvent);
Controller1.Axis2.changed(controllerAxisTwoChangedEvent);
Controller1.Axis3.changed(controllerAxisThreeChangedEvent);
Controller1.Axis4.changed(controllerAxisFourChangedEvent);

wait(15, msec);
}
``````

I have always preferred the latter because the code is more organized and there is structure to it.

This is what our drive code looked like last year (we used split arcade controls with tank drive). It used V5 Python, but the concept is the same. I am including this to show you how math with the positions can be implemented.

``````def updateDriveVelocity():
LeftFront.spin(FORWARD, controller_1.axis1.position() + controller_1.axis3.position(), PERCENT)
LeftMiddle.spin(FORWARD, controller_1.axis1.position() + controller_1.axis3.position(), PERCENT)
LeftRear.spin(FORWARD, controller_1.axis1.position() + controller_1.axis3.position(), PERCENT)
RightFront.spin(FORWARD, controller_1.axis1.position() - controller_1.axis3.position(), PERCENT)
RightMiddle.spin(FORWARD, controller_1.axis1.position() - controller_1.axis3.position(), PERCENT)
RightRear.spin(FORWARD, controller_1.axis1.position() - controller_1.axis3.position(), PERCENT)

controller_1.axis1.changed(updateDriveVelocity)
controller_1.axis3.changed(updateDriveVelocity)
``````

If you have any other questions, feel free to ask!

1 Like