Issues with Dynamic Casting and Segmentation Fault

Requests regarding how to set up experiments in ARGoS.
Rluna319
Posts: 5
Joined: Sat Sep 17, 2022 9:33 pm

Issues with Dynamic Casting and Segmentation Fault

Postby Rluna319 » Wed Sep 27, 2023 1:15 pm

Hello,

I am currently encountering an issue related to dynamic casting and segmentation faults in my ARGoS simulation. I have already browsed through previous forum posts and attempted to implement the solutions provided, but the issue persists.

Problem Description:
I am working on a foraging simulation where I use multiple controllers. Initially, I was getting a std::bad_cast error, which, according to previous forum posts, seemed to be due to using dynamic casting with references. I followed the advice to switch to using pointers for dynamic casting as suggested.

Code:
Here is an extended snippet of my code where I loop through the controllers, perform dynamic casting, and print out type IDs:

Code: Select all

for(argos::CSpace::TMapPerType::iterator it = footbots.begin(); it != footbots.end(); it++) {
    argos::CFootBotEntity& footBot = *argos::any_cast<argos::CFootBotEntity*>(it->second);
    BaseController* c = dynamic_cast<BaseController*>(&footBot.GetControllableEntity().GetController());
    if(c != nullptr) {
        cout << "Type of BaseController pointer: " << typeid(c).name() << endl;
        CPFA_controller* c2 = dynamic_cast<CPFA_controller*>(c);
        Detractor_controller* c3 = dynamic_cast<Detractor_controller*>(c);
        if(c2 != nullptr) {
            c2->SetLoopFunctions(this);
            cout << "CPFA_controller cast successful" << endl;
        } else {
            cout << "CPFA_controller cast failed" << endl;
            cout << "Actual type of c2: " << typeid(c2).name() << endl;
        }
        if(c3 != nullptr) {
            c3->SetLoopFunctions(this);
            cout << "Detractor_controller cast successful" << endl;
        } else {
            cout << "Detractor_controller cast failed" << endl;
            cout << "Actual type of c3: " << typeid(c3).name() << endl;
        }
    } else {
        cout << "BaseController cast failed" << endl;
    }
}
After running the above snippet, I get the following type of output (formatted here for readability):

Code: Select all

Type of BaseController pointer: P14BaseController
CPFA_controller cast successful
Detractor_controller cast failed
Actual type of c3: P20Detractor_controller

Type of BaseController pointer: P14BaseController
CPFA_controller cast successful
Detractor_controller cast failed
Actual type of c3: P20Detractor_controller

...

Type of BaseController pointer: P14BaseController
CPFA_controller cast failed
Actual type of c2: P15CPFA_controller
Detractor_controller cast successful

Type of BaseController pointer: P14BaseController
CPFA_controller cast failed
Actual type of c2: P15CPFA_controller
Detractor_controller cast successful

The output suggests that some casts to CPFA_controller are successful, while others are failing. Similarly, some casts to Detractor_controller are failing as well. This inconsistency is puzzling, especially since the pointer does not appear to be null when inspected with GDB.

GDB Output:

Code: Select all

Thread 1 "argos3" hit Breakpoint 1, ...
(gdb) print c
$9 = (CPFA_controller *) 0xbb754be082d0ba00
(gdb) print typeid(c)
$10 = {_vptr.type_info = 0x7ffff7e9bc38 <vtable for __cxxabiv1::__pointer_type_info+16>, 
  __name = 0x7ffff276a110 <typeinfo name for CPFA_controller*> "P15CPFA_controller"}
Shortly after this is where the segmentation fault occurs.

GDB Output:

Code: Select all

hread 1 "argos3" received signal SIGSEGV, Segmentation fault.
0x00007ffff2740022 in CPFA_controller::IsHoldingFood (this=0x0) at /home/Ryan/Foraging_Swarm_Defense/CPFA/source/CPFA/CPFA_controller.cpp:161
161			return isHoldingFood;
(gdb) bt
#0  0x00007ffff2740022 in CPFA_controller::IsHoldingFood() (this=0x0) at /home/Ryan/Foraging_Swarm_Defense/CPFA/source/CPFA/CPFA_controller.cpp:161
#1  0x00007ffff2600406 in CPFA_qt_user_functions::DrawOnRobot(argos::CFootBotEntity&) (this=0x555555b9cd80, entity=...)
    at /home/Ryan/Foraging_Swarm_Defense/CPFA/source/CPFA/CPFA_qt_user_functions.cpp:19
#2  0x00007ffff260258b in argos::CQTOpenGLUserFunctions::Thunk<CPFA_qt_user_functions, argos::CFootBotEntity>(argos::CEntity&) (this=0x555555b9cd80, c_entity=...)
    at /usr/include/argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_user_functions.h:477
#3  0x00007ffff6840616 in argos::CQTOpenGLWidget::paintGL() () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#4  0x00007ffff5ea5e05 in  () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#5  0x00007ffff5e832b6 in QWidget::event(QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#6  0x00007ffff5e40a66 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#7  0x00007ffff5e4a0f0 in QApplication::notify(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#8  0x00007ffff6818157 in argos::CQTOpenGLApplication::notify(QObject*, QEvent*) () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#9  0x00007ffff542880a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Core.so.5
#10 0x00007ffff5e7bf4a in QWidgetPrivate::sendPaintEvent(QRegion const&) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#11 0x00007ffff5ea5c2f in QOpenGLWidget::resizeEvent(QResizeEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#12 0x00007ffff6842687 in argos::CQTOpenGLWidget::resizeEvent(QResizeEvent*) () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#13 0x00007ffff5e83947 in QWidget::event(QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#14 0x00007ffff5e40a66 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#15 0x00007ffff5e4a0f0 in QApplication::notify(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#16 0x00007ffff6818157 in argos::CQTOpenGLApplication::notify(QObject*, QEvent*) () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#17 0x00007ffff542880a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Core.so.5
#18 0x00007ffff5e7be6e in QWidgetPrivate::sendPendingMoveAndResizeEvents(bool, bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#19 0x00007ffff5e7fbd7 in QWidgetPrivate::show_helper() () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#20 0x00007ffff5e82d6b in QWidgetPrivate::setVisible(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#21 0x00007ffff5e7fb61 in QWidgetPrivate::showChildren(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#22 0x00007ffff5e7fbf3 in QWidgetPrivate::show_helper() () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#23 0x00007ffff5e82d6b in QWidgetPrivate::setVisible(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#24 0x00007ffff5e7fb61 in QWidgetPrivate::showChildren(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#25 0x00007ffff5e7fbf3 in QWidgetPrivate::show_helper() () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#26 0x00007ffff5e82d6b in QWidgetPrivate::setVisible(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#27 0x00007ffff6836612 in argos::CQTOpenGLRender::Execute() () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
Steps Taken:
  • Switched from dynamic casting with references to pointers.
  • Checked for null pointers before accessing methods.
  • Validated object lifetimes.
Despite these steps, the issue remains unresolved. I would greatly appreciate any guidance or suggestions for resolving this issue.

Thank you in advance for your help.

Best regards,
Ryan

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

Re: Issues with Dynamic Casting and Segmentation Fault

Postby pincy » Thu Sep 28, 2023 2:30 am

I don't have sufficient information to know what is going on, since the crucial piece of the puzzle is how you define your three controller classes. From the output, I can infer that
  • you have a BaseController that extends CCI_Controller
  • you have CPFA_controller that extends BaseController
  • you have a Detractor_controller that extends BaseController
Again from the output, it seems that CPFA_controller and Detractor_controller do not extend each other (which is a good thing). If what I am getting is correct, then the casts make sense and the output you get is correct.

