当我运行此代码并推动turtlebot的保险杠时,它会显示以下消息:
[WARN] [1430824597.483791184]:发出声音命令,但没有节点订阅该主题。也许你忘了运行soundplay_node.py。
尽管我很确定我已经订阅了声音播放节点
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "kobuki_msgs/BumperEvent.h"
#include "geometry_msgs/Twist.h"
#include "kobuki_msgs/WheelDropEvent.h"
#include "sstream"
#include <sound_play/sound_play.h>
using namespace std;
//sound_play::SoundClient sc ;
//sound_play::SoundClient soundClient;
void soundPlaynode(int n);
void bumperCallback(const kobuki_msgs::BumperEvent &bmsg) {
int state=bmsg.state;
int side=bmsg.bumper;
if (side==0 && state==1) {
cout << "I am rotating to Right" << endl;
//soundClient.say("I am rotating to Right");
//soundPlaynode(0);
geometry_msgs::Twist vel_msg;
double secs =ros::Time::now().toSec();
while(secs < 2.5) {
vel_msg.angular.x = 0;
vel_msg.angular.y = 0;
vel_msg.angular.z =-0.6;
}
while(secs < 5) {
vel_msg.linear.x =0.4;
vel_msg.linear.y =0;
vel_msg.linear.z =0;
}
}
else if (side==1 && state==1) {
cout << "Front side is pressed" << endl;
soundPlaynode(1);
geometry_msgs::Twist vel_msg;
double secs =ros::Time::now().toSec();
while(secs < 3.14) {
vel_msg.angular.x = 0;
vel_msg.angular.y = 0;
vel_msg.angular.z =(double)1;
}
secs = 0 ;
while(secs < 20) {
vel_msg.linear.x =1.0;
vel_msg.linear.y =0;
vel_msg.linear.z =0;
}
}
else if (side==2 && state==1) {
cout << "I am rotating to Left" << endl;
//soundClient.say("I am rotating to Left");
soundPlaynode(2);
geometry_msgs::Twist vel_msg;
double secs =ros::Time::now().toSec();
while(secs < 2.5) {
vel_msg.angular.x = 0;
vel_msg.angular.y = 0;
vel_msg.angular.z =0.6;
}
while(secs < 5) {
vel_msg.linear.x =0.4;
vel_msg.linear.y =0;
vel_msg.linear.z =0;
}
}
}
int counter = 0 ;
int main(int argc, char **argv) {
ros::init(argc, argv, "collision_reaction_node");
ros::NodeHandle node;
sound_play::SoundClient sc;
sc.say("Hello world!");
// soundPlaynode(1);
ros::Subscriber bumperSubscriber = node.subscribe("/mobile_base/events/bumper", 100, bumperCallback);
ros::Publisher velocity_publisher = node.advertise<geometry_msgs::Twist> ("/turtle1/cmd_vel", 1000);
ros::Rate loop_rate(1);
// ros::Publisher voicePlayerPublisher = node.advertise<std_msgs::String>("/voice_player/message_to_play", 10);
// ros::Publisher voicePlayerPublisher = node.advertise<std_msgs::String>("/robotsound", 10);
ros::spin();
return 0;
}
void soundPlaynode(int n) {
sound_play::SoundClient soundClient;
if(n == 0)
soundClient.say("I am rotating to the right");
else if(n == 1) {
soundClient.say("I am rotating 180 degrees");
}
else if(n == 2)
soundClient.say("I am rotating to the left");
}
}