做机械臂导航时遇到的问题6:实现ROS串口通信的其他方式

来源:互联网 发布:淘宝卖家寄屎 编辑:程序博客网 时间:2024/05/22 05:27

实验室同学做过串口通信方面的任务,留下了一些程序文件,本文参考实验室同学的文件,实现了ROS串口通信。

代码文件上传在百度云:点击链接:https://pan.baidu.com/s/1hsIm1HM 密码:xlpj

共四个文件


本文是在“做机械臂导航时遇到的问题4:如何订阅joint_states话题(输出关节状态)”基础上实现的。


一、复制、修改文件

1、将cJSON.h、uart.h文件拷贝到beginner_tutorials包内的include文件夹内

2、将cJSON.c、uart.c文件拷贝到beginner_tutorials包内的src文件夹内

3、修改beginner_tutorials目录下的CMakeList.txt内容,修改后如下:(倒数第二段的talker部分是当初测试用的,可以不加)

cmake_minimum_required(VERSION 2.8.3)project(beginner_tutorials)## Find catkin and any catkin packagesfind_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)## Declare ROS messages and services#add_message_files(FILES Num.msg)#add_service_files(FILES AddTwoInts.srv)## Generate added messages and services#generate_messages(DEPENDENCIES std_msgs)## Declare a catkin packagecatkin_package()## Build talker and listenerinclude_directories(include)AUX_SOURCE_DIRECTORY(src DIR_SRCS)#set environment variableSET(TEST_MATH${DIR_SRCS})add_executable(talker src/talker.cpp)target_link_libraries(talker ${catkin_LIBRARIES})add_dependencies(talker beginner_tutorials_generate_messages_cpp)add_executable(listener src/listener.cpp src/cJSON.c src/uart.c)target_link_libraries(listener ${catkin_LIBRARIES})add_dependencies(listener beginner_tutorials_generate_messages_cpp)
4、修改beginner_tutorials目录下的package.xml内容,如下:

<?xml version="1.0"?><package>  <name>beginner_tutorials</name>  <version>0.0.0</version>  <description>The beginner_tutorials package</description>  <!-- One maintainer tag required, multiple allowed, one person per tag -->  <!-- Example:  -->  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->  <maintainer email="xs@todo.todo">xs</maintainer>  <!-- One license tag required, multiple allowed, one license per tag -->  <!-- Commonly used license strings: -->  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->  <license>TODO</license>  <!-- Url tags are optional, but multiple are allowed, one per tag -->  <!-- Optional attribute type can be: website, bugtracker, or repository -->  <!-- Example: -->  <!-- <url type="website">http://wiki.ros.org/beginner_tutorials</url> -->  <!-- Author tags are optional, multiple are allowed, one per tag -->  <!-- Authors do not have to be maintainers, but could be -->  <!-- Example: -->  <!-- <author email="jane.doe@example.com">Jane Doe</author> -->  <!-- The *_depend tags are used to specify dependencies -->  <!-- Dependencies can be catkin packages or system dependencies -->  <!-- Examples: -->  <!-- Use build_depend for packages you need at compile time: -->  <!--   <build_depend>message_generation</build_depend> -->  <!-- Use buildtool_depend for build tool packages: -->  <!--   <buildtool_depend>catkin</buildtool_depend> -->  <!-- Use run_depend for packages you need at runtime: -->  <!--   <run_depend>message_runtime</run_depend> -->  <!-- Use test_depend for packages you need only for testing: -->  <!--   <test_depend>gtest</test_depend> -->  <buildtool_depend>catkin</buildtool_depend>  <build_depend>roscpp</build_depend>  <build_depend>rospy</build_depend>  <build_depend>std_msgs</build_depend>  <run_depend>roscpp</run_depend>  <run_depend>rospy</run_depend>  <run_depend>std_msgs</run_depend>  <!-- The export tag contains other, unspecified, tags -->  <export>    <!-- Other tools can request additional information be placed here -->  </export></package>

二、创建listener.cpp文件

在“做机械臂导航时遇到的问题4”中我们创建了listener.cpp订阅/jointstates话题,在这里我们修改listener.cpp内容实现ROS串口通信,listener.cpp代码内容如下:

#include "ros/ros.h"#include "std_msgs/String.h"#include "uart.h"#include "cJSON.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <sstream>#include "sensor_msgs/JointState.h"#include <unistd.h>#include <ctype.h>#include <signal.h> int g_quit;unsigned char jointstates[2];static UART_HANDLE uart_hd;//回调函数,注意话题消息的类型void statesCallback(const sensor_msgs::JointStateConstPtr& msg){  float pos[3],vel[3];  unsigned char info[100];//将需要的信息取出  pos[0]=msg->position[2];  pos[1]=msg->position[0];  pos[2]=msg->position[1];  vel[0]=msg->velocity[2];  vel[1]=msg->velocity[0];  vel[2]=msg->velocity[1];  char a[20]; //将数据进行Json封装    char *statesJson;  cJSON *statesJson1, *statesJson2,*statesJson3,*statesJson4;  statesJson1=cJSON_CreateObject();  cJSON_AddItemToObject(statesJson1,"joint_name",cJSON_CreateString("joint_msgs"));  cJSON_AddItemToObject(statesJson1,"joint1",statesJson2 = cJSON_CreateObject());  sprintf(a,"%f",pos[0]);  cJSON_AddStringToObject(statesJson2,"position",a);  sprintf(a,"%f",vel[0]);  cJSON_AddStringToObject(statesJson2,"velocity",a);  cJSON_AddItemToObject(statesJson1,"joint2",statesJson3 = cJSON_CreateObject());  sprintf(a,"%f",pos[1]);  cJSON_AddStringToObject(statesJson3,"position",a);  sprintf(a,"%f",vel[1]);  cJSON_AddStringToObject(statesJson3,"velocity",a);  cJSON_AddItemToObject(statesJson1,"joint3",statesJson4 = cJSON_CreateObject());  sprintf(a,"%f",pos[2]);  cJSON_AddStringToObject(statesJson4,"position",a);  sprintf(a,"%f",vel[2]);  cJSON_AddStringToObject(statesJson4,"velocity",a);  //格式转换  statesJson=cJSON_Print(statesJson1);  printf("jointmessage:\n%s\n",statesJson);    //将char *statesJson格式转换成unsigned char,将statesJson里的内容传给jointstates  int tag;  unsigned char jointstates[strlen(statesJson)];  memset(jointstates,0,strlen(statesJson));  for(int i=0;i<strlen(statesJson);i++)     jointstates[i]=statesJson[i];  //发送数据  tag=uart_send(uart_hd,jointstates,strlen(statesJson));  printf("\ntag=%d   message have been sent \n",tag);  }     //接收数据void uart_rec(const void *msg, unsigned int msglen, void *user_data){   if(msglen>0)  {    printf("\nI have received a message form serial port\n");  }}#define UART_DEVICE "/dev/ttyUSB0"int main(int argc, char **argv){   ros::init(argc, argv, "listener");  ros::NodeHandle n; ros::Subscriber sub = n.subscribe("/joint_states", 1000, statesCallback);  int ret = 0; printf("uart test\n");   //初始化uart串口   ret = uart_init(&uart_hd, UART_DEVICE, 115200, uart_rec, NULL);   if (0 != ret)   {printf("uart_init error ret = %d\n", ret);printf("please check up USB port or USB power supply\n");//return -1;   }       ros::spin();   return 0;}

三、测试

1、catkin_make 一下工作空间,并source

$catkin_make$source devel/setup.bash
2、连接好两台电脑的串口线,在另一台电脑上启动串口助手

3、打开新终端,启动listener节点

$rosrun beginner_tutorials listener
4、在串口助手上可以接收到信息



5、Ctrl C终止运行,完成测试


阅读全文
0 0
原创粉丝点击