Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Requests regarding how to set up experiments in ARGoS.
AntonioC
Posts: 48
Joined: Mon Aug 30, 2021 7:02 pm

Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby AntonioC » Sun Jan 23, 2022 2:14 pm

To make the system detect the GetColor () function for the Kilobot led color, it was necessary to cast. During the compilation phase I do not get any error but when I start the experiment I get the following message

Code: Select all

[FATAL] Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*
How can I solve to get the Kilobot led color?

Code: Select all

#include "trajectory_loop_functions.h" #include "argos3/core/control_interface/ci_controller.h" #include "argos3/plugins/simulator/entities/directional_led_entity.h" #include "argos3/core/utility/datatypes/color.h" #include <string> void CTrajectoryLoopFunctions::Init(TConfigurationNode& t_tree) { CSpace::TMapPerType& tKBMap = GetSpace().GetEntitiesByType("kilobot"); std::string unKilobotID; for(CSpace::TMapPerType::iterator it = tKBMap.begin(); it != tKBMap.end(); ++it) { /* Create a pointer to the current kilobot */ CKilobotEntity* pcKB = any_cast<CKilobotEntity*>(it->second); unKilobotID = pcKB->GetId(); std::cout << unKilobotID << " - " << pcKB->GetEmbodiedEntity().GetOriginAnchor().Position << "\n"; } } void CTrajectoryLoopFunctions::Reset(){} void CTrajectoryLoopFunctions::PostStep() { /* Get the map of all kilobots from the space */ CSpace::TMapPerType& tKBMap = GetSpace().GetEntitiesByType("kilobot"); /* Go through them */ std::string unKilobotID; argos::CColor colorKilobotLed; for(CSpace::TMapPerType::iterator it = tKBMap.begin(); it != tKBMap.end(); ++it) { /* Create a pointer to the current kilobot */ CKilobotEntity* pcKB = any_cast<CKilobotEntity*>(it->second); CDirectionalLEDEntity* pcKBLED = any_cast<CDirectionalLEDEntity*>(it->second); unKilobotID = pcKB->GetId(); colorKilobotLed = pcKBLED->GetColor(); std::cout << unKilobotID << " - " << colorKilobotLed << " - " << pcKB->GetEmbodiedEntity().GetOriginAnchor().Position << "\n"; } } REGISTER_LOOP_FUNCTIONS(CTrajectoryLoopFunctions, "trajectory_loop_functions")

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby pincy » Sun Jan 23, 2022 6:18 pm

This line is wrong:

Code: Select all

CDirectionalLEDEntity* pcKBLED = any_cast<CDirectionalLEDEntity*>(it->second);
You're casting it->second, which you have already cast in the previous line to CKilobotEntity. I would suggest you familiarize with C++ and casting before diving into ARGoS, and make sure understand what casting does.

The correct code is:

Code: Select all

colorKilobotLed = pcKB ->GetLEDEquippedEntity()->GetLED(0)->GetColor();
Please familiarize with the following headers:
I made ARGoS.

AntonioC
Posts: 48
Joined: Mon Aug 30, 2021 7:02 pm

Re: Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby AntonioC » Mon Jan 24, 2022 6:00 pm

I used the code you wrote but I get the following error at compile time.

Code: Select all

