tMotor to string - from ROBOTC tech forum

This question was asked in the ROBOTC tech forum (which most of can’t answer in, although I think at this point they should add me to the list :slight_smile: )

There really is no good way that I know of, tMotor is in essence a numeric variable, the names you give to your motors are only visible to the pre-processor. Here is some code that does give two examples of what you want, but why do you need this?

#pragma config(Motor,  port1,           armMotor,      tmotorVex393, openLoop)
#pragma config(Motor,  port2,           driveMotor,    tmotorVex393, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

char  *
getMotorPort( tMotor index )
    static  char str[16];
    sprintf(str,"port%d", index+1 );

char *
getMotorName( tMotor index )
    switch( index )
        case   armMotor:
            return( "armMotor ");
        case   driveMotor:
            return( "driveMotor ");

task main
    writeDebugStreamLine( "motor is %s", getMotorPort( port2 ) );
    writeDebugStreamLine( "motor is %s", getMotorName( port2 ) );
1 Like

Thank you for the reply. Its a little disappointing, but it’s fine. I have a bit of code that I’m attempting to generalize such that you change the values in a few tMotor arrays and you can use it for most robots without having to rename your motors or anything like that. I can just do what I’m doing now though, which is have separate string arrays for the names. I was just trying to avoid the redundancy of having to write them twice.

1 Like

That’s quite possible, but I don’t really see why you need the motor names. For example, have a look at the code in this post.

Open source robot code

In particular look at driveLib, a generalized library for arcade or mecanum drives. The user code initializes it like this.

    // Init drive system and use Ch3 (fwd), Ch4 (strafe) and Btn8R/Btn8L (rotate)
    DriveSystemInit( Mecanum4Motor, MotorLF, MotorLB, MotorRF, MotorRB );
    DriveSystemAttachControls( Ch3, Ch4,  Btn8R, Btn8L );

The library needs to know which four (or two) motors are involved and what the control (either analog sticks or buttons) control them. The motor names are only used for readability at the top level, once in the library then everything is just numbers.

1 Like