From what you show me, I wouldn't diagnose the segfault as a casting problem. It's likely that the issue is elsewhere, but without the full source code I can't exactly tell.
I made ARGoS.

Rluna319
Posts: 5
Joined: Sat Sep 17, 2022 9:33 pm

Re: Issues with Dynamic Casting and Segmentation Fault

Postby Rluna319 » Sun Oct 08, 2023 3:32 pm

Thank you for your response. I apologize for the delay in my reply. You are correct in your understanding of the class hierarchy:

- BaseController extends CCI_Controller
- CPFA_controller extends BaseController
- Detractor_controller extends BaseController
- CPFA_controller and Detractor_controller do not extend each other

I would hate to overburden you with the full source code but here is the link to the repo for your reference:
https://github.com/MARSLab-UTRGV/Foraging_Swarm_Defense. The branch with the issue in reference is ryan-dev.

Another important note is that if I run the code with visualization off, I do not encounter the segmentation fault.

Along with the debug messages received through GDB:

Code: Select all

#0  0x00007ffff2732042 in CPFA_controller::IsHoldingFood (this=0x0)
    at /home/ryan/swarm-def/CPFA/source/CPFA/CPFA_controller.cpp:161
#1  0x00007ffff25fb3f6 in CPFA_qt_user_functions::DrawOnRobot (this=0x555555b01930, entity=...)
    at /home/ryan/swarm-def/CPFA/source/CPFA/CPFA_qt_user_functions.cpp:19
        c = 0x0
#2  0x00007ffff25fd57b in argos::CQTOpenGLUserFunctions::Thunk<CPFA_qt_user_functions, argos::CFootBotEntity> (
    this=0x555555b01930, c_entity=...)
    at /usr/include/argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_user_functions.h:477
        cImpl = @0x555555b01930: {<argos::CQTOpenGLUserFunctions> = {<No data fields>}, loopFunctions = 
    @0x5555555fac20}
        cEntity = @0x5555556fc720: <incomplete type>
        cFunctionHolder = @0x555555b52760: {<argos::CQTOpenGLUserFunctions::CFunctionHolder> = {<No data fields>}, 
          Function = (void (CPFA_qt_user_functions::*)(CPFA_qt_user_functions * const, 
    argos::CFootBotEntity &)) 0x7ffff25fb37c <CPFA_qt_user_functions::DrawOnRobot(argos::CFootBotEntity&)>}
The error only starts when CPFA_controller::IsHoldingFood() is called from within CPFA_qt_user_functions::DrawOnRobot().

When called in CPFA_loop_functions::Init(argos::TConfigurationNode &node) as follows:

Code: Select all

for(argos::CSpace::TMapPerType::iterator it = footbots.begin(); it != footbots.end(); it++) {
		argos::CFootBotEntity& footBot = *argos::any_cast<argos::CFootBotEntity*>(it->second);
		BaseController* c = dynamic_cast<BaseController*>(&footBot.GetControllableEntity().GetController());
		if(c != nullptr) {
			// cout << "Type of BaseController pointer: " << typeid(c).name() << endl;
			CPFA_controller* c2 = dynamic_cast<CPFA_controller*>(c);
			Detractor_controller* c3 = dynamic_cast<Detractor_controller*>(c);
			if(c2 != nullptr) {
				c2->SetLoopFunctions(this);
				// cout << "CPFA_controller cast successful" << endl;
				bool isHolding = c2->IsHoldingFood();
				if(isHolding)cout << "Robot holding food." << endl;
				else cout << "Robot NOT holding food." << endl;
				cout << "Bot count = " << bot_count << endl;
				bot_count++;
			}
			else {
				// cout << "CPFA_controller cast failed" << endl;
				// cout << "Actual type of c2: " << typeid(c2).name() << endl;
			}
			if(c3 != nullptr) {
				c3->SetLoopFunctions(this);
				// cout << "Detractor_controller cast successful" << endl;
				bool isHolding = c3->IsHoldingFood();
				if(isHolding)cout << "Robot holding food." << endl;
				else cout << "Robot NOT holding food." << endl;
				cout << "Bot count = " << bot_count << endl;
				bot_count++;
			}
			else {
				// cout << "Detractor_controller cast failed" << endl;
				// cout << "Actual type of c3: " << typeid(c3).name() << endl;
			}
		}
		else {
			// cout << "BaseController cast failed" << endl;
		}
	}
There is no segmentation fault. IsHoldingFood() returns False for all controllers as expected, as that is the initialized value.

CPFA_controller.h:

Code: Select all

#ifndef CPFA_CONTROLLER_H
#define CPFA_CONTROLLER_H

#include <source/Base/BaseController.h>
#include <source/Base/Pheromone.h>
#include <source/CPFA/CPFA_loop_functions.h>
/* Definition of the LEDs actuator */
#include <argos3/plugins/robots/generic/control_interface/ci_leds_actuator.h>

// Ryan Luna 12/28/22
#include <source/Base/QuarantineZone.h>
#include <source/Base/Food.h>

#include <source/Base/Attacker_Nest.h>

using namespace std;
using namespace argos;

static unsigned int num_targets_collected = 0;

class CPFA_loop_functions;

class CPFA_controller : public BaseController {

	public:

		CPFA_controller();

		// CCI_Controller inheritence functions
		void Init(argos::TConfigurationNode &node);
		void ControlStep();
		void Reset();

		bool IsHoldingFood();
		bool IsHoldingFakeFood();	// Ryan Luna 11/12/22
		bool IsUsingSiteFidelity();
		bool IsInTheNest();
		bool IsInTheBadNest();

		Real FoodDistanceTolerance;

		void SetLoopFunctions(CPFA_loop_functions* lf);
  
		size_t     GetSearchingTime();//qilu 09/26/2016
		size_t      GetTravelingTime();//qilu 09/26/2016
		string      GetStatus();//qilu 09/26/2016
		size_t      startTime;//qilu 09/26/2016

		/* quarantine zone functions */		// Ryan Luna 12/28/22
		void ClearZoneList();
		void ClearLocalFoodList();
		void AddZone(QZone newZone);
		void AddLocalFood(Food newFood);
		void RemoveZone(QZone Z);
		void RemoveLocalFood(Food F);
		bool TargetInQZone(CVector2 target);

	private:

		/* quarantine zone variables */		// Ryan Luna 12/28/22
		vector<QZone>	QZoneList;
		vector<Food>	LocalFoodList;

		Food FoodBeingHeld;		// Ryan Luna 1/24/23

  		string 			controllerID;//qilu 07/26/2016

		CPFA_loop_functions* LoopFunctions;
		argos::CRandom::CRNG* RNG;

		/* pheromone trail variables */
		std::vector<argos::CVector2> TrailToShare;
		std::vector<argos::CVector2> TrailToFollow;
		std::vector<argos::CRay3>    MyTrail;

		/* robot position variables */
		argos::CVector2 SiteFidelityPosition;
  		bool			 updateFidelity; //qilu 09/07/2016
  
		vector<CRay3> myTrail;
		CColor        TrailColor;

		bool isInformed;
		bool isWronglyInformed;
		bool isHoldingFood;
		bool isHoldingFakeFood;		// Ryan Luna 11/12/22
		bool isUsingSiteFidelity;
		bool isGivingUpSearch;
		bool QZoneStrategy;		// to turn ON/OFF Quarantine Zones
  
		size_t ResourceDensity;
		size_t MaxTrailSize;
		size_t SearchTime;//for informed search
		size_t BadFoodCount;	// Ryan Luna 01/30/23
		size_t BadFoodLimit;	// Ryan Luna 01/30/23
		QZone* CurrentZone;		// Ryan Luna 01/30/23
		bool UseQZones;			// Ryan Luna 02/05/23
		size_t MergeMode;
  
