ros2语音交互,实现机器人【口耳】,使用科大讯飞sdk
ros1参考:https://blog.csdn.net/qq_39400324/article/details/125351722
- ros1转ros2问题
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
int main(int argc, char* argv[])
{
// 初始化ROS
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>("voiceRecognition");
rclcpp::WallRate loop_rate(10.0);
// 声明Publisher和Subscriber
// 订阅唤醒语音识别的信号
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr wakeUpSub = node->create_subscription<std_msgs::msg::String>("voiceWakeup", 1000, WakeUp);
// 订阅唤醒语音识别的信号
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr voiceWordsPub = node->create_publisher<std_msgs::msg::String>("voiceWords", 1000);
RCLCPP_INFO(node->get_logger(), "Sleeping...");
int count=0;
// kd code down
int ret = MSP_SUCCESS;
int upload_on = 1; /* whether upload the user word */
/* login params, please do keep the appid correct */
const char* login_params = "appid = 52b7f17b, work_dir = .";
int aud_src = 0; /* from mic or file */
/*
* See "iFlytek MSC Reference Manual"
*/
const char* session_begin_params =
"sub = iat, domain = iat, language = zh_cn, "
"accent = mandarin, sample_rate = 16000, "
"result_type = plain, result_encoding = utf8";
/* Login first. the 1st arg is username, the 2nd arg is password
* just set them as NULL. the 3rd arg is login paramertes
* */
ret = MSPLogin(NULL, NULL, login_params);
if (MSP_SUCCESS != ret) {
printf("MSPLogin failed , Error code %d.\n",ret);
goto exit; // login fail, exit the program
}
while(rclcpp::ok())
{
// 语音识别唤醒
if(wakeupFlag)
{
printf("Demo recognizing the speech from microphone\n");
printf("Speak in 8 seconds\n");
demo_mic(session_begin_params);
printf("8 sec passed\n");
wakeupFlag=0;
}
// 语音识别完成
if(resultFlag){
resultFlag=0;
std_msgs::msg::String msg;
msg.data = g_result;
voiceWordsPub->publish(msg);
}
rclcpp::spin_some(node);
loop_rate.sleep();
count++;
}
exit:
MSPLogout(); // Logout...
return 0;
}
- cmakelist.txt 编译依赖问题
cmake_minimum_required(VERSION 3.8)
project(voice)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
include_directories(include)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
set(dependencies
rclcpp
std_msgs
)
add_executable(iat_publish src/iat_publish.cpp src/speech_recognizer.c src/linuxrec.c)
ament_target_dependencies(iat_publish ${dependencies})
add_executable(tts_subscribe src/tts_subscribe.cpp)
ament_target_dependencies(tts_subscribe ${dependencies})
target_link_libraries(iat_publish
${PROJECT_SOURCE_DIR}
libmsc.so -ldl -lpthread -lm -lrt -lasound
)
target_link_libraries(tts_subscribe
${PROJECT_SOURCE_DIR}
libmsc.so -ldl -pthread
)
install(TARGETS
iat_publish
DESTINATION lib/${PROJECT_NAME}
)
install(TARGETS
tts_subscribe
DESTINATION lib/${PROJECT_NAME}
)
- sh: 1: play: not found问题
sudo apt-get install sox
sudo apt-get install sox libsox-fmt-all
play tts_sample.wav