voice_teleop.cpp
Go to the documentation of this file.
00001 /*
00002  *  Gaitech Educational Portal
00003  *
00004  * Copyright (c) 2016
00005  * All rights reserved.
00006  *
00007  * License Type: GNU GPL
00008  *
00009  *   This program is free software: you can redistribute it and/or modify
00010  *   it under the terms of the GNU General Public License as published by
00011  *   the Free Software Foundation, either version 3 of the License, or
00012  *   (at your option) any later version.
00013  *   This program is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  *   You should have received a copy of the GNU General Public License
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 #include <ros/ros.h>
00022 #include <geometry_msgs/Twist.h>
00023 #include <std_msgs/String.h>
00024 #include "boost/thread/mutex.hpp"
00025 #include "boost/thread/thread.hpp"
00026 #include "ros/console.h"
00027 #include <string>
00028 
00029 std_msgs::String readCommand;
00030 
00031 class RobotVoiceTeleop
00032 {
00033 public:
00034         RobotVoiceTeleop();
00035 
00036 private:
00037         void commandCallBack(const std_msgs::String& command);
00038         void publish();
00039 
00040         ros::NodeHandle nodeHandle;
00041 
00042 
00043         int linearVelocity, angularVelocity;
00044         ros::Publisher velocityPublisher;
00045         ros::Subscriber voiceCommandSubscriber;
00046         geometry_msgs::Twist cmd_vel;
00047 
00048 
00049 };
00051 RobotVoiceTeleop::RobotVoiceTeleop():
00052                                                   nodeHandle("~"),
00053                                                   linearVelocity(1),
00054                                                   angularVelocity(0)
00055 {
00056         nodeHandle.param("linear_velocity", linearVelocity, linearVelocity);
00057         nodeHandle.param("angular_velocity", angularVelocity, angularVelocity);
00058 
00059         velocityPublisher = nodeHandle.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
00060         voiceCommandSubscriber = nodeHandle.subscribe("/recognizer/output", 1000, &RobotVoiceTeleop::commandCallBack, this);
00061 
00062         printf("start control\n");
00063         ros::Rate rate(5);
00064         while (ros::ok()){
00065                 velocityPublisher.publish(cmd_vel);
00066                 rate.sleep();
00067                 ros::spinOnce();
00068         }
00069 
00070 }
00072 void RobotVoiceTeleop::commandCallBack(const std_msgs::String& command)
00073 { 
00074         if(command.data.compare("stop") == 0)
00075         {
00076                 cmd_vel.angular.z = 0;
00077                 cmd_vel.linear.x = 0;
00078         }
00079         else if(command.data.compare("forward") == 0)
00080         {
00081                 cmd_vel.linear.x = 0.3;
00082                 cmd_vel.angular.z = 0;
00083         }
00084         else if(command.data.compare("backward") == 0)
00085         {
00086                 cmd_vel.linear.x = -0.3;
00087                 cmd_vel.angular.z = 0;
00088         }
00089         else if(command.data.compare("left") == 0)
00090         {
00091                 cmd_vel.linear.x = 0.0;
00092                 cmd_vel.angular.z = 0.5;
00093         }
00094         else if(command.data.compare("right") == 0)
00095         {
00096                 cmd_vel.linear.x = 0.0;
00097                 cmd_vel.angular.z = -0.5;
00098         }
00099         else {
00100                 cmd_vel.angular.z = 0;
00101                 cmd_vel.linear.x = 0;
00102         }
00103 
00104         printf("cmd_vel.linear.x = %.2f, cmd_vel.angular.z = %.2f\n", cmd_vel.linear.x, cmd_vel.angular.z);
00105 
00106 }
00107 
00108 
00109 int main(int argc, char** argv)
00110 {
00111         ros::init(argc, argv, "voice_teleop_node");
00112         RobotVoiceTeleop voice_teleop;
00113         ros::spin();
00114 }


gapter
Author(s):
autogenerated on Thu Jun 6 2019 22:05:13