		size_t           searchingTime; //qilu 09/26
		size_t           travelingTime;//qilu 09/26

		Real	FFdetectionAcc;
		Real    RFdetectionAcc;

		/* Detractor related stuff */
		bool captured;
		size_t captureTime;
        
  
		/* iAnt CPFA state variable */
		enum CPFA_state {
			DEPARTING = 0,
			SEARCHING = 1,
			RETURNING = 2,
			SURVEYING = 3,
			CAPTURED  = 4,				// new CPFA state for defined behavior after being captured
		} CPFA_state;

		/* iAnt CPFA state functions */
		void CPFA();
		void Departing();
		void Searching();
		void Returning();
		void Surveying();
		void Captured();

		/* CPFA helper functions */
		void SetRandomSearchLocation();
		void SetHoldingFood();
		void SetLocalResourceDensity();
		void SetFidelityList(argos::CVector2 newFidelity);
		void SetFidelityList();
		bool SetTargetPheromone();

		argos::Real GetExponentialDecay(argos::Real value, argos::Real time, argos::Real lambda);
		argos::Real GetBound(argos::Real value, argos::Real min, argos::Real max);
		argos::Real GetPoissonCDF(argos::Real k, argos::Real lambda);

		void UpdateTargetRayList();
  
		CVector2 previous_position;

		string results_path;
		string results_full_path;
		bool isUsingPheromone;

		unsigned int survey_count;
		/* Pointer to the LEDs actuator */
        CCI_LEDsActuator* m_pcLEDs;
};

#endif /* CPFA_CONTROLLER_H */
CPFA_controller::IsHoldingFood()

Code: Select all

bool CPFA_controller::IsHoldingFood() {
		return isHoldingFood;
}
Detractor_controller.h:

Code: Select all

#ifndef DETRACTOR_CONTROLLER_H
#define DETRACTOR_CONTROLLER_H

#include <source/Base/BaseController.h>
#include <source/Base/Pheromone.h>
#include <source/CPFA/CPFA_loop_functions.h>
/* Definition of the LEDs actuator */
#include <argos3/plugins/robots/generic/control_interface/ci_leds_actuator.h>

// Ryan Luna 12/28/22
#include <source/Base/QuarantineZone.h>
#include <source/Base/Food.h>

using namespace std;
using namespace argos;

class CPFA_loop_functions;

class Detractor_controller : public BaseController {

	public:

		Detractor_controller();

		// CCI_Controller inheritence functions
		void Init(argos::TConfigurationNode &node);
		void ControlStep();
		void Reset();

		bool IsHoldingFood();
		bool IsHoldingFakeFood();	// Ryan Luna 11/12/22
		bool IsUsingSiteFidelity();
		bool IsInTheNest();
		bool IsInTheBadNest();

		Real FoodDistanceTolerance;

		void SetLoopFunctions(CPFA_loop_functions* lf);
  
		size_t     GetSearchingTime();//qilu 09/26/2016
		size_t      GetTravelingTime();//qilu 09/26/2016
		string      GetStatus();//qilu 09/26/2016
		string 		GetDetractorStatus();
		size_t      startTime;//qilu 09/26/2016

		/* quarantine zone functions */		// Ryan Luna 12/28/22
		void ClearZoneList();
		void ClearLocalFoodList();
		void AddZone(QZone newZone);
		void AddLocalFood(Food newFood);
		void RemoveZone(QZone Z);
		void RemoveLocalFood(Food F);
		bool TargetInQZone(CVector2 target);

	private:

		/* quarantine zone variables */		// Ryan Luna 12/28/22
		vector<QZone>	QZoneList;
		vector<Food>	LocalFoodList;

		Food FoodBeingHeld;		// Ryan Luna 1/24/23

  		string 			controllerID;//qilu 07/26/2016

		CPFA_loop_functions* LoopFunctions;
		argos::CRandom::CRNG* RNG;

