How to add code for the controller manually?

When I am trying to code the controller manually, I don’t know which identifier to put for Controller1.Axis2 == identifer… Does anyone know what to put?
Screenshot 2023-03-18 at 5.57.35 PM

What are you trying to do with axis2? Like are you trying to drive the robot?

I am trying to manually code the robot without using the drivetrain setting made by vex

I am trying the drive the robot

While I have absolutely no idea how to text code, I am able to supply you with the text code generated by Vexcode V5 for my block coded program.
(motors 1 & 2 and the left side, 3 & 4 and the right side)
All of these are exponential drive, meaning they have a curve to actually get up to max speed, giving more ability to drive slow but still be able to reach the max speed

Python, Arcade, exponential drive:

def when_started1():
    global myVariable
    motor_1.set_stopping(COAST)
    motor_2.set_stopping(COAST)
    motor_3.set_stopping(COAST)
    motor_4.set_stopping(COAST)
    while True:
        motor_1.set_velocity((((controller_1.axis3.position() + controller_1.axis1.position()) * math.fabs(controller_1.axis3.position() + controller_1.axis1.position())) / 100), PERCENT)
        motor_2.set_velocity((((controller_1.axis3.position() + controller_1.axis1.position()) * math.fabs(controller_1.axis3.position() + controller_1.axis1.position())) / 100), PERCENT)
        motor_3.set_velocity((((controller_1.axis3.position() - controller_1.axis1.position()) * math.fabs(controller_1.axis3.position() - controller_1.axis1.position())) / 100), PERCENT)
        motor_4.set_velocity((((controller_1.axis3.position() - controller_1.axis1.position()) * math.fabs(controller_1.axis3.position() - controller_1.axis1.position())) / 100), PERCENT)
        motor_1.spin(FORWARD)
        motor_2.spin(FORWARD)
        motor_3.spin(FORWARD)
        motor_4.spin(FORWARD)
        wait(5, MSEC)

when_started1()

C++, arcade, exponential drive:

// "when started" hat block
int whenStarted1() {
  Motor1.setStopping(coast);
  Motor2.setStopping(coast);
  Motor3.setStopping(coast);
  Motor4.setStopping(coast);
  while (true) {
    Motor1.setVelocity((((Controller1.Axis3.position() + Controller1.Axis1.position()) * fabs(static_cast<float>(Controller1.Axis3.position() + Controller1.Axis1.position()))) / 100.0), percent);
    Motor2.setVelocity((((Controller1.Axis3.position() + Controller1.Axis1.position()) * fabs(static_cast<float>(Controller1.Axis3.position() + Controller1.Axis1.position()))) / 100.0), percent);
    Motor3.setVelocity((((Controller1.Axis3.position() - Controller1.Axis1.position()) * fabs(static_cast<float>(Controller1.Axis3.position() - Controller1.Axis1.position()))) / 100.0), percent);
    Motor4.setVelocity((((Controller1.Axis3.position() - Controller1.Axis1.position()) * fabs(static_cast<float>(Controller1.Axis3.position() - Controller1.Axis1.position()))) / 100.0), percent);
    Motor1.spin(forward);
    Motor2.spin(forward);
    Motor3.spin(forward);
    Motor4.spin(forward);
  wait(5, msec);
  }
  return 0;
}


int main() {
  // post event registration

  // set default print color to black
  printf("\033[30m");

  // wait for rotation sensor to fully initialize
  wait(30, msec);

  whenStarted1();
}

(C++ added weird printcolor and rotation sensor things)

Python, tank drive, exponential:

