Page 1 of 1

Subclass a robot controller

Posted: Fri Apr 03, 2015 5:08 pm
by Elendurwen
Hi, I'd like to have a basic robot controller class, then subclass it for specific controllers. I am struggling to make the makefile instructions for this in the controllers/CMakeLists.txt file of my argos project.

If you had only a single class, you would do:

Code: Select all

add_library(baseRobot SHARED baseRobot.h baseRobot.cpp) target_link_libraries( baseRobot ${GSL_LIBRARIES} argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_sensors) #${GSL_LIBRARIES}
So for a new subclass of baseRobot, called localBroadcaster, I am trying:

Code: Select all

add_library(baseRobot SHARED baseRobot.h baseRobot.cpp) target_link_libraries( baseRobot ${GSL_LIBRARIES} argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_sensors) add_library(localBroadcaster SHARED localBroadcaster.h localBroadcaster.cpp) target_link_libraries( baseRobot ${GSL_LIBRARIES} argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_sensors) #${GSL_LIBRARIES}
But I keep getting linkage errors like:

Code: Select all

Scanning dependencies of target localBroadcaster [ 40%] Building CXX object controllers/CMakeFiles/localBroadcaster.dir/localBroadcaster.cpp.o Linking CXX shared library liblocalBroadcaster.dylib Undefined symbols for architecture x86_64: "argos::CCI_Controller::~CCI_Controller()", referenced from: BaseRobot::~BaseRobot() in localBroadcaster.cpp.o
How could I do this?

Re: Subclass a robot controller

Posted: Fri Apr 03, 2015 5:23 pm
by Elendurwen
I should also say, if I just go for having the localBroadcaster built, by using

Code: Select all

add_library(localBroadcaster SHARED localBroadcaster.h localBroadcaster.cpp baseRobot.h baseRobot.cpp) target_link_libraries( localBroadcaster ${GSL_LIBRARIES} argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_sensors) #${GSL_LIBRARIES}
And then I use it in the argos . xml file as

Code: Select all

<footbot_foraging_controller id="localBroadcasterController" library="build/controllers/liblocalBroadcaster.dylib">
The constructor and functions that are running are from the base class, not the overridden functions of the subclass.

Re: Subclass a robot controller - SOLUTION

Posted: Fri Apr 03, 2015 6:43 pm
by Elendurwen
Ok, so after googling and fiddling around, I found a solution to having a super class (BaseRobot) and 2 different controllers that subclass it (Solitary and LocalBroadcaster). I was making a number of mistakes before, this is how it should be:

In controllers/CMakeLists.txt:

Code: Select all

add_library(baseRobot SHARED baseRobot.h baseRobot.cpp) target_link_libraries( baseRobot ${GSL_LIBRARIES} argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_sensors) add_library(solitary SHARED solitary.h solitary.cpp) target_link_libraries( solitary baseRobot ${GSL_LIBRARIES} argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_sensors) add_library(localBroadcaster SHARED localBroadcaster.h localBroadcaster.cpp) target_link_libraries( localBroadcaster baseRobot ${GSL_LIBRARIES} argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_sensors)

Then, at the end of each cpp file for the subclassed controllers, add lines like:

Code: Select all

REGISTER_CONTROLLER(Solitary, "solitaryController")
To register the classes with argos. The 2nd argument is the name of the node in the xml config file.

You can then make a config file like:

Code: Select all

... <controllers> <localBroadcasterController id="localBroadcasterController" library="build/controllers/liblocalBroadcaster.dylib"> ... </localBroadcasterController> <solitaryController id="solitaryController" library="build/controllers/libsolitary.dylib"> ... </solitaryController> </controllers>
and use any of the controllers later in the config file to instantiate robots, e.g.:

Code: Select all

<distribute> <position method="uniform" min="-2,-2,0" max="2,2,0" /> <orientation method="uniform" min="0,0,0" max="360,0,0" /> <entity quantity="2" max_trials="100"> <foot-bot id="robot"> <controller config="solitaryController" /> </foot-bot> </entity> </distribute>

The fact that in the make file, a separate library is created for the base class and for the subclasses means that you can e.g. loop through the robots in a loop function, no matter their class. To do that, add the baseRobot library in the loop functions/CMakeLists.txt:

Code: Select all

... add_library(mainLoopFunctions MODULE ${mainLoopFunctions_SOURCES}) target_link_libraries(mainLoopFunctions baseRobot argos3core_simulator argos3plugin_simulator_actuators argos3plugin_simulator_dynamics2d argos3plugin_simulator_entities argos3plugin_simulator_footbot argos3plugin_simulator_genericrobot argos3plugin_simulator_media argos3plugin_simulator_sensors) ...
This allows you to do things like this in your loop function:

Code: Select all

CSpace::TMapPerType& m_cFootbots = GetSpace().GetEntitiesByType("foot-bot"); for(CSpace::TMapPerType::iterator it = m_cFootbots.begin(); it != m_cFootbots.end(); ++it) { CFootBotEntity& footBotEntity = *any_cast<CFootBotEntity*>(it->second); BaseRobot& footBot = dynamic_cast<BaseRobot&>(footBotEntity.GetControllableEntity().GetController());

Re: Subclass a robot controller

Posted: Fri Apr 03, 2015 7:38 pm
by pincy
Hi Eledurwen,

I was about to reply with the very same solution you have posted. Great job! :-)

Cheers,
Carlo

Re: Subclass a robot controller

Posted: Fri Apr 03, 2015 7:40 pm
by Elendurwen
Thanks Carlo, I almost threw away my computer during the process of discovery though :lol:

Re: Subclass a robot controller

Posted: Fri Apr 03, 2015 7:42 pm
by pincy
C++ is a game of patience :ugeek: