Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port sound_play.h to ros2 #246

Draft
wants to merge 4 commits into
base: ros2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions .github/workflows/ros2.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
name: ROS2 CI
on:
push:
branches:
- ros2
pull_request:
schedule:
- cron: "0 0 * * *"
workflow_dispatch:
jobs:
industrial_ci:
strategy:
matrix:
env:
- ROS_DISTRO: foxy
ROS_REPO: testing
CMAKE_ARGS: '-DCMAKE_BUILD_TYPE=Debug'
- ROS_DISTRO: foxy
ROS_REPO: testing
CMAKE_ARGS: '-DCMAKE_BUILD_TYPE=Release'
- ROS_DISTRO: galactic
ROS_REPO: testing
CMAKE_ARGS: '-DCMAKE_BUILD_TYPE=Debug'
- ROS_DISTRO: galactic
ROS_REPO: testing
CMAKE_ARGS: '-DCMAKE_BUILD_TYPE=Release'
ROS_DISTRO: [humble, iron, rolling]
ROS_REPO: [testing, main]
CMAKE_ARGS: ['-DCMAKE_BUILD_TYPE=Debug', '-DCMAKE_BUILD_TYPE=Release']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- uses: 'ros-industrial/industrial_ci@master'
env: ${{matrix.env}}
env:
ROS_DISTRO: ${{ matrix.ROS_DISTRO }}
ROS_REP: ${{ matrix.ROS_REPO }}
CMAKE_ARGS: ${{ matrix.CMAKE_ARGS }}
21 changes: 3 additions & 18 deletions sound_play/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,10 @@ find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(audio_common_msgs REQUIRED)
find_package(action_msgs REQUIRED)
find_package(sound_play_msgs)

include_directories(rclcpp audio_common_msgs action_msgs builtin_interfaces)

set(msg_files
"msg/SoundRequest.msg"
)

set(action_files
"action/SoundRequest.action"
)

rosidl_generate_interfaces(${PROJECT_NAME}
${msg_files}
${action_files}
DEPENDENCIES
action_msgs
audio_common_msgs
builtin_interfaces
)

ament_python_install_package(${PROJECT_NAME})

