Ask Your Question
0

Thread related problem while subscribing/using data

asked 2016-12-20 10:09:30 -0600

l4ncelot gravatar image

Hi,

I've been debugging my code recently and I've encountered this strange value assignments in my code (e.g. double value was set to 48342750631830156809646198744631837214400839096756594218130390397264833847717452548524106800103300384016854007123474855776033541556472495167585138333188621137621421726366230289867389262961030442796182455676615285992044974047232 ) for not exactly obvious reasons.

I then figured out that it could be thread related problem, where what I'm doing is writing and reading this variable at the same time which causes this strange assignments, but I'm not exactly sure.

I'will show you simplified idea of my problem. Lets say I have this main method:

int main(int argc, char** argv){
    ros::init(argc, argv, "attitude_controller");
    ros::NodeHandle nh;

    attitude_controller_ = new AttitudeController(nh);

    ros::Rate rate(20.0);
    ros::Time previous_time = ros::Time::now();
    while(ros::ok()){
        ros::Duration time_difference = ros::Time::now() - previous_time;
        previous_time = ros::Time::now();

        attitude_controller_->control(time_difference);  

        ros::spinOnce();
        rate.sleep();
    }

    return 0;
}

And I have AttitudeController class which has FlyingStrategy class in it that looks similar to this:

class FlyingStrategy {
public:
    FlyingStrategy(const ros::NodeHandle& nh);

    virtual ~FlyingStrategy();

    virtual void init();

    void control(const ros::Duration& time_difference);

    void callbackCarrotPose(const geometry_msgs::PoseConstPtr& carrot_pose);

private:
    virtual void pidControlLoop(const ros::Duration& time_difference, const tf::StampedTransform& transform_world_base);

protected:
    ros::NodeHandle nh_;

    ros::Subscriber sub_carrot_pose_;
    tf::Vector3 carrot_;
};

The main functions are here callbackCarrotPose and control. callbackCarrotPose is just assignment like this:

void FlyingStrategy::callbackCarrotPose(const geometry_msgs::PoseConstPtr& carrot_pose) {
    carrot_.setX(carrot_pose->position.x);
    carrot_.setY(carrot_pose->position.y);
    carrot_.setZ(carrot_pose->position.z);
}

The problem in my opinion occurs when I'm trying to set carrot_ variable in callbackCarrotPose function and using this variable in control function at the same time. Because when I printed all the values coming to callbackCarrotPose function, all values were fine. But after printing them in control function sometimes the values were as show before.

How should I fix this issue then? Should I use some kind of thread lock in callback function and in my control function? And if so, how could I do it? I've not been using threads yet, so bear with me :).

Thank you for any advice.

edit retag flag offensive close merge delete

Comments

Are you creating any threads in FlyingStrategy yourself? Because your main uses single-threaded eventqueue processing, so your callback cannot be called when attitude_controller_->control(time_difference) is executing.

gvdhoorn gravatar imagegvdhoorn ( 2016-12-20 11:06:10 -0600 )edit

No, I'm not creating any threads by myself. But I have some classes that inherit from FlyingStrategy and there I use polymorphism. So I constantly change type of FlyingStrategy. Could it be the case perhaps? I can describe it more accurately if you want to.

l4ncelot gravatar imagel4ncelot ( 2016-12-20 16:32:18 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-12-21 09:59:23 -0600

l4ncelot gravatar image

So after a while me and my friend figured this out. It wasn't thread related problem at all. I had in my class tf::Vector3object which I initialized only after callbackCarrotPose was called. And because tf::Vector3 has an empty constructor and it's member variable is some array, it never had any values before callbackCarrotPose was called, therefore those strange values occurred.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-12-20 10:09:30 -0600

Seen: 41 times

Last updated: Dec 21 '16