Skip to content
Snippets Groups Projects
Commit a63b5619 authored by Joan Vallvé Navarro's avatar Joan Vallvé Navarro
Browse files

subscriber/publisher new API

parent d6fad9c7
No related branches found
No related tags found
2 merge requests!11new release,!10new release
...@@ -156,15 +156,19 @@ namespace wolf ...@@ -156,15 +156,19 @@ namespace wolf
* *
* You can also check the code in the example file ````src/examples/test_wolf_factories.cpp````. * You can also check the code in the example file ````src/examples/test_wolf_factories.cpp````.
*/ */
class Publisher; class Publisher;
typedef Factory<Publisher> FactoryPublisher; typedef Factory<Publisher,
const std::string&,
const ParamsServer&,
const ProblemPtr,
ros::NodeHandle&> FactoryPublisher;
template<> template<>
inline std::string FactoryPublisher::getClass() const inline std::string FactoryPublisher::getClass() const
{ {
return "FactoryPublisher"; return "FactoryPublisher";
} }
#define WOLF_REGISTER_PUBLISHER(PublisherType) \ #define WOLF_REGISTER_PUBLISHER(PublisherType) \
namespace{ const bool WOLF_UNUSED PublisherType##Registered = \ namespace{ const bool WOLF_UNUSED PublisherType##Registered = \
wolf::FactoryPublisher::registerCreator(#PublisherType, PublisherType::create); } \ wolf::FactoryPublisher::registerCreator(#PublisherType, PublisherType::create); } \
......
...@@ -160,7 +160,8 @@ namespace wolf ...@@ -160,7 +160,8 @@ namespace wolf
typedef Factory<Subscriber, typedef Factory<Subscriber,
const std::string&, const std::string&,
const ParamsServer&, const ParamsServer&,
const SensorBasePtr> FactorySubscriber; const SensorBasePtr,
ros::NodeHandle&> FactorySubscriber;
template<> template<>
inline std::string FactorySubscriber::getClass() const inline std::string FactorySubscriber::getClass() const
{ {
......
...@@ -10,53 +10,88 @@ ...@@ -10,53 +10,88 @@
**************************/ **************************/
#include "core/common/wolf.h" #include "core/common/wolf.h"
#include "core/problem/problem.h" #include "core/problem/problem.h"
#include "factory_publisher.h"
namespace wolf namespace wolf
{ {
WOLF_PTR_TYPEDEFS(Publisher); WOLF_PTR_TYPEDEFS(Publisher);
/* /*
* Macro for defining Autoconf subscriber creator for WOLF's high level API. * Macro for defining Autoconf publisher creator for WOLF's high level API.
* *
* Place a call to this macro inside your class declaration (in the subscriber_class.h file), * Place a call to this macro inside your class declaration (in the publisher_class.h file),
* preferably just after the constructors. * preferably just after the constructors.
* *
* In order to use this macro, the derived subscriber class, PublisherClass, * In order to use this macro, the derived publisher class, PublisherClass,
* must have a constructor available with the API: * must have a constructor available with the API:
* *
* PublisherClass(const std::string& _unique_name, * PublisherClass(const std::string& _unique_name,
* const ParamsServer& _server, * const ParamsServer& _server,
* const SensorBasePtr _sensor_ptr); * const ProblemPtr _problem);
*/ */
#define WOLF_PUBLISHER_CREATE(PublisherClass) \ #define WOLF_PUBLISHER_CREATE(PublisherClass) \
static PublisherPtr create(const std::string& _unique_name, \ static PublisherPtr create(const std::string& _unique_name, \
const ParamsServer& _server) \ const ParamsServer& _server, \
{ \ const ProblemPtr _problem, \
return std::make_shared<PublisherClass>(_unique_name, _server); \ ros::NodeHandle& _nh) \
} \ { \
PublisherPtr pub = std::make_shared<PublisherClass>(_unique_name, _server, _problem); \
pub->initialize(_nh, pub->getTopic()); \
return pub; \
} \
class Publisher class Publisher
{ {
public: public:
Publisher(const std::string& _unique_name, Publisher(const std::string& _unique_name,
const ParamsServer& _server) const ParamsServer& _server,
: period_(1) const ProblemPtr _problem) :
, last_publish_time_(ros::Time(0)) problem_(_problem),
{}; last_publish_time_(ros::Time(0)),
prefix_("ROS publisher/" + _unique_name)
{
period_ = _server.getParam<double>(prefix_ + "/period");
topic_ = _server.getParam<std::string>(prefix_ + "/topic");
};
virtual ~Publisher(){}; virtual ~Publisher(){};
virtual void initialize(ros::NodeHandle& nh, const std::string& topic) = 0; virtual void initialize(ros::NodeHandle& nh, const std::string& topic) = 0;
virtual void publish(const ProblemPtr problem) = 0; virtual void publish() final;
double period_; virtual void publishDerived() = 0;
ros::Time last_publish_time_;
protected: virtual bool ready();
ros::Publisher publisher_; std::string getTopic() const;
protected:
ProblemPtr problem_;
ros::Publisher publisher_;
double period_;
ros::Time last_publish_time_;
std::string prefix_;
std::string topic_;
}; };
inline void Publisher::publish()
{
last_publish_time_ = ros::Time::now();
publishDerived();
}
inline bool Publisher::ready()
{
return (ros::Time::now() - last_publish_time_).toSec() > period_;
}
inline std::string Publisher::getTopic() const
{
return topic_;
}
} // namespace wolf } // namespace wolf
#endif #endif
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
**************************/ **************************/
#include "core/problem/problem.h" #include "core/problem/problem.h"
#include "factory_publisher.h"
#include "publisher.h" #include "publisher.h"
namespace wolf namespace wolf
...@@ -18,12 +17,15 @@ class PublisherPose: public Publisher ...@@ -18,12 +17,15 @@ class PublisherPose: public Publisher
public: public:
PublisherPose(const std::string& _unique_name, PublisherPose(const std::string& _unique_name,
const ParamsServer& _server); const ParamsServer& _server,
const ProblemPtr _problem);
WOLF_PUBLISHER_CREATE(PublisherPose); WOLF_PUBLISHER_CREATE(PublisherPose);
virtual ~PublisherPose(){}; virtual ~PublisherPose(){};
void initialize(ros::NodeHandle &nh, const std::string& topic) override; void initialize(ros::NodeHandle &nh, const std::string& topic) override;
void publish(const ProblemPtr problem) override;
static std::shared_ptr<Publisher> create(); void publishDerived() override;
}; };
WOLF_REGISTER_PUBLISHER(PublisherPose) WOLF_REGISTER_PUBLISHER(PublisherPose)
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
* ROS includes * * ROS includes *
**************************/ **************************/
#include <ros/ros.h> #include <ros/ros.h>
#include <nav_msgs/Odometry.h>
namespace wolf { namespace wolf {
WOLF_PTR_TYPEDEFS(Subscriber); WOLF_PTR_TYPEDEFS(Subscriber);
...@@ -29,13 +28,16 @@ WOLF_PTR_TYPEDEFS(Subscriber); ...@@ -29,13 +28,16 @@ WOLF_PTR_TYPEDEFS(Subscriber);
* const ParamsServer& _server, * const ParamsServer& _server,
* const SensorBasePtr _sensor_ptr); * const SensorBasePtr _sensor_ptr);
*/ */
#define WOLF_SUBSCRIBER_CREATE(SubscriberClass) \ #define WOLF_SUBSCRIBER_CREATE(SubscriberClass) \
static SubscriberPtr create(const std::string& _unique_name, \ static SubscriberPtr create(const std::string& _unique_name, \
const ParamsServer& _server, \ const ParamsServer& _server, \
const SensorBasePtr _sensor_ptr) \ const SensorBasePtr _sensor_ptr, \
{ \ ros::NodeHandle& _nh) \
return std::make_shared<SubscriberClass>(_unique_name, _server, _sensor_ptr); \ { \
} \ SubscriberPtr sub = std::make_shared<SubscriberClass>(_unique_name, _server, _sensor_ptr); \
sub->initialize(_nh, sub->getTopic()); \
return sub; \
} \
class Subscriber class Subscriber
{ {
...@@ -43,6 +45,7 @@ class Subscriber ...@@ -43,6 +45,7 @@ class Subscriber
//wolf //wolf
SensorBasePtr sensor_ptr_; SensorBasePtr sensor_ptr_;
std::string prefix_; std::string prefix_;
std::string topic_;
// ros // ros
ros::Subscriber sub_; ros::Subscriber sub_;
...@@ -54,10 +57,20 @@ class Subscriber ...@@ -54,10 +57,20 @@ class Subscriber
sensor_ptr_(_sensor_ptr), sensor_ptr_(_sensor_ptr),
prefix_("ROS subscriber/" + _unique_name) prefix_("ROS subscriber/" + _unique_name)
{ {
topic_ = _server.getParam<std::string>(prefix_ + "/topic");
} }
virtual ~Subscriber(){}; virtual ~Subscriber(){};
virtual void initSubscriber(ros::NodeHandle& nh, const std::string& topic) = 0; virtual void initialize(ros::NodeHandle& nh, const std::string& topic) = 0;
std::string getTopic() const;
}; };
inline std::string Subscriber::getTopic() const
{
return topic_;
}
} }
#endif #endif
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* WOLF includes * * WOLF includes *
**************************/ **************************/
#include <core/common/wolf.h> #include <core/common/wolf.h>
#include <core/utils/params_server.h> #include "subscriber.h"
/************************** /**************************
* ROS includes * * ROS includes *
...@@ -11,8 +11,6 @@ ...@@ -11,8 +11,6 @@
#include <nav_msgs/Odometry.h> #include <nav_msgs/Odometry.h>
#include "sensor_msgs/JointState.h" #include "sensor_msgs/JointState.h"
#include "subscriber.h"
namespace wolf namespace wolf
{ {
class SubscriberDiffdrive : public Subscriber class SubscriberDiffdrive : public Subscriber
...@@ -31,7 +29,7 @@ class SubscriberDiffdrive : public Subscriber ...@@ -31,7 +29,7 @@ class SubscriberDiffdrive : public Subscriber
const SensorBasePtr _sensor_ptr); const SensorBasePtr _sensor_ptr);
WOLF_SUBSCRIBER_CREATE(SubscriberDiffdrive); WOLF_SUBSCRIBER_CREATE(SubscriberDiffdrive);
virtual void initSubscriber(ros::NodeHandle& nh, const std::string& topic); virtual void initialize(ros::NodeHandle& nh, const std::string& topic);
void callback(const sensor_msgs::JointState::ConstPtr& msg); void callback(const sensor_msgs::JointState::ConstPtr& msg);
}; };
......
...@@ -27,7 +27,7 @@ class SubscriberOdom2d : public Subscriber ...@@ -27,7 +27,7 @@ class SubscriberOdom2d : public Subscriber
const SensorBasePtr _sensor_ptr); const SensorBasePtr _sensor_ptr);
WOLF_SUBSCRIBER_CREATE(SubscriberOdom2d); WOLF_SUBSCRIBER_CREATE(SubscriberOdom2d);
virtual void initSubscriber(ros::NodeHandle& nh, const std::string& topic); virtual void initialize(ros::NodeHandle& nh, const std::string& topic);
void callback(const nav_msgs::Odometry::ConstPtr& msg); void callback(const nav_msgs::Odometry::ConstPtr& msg);
}; };
......
...@@ -52,9 +52,7 @@ WolfRosNode::WolfRosNode() ...@@ -52,9 +52,7 @@ WolfRosNode::WolfRosNode()
std::string topic = it["topic"]; std::string topic = it["topic"];
std::string sensor = it["sensor_name"]; std::string sensor = it["sensor_name"];
WOLF_TRACE("From sensor {" + sensor + "} subscribing {" + subscriber + "} to {" + topic + "} topic"); WOLF_TRACE("From sensor {" + sensor + "} subscribing {" + subscriber + "} to {" + topic + "} topic");
auto subscriber_wrapper = FactorySubscriber::create(subscriber, topic, server, problem_ptr_->getSensor(sensor)); subscribers_.push_back(FactorySubscriber::create(subscriber, topic, server, problem_ptr_->getSensor(sensor), nh_));
subscribers_.push_back(subscriber_wrapper);
subscribers_.back()->initSubscriber(nh_, topic);
} }
// // ROS VISUALIZER // // ROS VISUALIZER
...@@ -72,12 +70,8 @@ WolfRosNode::WolfRosNode() ...@@ -72,12 +70,8 @@ WolfRosNode::WolfRosNode()
{ {
for (auto it : server.getParam<std::vector<std::map<std::string, std::string>>>("ROS publisher")) for (auto it : server.getParam<std::vector<std::map<std::string, std::string>>>("ROS publisher"))
{ {
std::string pub = it["type"]; WOLF_INFO("Pub: ", it["type"]);
WOLF_INFO("Pub: ", pub); publishers_.push_back(FactoryPublisher::create(it["type"], it["topic"], server, problem_ptr_, nh_));
auto publisher = FactoryPublisher::create(pub);
publisher->period_ = converter<double>::convert(it["period"]);
publishers_.push_back(publisher);
publishers_.back()->initialize(nh_,it["topic"]);
} }
} }
catch (MissingValueException& e) catch (MissingValueException& e)
...@@ -240,11 +234,8 @@ int main(int argc, char **argv) ...@@ -240,11 +234,8 @@ int main(int argc, char **argv)
// publish periodically // publish periodically
for(auto pub : wolf_node.publishers_) for(auto pub : wolf_node.publishers_)
if ((ros::Time::now() - pub->last_publish_time_).toSec() >= pub->period_) if (pub->ready())
{ pub->publish();
pub->publish(wolf_node.problem_ptr_);
pub->last_publish_time_ = ros::Time::now();
}
// execute pending callbacks // execute pending callbacks
ros::spinOnce(); ros::spinOnce();
......
...@@ -10,9 +10,10 @@ ...@@ -10,9 +10,10 @@
namespace wolf namespace wolf
{ {
PublisherPose::PublisherPose(const std::string& _unique_name, PublisherPose::PublisherPose(const std::string& _unique_name,
const ParamsServer& _server) : const ParamsServer& _server,
Publisher(_unique_name, _server) const ProblemPtr _problem) :
Publisher(_unique_name, _server, _problem)
{ {
} }
...@@ -22,10 +23,10 @@ void PublisherPose::initialize(ros::NodeHandle& nh, const std::string& topic) ...@@ -22,10 +23,10 @@ void PublisherPose::initialize(ros::NodeHandle& nh, const std::string& topic)
nh.param<std::string>("map_frame_id", map_frame_id_, "map"); nh.param<std::string>("map_frame_id", map_frame_id_, "map");
} }
void PublisherPose::publish(const ProblemPtr _problem) void PublisherPose::publishDerived()
{ {
VectorComposite current_state = _problem->getState("PO"); VectorComposite current_state = problem_->getState("PO");
TimeStamp loc_ts = _problem->getTimeStamp(); TimeStamp loc_ts = problem_->getTimeStamp();
// state not ready // state not ready
if (current_state.count("P") == 0 or if (current_state.count("P") == 0 or
...@@ -41,7 +42,7 @@ void PublisherPose::publish(const ProblemPtr _problem) ...@@ -41,7 +42,7 @@ void PublisherPose::publish(const ProblemPtr _problem)
msg.header.stamp = ros::Time(loc_ts.getSeconds(), loc_ts.getNanoSeconds()); msg.header.stamp = ros::Time(loc_ts.getSeconds(), loc_ts.getNanoSeconds());
// 2D // 2D
if (_problem->getDim() == 2) if (problem_->getDim() == 2)
{ {
msg.pose.pose.position.x = current_state["P"](0); msg.pose.pose.position.x = current_state["P"](0);
msg.pose.pose.position.y = current_state["P"](1); msg.pose.pose.position.y = current_state["P"](1);
......
...@@ -2,11 +2,8 @@ ...@@ -2,11 +2,8 @@
* WOLF includes * * WOLF includes *
**************************/ **************************/
#include <core/capture/capture_diff_drive.h> #include <core/capture/capture_diff_drive.h>
//#include <core/processor/processor_diff_drive.h>
//#include <core/processor/processor_motion.h>
#include <core/sensor/sensor_diff_drive.h> #include <core/sensor/sensor_diff_drive.h>
// #include "Eigen/src/Core/Matrix.h"
#include "core/math/rotations.h" #include "core/math/rotations.h"
#include "subscriber_diffdrive.h" #include "subscriber_diffdrive.h"
...@@ -23,7 +20,7 @@ SubscriberDiffdrive::SubscriberDiffdrive(const std::string& _unique_name, ...@@ -23,7 +20,7 @@ SubscriberDiffdrive::SubscriberDiffdrive(const std::string& _unique_name,
ticks_cov_factor_ = std::static_pointer_cast<SensorDiffDrive>(_sensor_ptr)->getParams()->ticks_cov_factor; ticks_cov_factor_ = std::static_pointer_cast<SensorDiffDrive>(_sensor_ptr)->getParams()->ticks_cov_factor;
} }
void SubscriberDiffdrive::initSubscriber(ros::NodeHandle& nh, const std::string& topic) void SubscriberDiffdrive::initialize(ros::NodeHandle& nh, const std::string& topic)
{ {
sub_ = nh.subscribe(topic, 100, &SubscriberDiffdrive::callback, this); sub_ = nh.subscribe(topic, 100, &SubscriberDiffdrive::callback, this);
} }
......
...@@ -33,7 +33,7 @@ SubscriberOdom2d::SubscriberOdom2d(const std::string& _unique_name, ...@@ -33,7 +33,7 @@ SubscriberOdom2d::SubscriberOdom2d(const std::string& _unique_name,
{ {
} }
void SubscriberOdom2d::initSubscriber(ros::NodeHandle& nh, const std::string& topic) void SubscriberOdom2d::initialize(ros::NodeHandle& nh, const std::string& topic)
{ {
sub_ = nh.subscribe(topic, 100, &SubscriberOdom2d::callback, this); sub_ = nh.subscribe(topic, 100, &SubscriberOdom2d::callback, this);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment