1、首先下载串口通信需要的uart文件,用于数据封装json文件。
下载地址:
http://download.csdn.net/download/m_zhangjingdong/10164628
http://download.csdn.net/download/m_zhangjingdong/10164611
2、假设在ros环境下操作,并且已经安装ros wiki上的说明创建了
beginner_tutorials包
a、将cJSON.h、uart.h文件拷贝到beginner_tutorials包内的include文件夹内
b、将cJSON.c、uart.c文件拷贝到beginner_tutorials包内的src文件夹内
c、修改beginner_tutorials目录下的CMakeList.txt内容,修改后如下:(倒数第二段的talker部分是当初测试用的,可以不加)
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
## Find catkin and any catkin packages
find_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 package
catkin_package()
## Build talker and listener
include_directories(include)
AUX_SOURCE_DIRECTORY(src DIR_SRCS)
#set environment variable
SET(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)
3、修改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="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">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="[email protected]">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>
4、创建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;
}
5、
catkin_make 一下工作空间,并source
$catkin_make
$source devel/setup.bash