26 #include <std_msgs/Int32.h> 27 #include <std_msgs/String.h> 28 #include <boost/thread.hpp> 29 #include <yaml-cpp/yaml.h> 30 #include "robotis_controller_msgs/StatusMsg.h" 31 #include "thormang3_action_module_msgs/IsRunning.h" 32 #include "thormang3_action_module_msgs/StartAction.h" 34 #define JOINT_NAME_KEY "joint_name" 35 #define ACTION_PLAY_CMD_NAME "play" 36 #define MP3_PLAY_CMD_NAME "mp3" 37 #define WAIT_ACTION_PLAY_FINISH_CMD_NAME "wait" 38 #define SLEEP_CMD_NAME "sleep" 64 std::ostringstream ostr;
71 return atoi(str.c_str());
96 YAML::Node action_script_file_doc;
101 }
catch (
const std::exception& e)
103 ROS_ERROR(
"Failed to load action script file.");
109 YAML::Node action_script_doc = action_script_file_doc[script_index_key];
110 if (action_script_doc == NULL)
112 std::string status_msg =
"Failed to find action script #" +
convertIntToString(action_script_index);
118 std::string cmd_key =
"";
123 if (joint_name_doc != NULL)
130 YAML::Node action_script_cmd_doc = action_script_doc[cmd_key];
131 if (action_script_cmd_doc == NULL)
138 if (action_script_cmd_doc[
"cmd_name"] == NULL)
146 temp_cmd.
cmd_name = action_script_cmd_doc[
"cmd_name"].as<std::string>();
147 if ((temp_cmd.
cmd_name !=
"wait") && (action_script_cmd_doc[
"cmd_arg"] == NULL))
157 temp_cmd.
cmd_arg_int = action_script_cmd_doc[
"cmd_arg"].as<
int>();
161 temp_cmd.
cmd_arg_str = action_script_cmd_doc[
"cmd_arg"].as<std::string>();
170 temp_cmd.
cmd_arg_int = action_script_cmd_doc[
"cmd_arg"].as<
int>();
190 }
catch (
const std::exception& e)
205 if (action_script_index < 0)
207 std::string status_msg =
"Invalid Action Script Index";
214 std::string status_msg =
"Previous action playing is not finished.";
222 std_msgs::Int32 action_page_num_msg;
223 std_msgs::String sound_file_name_msg;
224 thormang3_action_module_msgs::StartAction start_action_msg;
227 for (
unsigned int joint_name_idx = 0; joint_name_idx <
g_joint_name_list.size(); joint_name_idx++)
231 for(
unsigned int action_script_data_idx = 0; action_script_data_idx <
g_action_script_data.size(); action_script_data_idx++)
235 boost::this_thread::interruption_point();
241 g_start_action_pub.
publish(start_action_msg);
246 g_action_page_num_pub.
publish(action_page_num_msg);
252 g_sound_file_name_pub.
publish(sound_file_name_msg);
261 boost::this_thread::sleep(boost::posix_time::milliseconds(32));
266 boost::this_thread::sleep(boost::posix_time::milliseconds(
g_action_script_data[action_script_data_idx].cmd_arg_int));
270 boost::this_thread::interruption_point();
275 }
catch (boost::thread_interrupted&)
277 ROS_INFO(
"Action Script Thread is Interrupted");
284 if ((msg->data == -1) || (msg->data == -2))
286 std_msgs::Int32 action_page_num_msg;
287 action_page_num_msg.data = msg->data;
288 g_action_page_num_pub.
publish(action_page_num_msg);
325 std::string status_msg =
"Previous action script is not finished.";
331 int main(
int argc,
char **argv)
333 ros::init(argc, argv,
"thormang3_action_script_player");
339 g_action_page_num_pub = ros_node_handle.
advertise<std_msgs::Int32>(
"/robotis/action/page_num", 0);
340 g_start_action_pub = ros_node_handle.
advertise<thormang3_action_module_msgs::StartAction>(
"/robotis/action/start_action", 0);
341 g_sound_file_name_pub = ros_node_handle.
advertise<std_msgs::String>(
"/play_sound_file", 0);
342 g_is_running_client = ros_node_handle.
serviceClient<thormang3_action_module_msgs::IsRunning>(
"/robotis/action/is_running");
345 std::string temp_action_script_file_path =
ros::package::getPath(
"thormang3_action_script_player") +
"/list/action_script.yaml";
349 ROS_WARN(
"Failed to get action script_file_path.");
350 ROS_WARN(
"The default action script file path will be used.");
353 ROS_INFO(
"Start ThorMang3 Action Script Player");
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
#define WAIT_ACTION_PLAY_FINISH_CMD_NAME
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::ServiceClient g_is_running_client
ros::Subscriber g_action_script_num_sub
void actionScriptPlayThreadFunc(int action_script_index)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
std::string convertIntToString(int n)
int main(int argc, char **argv)
int convertStringToInt(std::string str)
std::vector< action_script_cmd > g_action_script_data
ros::Publisher g_start_action_pub
boost::thread * g_action_script_play_thread
void actionScriptNumberCallback(const std_msgs::Int32::ConstPtr &msg)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool isActionRunning(void)
ros::Publisher g_action_page_num_pub
#define MP3_PLAY_CMD_NAME
ROSLIB_DECL std::string getPath(const std::string &package_name)
std::vector< std::string > g_joint_name_list
bool getParam(const std::string &key, std::string &s) const
bool parseActionScript(int action_script_index)
ros::Publisher g_sound_file_name_pub
#define ROS_ERROR_STREAM(args)
std::string g_action_script_file_path
thormang3_action_module_msgs::IsRunning g_is_running_srv
#define ACTION_PLAY_CMD_NAME