What does SendableChooser.GetSelected() return as?

http://first.wpi.edu/FRC/roborio/release/docs/cpp/classSendableChooser.html

The library says it returns as a void pointer, but what does that information return like? Does it return the name of the option that is put in with this,

autoChooser.AddObject(“Lowbar Auto”, new Auto);

“Lowbar Auto” is the name of the option, and can I use it in an if statement like,

if (autoChooser.GetSelected() == “Lowbar Auto”) {
//Stuff for lowbar auto
}

I haven’t used C++ for robot programming but being familiar with C and C++ APIs, it looks like it will return the object you passed in as the second parameter to AddObject().

This code:

if (autoChooser.GetSelected() == "Lowbar Auto") {
//Stuff for lowbar auto
}

would not work because it would compare the void* (address) to the address of the “Lowbar Auto” string literal (char*, wchar_t*, etc. depending on compiler). Instead, you should cast the return value of GetSelected() back to the pointer type which you passed in. Then you can dereference it, or do whatever else you need in order to decide what auto mode to run.

Also,

autoChooser.AddObject("Lowbar Auto", new Auto);

is likely to cause a memory leak since the SendableChooser won’t be able to delete the Auto as it will see it as a void*. You will probably need to manage that object yourself.

I knew already it didn’t work, so that’s why I was asking. And I found the solution anyways

autoSelected = ((std::string)chooser->GetSelected());

if(autoSelected == autoNameCustom){
//Custom Auto goes here
} else {
//Default Auto goes here
}

Out of curiosity, did you also change your second parameter to AddObject to a std::string?

It returns a void*, which you cast to a Whatever* that you passed into AddObject (in your case, Command*)



autoChooser.AddObject("Lowbar Auto", new Auto);

// Then in autonomous init 
((Command*) autoChooser.GetSelected())->Start();


(A templated SendableChooser might solve this confusion)

It’s not a memory leak since you’d never normally delete your auto commands anyway.

Yes I did, but I just didn’t show it.