MrTeleoperator.cpp
Go to the documentation of this file.
00001 /*
00002  * MrmTeleoperator.cpp
00003  *
00004  *  Created on: Nov 12, 2013
00005  *      Author: blackpc
00006  */
00007 
00012 #include <iostream>
00013 #include <ros/ros.h>
00014 #include <sensor_msgs/Joy.h>
00015 #include <geometry_msgs/Twist.h>
00016 #include <std_msgs/Int32.h>
00017 
00018 #include "Utilities.hpp"
00019 
00020 using namespace std;
00021 
00022 
00023 vector<ros::Publisher> _publishers;
00024 ros::Subscriber _joySubscriber;
00025 ros::Subscriber _activeRobotSubscriber;
00026 
00027 int32_t _activeRobotNumber = 1;
00028 double _aScale;
00029 double _lScale;
00030 ros::Timer _publishTimer;
00031 bool _deadmanPressed = false;
00032 geometry_msgs::Twist _lastVelocityMessage;
00033 bool _enabled = false;
00034 
00035 void joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
00036 {
00037         geometry_msgs::Twist vel;
00038         vel.angular.z = _aScale * joy->axes[3];
00039         vel.linear.x = _lScale * joy->axes[4];
00040         _deadmanPressed = joy->buttons[4];
00041         _lastVelocityMessage = vel;
00042 }
00043 
00044 void publishVelocity(const ros::TimerEvent& event) {
00045         if (_enabled) {
00046                 _publishers[_activeRobotNumber - 1].publish(_lastVelocityMessage);
00047         }
00048 }
00049 
00050 void initPublishers(ros::NodeHandle& node, vector<string> topicNames) {
00051         for (size_t i = 0; i < topicNames.size(); i++) {
00052                 _publishers.push_back(node.advertise<geometry_msgs::Twist>(topicNames[i], 1, false));
00053                 ROS_INFO("Advertising topic '%s'", topicNames[i].c_str());
00054         }
00055 }
00056 
00057 void onRobotSelect(const std_msgs::Int32 robotNumber) {
00058         if (robotNumber.data > 0 && robotNumber.data <= _publishers.size()) {
00059                 ROS_INFO("Controlled robot set to #%i", robotNumber.data);
00060                 _activeRobotNumber = robotNumber.data;
00061                 _enabled = true;
00062         }
00063 
00064         if (robotNumber.data == 0) {
00065                 ROS_INFO("Controlled robot set to none", robotNumber.data);
00066                 _activeRobotNumber = 0;
00067                 _enabled = false;
00068         }
00069 }
00070 
00071 int main(int argc, char **argv) {
00072         ros::init(argc, argv, "mr_teleoperator");
00073 
00074         ros::NodeHandle node("~");
00075 
00076         string joyTopic;
00077         node.param<string>("joy_topic", joyTopic, "/joy");
00078 
00079         int robotsCount;
00080         node.param<int>("robots_count", robotsCount, 10);
00081 
00082         string robotNsFormat;
00083         node.param<string>("robot_ns_format", robotNsFormat, "/pioneer_{robotId}");
00084 
00085         string velocityTopic;
00086         node.param<string>("velocity_topic", velocityTopic, "cmd_vel");
00087 
00088         node.param("scale_angular", _aScale, 0.9);
00089         node.param("scale_linear" , _lScale, 2.0);
00090 
00091         vector<string> topics = Utilities::getTopicNames(robotsCount, robotNsFormat, velocityTopic);
00092         initPublishers(node, topics);
00093 
00094         _joySubscriber = node.subscribe<sensor_msgs::Joy>(joyTopic, 10, joyCallback);
00095         _activeRobotSubscriber = node.subscribe<std_msgs::Int32>("select_robot", 1, onRobotSelect);
00096 
00097         _publishTimer = node.createTimer(ros::Duration(0.1), publishVelocity);
00098 
00099         ros::spin();
00100 }
00101 
00102 
00103 


mr_tools
Author(s): Igor Makhtes
autogenerated on Fri Aug 28 2015 11:35:35