Last active
August 29, 2015 14:12
-
-
Save detik19/c228cc2e1e8ececde9ac to your computer and use it in GitHub Desktop.
Connect USB Joystick to Turtlesim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* teleop_turtle_joy.cpp | |
* | |
* Created on: Dec 25, 2014 | |
* Author: tedy | |
*/ | |
#include <ros/ros.h> | |
#include <turtlesim/Velocity.h> | |
#include <sensor_msgs/Joy.h> | |
class TeleopTurtle | |
{ | |
public: | |
TeleopTurtle(); | |
private: | |
void joyCallback(const sensor_msgs::Joy::ConstPtr& joy); | |
ros::NodeHandle nh_; | |
int linear_, angular_; | |
double l_scale_, a_scale_; | |
ros::Publisher vel_pub_; | |
ros::Subscriber joy_sub_; | |
}; | |
TeleopTurtle::TeleopTurtle(): | |
linear_(1), | |
angular_(2) | |
{ | |
nh_.param("axis_linear", linear_, linear_); | |
nh_.param("axis_angular", angular_, angular_); | |
nh_.param("scale_angular", a_scale_, a_scale_); | |
nh_.param("scale_linear", l_scale_, l_scale_); | |
vel_pub_ = nh_.advertise<turtlesim::Velocity>("turtle1/command_velocity", 1); | |
joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopTurtle::joyCallback, this); | |
} | |
void TeleopTurtle::joyCallback(const sensor_msgs::Joy::ConstPtr& joy) | |
{ | |
turtlesim::Velocity vel; | |
vel.angular = a_scale_*joy->axes[angular_]; | |
vel.linear = l_scale_*joy->axes[linear_]; | |
vel_pub_.publish(vel); | |
} | |
int main(int argc, char** argv) | |
{ | |
ros::init(argc, argv, "teleop_turtle"); | |
TeleopTurtle teleop_turtle; | |
ros::spin(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment