【二】、使用游戏手柄驱动二轮差分底盘

新建手柄节点

#include<ros/ros.h>
#include<geometry_msgs/Twist.h>
#include<sensor_msgs/Joy.h>
#include<iostream>

using namespace std;

class TeleopJoy{
public:
  TeleopJoy();
private:
  void callBack(const sensor_msgs::Joy::ConstPtr& joy);
  ros::NodeHandle n;
  ros::Publisher pub;
  ros::Subscriber sub;
  int i_velLinear, i_velAngular;
};

TeleopJoy::TeleopJoy()
{    
    n.param("axis_linear",i_velLinear,i_velLinear);
    n.param("axis_angular",i_velAngular,i_velAngular);
    pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",1);
    sub = n.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopJoy::callBack, this);
}

void TeleopJoy::callBack(const sensor_msgs::Joy::ConstPtr& joy)
{
    geometry_msgs::Twist vel;
    vel.angular.z = joy->axes[i_velAngular];
    vel.linear.x = joy->axes[i_velLinear];
    pub.publish(vel);
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "teleopJoy");
    TeleopJoy teleop_turtle;
    ros::spin();
}

使用 launch 启动手柄节点和 serial_node.py 节点

<?xml version="1.0" ?>
<launch>
        <node pkg="rosserial_python" type="serial_node.py" name="serial_node" >
         <param name="port" value="/dev/ttyACM0"/>
         <param name="baud" value="57600"/>
        </node>

        <node pkg="chapter4_tutorials" type="c4_example1" name="c4_example1" />
         <param name="axis_linear" value="3" type="int" />
         <param name="axis_angular" value="0" type="int" />

        <node respawn="true" pkg="joy" type="joy_node" name="joy_node">
         <param name="dev" type="string" value="/dev/input/js1" />
         <param name="deadzone" value="0.12" />
        </node>
</launch>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值