def when_started1():
    global myVariable
    motor_1.set_stopping(COAST)
    motor_2.set_stopping(COAST)
    motor_3.set_stopping(COAST)
    motor_4.set_stopping(COAST)
    while True:
        motor_1.set_velocity(((controller_1.axis3.position() * math.fabs(controller_1.axis3.position())) / 100), PERCENT)
        motor_2.set_velocity(((controller_1.axis3.position() * math.fabs(controller_1.axis3.position())) / 100), PERCENT)
        motor_3.set_velocity(((controller_1.axis2.position() * math.fabs(controller_1.axis2.position())) / 100), PERCENT)
        motor_4.set_velocity(((controller_1.axis2.position() * math.fabs(controller_1.axis2.position())) / 100), PERCENT)
        motor_1.spin(FORWARD)
        motor_2.spin(FORWARD)
        motor_3.spin(FORWARD)
        motor_4.spin(FORWARD)
        wait(5, MSEC)

when_started1()

C++, tank drive, exponential:

// "when started" hat block
int whenStarted1() {
  Motor1.setStopping(coast);
  Motor2.setStopping(coast);
  Motor3.setStopping(coast);
  Motor4.setStopping(coast);
  while (true) {
    Motor1.setVelocity(((Controller1.Axis3.position() * fabs(static_cast<float>(Controller1.Axis3.position()))) / 100.0), percent);
    Motor2.setVelocity(((Controller1.Axis3.position() * fabs(static_cast<float>(Controller1.Axis3.position()))) / 100.0), percent);
    Motor3.setVelocity(((Controller1.Axis2.position() * fabs(static_cast<float>(Controller1.Axis2.position()))) / 100.0), percent);
    Motor4.setVelocity(((Controller1.Axis2.position() * fabs(static_cast<float>(Controller1.Axis2.position()))) / 100.0), percent);
    Motor1.spin(forward);
    Motor2.spin(forward);
    Motor3.spin(forward);
    Motor4.spin(forward);
  wait(5, msec);
  }
  return 0;
}


int main() {
  // post event registration

  // set default print color to black
  printf("\033[30m");

  // wait for rotation sensor to fully initialize
  wait(30, msec);

  whenStarted1();
}
1 Like

Firstly, you will need to put it inside of driver control. Second, you need to use Controller1.Axis2.value(). This will return a value between -100 to 100. Typically, vex teams will set the speed of the drivebase to this number in percent using each drive stick to control a seperate side of the robot. This is referred to as a tank drive.

1 Like

Which is not a great idea. The code generated by the block to C or Python generator, although being functional, is not particularly good code.

7 Likes

controller::axis::value() will return an integer from [-127, 127] (don’t use this)
controller::axis::position() will return an integer from [-100, 100]

Here’s basic tank drive using two motor groups called LeftMotors and RightMotors (and a controller called Controller):

int usercontrol() {
    while (true) {
        int32_t left_speed = Controller.Axis3.position();
        int32_t right_speed = Controller.Axis2.position();

        LeftMotors.spin(vex::forward, left_speed, vex::percent);
        RightMotors.spin(vex::forward, right_speed, vex::percent);

        vex::wait(10, vex::msec);
    }
}

You can swap out the values left_speed and right_speed to get other input methods.

For example, here’s dual-stick arcade:

int32_t left_speed = Controller.Axis3.position() + Controller.Axis1.position();
int32_t right_speed = Controller.Axis3.position() -  Controller.Axis1.position();

… or single stick arcade using just the right joystick:

int32_t left_speed = Controller.Axis2.position() + Controller.Axis1.position();
int32_t right_speed = Controller.Axis2.position() -  Controller.Axis1.position();

The actual actual vex::drivetrain class also implements a deadband to account for slight cases of joystick drift, although you probably don’t need this:

#include <cmath>

int usercontrol() {
    constexpr int32_t DEADBAND = 5;

    while (true) {
        int32_t left_speed = Controller.Axis3.position();
        int32_t right_speed = Controller.Axis2.position();

        if (std::abs(left_speed) < DEADBAND) left_speed = 0;
        if (std::abs(right_speed) < DEADBAND) right_speed = 0;

        LeftMotors.spin(vex::forward, left_speed, vex::percent);
        RightMotors.spin(vex::forward, right_speed, vex::percent);

        vex::wait(10, vex::msec);
    }
}
6 Likes