ROS主题发布订阅控制真实的机器人下位机

 

先模拟控制小乌龟

新建cmd_node.ccpp文件:

#include"ros/ros.h"
#include"geometry_msgs/Twist.h"                             //包含geometry_msgs::Twist消息头文件
#include <stdlib.h>
#include<stdlib.h>


int main(int argc, char **argv)
{
	ros::init(argc, argv, "cmd_node");
	ros::NodeHandle n;

    ros::Publisher cmd_pub = n.advertise<geometry_msgs::Twist>("turtle1/cmd_vel",1000);
    ros::Rate loop_rate(10);

    while(ros::ok())
    {
        geometry_msgs::Twist msg;
        msg.linear.x = (double)(rand()/(double(RAND_MAX)));                     //随机函数
        msg.angular.z = (double)(rand()/(double(RAND_MAX)));

        cmd_pub.publish(msg);
        ROS_INFO("msg.linear.x:%f ,  msg.angular.z: %f",msg.linear.x,msg.angular.z);
        loop_rate.sleep();
    }
	return 0;
}

编译成功产生

 

测试

roscore
rosrun turtlesim turtlesim_node
rosrun zxwtest_package hello_node

查看节点 框图:

rqt_graph

我们订阅/turtle1/cmd_vel话题上的turtlesim移动的角速度和线速度信息

#include"ros/ros.h"
#include"geometry_msgs/Twist.h"
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <math.h>

void chatterCallback(const geometry_msgs::Twist& msg)
{
    ROS_INFO_STREAM(std::setprecision(2) << std::fixed << "linear.x=("<< msg.linear.x<<" msg.angular.z="<<msg.angular.z);

}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "listener");

    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe("/turtle1/cmd_vel", 1000,&chatterCallback);
    ros::spin();

    return 0;
}

 运行

roscore
rosrun odom_tf_package  listen_node 
 rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key

 

修改回调函数,添加向下位机发送串口数据

 

#include <ros/ros.h>
#include "geometry_msgs/Twist.h"
#include <odom_tf_package/serial_boost.h>
#include <iomanip>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <math.h>

#define MS_RADMIN 233
#define width_robot 0.31
#define BYTE0(pointer) (*((char*)(&pointer)+0));

using namespace std;
using namespace boost::asio;

unsigned char buf[6] = {0xff,0x00,0x00,0x00,0x00,0xfe};


void chatterCallback(const geometry_msgs::Twist& msg)
{
     io_service iosev;  //boos


    double vel_x = msg.linear.x / 10.0;         //速度缩小十倍为0.2m/s
    double vel_th = msg.angular.z ;
    double right_vel = 0.0;
    double left_vel = 0.0;

     serial_port sp(iosev, "/dev/ttyUSB0");
     sp.set_option(serial_port::baud_rate(115200));
     sp.set_option(serial_port::flow_control(serial_port::flow_control::none));
     sp.set_option(serial_port::parity(serial_port::parity::none));
     sp.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
     sp.set_option(serial_port::character_size(8));


    if(vel_x == 0)
    {
        right_vel = vel_th * width_robot / 2.0;
        left_vel = (-1) * right_vel;
    }
    else if(vel_th == 0)
    {
        left_vel = right_vel = vel_x;
    }
    else
    {
        left_vel = vel_x - vel_th * width_robot / 2.0;
        right_vel = vel_x + vel_th * width_robot / 2.0;
    }

    right_vel = right_vel*MS_RADMIN;                //MS_RADMIN 是m/s转换为转每分的系数,根据会自己轮子大小计算
    left_vel  = left_vel*MS_RADMIN;

    ROS_INFO("The car speed r is %2f",right_vel);
    ROS_INFO("The car speed l is %2f",left_vel);

    buf[1] = (signed char)left_vel;
    buf[2] = (signed char)right_vel;

    write(sp, buffer(buf, 6));
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "listener");

    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe("/turtle1/cmd_vel", 1000,&chatterCallback);
    ros::spin();

    return 0;
}

 

 发给下位机速度数据:

下位机进行串口解包。

 

转载于:https://www.cnblogs.com/CZM-/p/5928277.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ROS机器人操作系统)是一款成熟的机器人操作系统,具有完备的生态体系,未来的发展意义可以成为机器人届中的”Linux、Android“。机器人的开发学习要综合软硬件的协同开发,硬件开发有些部分倾向于底层的设计和使用。软件开发可以依托C++、PYTHON等高级语言进行ROS接口应用开发,或者兼容ROS系统。本系列的ROS开发课程包含下位机开发、上位机开发、基于MBD(基于模型设计的开发)等。下位机作为机器人设计的基础部分是学习机器人操作系统必经之路,下位机我们通常会选择Arduino(适合学习不适合工程,代码执行效率差)、STM32系列产品(工程应用广泛,适合学习和工程开发,代码针对性强),当然还有NXP系列、51系列、TI DSP等,后期可以根据产品的性能和成本要求去考虑;上位机部分,主要是基于工控机、树莓派Raspberry、英伟达Jetson等可以运行操作系统的嵌入式设备再基于ROS操作系统进行实训学习。网络上对于ROS类的教学比较多,但是系统化从底层向高阶层层递升的教学方法偏少,知识碎片化严重,对机器人开发工程师深远的发展有负面影响,基于此本人通过多年自身的学习和工程实践,将机器人开发课程系统化、具象化、模块化地引导式学习,每节课程都有相应的课件和代码引导。对致力于机器人事业的学生有推动作用,且增强信心,系统化自己的机器人知识。为自己的职业规划和事业发展奠定坚实基础。最后,你们的支持,就是老师不断创作的动力!老师会不断更新机器人类相关知识,希望”与子同裳“。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值