ROS为上位机与STM32为下位机串口通讯(一)

STM32通过串口向ROS上位机发送信息

主要实现了STM32 通过串口向ROS上位机发送数据,发布者将接收到的数据发布出去并打印出来,订阅者订阅发布者发布的消息并打印出来,最后通过roslaunch启动。

STM32端

u16 times=0;
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
int main(void)
{   
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init(168);		
	uart_init(115200);	    //波特率115200
	while(1)
	{
			times++;
			printf("%3d\r\n",times);
			if(times>=100) times=0; 
			delay_ms(100);   
	}
}

ROS端

创建工作空间

mkdir -p ~/serial/src
cd ~/serial/src
catkin_init_workspace 

编译

cd ~/serial/
catkin_make

设置环境变量

echo "source ~/serial/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

创建功能包

catkin_create_pkg serial_communication roscpp std_msgs

在/catkin_ws/src/serial_communication/src/下新建serial_communication_pub.cpp和serial_communication_sub.cpp
serial_communication_pub.cpp内容如下

#include <string>
#include <ros/ros.h>// 包含ROS的头文件
#include <boost/asio.hpp>//包含boost库函数
#include <boost/bind.hpp>
#include <std_msgs/String.h>//ros定义的String数据类型

using namespace std;
using namespace boost::asio;//定义一个命名空间,用于后面的读写操作

unsigned char times_buf[5];//接收区

int main(int argc,char** argv)
{
    ros::init(argc,argv,"serial_communication_pub");//初始化节点
    ros::NodeHandle n;//创建节点句柄
    /*创建一个Publisher,发布名为chatter的topic,消息类型为std_msgs::String*/
    ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter",1000);
    ros::Rate loop_rate(10);//设置循环频率10Hz

    io_service iosev;
    serial_port sp(iosev, "/dev/ttyUSB0");//定义传输的串口
    sp.set_option(serial_port::baud_rate(115200));//波特率115200
    sp.set_option(serial_port::flow_control());//串口选项允许更改流量控制,默认值0
    sp.set_option(serial_port::parity());//奇偶性,默认值为none
    sp.set_option(serial_port::stop_bits()); //停止位,默认值为1
    sp.set_option(serial_port::character_size(8));  //数据位,默认值为8

    while(ros::ok())
    {
        read(sp,buffer(times_buf));
        string str(&times_buf[0],&times_buf[4]);//将数组转化为字符串

        std_msgs::String msg;
        std::stringstream ss;
        ss << str;

        msg.data = ss.str();

        ROS_INFO("%s",msg.data.c_str());//打印接受到的字符串

        chatter_pub.publish(msg);         //发布消息

        ros::spinOnce();

        loop_rate.sleep();       
    }
    iosev.run();
    return 0;
}


serial_communication_sub.cpp内容如下

#include <ros/ros.h>
#include <std_msgs/String.h>

//接收到订阅消息后,进入消息回调函数执行任务
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I hear:%s",msg->data.c_str());
}

int main(int argc, char **argv)
{
    /* code for main function */
    ros::init(argc, argv, "serial_communication_sub");//初始化ROS节点
    
    ros::NodeHandle n;//创建节点句柄

    /*创建一个Subscriber,订阅名为chatter的话题,注册回调函数chatterCallback*/
    ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
    
    ros::spin();//循环等待回调函数
    return 0;
}

打开 ~/serial/src/serial_communication/CMakeLists.txt, 最后面加上:

add_executable(serial_communication_pub src/serial_communication_pub.cpp)
target_link_libraries(serial_communication_pub ${catkin_LIBRARIES})

add_executable(serial_communication_sub src/serial_communication_sub.cpp)
target_link_libraries(serial_communication_sub ${catkin_LIBRARIES})

保存
在/catkin_ws/src/serial_communication/下新建文件夹launch
并在launch文件夹下新建一个serial_communication_pub.launch文件
未见内容如下

<launch>
	<node pkg="serial_communication" type="serial_communication_pub" name="serial_communication_pub" output="screen" />
	<node pkg="serial_communication" type="serial_communication_sub" name="serial_communication_sub" output="screen" />
</launch>

写好后记得再次编译

cd ~/serial/
catkin_make

最后运行

autolabor@autolabor-host:~$ roslaunch serial_communication serial_communication_pub.launch 
... logging to /home/autolabor/.ros/log/b1175c24-a2e2-11e9-8d53-000c299981e9/roslaunch-autolabor-host-11059.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://autolabor-host:40439/

SUMMARY
========

PARAMETERS
 * /rosdistro: kinetic
 * /rosversion: 1.12.13

NODES
  /
    serial_communication_pub (serial_communication/serial_communication_pub)
    serial_communication_sub (serial_communication/serial_communication_sub)

auto-starting new master
process[master]: started with pid [11069]
ROS_MASTER_URI=http://localhost:11311

setting /run_id to b1175c24-a2e2-11e9-8d53-000c299981e9
process[rosout-1]: started with pid [11082]
started core service [/rosout]
process[serial_communication_pub-2]: started with pid [11099]
process[serial_communication_sub-3]: started with pid [11100]
[ INFO] [1562743016.360922738]:  51
[ INFO] [1562743016.365478840]: I hear: 51
[ INFO] [1562743016.460078115]:  52
[ INFO] [1562743016.460510462]: I hear: 52
[ INFO] [1562743016.560876337]:  53
[ INFO] [1562743016.561298464]: I hear: 53
[ INFO] [1562743016.664422539]:  54
[ INFO] [1562743016.664840175]: I hear: 54
[ INFO] [1562743016.761167086]:  55
[ INFO] [1562743016.761580444]: I hear: 55
[ INFO] [1562743016.860703796]:  56
[ INFO] [1562743016.861023292]: I hear: 56
[ INFO] [1562743016.973725366]:  57
[ INFO] [1562743016.974084649]: I hear: 57
[ INFO] [1562743017.060380804]:  58
[ INFO] [1562743017.060940934]: I hear: 58
[ INFO] [1562743017.160667823]:  59
[ INFO] [1562743017.161122159]: I hear: 59
[ INFO] [1562743017.260142425]:  60
[ INFO] [1562743017.260559162]: I hear: 60
[ INFO] [1562743017.360150478]:  61
[ INFO] [1562743017.362249269]: I hear: 61
[ INFO] [1562743017.460271112]:  62
[ INFO] [1562743017.460853910]: I hear: 62

运行之前要先

sudo minicom

否则打印出的数据有问题

你可能感兴趣的:(ROS学习)