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.