Skip to content

Commit

Permalink
Fix building realtime_publisher using NON_POLLING (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjaget authored Jan 6, 2024
1 parent a92157f commit 164f6d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Travis Continuous Integration Configuration File For ROS Control Projects
language: generic
services:
- docker

notifications:
email:
recipients:
- bence.magyar.robotics@gmail.com
on_success: change #[always|never|change] # default: change
on_failure: change #[always|never|change] # default: always

env:
global:
- UPSTREAM_WORKSPACE='https://raw.github.com/ros-controls/ros_control/$ROS_DISTRO-devel/ros_control.rosinstall -realtime_tools'
matrix:
- ROS_DISTRO=melodic ROS_REPO=ros
- ROS_DISTRO=melodic ROS_REPO=ros-testing

install:
- git clone --quiet --depth 1 https://github.com/ros-industrial/industrial_ci.git .industrial_ci -b master
script:
- .industrial_ci/travis.sh
3 changes: 1 addition & 2 deletions include/realtime_tools/realtime_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class RealtimeBuffer
{
public:
RealtimeBuffer()
: new_data_available_(false)
{
// allocate memory
non_realtime_data_ = new T();
Expand Down Expand Up @@ -157,7 +156,7 @@ class RealtimeBuffer

T* realtime_data_;
T* non_realtime_data_;
bool new_data_available_;
bool new_data_available_{false};

// Set as mutable so that readFromNonRT() can be performed on a const buffer
mutable std::mutex mutex_;
Expand Down
24 changes: 12 additions & 12 deletions include/realtime_tools/realtime_publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class RealtimePublisher
~RealtimePublisher()
{
stop();
while (is_running())
while (is_running_)
{
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
Expand Down Expand Up @@ -124,7 +124,7 @@ class RealtimePublisher
}
else
{
msg_mutex_.unlock();
unlock();
return false;
}
}
Expand All @@ -143,10 +143,7 @@ class RealtimePublisher
void unlockAndPublish()
{
turn_ = NON_REALTIME;
msg_mutex_.unlock();
#ifdef NON_POLLING
updated_cond_.notify_one();
#endif
unlock();
}

/** \brief Get the data lock form non-realtime
Expand Down Expand Up @@ -174,6 +171,9 @@ class RealtimePublisher
void unlock()
{
msg_mutex_.unlock();
#ifdef NON_POLLING
updated_cond_.notify_one();
#endif
}

private:
Expand All @@ -185,16 +185,16 @@ class RealtimePublisher
{
publisher_ = node_.advertise<Msg>(topic_, queue_size, latched);
keep_running_ = true;
turn_ = REALTIME;
thread_ = std::thread(&RealtimePublisher::publishingLoop, this);
}


bool is_running() const { return is_running_; }

void publishingLoop()
{
is_running_ = true;
turn_ = REALTIME;
#ifdef NON_POLLING
std::unique_lock<std::mutex> cond_lock(msg_mutex_, std::defer_lock_t());
#endif

while (keep_running_)
{
Expand All @@ -205,7 +205,7 @@ class RealtimePublisher
while (turn_ != NON_REALTIME && keep_running_)
{
#ifdef NON_POLLING
updated_cond_.wait(lock);
updated_cond_.wait(cond_lock);
#else
unlock();
std::this_thread::sleep_for(std::chrono::microseconds(500));
Expand All @@ -232,7 +232,7 @@ class RealtimePublisher

std::thread thread_;

std::mutex msg_mutex_; // Protects msg_
std::mutex msg_mutex_; // Protects msg_ and turn_

#ifdef NON_POLLING
std::condition_variable updated_cond_;
Expand Down
2 changes: 2 additions & 0 deletions src/realtime_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ namespace realtime_tools
ROS_WARN_THROTTLE(1.0, "Time estimator has trouble transferring data between non-RT and RT");

// release lock
#ifndef NON_POLLING
guard.unlock();
#endif
r.sleep();
}
}
Expand Down

0 comments on commit 164f6d8

Please sign in to comment.