【二】、使用遊戲手柄驅動二輪差分底盤

新建手柄節點

#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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章