Motor.name & motor() array

Hi - I would like to know if there’s a way to get the motor’s name (assign name string)

Better yet, is there an array where all of the motors are kept?

Thanks!

You can create a motor array I think with the motor group command and the string part I am not so sure about.

Thanks!, interesting - I’ll look at that… Not thrilled about being static (I’m trying to build a template)

Not quite sure what you mean here - the motor class does not have a “name” member variable. If you mean the name of the variable that is a particular motor object, then presumably you already know that.

If you want a motor object with a variable to keep track of the motor name, you could achieve something similar by defining a child class of motor that has an additional variable to keep track of the motor’s name.


There is no global array where all the motor objects are kept. You could maintain one if you want though, for example by having a std::vector<*vex::motor> and adding a pointer to the new motor whenever you instantiate one.


Some more information about what you’re trying to do would likely be helpful in solving your problem.

3 Likes

Thanks - this is great info!

More info - I am trying to write code that will monitor the motor parameters and display them on the brain/controller.

I do this manually but is ugly if/else w/ duplicate code. I have a function where we set motor’s parameters and hoped to write these motors into an array to make the logging easier.

Hope this makes more sense…

So one way to achieve this would be to write a function that takes a pointer to a motor object and a coordinate pair, and prints whatever info you want at the specified location. Then you can spin off a thread that calls that function repeatedly for all the motors you’re interested in.

2 Likes

hmm, I can almost understand ‘pointer to motor object’ but I’m lost when I get to ‘coordinate pair’

Assuming I figure this out could I find the motor’s friendly name?

By “coordinate pair” I just mean a couple of integer parameters that represent where on the V5 brain’s screen to print the info, in either pixels or characters depending on how you want to print to the screen.

So the prototype for such a function might look like:

void printMotorData(vex::motor *m, int row, int col)

The motor class doesn’t have a “friendly name” member variable, so no (see post #4). If you want to print the “name of the motor” to the screen along with the debug info, probably the easiest thing to do is to have your printMotorFunction take an additional parameter for the motor name.

3 Likes

cool cool cool…I’m learning a lot - thx!

so the printMotorData() takes a pointer to the motor as well as the screen coordinates?

in preauton() we set-up the motors:
setUpMotor(MotorName)

void setUpMotor( motor(M) ) {
           M.setBraking()
           M.setMaxTorque...etc..
}

If I understand I should be using a pointer?

void setUpMotor( vex:: motor *m ) ){
&m.setBraking()
&.setMaxTorque…etc…
}

  • but still hung up on the ‘name’

We’re passing the ‘friendly’ name but it’s an object, not a String & can handle. I remember a ‘toString()’ but think it’s another language…

Thanks a million!

1 Like

You don’t have to use a pointer, but the advantage is that instead of copying over the entire object when the function is called you only have to copy a pointer - much less data. So the overhead associated with calling the function is reduced, which is useful if you’re calling the function many times a second.

Another way to do the same thing (probably a better way, syntax-wise, actually) is to pass by reference:

void setUpMotor(vex::motor &m){
    m.setBraking();
    //etc 
}

Passing by reference and passing a pointer do pretty much the same thing under the hood, but if you pass by reference you don’t have to constantly deal with pointer dereferencing, which is nice.


You’re not passing the “friendly name” of a variable to a function, you’re passing the variable itself, which you just happen to call by that name in your source code. By the time your program is compiled, that “friendly name” is gone - your program has no idea what it is. So if what you want is a function that you can call like this:

vex::motor myMotorName;
someFunction(myMotorName);

that results in someFunction printing the string “myMotorName” to the screen, that’s not really possible.

You could solve this constraint in many ways, but they’d pretty much all boil down to keeping track of some std::string objects, one for each motor you’ve declared, containing whatever you want the “friendly name” of the motor to be. One way to do this would be to extend the vex::motor class to have a “friendly name” member variable as I mentioned above. Another would be to keep a global map of motor objects (or pointers to motor objects) to “friendly names”, e.g. using an std::map.

What I meant by

was to add an additional string parameter to the printMotorData function, representing the “friendly name” of the motor, thus the prototype of that function might look like this:

void printMotorData(vex::motor &m, std::string name, int row, int col)

and your loop to print data for all the motors you’re interested in could look like this:

while (true){
    printMotorData(leftDriveMotor, "L Drive", 0, 0);
    printMotorData(rightDriveMotor, "R Drive", 1, 0);
    printMotorData(armMotor, "Arm", 2, 0);
    \\etc... 
}
6 Likes

THANK you so much! This has been very informative!

1 Like