antonio@antonio-Lenovo-V110-15AST:~/Documents/argos3-kilobot-master/build$ make [ 1%] Automatic MOC for target argos3plugin_simulator_kilolib [ 1%] Built target argos3plugin_simulator_kilolib_autogen [ 5%] Built target argos3plugin_simulator_kilolib [ 6%] Automatic MOC for target argos3plugin_simulator_kilobot [ 6%] Built target argos3plugin_simulator_kilobot_autogen [ 23%] Built target argos3plugin_simulator_kilobot [ 25%] Automatic MOC for target kilobot_diffusion [ 25%] Built target kilobot_diffusion_autogen [ 28%] Built target kilobot_diffusion [ 29%] Automatic MOC for target kilobot_phototaxis [ 29%] Built target kilobot_phototaxis_autogen [ 32%] Built target kilobot_phototaxis [ 33%] Automatic MOC for target orbit_star [ 33%] Built target orbit_star_autogen [ 36%] Built target orbit_star [ 37%] Automatic MOC for target test_speaker_mod [ 37%] Built target test_speaker_mod_autogen [ 40%] Built target test_speaker_mod [ 41%] Automatic MOC for target move_to_light [ 41%] Built target move_to_light_autogen [ 44%] Built target move_to_light [ 45%] Automatic MOC for target simple_movement [ 45%] Built target simple_movement_autogen [ 48%] Built target simple_movement [ 50%] Automatic MOC for target gradient_simple [ 50%] Built target gradient_simple_autogen [ 53%] Built target gradient_simple [ 54%] Automatic MOC for target test_listener [ 54%] Built target test_listener_autogen [ 57%] Built target test_listener [ 58%] Automatic MOC for target blinky [ 58%] Built target blinky_autogen [ 61%] Built target blinky [ 62%] Automatic MOC for target test_speaker [ 62%] Built target test_speaker_autogen [ 65%] Built target test_speaker [ 66%] Automatic MOC for target nonblocked_movement [ 66%] Built target nonblocked_movement_autogen [ 69%] Built target nonblocked_movement [ 70%] Automatic MOC for target test_listener_mod [ 70%] Built target test_listener_mod_autogen [ 73%] Built target test_listener_mod [ 75%] Automatic MOC for target disperse [ 75%] Built target disperse_autogen [ 78%] Built target disperse [ 79%] Automatic MOC for target orbit_planet [ 79%] Built target orbit_planet_autogen [ 82%] Built target orbit_planet [ 83%] Automatic MOC for target sync [ 83%] Built target sync_autogen [ 86%] Built target sync [ 87%] Automatic MOC for target test_debug [ 87%] Built target test_debug_autogen [ 90%] Built target test_debug [ 91%] Automatic MOC for target debug_loop_functions [ 91%] Built target debug_loop_functions_autogen [ 94%] Built target debug_loop_functions [ 95%] Automatic MOC for target trajectory_loop_functions [ 95%] Built target trajectory_loop_functions_autogen Scanning dependencies of target trajectory_loop_functions [ 96%] Building CXX object examples/loop_functions/trajectory_loop_functions/CMakeFiles/trajectory_loop_functions.dir/trajectory_loop_functions.cpp.o /home/antonio/Documents/argos3-kilobot-master/src/examples/loop_functions/trajectory_loop_functions/trajectory_loop_functions.cpp: In member function ‘virtual void CTrajectoryLoopFunctions::PostStep()’: /home/antonio/Documents/argos3-kilobot-master/src/examples/loop_functions/trajectory_loop_functions/trajectory_loop_functions.cpp:34:53: error: base operand of ‘->’ has non-pointer type ‘argos::CLEDEquippedEntity’ colorKilobotLed = pcKB->GetLEDEquippedEntity()->GetLED(0)->GetColor(); ^~ examples/loop_functions/trajectory_loop_functions/CMakeFiles/trajectory_loop_functions.dir/build.make:62: recipe for target 'examples/loop_functions/trajectory_loop_functions/CMakeFiles/trajectory_loop_functions.dir/trajectory_loop_functions.cpp.o' failed make[2]: *** [examples/loop_functions/trajectory_loop_functions/CMakeFiles/trajectory_loop_functions.dir/trajectory_loop_functions.cpp.o] Error 1 CMakeFiles/Makefile2:1654: recipe for target 'examples/loop_functions/trajectory_loop_functions/CMakeFiles/trajectory_loop_functions.dir/all' failed make[1]: *** [examples/loop_functions/trajectory_loop_functions/CMakeFiles/trajectory_loop_functions.dir/all] Error 2 Makefile:151: recipe for target 'all' failed make: *** [all] Error 2 antonio@antonio-Lenovo-V110-15AST:~/Documents/argos3-kilobot-master/build$

Code: Select all

#include "trajectory_loop_functions.h" #include "argos3/core/control_interface/ci_controller.h" #include "argos3/plugins/simulator/entities/directional_led_entity.h" #include "argos3/core/utility/datatypes/color.h" #include <string> #define KILOBOTNUB 5 int kFound = 0; void CTrajectoryLoopFunctions::Init(TConfigurationNode& t_tree) { CSpace::TMapPerType& tKBMap = GetSpace().GetEntitiesByType("kilobot"); std::string unKilobotID; for(CSpace::TMapPerType::iterator it = tKBMap.begin(); it != tKBMap.end(); ++it) { /* Create a pointer to the current kilobot */ CKilobotEntity* pcKB = any_cast<CKilobotEntity*>(it->second); unKilobotID = pcKB->GetId(); std::cout << unKilobotID << " - " << pcKB->GetEmbodiedEntity().GetOriginAnchor().Position << "\n"; } } void CTrajectoryLoopFunctions::Reset(){} void CTrajectoryLoopFunctions::PostStep() { /* Get the map of all kilobots from the space */ CSpace::TMapPerType& tKBMap = GetSpace().GetEntitiesByType("kilobot"); /* Go through them */ std::string unKilobotID; argos::CColor colorKilobotLed; for(CSpace::TMapPerType::iterator it = tKBMap.begin(); it != tKBMap.end(); ++it) { /* Create a pointer to the current kilobot */ CKilobotEntity* pcKB = any_cast<CKilobotEntity*>(it->second); unKilobotID = pcKB->GetId(); colorKilobotLed = pcKB->GetLEDEquippedEntity()->GetLED(0)->GetColor(); std::cout << unKilobotID << " - " << colorKilobotLed << " - " << pcKB->GetEmbodiedEntity().GetOriginAnchor().Position << "\n"; } } REGISTER_LOOP_FUNCTIONS(CTrajectoryLoopFunctions, "trajectory_loop_functions")

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby pincy » Mon Jan 24, 2022 6:05 pm

It's just a typo. Exercise for you to fix it :-)
I made ARGoS.

AntonioC
Posts: 48
Joined: Mon Aug 30, 2021 7:02 pm

Re: Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby AntonioC » Mon Jan 24, 2022 7:32 pm

Since not a is a pointer I cannot use the operator ->. You could use the period. But even that doesn't work. I also tried a cast but nothing.

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby pincy » Mon Jan 24, 2022 8:16 pm

Yes, you should use the point, and then maybe get a different error if something else is off.
I made ARGoS.

AntonioC
Posts: 48
Joined: Mon Aug 30, 2021 7:02 pm

Re: Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby AntonioC » Mon Jan 24, 2022 9:03 pm

I had to add the library as well #include <argos3/plugins/simulator/entities/led_equipped_entity.h>
Now everything works. :D

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Failed any_cast conversion from argos::CKilobotEntity* to argos::CDirectionalLEDEntity*

Postby pincy » Mon Jan 24, 2022 10:05 pm

Great! Yes, that makes sense.
I made ARGoS.


Return to “How to... ?”