[VCS C++] Program for motor testing all ports

I want to create a VCS program to test all V5 ports with motors plugged in to each port. (I’d like to be able to run this when a student is convinced their V5 brain is broken, it CAN’T be their code :wink: )

I also want this to happen during a standard match autonomous period so the teams can get used to running the auton from a V5 touch screen.

Would you be willing to check my code for flaws? I’m a programming noob, and even more when it’s VCS. How does this look…

#include "robot-config.h"

void motortest (tMotor motor_name)
{
    motor_name.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(700);
    motor_name.stop(brakeType::coast);
    Controller.Screen.clearScreen();
    Controller.Screen.print("previous motor " motor_name);
} 

// create a function containing the code to run 
void autonomous( void ) {
    Controller.rumble(".-.-");
    Brain.Screen.clearScreen();
    Brain.Screen.print("Motor testing");
    motortest(Motor1);
    motortest(Motor2);
    motortest(Motor3);
    motortest(Motor4);
    motortest(Motor5);
    motortest(Motor6);
    motortest(Motor7);
    motortest(Motor8);
    motortest(Motor9);
    motortest(Motor10);
    motortest(Motor11);
    motortest(Motor12);
    motortest(Motor13);
    motortest(Motor14);
    motortest(Motor15);
    motortest(Motor16);
    motortest(Motor17);
    motortest(Motor18);
    motortest(Motor19);
    motortest(Motor20);
    motortest(Motor21);
}

void usercontrol( void ) {
    // do things in user control mode...
}

int main() {
    // tell the competition code what to run for the autonomous period
    Competition.autonomous( autonomous );

    // tell the competition code what to run for the driver control period
    Competition.drivercontrol( usercontrol );

    //Prevent main from exiting with an infinite loop.                        
    while(1) {
        
        task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
    }
}

@Adam T


void motortest (tMotor motor_name)

tMotor is not a default type and I don’t see a definition for it , you might want a


vex::motor

?


Controller.Screen.print("previous motor " motor_name);

assuming tMotor is a


vex::motor

motor_name is not going to return the name of the motor

I.e it’s not going to return “Motor1”

You are correct, tMotor didn’t work. I was hopeful :slight_smile: I saw some references in my Google search to tMotor, but now I see it’s probably part of a code library (or the smart motor library?).

Is there a way to get a motor name as a variable into my function/task?

I guess I’ll just put separate commands in the Auton task, that should work fine.

Looks a little like bot generated SPAM @DRow

I am sure this is a somewhat crude program, but this is the what I am using to run the V5 through a first check. I’ll run this before giving any V5 to teams, and I’ll keep it around to use for troubleshooting later.

If anyone has tips to streamline the code I’m all ears.

#include "robot-config.h"

void MT1(int MT1t){
    Motor1.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT1t);
    Motor1.stop(brakeType::coast);
}

void MT2(int MT2t){
    Motor2.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT2t);
    Motor2.stop(brakeType::coast);
}

void MT3(int MT3t){
    Motor3.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT3t);
    Motor3.stop(brakeType::coast);
}
 
void MT4(int MT4t){
    Motor4.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT4t);
    Motor4.stop(brakeType::coast);
}

void MT5(int MT5t){    
    Motor5.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT5t);
    Motor5.stop(brakeType::coast);
}

void MT6(int MT6t){
    Motor6.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT6t);
    Motor6.stop(brakeType::coast);
}

void MT7(int MT7t){
    Motor7.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT7t);
    Motor7.stop(brakeType::coast);
}

void MT8(int MT8t){
    Motor8.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT8t);
    Motor8.stop(brakeType::coast);
}

void MT9(int MT9t){
    Motor9.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT9t);
    Motor9.stop(brakeType::coast);
}

void MT10(int MT10t){
    Motor10.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT10t);
    Motor10.stop(brakeType::coast);
}

void MT11(int MT11t){
    Motor11.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT11t);
    Motor11.stop(brakeType::coast);
}

void MT12(int MT12t){
    Motor12.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT12t);
    Motor12.stop(brakeType::coast);
}

void MT13(int MT13t){
    Motor13.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT13t);
    Motor13.stop(brakeType::coast);
}

void MT14(int MT14t){
    Motor14.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT14t);
    Motor14.stop(brakeType::coast);
}

void MT15(int MT15t){
    Motor15.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT15t);
    Motor15.stop(brakeType::coast);
}

void MT16(int MT16t){
    Motor16.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT16t);
    Motor16.stop(brakeType::coast);
}

void MT17(int MT17t){
    Motor17.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT17t);
    Motor17.stop(brakeType::coast);
}

void MT18(int MT18t){
    Motor18.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT18t);
    Motor18.stop(brakeType::coast);
}

void MT19(int MT19t){
    Motor19.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT19t);
    Motor19.stop(brakeType::coast);
}

void MT20(int MT20t){
    Motor20.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(MT20t);
    Motor20.stop(brakeType::coast);
}



// create a function containing the code to run 
void autonomous( void ) {
    Brain.Screen.clearScreen();
    Brain.Screen.print("Motor testing");
    
    MT1(2000);
    MT2(2000);
    MT3(2000);
    MT4(2000);
    MT5(2000);
    Controller1.Screen.clearScreen();
    Controller1.Screen.print("prev: Motors 1-5");
    Controller1.rumble(".-.-");    
    MT6(2000);
    MT7(2000);
    MT8(2000);
    MT9(2000);
    MT10(2000);
    Controller1.Screen.clearScreen();
    Controller1.Screen.print("prev: Motors 6-10");   
    Controller1.rumble(".-.-");
    MT11(2000);
    MT12(2000);
    MT13(2000);
    MT14(2000);
    MT15(2000);
    Controller1.Screen.clearScreen();
    Controller1.Screen.print("prev: Motors 11-15");  
    Controller1.rumble(".-.-");
    MT16(2000);
    MT17(2000);
    MT18(2000);
    MT19(2000);
    MT20(2000);
    Controller1.Screen.clearScreen();
    Controller1.Screen.print("prev: Motors 16-20");    
    task::sleep(2000);
    
//end autonomous        
}

