How to code a six motor drive train

If you want to know how to code a six motor drivetrain you do this -

#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 Motor10 = motor(PORT10, ratio18_1, false);

controller Controller1 = controller(primary);
motor leftMotorA = motor(PORT1, ratio18_1, false);
motor leftMotorB = motor(PORT3, ratio18_1, false);
motor leftMotorC = motor(PORT5, ratio18_1, false);

motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB, leftMotorC);
motor rightMotorA = motor(PORT2, ratio18_1, true);
motor rightMotorB = motor(PORT4, ratio18_1, true);
motor rightMotorC = motor(PORT6, ratio18_1, true);
motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB, rightMotorC);
drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 319.19, 295, 40, mm, 1);


digital_out Stuff = digital_out(Brain.ThreeWirePort.A);



// Helper to make playing sounds from the V5 in VEXcode easier and
// keeps the code cleaner by making it clear what is happening.
void playVexcodeSound(const char *soundName) {
  printf("VEXPlaySound:%s\n", soundName);
  wait(5, msec);
}



// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
// define variables used for controlling motors based on controller inputs
bool Controller1XBButtonsControlMotorsStopped = true;

// define a task that will handle monitoring inputs from Controller1
int rc_auto_loop_function_Controller1() {
  // process the controller input every 20 milliseconds
  // update the motors based on the input values
  while(true) {
    if(RemoteControlCodeEnabled) {
      // check the ButtonX/ButtonB status to control Motor10
      if (Controller1.ButtonX.pressing()) {
        Motor10.spin(forward);
        Controller1XBButtonsControlMotorsStopped = false;
      } else if (Controller1.ButtonB.pressing()) {
        Motor10.spin(reverse);
        Controller1XBButtonsControlMotorsStopped = false;
      } else if (!Controller1XBButtonsControlMotorsStopped) {
        Motor10.stop();
        // set the toggle so that we don't constantly tell the motor to stop when the buttons are released
        Controller1XBButtonsControlMotorsStopped = true;
      }
    }
    // wait before repeating the process
    wait(20, msec);
  }
  return 0;
}

task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);

#pragma endregion VEXcode Generated Robot Configuration

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       {author}                                                  */
/*    Created:      {date}                                                    */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// Include the V5 Library
#include "vex.h"
  
// Allows for easier use of the VEX Library
using namespace vex;

int main() {
}

edit: code tags added by moderators. Please remember to use them. Posts with untagged code may get rejected in future.

2 Likes

Since you have already got some motor groups set up, rest of things should be easy.
You need to simply bind you controller with the motor groups, like this:

void drive_controll() {
  float AX2, AX3;                 // the values of axis
  float dead_band = 10;

  // get the values
  AX2 = Controller1.Axis2.position();
  AX3 = Controller1.Axis3.position();

  // set dead bands area
  if (fabs(AX2) < dead_band) {
    AX2 = 0;
  }
  if (fabs(AX3) < dead_band) {
    AX3 = 0;
  }

  // drive the motors group
  if (AX3 != 0) {
    LeftDriveSmart.spin(forward, AX3, pct);
  } else {
    LeftDriveSmart.stop(coast);
  }
  if (AX2 != 0) {
    RightDriveSmart.spin(forward, AX2, pct);
  } else {
    RightDriveSmart.stop(coast);
  }
}

You could add this function to your loop that runs during the driver controll period, like this I guess:

int rc_auto_loop_function_Controller1() {
  // process the controller input every 20 milliseconds
  // update the motors based on the input values
  while(true) {
    if(RemoteControlCodeEnabled) {
      // ..............
      drive_controll();
     }
  wait(20, msec);
}

As for the autonomous part, you could simply use the internal functions that VEX has provides, like these:
屏幕截图 2023-12-17 103401
And you could view almost all these functions in VEXCode


As for me, I write some custom functions based on the offical api, such as:

Thank you for your help and feedback.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.