-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_speech_client.cpp
More file actions
75 lines (57 loc) · 1.89 KB
/
web_speech_client.cpp
File metadata and controls
75 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <web_speech_recognition/SpeechRecognition.h>
#include <geometry_msgs/Twist.h>
#include <geometry_msgs/Odometry.h>
ros::Publisher cmd_vel_pub;
nav_msgs::Odometry last_odom;
void odomCallback(const nav_msgs::Odometry::ConstPtr& msg){
last_odom = *msg;
}
void moveRobot(double distance){
ros::Rate loop_rate(10);
geometry_msgs::Twist twist;
double initial_x = last_odom.pose.pose.position.x;
double current_x = initial_x;
while((current_x - initial_x) < distance){
twist.linear.x = 0.5;
cmd_vel_pub.publish(twist);
ros::spinOnce();
loop_rate.sleep();
current_x = last_odom.pose.pose.position.x;
}
twist.linear.x = 0.0;
cmd_vel_pub.publish(twist);
}
void processRecognitionResul(const std::vector<std::string>&words){
//check command
for(const std::string& word : words){
//Check if the word is "go"
if (word == "go"){
moveRobot(1.0);
}
}
}
int main(int argc, char **argv){
//Initialize ROS node
ros ::init(argc, argv, "web_speech_recognition_client");
ros::NodeHandle nh;
//create a client for the SpeechRecognition Service
ros::ServiceClient client = nh.serviceClient<web_speech_recognition::SpeechRecognition>("/speech_recognition");
//Prepare request
web_speech_recognition::SpeechRecognition srv;
srv.request.timeout_sec = 10;
//Subscriber for odometry
ros::Subscriber odom_sub = nh.subscribe("/odom",10, odomCallback);
//Publisher for robot movement
cmd_vel_pub = nh.advertise<geometry_msgs::Twist>("/cmd_vel", 10)
//Call the service
if (client.call(srv)){
ROS_INFO("Recognition Result:");
processRecognitionResult(srv.response.transcript);
} else{
ROS_ERROR("Failed to call web_speech_recognition");
return 1;
}
return 0;
}