ros中rqt_plot画出速度曲线


前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、rqt_plot是什么?

一种工具,该工具是为了解决数据分析任务而创建的。

二、使用步骤

1. 代码

publish_node.cpp:

#include "ros/ros.h"
#include "std_msgs/String.h" //use data struct of std_msgs/String  
#include "mbot_linux_serial.h"
#include "topic_example/speed.h"
#include <sstream>
#include <iostream>
#include <math.h>

using namespace std;
//test send value
double testSend1 = 0.2;
double testSend2 = 0.4;
double testSend3 = 0.3;
double testSend4 = 0.3;
unsigned char testSend5 = 0x07;

//test receive value
double testRece1=0.0;                  
double testRece2=0.0;
double testRece3=0.0;
double testRece4=0.0;
unsigned char testRece5=0x00;

int main(int agrc,char **argv)
{
    ros::init(agrc,argv,"public_node");
    ros::NodeHandle nh;
    ros::Publisher my_pub = nh.advertise<topic_example::speed>("myspeed",1);

    topic_example::speed msg;
    ros::Rate loop_rate(50);
    
    //串口初始化
    serialInit();
    double t=0;
    while(ros::ok())
    {
        ros::spinOnce();
        //向STM32端发送数据,前两个为double类型,最后一个为unsigned char类型
	    writeSpeed(testSend1,testSend2,testSend3,testSend4,testSend5);
        //打印数据
	    ROS_INFO("send to stm32:%f,%f,%f,%f,%d\n",testSend1,testSend2,testSend3,testSend4,testSend5);
        //从STM32接收数据,输入参数依次转化为小车的线速度、角速度、航向角(角度)、预留控制位
	    readSpeed(testRece1,testRece2,testRece3,testRece4,testRece5);

        msg.a=testRece1;
        msg.b=testRece2;
        msg.c=testRece3;
        msg.d=testRece4;
        my_pub.publish(msg);

        //打印数据
	    ROS_INFO("recevie form STM32:%f,%f,%f,%f,%d\n",testRece1,testRece2,testRece3,testRece4,testRece5);

        loop_rate.sleep();
	t+=1.0/50.0;
    }
    return 0;
}
 



2.操作实现:

代码如下(示例):

$ roscore 
$ rosrun topic_example publish_node
$ rqt_plot /myspeed

在这里插入图片描述


三、速度参数更改;

不完善:

1. 代码

在这里插入图片描述
增加节点:
speed_publisher.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将发布/speed_info话题,自定义消息类型topic_example::Person

import rospy
from topic_example.msg import speed

def velocity_publisher():
	# ROS节点初始化
    rospy.init_node('speed_publisher', anonymous=True)

	# 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
    speed_info_pub = rospy.Publisher('/speed_info', speed, queue_size=10)

	#设置循环的频率
    rate = rospy.Rate(10) 
 
    while not rospy.is_shutdown():
		# 初始化learning_topic::Person类型的消息
    	speed_msg = speed()
    	speed_msg.a = 0.3;
    	speed_msg.b = 0.2;
    	speed_msg.c = 0.4;

		# 发布消息
        speed_info_pub.publish(speed_msg)
    	rospy.loginfo("Publsh speed message[%f, %f, %f]", 
				speed_msg.a, speed_msg.b, speed_msg.c)

		# 按照循环频率延时
        rate.sleep()

if __name__ == '__main__':
    try:
        velocity_publisher()
    except rospy.ROSInterruptException:
        pass




更改:
publish_node.cpp:

#include "ros/ros.h"
#include "std_msgs/String.h" //use data struct of std_msgs/String  
#include "mbot_linux_serial.h"
#include "topic_example/speed.h"
#include <sstream>
#include <iostream>
#include <math.h>

using namespace std;
//test send value
//double testSend1 = 0.2;
//double testSend2 = 0.4;
//double testSend3 = 0.3;
//double testSend4 = 0.3;
//unsigned char testSend5 = 0x07;

//test receive value
double testRece1=0.0;                                               
double testRece2=0.0;
double testRece3=0.0;
double testRece4=0.0;
unsigned char testRece5=0x00;

// 接收到订阅的消息后,会进入消息回调函数
void speedInfoCallback(const topic_example::speed::ConstPtr& msg)
{
    // 将接收到的消息打印出来
    ROS_INFO("Subcribe speed Info: a:%f  b:%f  c:%f", 
			 msg->a, msg->b, msg->c);

//test send value
double testSend1 =msg->a;
double testSend2 =msg->b;
double testSend3 = msg->c;
double testSend4 = 0;
unsigned char testSend5 = 0x07;

        //向STM32端发送数据,前两个为double类型,最后一个为unsigned char类型
	    writeSpeed(testSend1,testSend2,testSend3,testSend4,testSend5);
        //打印数据
	    ROS_INFO("send to stm32:%f,%f,%f,%f,%d\n",testSend1,testSend2,testSend3,testSend4,testSend5);


}

int main(int agrc,char **argv)
{
    ros::init(agrc,argv,"public_node");
    ros::NodeHandle nh;
    ros::Publisher my_pub = nh.advertise<topic_example::speed>("myspeed",1);

// 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
    ros::Subscriber person_info_sub = nh.subscribe("/speed_info", 10, speedInfoCallback);

    topic_example::speed msg;
    ros::Rate loop_rate(50);
    
    //串口初始化
    serialInit();
    double t=0;
    while(ros::ok())
    {
        ros::spinOnce();

        //从STM32接收数据,输入参数依次转化为小车的线速度、角速度、航向角(角度)、预留控制位
	    readSpeed(testRece1,testRece2,testRece3,testRece4,testRece5);

        msg.a=testRece1;
        msg.b=testRece2;
        msg.c=testRece3;
        msg.d=testRece4;
        my_pub.publish(msg);

        //打印数据
	    ROS_INFO("recevie form STM32:%f,%f,%f,%f,%d\n",testRece1,testRece2,testRece3,testRece4,testRece5);
        loop_rate.sleep();
	t+=1.0/50.0;
    }
    return 0;
}
 




2.操作实现:

代码如下(示例):

$ roscore 
$ rosrun topic_example publish_node
$ rqt_plot /myspeed

总结

以上就是今天要讲的内容,本文仅仅简单介绍了 rqt_plot 的使用,而 rqt_plot 提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 2
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
rqt_plotROS的一个可视化工具,用于绘制ROS话题的数据。如果你在使用rqt_plot时遇到了"未找到命令"的错误,这可能是因为你没有正确安装rqt_plot或者没有将其添加到系统的环境变量。 要解决这个问题,首先确保你已经正确安装了rqt_plot。你可以使用以下命令来安装rqt_plot: ``` python -m pip install -U pip python -m pip install -U matplotlib ``` 这将使用pip来安装最新版本的matplotlib库,其包括rqt_plot。 如果你已经安装了rqt_plot但仍然遇到问题,那么可能是因为rqt_plot没有添加到系统的环境变量。你可以尝试使用以下命令来添加rqt_plot到环境变量: ``` export PATH=$PATH:/path/to/rqt_plot ``` 将"/path/to/rqt_plot"替换为你实际安装rqt_plot的路径。 通过执行这些步骤,你应该能够解决"rqt_plot:未找到命令"的问题,并成功使用rqt_plot来绘制ROS话题的数据。 #### 引用[.reference_title] - *1* [在终端运行rqt_plot命令报错](https://blog.csdn.net/renmengqisheng/article/details/120479803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [rqt_plot 绘制 joint_states 信息](https://blog.csdn.net/huangjunsheng123/article/details/115638286)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值