Rviz下可视化插件开发教程1-JSK界面遥控与按键模式切换

本教程结合ROS jsk_rviz_plugins插件完成在Rviz下操控、导航、标注功能的开发,从而实现完全脱离操控端基于Rviz完成机器人基本操控与任务指派

1 按键界面修改发布自定义话题

首先完成安装:
sudo apt-get install ros-melodic-jsk_rviz_plugins
官方教程:
完成下载:
打开/home/bingda/catkin_ws/src/jsk_visualization/jsk_rviz_plugins/src/tablet_controller_panel.cpp在对应代码处增加按键指令:
void TabletControllerPanel::taskButtonClicked(){ task_dialog_ = new QDialog(); task_dialog_->setBackgroundRole(QPalette::Base); task_dialog_->setAutoFillBackground(true); task_dialog_layout_ = new QVBoxLayout(); task_radio_buttons_.clear(); std::vector<std::string> tasks; tasks.push_back("en_sdk"); tasks.push_back("dis_sdk"); tasks.push_back("search_sdk"); tasks.push_back("home_sdk"); tasks.push_back("stop_sdk"); tasks.push_back("guard_sdk"); tasks.push_back("set_home_sdk"); tasks.push_back("safe_sdk");
同时在头文件中增加发布的话题:
在按键选择回调函数中将获取的按键名称作为话题进行发布:
void TabletControllerPanel::taskExecuteClicked() { for (size_t i = 0; i < task_radio_buttons_.size(); i++) { QRadioButton* radio = task_radio_buttons_[i]; if (radio->isChecked()) { ros::NodeHandle nh; std::string task = radio->text().toStdString(); ROS_INFO("task: %s", task.c_str()); task_dialog_->reject(); jsk_rviz_plugins::StringStamped command; command.data = task; command.header.stamp = ros::Time::now(); pub_start_demo_.publish(command); std_msgs::Empty temp; pub_sdk[0] = nh.advertise<std_msgs::Empty>(task.c_str(),1); pub_sdk[0].publish(temp);
修改完成后完成catkin_make,刷新插件,打开Rviz在Panels中插入TabletControllerPanel:
Command中选择指令执行则发布话题,该功能可以实现基本的按键和陌生切换

2 虚拟摇杆操控

TabletControllerPanel插件中自带了一个虚拟摇杆,其发布的话题可以在tablet_controller_panel.cpp修改safe_vel自己定义,则在拖动摇杆后会发布对应数据,但是停住不动并不会继续发布!
因此要使用该速度可以订阅:
ros::Subscriber sub_cmd4 = n.subscribe("/navigation/unsafe_vel", 10,callback1_rc);
再回调函数中读取,由于刚才提到该摇杆仅响应拖动事件,持续维持在某行程并不会连续发生,因此可以对摇杆值进行缓存:
int rc_control_flag=0; float rc_control_loss=0; float rc_input_reg[3]={0}; void callback1_rc(const geometry_msgs::Twist& cmd_vel)//2 virual RC input { cmd_aux_flag[2]=1; cmd_in_ot[2]=0; ROS_INFO("ROS-RC Connect In"); ROS_INFO("Linear Components:[%f,%f,%f]",cmd_vel.linear.x,cmd_vel.linear.y,-cmd_vel.linear.z); ROS_INFO("Angular Components:[%f,%f,%f]",cmd_vel.angular.x,cmd_vel.angular.y,-cmd_vel.angular.z); rc_input_reg[0]=cmd_vel.linear.x; rc_input_reg[1]=-cmd_vel.linear.y/2; if(fabs(rc_input_reg[0])>0.01||fabs(rc_input_reg[1])>0.01) rc_control_flag=1; else if(rc_input_reg[0]==0&&rc_input_reg[1]==0) rc_control_flag=0; rc_control_loss=0; set_spd_x(cmd_vel.linear.x); set_spd_y(-cmd_vel.linear.y/2); set_spd_yaw_rate(cmd_vel.angular.z*0); if(!ros_connect){ moco_sdk.mode_ros=ros_connect=1; ROS_INFO("ROS RC cmd/vel Connect In!"); } ros_loss_cnt=0; }
通过标志位判断,在主函数中连续发送直达摇杆归零复位:
if(rc_control_flag==1){ rc_control_loss+=0.002; set_spd_x(rc_input_reg[0]); set_spd_y(rc_input_reg[1]); set_spd_yaw_rate(0); moco_sdk.mode_ros=ros_connect=1; ros_loss_cnt=0; if(rc_control_loss>2&&0){ ROS_INFO("ROS-RC Stop"); rc_control_flag=rc_control_loss=0; } }
2023-03-16