install(PROGRAMS
Expand All @@ -50,6 +34,7 @@ install(PROGRAMS
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION include/${PROJECT_NAME}
)
ament_export_include_directories(include)

install(DIRECTORY launch
DESTINATION share/${PROJECT_NAME}
Expand All @@ -59,6 +44,6 @@ install(DIRECTORY sounds
DESTINATION share/${PROJECT_NAME}
)

ament_export_dependencies(rosidl_default_runtime)
ament_export_dependencies(rosidl_default_runtime sound_play_msgs)

ament_package()
75 changes: 38 additions & 37 deletions sound_play/include/sound_play/sound_play.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@
#define __SOUND_PLAY__SOUND_PLAY__H__

#include <string>
#include <ros/ros.h>
#include <ros/node_handle.h>
#include <sound_play/SoundRequest.h>
#include <boost/thread.hpp>
#include <rclcpp/rclcpp.hpp>
#include <sound_play_msgs/msg/sound_request.hpp>
#include <mutex>

namespace sound_play
{
Expand Down Expand Up @@ -87,7 +86,7 @@ class SoundClient
*/
void play()
{
client_->sendMsg(snd_, SoundRequest::PLAY_ONCE, arg_, arg2_, vol_);
client_->sendMsg(snd_, sound_play_msgs::msg::SoundRequest::PLAY_ONCE, arg_, arg2_, vol_);
}

/** \brief Play the Sound repeatedly.
Expand All @@ -97,7 +96,7 @@ class SoundClient
*/
void repeat()
{
client_->sendMsg(snd_, SoundRequest::PLAY_START, arg_, arg2_, vol_);
client_->sendMsg(snd_, sound_play_msgs::msg::SoundRequest::PLAY_START, arg_, arg2_, vol_);
}

/** \brief Stop Sound playback.
Expand All @@ -106,7 +105,7 @@ class SoundClient
*/
void stop()
{
client_->sendMsg(snd_, SoundRequest::PLAY_STOP, arg_, arg2_, vol_);
client_->sendMsg(snd_, sound_play_msgs::msg::SoundRequest::PLAY_STOP, arg_, arg2_, vol_);
}
};

Expand All @@ -119,18 +118,20 @@ class SoundClient
*
* \param topic Topic to publish to.
*/
SoundClient(ros::NodeHandle &nh, const std::string &topic)
SoundClient(rclcpp::Node::SharedPtr nh, const std::string &topic)
{
init(nh, topic);
}

/** \brief Create a SoundClient with the default topic
*
* Creates a SoundClient that publishes to "robotsound".
*
* \param nh Node handle to use when creating the topic.
*/
SoundClient()
SoundClient(rclcpp::Node::SharedPtr nh)
{
init(ros::NodeHandle(), "robotsound");
init(nh, "robotsound");
}

/** \brief Create a voice Sound.
Expand All @@ -142,7 +143,7 @@ class SoundClient
*/
Sound voiceSound(const std::string &s, float volume = 1.0f)
{
return Sound(this, SoundRequest::SAY, s, "", volume);
return Sound(this, sound_play_msgs::msg::SoundRequest::SAY, s, "", volume);
}

/** \brief Create a wave Sound.
Expand All @@ -155,7 +156,7 @@ class SoundClient
*/
Sound waveSound(const std::string &s, float volume = 1.0f)
{
return Sound(this, SoundRequest::PLAY_FILE, s, "", volume);
return Sound(this, sound_play_msgs::msg::SoundRequest::PLAY_FILE, s, "", volume);
}

/** \brief Create a wave Sound from a package.
Expand All @@ -169,7 +170,7 @@ class SoundClient
*/
Sound waveSoundFromPkg(const std::string &p, const std::string &s, float volume = 1.0f)
{
return Sound(this, SoundRequest::PLAY_FILE, s, p, volume);
return Sound(this, sound_play_msgs::msg::SoundRequest::PLAY_FILE, s, p, volume);
}

/** \brief Create a builtin Sound.
Expand All @@ -194,7 +195,7 @@ class SoundClient
*/
void say(const std::string &s, const std::string &voice="voice_kal_diphone", float volume = 1.0f)
{
sendMsg(SoundRequest::SAY, SoundRequest::PLAY_ONCE, s, voice, volume);
sendMsg(sound_play_msgs::msg::SoundRequest::SAY, sound_play_msgs::msg::SoundRequest::PLAY_ONCE, s, voice, volume);
}

/** \brief Say a string repeatedly
Expand All @@ -206,7 +207,7 @@ class SoundClient
*/
void repeat(const std::string &s, float volume = 1.0f)
{
sendMsg(SoundRequest::SAY, SoundRequest::PLAY_START, s, "", volume);
sendMsg(sound_play_msgs::msg::SoundRequest::SAY, sound_play_msgs::msg::SoundRequest::PLAY_START, s, "", volume);
}

/** \brief Stop saying a string
Expand All @@ -218,7 +219,7 @@ class SoundClient
*/
void stopSaying(const std::string &s)
{
sendMsg(SoundRequest::SAY, SoundRequest::PLAY_STOP, s, "");
sendMsg(sound_play_msgs::msg::SoundRequest::SAY, sound_play_msgs::msg::SoundRequest::PLAY_STOP, s, "");
}

/** \brief Plays a WAV or OGG file
Expand All @@ -232,7 +233,7 @@ class SoundClient
*/
void playWave(const std::string &s, float volume = 1.0f)
{
sendMsg(SoundRequest::PLAY_FILE, SoundRequest::PLAY_ONCE, s, "", volume);
sendMsg(sound_play_msgs::msg::SoundRequest::PLAY_FILE, sound_play_msgs::msg::SoundRequest::PLAY_ONCE, s, "", volume);
}

/** \brief Plays a WAV or OGG file repeatedly
Expand All @@ -245,7 +246,7 @@ class SoundClient
*/
void startWave(const std::string &s, float volume = 1.0f)
{
sendMsg(SoundRequest::PLAY_FILE, SoundRequest::PLAY_START, s, "", volume);
sendMsg(sound_play_msgs::msg::SoundRequest::PLAY_FILE, sound_play_msgs::msg::SoundRequest::PLAY_START, s, "", volume);
}

/** \brief Stop playing a WAV or OGG file
Expand All @@ -257,7 +258,7 @@ class SoundClient
*/
void stopWave(const std::string &s)
{
sendMsg(SoundRequest::PLAY_FILE, SoundRequest::PLAY_STOP, s);
sendMsg(sound_play_msgs::msg::SoundRequest::PLAY_FILE, sound_play_msgs::msg::SoundRequest::PLAY_STOP, s);
}

/** \brief Plays a WAV or OGG file from a package
Expand All @@ -272,7 +273,7 @@ class SoundClient
*/
void playWaveFromPkg(const std::string &p, const std::string &s, float volume = 1.0f)
{
sendMsg(SoundRequest::PLAY_FILE, SoundRequest::PLAY_ONCE, s, p, volume);
sendMsg(sound_play_msgs::msg::SoundRequest::PLAY_FILE, sound_play_msgs::msg::SoundRequest::PLAY_ONCE, s, p, volume);
}

/** \brief Plays a WAV or OGG file repeatedly
Expand All @@ -286,7 +287,7 @@ class SoundClient
*/
void startWaveFromPkg(const std::string &p, const std::string &s, float volume = 1.0f)
{
sendMsg(SoundRequest::PLAY_FILE, SoundRequest::PLAY_START, s, p, volume);
sendMsg(sound_play_msgs::msg::SoundRequest::PLAY_FILE, sound_play_msgs::msg::SoundRequest::PLAY_START, s, p, volume);
}

/** \brief Stop playing a WAV or OGG file
Expand All @@ -300,7 +301,7 @@ class SoundClient
*/
void stopWaveFromPkg(const std::string &p, const std::string &s)
{
sendMsg(SoundRequest::PLAY_FILE, SoundRequest::PLAY_STOP, s, p);
sendMsg(sound_play_msgs::msg::SoundRequest::PLAY_FILE, sound_play_msgs::msg::SoundRequest::PLAY_STOP, s, p);
}

/** \brief Play a buildin sound
Expand All @@ -313,7 +314,7 @@ class SoundClient
*/
void play(int sound, float volume = 1.0f)
{
sendMsg(sound, SoundRequest::PLAY_ONCE, "", "", volume);
sendMsg(sound, sound_play_msgs::msg::SoundRequest::PLAY_ONCE, "", "", volume);
}

/** \brief Play a buildin sound repeatedly
Expand All @@ -326,7 +327,7 @@ class SoundClient
*/
void start(int sound, float volume = 1.0f)
{
sendMsg(sound, SoundRequest::PLAY_START, "", "", volume);
sendMsg(sound, sound_play_msgs::msg::SoundRequest::PLAY_START, "", "", volume);
}

/** \brief Stop playing a built-in sound
Expand All @@ -337,7 +338,7 @@ class SoundClient
*/
void stop(int sound)
{
sendMsg(sound, SoundRequest::PLAY_STOP);
sendMsg(sound, sound_play_msgs::msg::SoundRequest::PLAY_STOP);
}

/** \brief Stop all currently playing sounds
Expand All @@ -346,7 +347,7 @@ class SoundClient
*/
void stopAll()
{
stop(SoundRequest::ALL);
stop(sound_play_msgs::msg::SoundRequest::ALL);
}

/** \brief Turns warning messages on or off.
Expand All @@ -363,21 +364,21 @@ class SoundClient
}

private:
void init(ros::NodeHandle nh, const std::string &topic)
void init(rclcpp::Node::SharedPtr nh, const std::string &topic)
{
nh_ = nh;
pub_ = nh.advertise<sound_play::SoundRequest>(topic, 5);
pub_ = nh->create_publisher<sound_play_msgs::msg::SoundRequest>(topic, 5);
quiet_ = false;
}

void sendMsg(int snd, int cmd, const std::string &s = "", const std::string &arg2 = "", const float &vol = 1.0f)
{
boost::mutex::scoped_lock lock(mutex_);
const std::lock_guard<std::mutex> lock(mutex_);

if (!nh_.ok())
if (!nh_ || !pub_)
return;

SoundRequest msg;
sound_play_msgs::msg::SoundRequest msg;
msg.sound = snd;
msg.command = cmd;
msg.arg = s;
Expand All @@ -391,16 +392,16 @@ class SoundClient
else
msg.volume = vol;

pub_.publish(msg);
pub_->publish(msg);

if (pub_.getNumSubscribers() == 0 && !quiet_)
ROS_WARN("Sound command issued, but no node is subscribed to the topic. Perhaps you forgot to run soundplay_node.py");
if (pub_->get_subscription_count() == 0 && !quiet_)
RCLCPP_WARN(nh_->get_logger(), "Sound command issued, but no node is subscribed to the topic. Perhaps you forgot to run soundplay_node.py");
}

bool quiet_;
ros::NodeHandle nh_;
ros::Publisher pub_;
boost::mutex mutex_;
rclcpp::Node::SharedPtr nh_;
rclcpp::Publisher<sound_play_msgs::msg::SoundRequest>::SharedPtr pub_;
std::mutex mutex_;
};

typedef SoundClient::Sound Sound;
Expand Down
7 changes: 2 additions & 5 deletions sound_play/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>ament_cmake_python</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
<buildtool_depend>rosidl_generator_py</buildtool_depend>
<buildtool_depend>python3-setuptools</buildtool_depend>

<depend>sound_play_msgs</depend>

<build_depend>action_msgs</build_depend>
<build_depend>audio_common_msgs</build_depend>
<build_depend>boost</build_depend>
Expand All @@ -33,7 +33,6 @@
<exec_depend>festival</exec_depend>
<exec_depend>launch_xml</exec_depend>
<exec_depend>rclpy</exec_depend>
<exec_depend>rosidl_default_runtime</exec_depend>

<exec_depend>gstreamer1.0</exec_depend>
<exec_depend>gstreamer1.0-alsa</exec_depend>
Expand All @@ -42,8 +41,6 @@
<exec_depend>gstreamer1.0-plugins-good</exec_depend>
<exec_depend>python3-gi</exec_depend>

<member_of_group>rosidl_interface_packages</member_of_group>

<export>
<build_type>ament_cmake</build_type>
</export>
Expand Down
2 changes: 1 addition & 1 deletion sound_play/scripts/soundclient_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from sound_play.libsoundplay import SoundClient

from sound_play.msg import SoundRequest
from sound_play_msgs.msg import SoundRequest


def play_explicit(node):
Expand Down
4 changes: 2 additions & 2 deletions sound_play/scripts/soundplay_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
from diagnostic_msgs.msg import DiagnosticArray
from diagnostic_msgs.msg import DiagnosticStatus
from diagnostic_msgs.msg import KeyValue
from sound_play.action import SoundRequest as SoundRequestAction
from sound_play.msg import SoundRequest
from sound_play_msgs.action import SoundRequest as SoundRequestAction
from sound_play_msgs.msg import SoundRequest


try:
Expand Down
Loading