ROS is the upper computer and STM32 is the serial communication of the lower computer (1)

 

Retrieved from: https://blog.csdn.net/qq_38422317/article/details/95335967?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog- BlogCommendFromMachineLearnPai2-1.nonecase

ROS is the upper computer and STM32 is the serial communication of the lower computer (1)

 

Eleven-boy 2019-07-10 15:27:27 1715 Collection 15

Category column: ROS learning

copyright

STM32 sends information to the ROS host computer through the serial port

The main realization is that STM32 sends data to the ROS host computer through the serial port, the publisher publishes the received data and prints it out, the subscriber subscribes to the news published by the publisher and prints it out, and finally starts through roslaunch.

STM32 side

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);   
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

ROS end

Create workspace

mkdir -p ~/serial/src
cd ~/serial/src
catkin_init_workspace 
  • 1
  • 2
  • 3

Compile

cd ~/serial/
catkin_make
  • 1
  • 2

Set environment variables

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

Create feature pack

catkin_create_pkg serial_communication roscpp std_msgs
  • 1

Create new serial_communication_pub.cpp and serial_communication_sub.cpp
serial_communication_pub.cpp under /catkin_ws/src/serial_communication/src/ as follows

#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;
}


The content of serial_communication_sub.cpp is as follows

#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;
}

Open ~/serial/src/serial_communication/CMakeLists.txt, and add:

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})
  • 1
  • 2
  • 3
  • 4
  • 5

Save it
in /catkin_ws/src/serial_communication/, create a new folder launch
and create a new serial_communication_pub.launch file under the launch folder. The
unseen content is as follows

<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>
  • 1
  • 2
  • 3
  • 4

Remember to compile again after writing

cd ~/serial/
catkin_make
  • 1
  • 2

Last run

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

Before running

sudo minicom

Otherwise there is a problem with the printed data

Guess you like

Origin blog.csdn.net/sinat_16643223/article/details/108895138