void usercontrol( void ) {
    while(1){
        //first shift set
    if(Controller1.ButtonL1.pressing()) {
        
        //left set of controller buttons
        if(Controller1.ButtonUp.pressing()) {
            Motor9.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonRight.pressing()) {
            Motor10.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonDown.pressing()) {
            Motor11.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonLeft.pressing()) {
            Motor12.spin(directionType::fwd,50,velocityUnits::pct);
        }
        //right side of controller buttons
        else if(Controller1.ButtonX.pressing()) {
            Motor13.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonA.pressing()) {
            Motor14.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonB.pressing()) {
            Motor15.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonY.pressing()) {
            Motor16.spin(directionType::fwd,50,velocityUnits::pct);
        }        
        else { //If these buttons are not pressed...
            Motor9.stop(brakeType::coast);
            Motor10.stop(brakeType::coast);
            Motor11.stop(brakeType::coast);
            Motor12.stop(brakeType::coast);
            Motor13.stop(brakeType::coast);
            Motor14.stop(brakeType::coast);
            Motor15.stop(brakeType::coast);
            Motor16.stop(brakeType::coast);
        }
    }

        //second shift set    
    else if(Controller1.ButtonL2.pressing()) {

        //left set of controller buttons
        if(Controller1.ButtonUp.pressing()) {
            Motor17.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonRight.pressing()) {
            Motor18.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonDown.pressing()) {
            Motor19.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonLeft.pressing()) {
            Motor20.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else { //If these buttons are not pressed...
            Motor17.stop(brakeType::coast);
            Motor18.stop(brakeType::coast);
            Motor19.stop(brakeType::coast);
            Motor20.stop(brakeType::coast);
        }
    }    
    
        //no shift set    
    else {
        
        //left set of controller buttons
        if(Controller1.ButtonUp.pressing()) {
            Motor1.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonRight.pressing()) {
            Motor2.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonDown.pressing()) {
            Motor3.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonLeft.pressing()) {
            Motor4.spin(directionType::fwd,50,velocityUnits::pct);
        }
        //right side of controller buttons
        else if(Controller1.ButtonX.pressing()) {
            Motor5.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonA.pressing()) {
            Motor6.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonB.pressing()) {
            Motor7.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller1.ButtonY.pressing()) {
            Motor8.spin(directionType::fwd,50,velocityUnits::pct);
        }        
        else { //If these buttons are not pressed...
            Motor1.stop(brakeType::coast);
            Motor2.stop(brakeType::coast);
            Motor3.stop(brakeType::coast);
            Motor4.stop(brakeType::coast);
            Motor5.stop(brakeType::coast);
            Motor6.stop(brakeType::coast);
            Motor7.stop(brakeType::coast);
            Motor8.stop(brakeType::coast);
        }
    }   
        task::sleep(100);
    }
//end usercontrol    
    
}

int main() {
    // tell the competition code what to run for the autonomous period
    Competition.autonomous( autonomous );

    // tell the competition code what to run for the driver control period
    Competition.drivercontrol( usercontrol );

    //Prevent main from exiting with an infinite loop.                        
    while(1) {
        
        task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
    }
}

My god.

I’m not too familiar with VCS, but couldn’t you put all your motors in an array and then use a for loop to loop through them? Again, not too familiar with VCS but this seems like a waay better system

Motor arr [20] = {Motor1, Motor2, Motor3, Motor4, Motor5, Motor6, Motor7, Motor8, Motor9, Motor10, Motor11, Motor12, Motor13, Motor14, Motor15, Motor16, Motor17, Motor18, Motor19, Motor20};//You can add the motors that you are using here I guess :\

void testMtrs(){
  for(int i = 0; i < (sizeof(arr)/sizeof(arr[0]); i++){
    Controller1.Screen.print("Motor " + i + " - Spinning");
    arr*.spin(directionType::fwd,50,velocityUnits::pct);
    task::sleep(2000);
  }
}

so for a simple test like running a motor on each port, just create motor instances as necessary.

void
motorPortCheck() {
    Brain.Screen.setFont(vex::fontType::mono30);
  
    for( int32_t port = vex::PORT1;port <= vex::PORT21;port++ ) {
        Brain.Screen.printAt( 10, 40, "Port %2d:", port + 1);

        // new motor instance
        motor m = vex::motor( port );
    
        // check to see if installed
        if( m.installed() ) {
            Brain.Screen.printAt( 140, 40, "Installed    " );
            // now run
            m.spin( directionType::fwd, 50, velocityUnits::pct );
            task::sleep(1000);
            // and stop
            m.stop();
        } else {
            Brain.Screen.printAt( 140, 40, "Not installed" );
        }
    
        task::sleep(100);
    }
}

You can also test motors plugged into the V5 directly from its device screen on the V5.

Thanks all. I ran all 15 of the V5s and just as expected everything worked just fine.

Thanks @jpearman for a nice succinct code example. I’m going to try to study it line by line because I don’t understand everything that’s going on right off the bat and there is a lot of great concepts happening there.

First thing I’ll focus on understainding is this…


for( int32_t port = vex::PORT1;port <= vex::PORT21;port++ ) {
        Brain.Screen.printAt( 10, 40, "Port %2d:", port + 1);

It is simply going through the range of valid ports that are predefined as PORT1 to PORT21 - my guess is that these are actually defined from 0 to 20… Which gets to the second line of which prints the port number as port+1 - this allows the user to see the value of the port as it is printed on the V5 Robot Brain. You’ll note there is no port 0 on the brain. Humans like counting things sequentially starting at 1 - 1,2,3,4… trips people up when looking at programs where counting starts at 0 for array index values.