ROS无人机机械爪使用

devtools/2024/10/22 14:00:34/

引言:使用飞控的主通道5-8作为舵机控制输出,需要提前设置好飞控参数,否则无效。本节资料文件尚未整理完毕,整理完毕后会在B站进行视频讲解,并进行开源

1、启动mavros通信,用于订阅遥控器的按键信息,添加机械爪是否使用的安全开关

roslaunch robot_bringup px4.launch

2、启动D435深度摄像头,启动深度与彩图对齐文件

注:先查看深度摄像头是否工作正常,采用USB3.0线

roslaunch realsense2_camera rs_aligned_depth.launch 

3、启动yolov8物体识别节点

roslaunch yolov8_ros yolo_v8_d435.launch

4、启动坐标转换节点

roslaunch object_position object_position.launch 

5、启动机械爪控制节点

rosrun arm_grasp arm_grasp

机械爪控制代码如下:

//说明:舵机方向根据实际调整,这里用的占空比0%表示闭合,100%表示打开
#include <api_library.h>
#include <lib_library.h>
#include <arm_grasp/ctrl.h>
#include <mavros_msgs/RCIn.h>int arm_first  = 0;
int arm_second = 0;
int arm_third  = 0;	
int arm_fourth = 0;
bool target_locked_flag = false;
int  target_locked_area = 100;
arm_grasp::ctrl ctrl_msg;
//无人机遥控器订阅
mavros_msgs::RCIn global_rc_in;
void rc_in_cb(const mavros_msgs::RCIn::ConstPtr& msg)
{global_rc_in = *msg;    	if(global_rc_in.channels[6]>=1250&&global_rc_in.channels[6]<=2050){ctrl_msg.data = true;}else{ctrl_msg.data = false;}    
}/**************************************************
//此处添加计数器防止误识别判断
//补充说明,此处采用摄像头的左上前坐标系,解释如下:
//X为负,表示物体在摄像头的左方,反之右方,正中间为0;
//Y为负,表示物体在摄像头的前方,反之后 方,正中间为0;
//Z表示距离,始终为正
***************************************************/
int last_count    = 0;
int current_count = 0;
int last_diff     = 0;
int current_diff  = 0;
int sum = 0;
int visual_position(string str)
{if(object_pos.header.frame_id == str){position_detec_x = object_pos.point.x;position_detec_y = object_pos.point.y;position_detec_z = object_pos.point.z;}if((fabs(position_detec_x)<=0.03)&&position_detec_y<=-0.03){current_count= current_count + 1;//	cout<<"目标在正前方"<<endl;}else if((fabs(position_detec_x)<=0.03)&&position_detec_y>=0.03){current_count= current_count + 2;//	cout<<"目标在正后方"<<endl;}else if((fabs(position_detec_y)<=0.03)&&position_detec_x<=-0.03){current_count= current_count + 3;//	cout<<"目标在正左方"<<endl;}else if((fabs(position_detec_y)<=0.03)&&position_detec_x>=0.03){	current_count= current_count + 4;//	cout<<"目标在正右方"<<endl;}else if((position_detec_x<-0.03)&&position_detec_y<-0.03){current_count= current_count + 5;//	cout<<"目标在左前方"<<endl;}else if((position_detec_x<-0.03)&&position_detec_y>0.03){current_count= current_count + 6;//	cout<<"目标在左后方"<<endl;}else if((position_detec_x>0.03)&&position_detec_y<-0.03){current_count= current_count + 7;//	cout<<"目标在右前方"<<endl;}else if((position_detec_x>0.03)&&position_detec_y>0.03){current_count= current_count + 8;	//	cout<<"目标在右后方"<<endl;}else if((fabs(position_detec_x)<=0.03)&&fabs(position_detec_y)<=0.03&&position_detec_x!=0&&position_detec_y!=0){current_count= current_count + 9;//	cout<<"目标在正中间"<<endl;}else{}//cout<<current_count<<endl;//cout<<last_count<<endl;if(current_count ==last_count&&last_count!=0){sum++;if(sum>=100){cout<<"目标锁定"<<endl;target_locked_area = current_count;cout<<target_locked_area<<endl;target_locked_flag = true;}}else{sum = 0;}last_count=current_count;current_count = 0;
}int main(int argc, char** argv)
{setlocale(LC_ALL, "");ros::init(argc, argv, "complete_mission");ros::NodeHandle nh; ros::Subscriber object_pos_sub = nh.subscribe<geometry_msgs::PointStamped>("object_position", 100, object_pos_cb); ros::Subscriber rc_in_sub = nh.subscribe<mavros_msgs::RCIn>("/mavros/rc/in", 100, rc_in_cb); ros::ServiceClient ctrl_pwm_client = nh.serviceClient<mavros_msgs::CommandLong>("mavros/cmd/command");ros::Time last_request = ros::Time::now();lib_pwm_control(50,50,50,50);ctrl_pwm_client.call(lib_ctrl_pwm);ctrl_msg.data = false;ros::Rate rate(20);  while(ros::ok()){visual_position("cylinder");//cout<<ctrl_msg.data<<endl;if(ctrl_msg.data){cout<<target_locked_area<<endl;//正前方if(target_locked_area==1||target_locked_area==5||target_locked_area==7){arm_first   = 100;arm_second	= 0;}else if(target_locked_area==2||target_locked_area==6||target_locked_area==8){arm_first   = 0;arm_second	= 100;}//中间else{arm_first   = 50; arm_second	= 50;  		}		lib_pwm_control(arm_first,arm_second,arm_third,arm_fourth);ctrl_pwm_client.call(lib_ctrl_pwm);if(lib_time_record_func(5.0,ros::Time::now())){arm_fourth = 100;ROS_INFO("执行抓取");	  	}}else{lib_pwm_control(100,50,50,50);ctrl_pwm_client.call(lib_ctrl_pwm);ROS_INFO("机械爪还原");}ros::spinOnce();rate.sleep();}return 0;
}

http://www.ppmy.cn/devtools/122129.html

相关文章

FireRedTTS - 小红书最新开源AI语音克隆合成系统 免训练一键音频克隆 本地一键整合包下载

小红书技术团队FireRed最近推出了一款名为FireRedTTS的先进语音合成系统&#xff0c;该系统能够基于少量参考音频快速模仿任意音色和说话风格&#xff0c;实现独特的音频内容创造。 FireRedTTS 只需要给定文本和几秒钟参考音频&#xff0c;无需训练&#xff0c;就可模仿任意音色…

基于springboot vue 电影推荐系统

博主介绍&#xff1a;专注于Java&#xff08;springboot ssm 等开发框架&#xff09; vue .net php python(flask Django) 小程序 等诸多技术领域和毕业项目实战、企业信息化系统建设&#xff0c;从业十五余年开发设计教学工作☆☆☆ 精彩专栏推荐订阅☆☆☆☆☆不然下次找…

【C#生态园】探索地理信息系统软件套件与库:功能、API和应用

探索地理信息系统&#xff1a;软件套件与库详解 前言 地理信息系统&#xff08;GIS&#xff09;是当今世界上广泛使用的技术之一&#xff0c;它以空间数据为基础&#xff0c;能够提供丰富的地理信息分析和可视化功能。在GIS领域&#xff0c;有许多优秀的软件套件和库&#xf…

Florr.io 花瓣图鉴之Dark Mark[黑暗标记]

描述 一个将自己束缚在堕落灵魂上的黑暗印记。 &#xff08;A dark mark that binds itself to a fallen soul.&#xff09; 你知道吗 超级 1.0s 4.0s 究极 1.0s 10.0s 神话 1.0s 30.0s 传奇 1.0s 60.0s 史诗 1.0s 120.0s 稀…

torchvision.transforms.Resize()的用法

今天我在使用torchvision.transforms.Resize()的时候发现&#xff0c;一般Resize中放的是size或者是(size,size)这样的二元数。 这两个里面&#xff0c;torchvision.transforms.Resize((size,size))&#xff0c;大家都很清楚&#xff0c;会将图像的h和w大小都变成size。 但是…

『Linux』 第三章 Linux环境基础开发工具使用

Linux 软件包管理器yum 什么是软件包 在Linux下安装软件&#xff0c;一个通常的办法是下载到程序的源代码&#xff0c;进行编译&#xff0c;得到可执行程序但是这样太麻烦了&#xff0c;于是有些人把一些常用的软件提前编译好&#xff0c;做成软件包&#xff08;可以理解成wi…

flutter_鸿蒙next(win)环境搭建

第一步 拉取鸿蒙版本flutterSDK仓库 仓库地址&#xff1a;OpenHarmony-SIG/flutter_flutter 第二步 找到拉取的仓库中的README.md 并根据说明配置环境 第三步 配置好环境变量之后 用管理员开启cmd 输入&#xff1a;flutter dcotor 并查看此时flutter所支持的系统 包括&…

微信小程序处理交易投诉管理,支持多小程序

大家好&#xff0c;我是小悟 1、问题背景 玩过微信小程序生态的&#xff0c;或许就有这种感受&#xff0c;如果收到投诉单&#xff0c;不会及时通知到手机端&#xff0c;而是每天早上10:00向小程序的管理员及运营者推送通知。通知内容为截至前一天24时该小程序账号内待处理的交…