		/* pheromone trail variables */
		std::vector<argos::CVector2> TrailToShare;
		std::vector<argos::CVector2> TrailToFollow;
		std::vector<argos::CRay3>    MyTrail;

		/* robot position variables */
		argos::CVector2 SiteFidelityPosition;
  		bool			 updateFidelity; //qilu 09/07/2016
  
		vector<CRay3> myTrail;
		CColor        TrailColor;

		bool isInformed;
		bool isWronglyInformed;
		bool isHoldingFood;
		bool isHoldingFakeFood;		// Ryan Luna 11/12/22
		bool isUsingSiteFidelity;
		bool isGivingUpSearch;
		bool QZoneStrategy;		// to turn ON/OFF Quarantine Zones
  
		size_t ResourceDensity;
		size_t MaxTrailSize;
		size_t SearchTime;//for informed search
		size_t BadFoodCount;	// Ryan Luna 01/30/23
		size_t BadFoodLimit;	// Ryan Luna 01/30/23
		QZone* CurrentZone;		// Ryan Luna 01/30/23
		bool UseQZones;			// Ryan Luna 02/05/23
		size_t MergeMode;
  
		size_t           searchingTime; //qilu 09/26
		size_t           travelingTime;//qilu 09/26

		Real	FFdetectionAcc;
		Real    RFdetectionAcc;
        
  
		/* iAnt Detractor state variable */
		enum CPFA_state {
			DEPARTING = 0,
			SEARCHING = 1,
			RETURNING = 2,
			SURVEYING = 3
		}	CPFA_state;

		enum Detractor_state {
			_HOME_			= 0,			// robot is at its home (attacker) nest
			_DEPARTING_ 	= 1,			// departing from the attacker nest to the defender nest
			_DELIVERING_	= 2,			// delivering fake food and deploying false pheromone trail
			_RETURNING_		= 3,			// returning to the attacker nest from the defender nest after delivering fake food
		}	Detractor_state;

		bool isDetractor;					// boolean to set at initialization to determine if robot is a detractor or not
		argos::CVector2 badNestPos;			// location of the attacker nest (used when laying pheromone trails back to bad nest position.

		/* iAnt CPFA state functions */
		void CPFA();
		void Departing();
		void Searching();
		void Returning();
		void Surveying();
		
		/* iAnt Detractor state functions */
		void Detract();
		void Home_d();
		void Departing_d();
		void Delivering_d();
		void Returning_d();

		/* CPFA/Detractor helper functions */
		void SetRandomSearchLocation();
		void SetHoldingFood();
		void SetLocalResourceDensity();
		void SetFidelityList(argos::CVector2 newFidelity);
		void SetFidelityList();
		bool SetTargetPheromone();

		argos::Real GetExponentialDecay(argos::Real value, argos::Real time, argos::Real lambda);
		argos::Real GetBound(argos::Real value, argos::Real min, argos::Real max);
		argos::Real GetPoissonCDF(argos::Real k, argos::Real lambda);

		void UpdateTargetRayList();
  
		CVector2 previous_position;

		string results_path;
		string results_full_path;
		bool isUsingPheromone;

		unsigned int survey_count;
		/* Pointer to the LEDs actuator */
        CCI_LEDsActuator* m_pcLEDs;
};

#endif /* DETRACTOR_CONTROLLER_H */
BaseController.h:

Code: Select all

#ifndef BASECONTROLLER_H
#define BASECONTROLLER_H

#include <argos3/core/utility/logging/argos_log.h>
#include <argos3/core/control_interface/ci_controller.h>
#include <argos3/plugins/robots/generic/control_interface/ci_positioning_sensor.h>
#include <argos3/plugins/robots/generic/control_interface/ci_differential_steering_actuator.h>
#include <argos3/plugins/robots/foot-bot/control_interface/ci_footbot_proximity_sensor.h>
#include <argos3/core/simulator/loop_functions.h>
#include <cmath>
#include <stack>

/**
 * BaseController
 * @author Antonio Griego
 */
class BaseController : public argos::CCI_Controller {

	public:

		BaseController();

		/*  navigation functions */
		argos::CRadians GetHeading();
		argos::CVector2 GetPosition();
		argos::CVector2 GetTarget();
  unsigned int GetCollisionTime();//qilu 09/26/2016
		void SetTarget(argos::CVector2 t);
		void SetStartPosition(argos::CVector3 sp);
		argos::CVector3 GetStartPosition();
		size_t GetMovementState();

		void Stop();
		void Move();
		bool Wait();
		void Wait(size_t wait_time_in_seconds);

		/*  time calculation functions */
		size_t SimulationTick();
		size_t SimulationTicksPerSecond();
		argos::Real SimulationSecondsPerTick();
		argos::Real SimulationTimeInSeconds();

		void SetIsHeadingToNest(bool n);
		bool IsAtTarget();

	protected:

		argos::CRandom::CRNG* RNG;

		unsigned int collision_counter;
		float DestinationNoiseStdev; // for introducing error in destination positions
		float PositionNoiseStdev; // for introducing error in current position

		size_t WaitTime;
        size_t collisionDelay;
        bool collisionFlag;
                
		argos::CRadians TargetAngleTolerance;
		argos::Real NestDistanceTolerance;
		argos::CRadians NestAngleTolerance;
		argos::Real TargetDistanceTolerance;
		argos::Real SearchStepSize;

		argos::CRange<argos::Real> ForageRangeX;
		argos::CRange<argos::Real> ForageRangeY;
		argos::CRange<argos::Real> GoStraightAngleRangeInDegrees;

		//  base controller movement parameters
		argos::Real RobotForwardSpeed;
		argos::Real RobotRotationSpeed;
		argos::Real TicksToWaitWhileMoving;

		// foot-bot components: sensors and actuators
		argos::CCI_PositioningSensor* compassSensor;
		argos::CCI_DifferentialSteeringActuator* wheelActuator;
		argos::CCI_FootBotProximitySensor* proximitySensor;

		// controller state variables
		enum MovementState {
			STOP    = 0,
			LEFT    = 1,
			RIGHT   = 2,
			FORWARD = 3,
			BACK    = 4
		} CurrentMovementState;

		/* movement definition variables */
		struct Movement {
			size_t type;
			argos::Real magnitude;
		};

		Movement previous_movement;
		argos::CVector2 previous_pattern_position;
	
		std::stack<Movement> MovementStack;

	private:

		argos::CLoopFunctions& LF;

		argos::CVector3 StartPosition;
		argos::CVector2 TargetPosition;

		/* private navigation helper functions */
		void SetNextMovement();
		void SetTargetAngleDistance(argos::Real newAngleToTurnInDegrees);
		void SetTargetTravelDistance(argos::Real newTargetDistance);
		void SetLeftTurn(argos::Real newTargetAngle);
		void SetRightTurn(argos::Real newTargetAngle);
		void SetMoveForward(argos::Real newTargetDistance);
		void SetMoveBack(argos::Real newTargetDistance);
		void PushMovement(size_t moveType, argos::Real moveSize);
		void PopMovement();

		/* collision detection functions */
		bool CollisionDetection();
		argos::CVector2 GetCollisionVector();

		bool heading_to_nest;

};

#endif /* IANTBASECONTROLLER_H */
The CPFA and Detractor controllers are quite similar. The Detractor controller is a slightly modified version of the CPFA controller.

I will also add that this error happens during the initialization steps, before the visualization window pops up.

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

Re: Issues with Dynamic Casting and Segmentation Fault

Postby pincy » Mon Oct 16, 2023 5:36 pm

Thanks, with all this info I can look into the problem. I'll set some time aside tomorrow.
I made ARGoS.


Return to “How to... ?”