RobotThread.cpp
Go to the documentation of this file.
00001 #include "RobotThread.h"
00002 
00003 namespace server {
00004 RobotThread::RobotThread(int argc, char** pArgv)
00005     :   m_Init_argc(argc),
00006         m_pInit_argv(pArgv)
00007 {}
00008 
00009 RobotThread::~RobotThread()
00010 {
00011     if (ros::isStarted())
00012     {
00013         ros::shutdown();
00014         ros::waitForShutdown();
00015     }//end if
00016 
00017     wait();
00018 }//end destructor
00019 
00020 bool RobotThread::init()
00021 {
00022     ros::init(m_Init_argc, m_pInit_argv, "gui_command");
00023 
00024     if (!ros::master::check())
00025         return false;//do not start without ros.
00026 
00027     ros::start();
00028     ros::Time::init();
00029     ros::NodeHandle nh;
00030     //rostopic pub p2os_driver/MotorState cmd_motor_state -- 1.0
00031     cmd_publisher = nh.advertise<std_msgs::String>("/gui_cmd", 1000);
00032     sim_velocity  = nh.advertise<geometry_msgs::Twist>("/cmd_vel", 100);
00033     //sim_velocity = nh.advertise<turtlesim::Velocity>("/turtle1/command_velocity", 100);
00034     pose_listener = nh.subscribe("/pose", 10, &RobotThread::callback, this);
00035     //pose_listener = nh.subscribe("/turtle1/pose", 10, &RobotThread::callback, this);
00036     scan_listener = nh.subscribe("/scan", 1000, &RobotThread::scanCallBack, this);
00037     start();
00038     return true;
00039 }//set up the ros toys.
00040 
00042 void RobotThread::callback(nav_msgs::Odometry msg)
00043 {
00044     m_xPos = msg.pose.pose.position.x;
00045     m_yPos = msg.pose.pose.position.y;
00046     m_aPos = msg.pose.pose.orientation.w;
00047 
00048     //ROS_INFO("Pose: (%f, %f, %f)", m_xPos, m_yPos, m_aPos);
00049     Q_EMIT newPose(m_xPos, m_yPos, m_aPos);
00050 }//callback method to update the robot's position.
00051 
00052 /*void RobotThread::callback(turtlesim::Pose msg)
00053 {
00054     m_xPos = msg.x;
00055     m_yPos = msg.y;
00056     m_aPos = msg.theta;
00057 
00058     Q_EMIT newPose(m_xPos, m_yPos, m_aPos);
00059 }*/
00060 
00061 void RobotThread::scanCallBack(sensor_msgs::LaserScan scan)
00062 {
00063     ranges.clear();
00064 
00065     m_maxRange = scan.range_max;
00066     m_minRange = scan.range_min;
00067 
00068     for (unsigned int x = 0; x < scan.ranges.size(); x++)
00069         ranges.push_back(scan.ranges.at(x));
00070 
00071     int mid = scan.ranges.size() / 2; // get the middle index
00072 
00073     Q_EMIT(newMidLaser(ranges.at(mid)));
00074 }//callback method for updating the laser scan data.
00075 
00076 void RobotThread::goToXYZ(geometry_msgs::Point goTo)
00077 {
00078     MoveBaseClient ac("move_base", true);
00079 
00080     while(!ac.waitForServer(ros::Duration(5.0))){
00081       ROS_INFO("Waiting for the move_base action server to come up");
00082     }
00083 
00084     move_base_msgs::MoveBaseGoal goal;
00085 
00086     //we'll send a goal to the robot to move 1 meter forward
00087     goal.target_pose.header.frame_id = "base_link";
00088     goal.target_pose.header.stamp = ros::Time::now();
00089 
00090     goal.target_pose.pose.position.x = goTo.x;
00091     goal.target_pose.pose.position.y = goTo.y;
00092     goal.target_pose.pose.orientation.w = goTo.z; // the angle will be on z.
00093 
00094     ROS_INFO("Sending goal");
00095     ac.sendGoal(goal);
00096 
00097     ac.waitForResult();
00098 
00099     if(ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
00100       ROS_INFO("Hooray, the base moved 1 meter forward");
00101     else
00102       ROS_INFO("The base failed to move forward 1 meter for some reason");
00103 }//go to pose
00104 
00105 void RobotThread::run()
00106 {
00107     ros::Rate loop_rate(1);
00108     command = "empty";
00109     while (ros::ok())
00110     {
00111         std_msgs::String msg;
00112         std::stringstream ss;
00113         ss << command.toStdString();
00114 
00115         msg.data = ss.str();
00116         
00117         /*turtlesim::Velocity cmd_msg;
00118         cmd_msg.angular = m_angle;
00119         cmd_msg.linear = m_speed;// sim robots */
00120 
00122         geometry_msgs::Twist cmd_msg;
00123         cmd_msg.linear.x = m_speed;
00124         cmd_msg.angular.z = m_angle;
00125 
00126         cmd_publisher.publish(msg);
00127         sim_velocity.publish(cmd_msg);
00128         ros::spinOnce();
00129         loop_rate.sleep();
00130     }//do ros things.
00131 }
00132 
00133 void RobotThread::SetSpeed(double speed, double angle)
00134 {
00135     ROS_INFO("SetSpeed received");
00136     m_speed = speed;
00137     m_angle = angle;
00138     //rostopic pub -1 /turtle1/command_velocity turtlesim/Velocity  --2.0 --0.0
00139 }//set the speed of the robot.
00140 
00141 void RobotThread::setCommand(QString cmd)
00142 {
00143     command = cmd;
00144 }//get a command from another thread.
00145 
00146 void RobotThread::setPose(QList<double> to_set)
00147 {
00148     if (to_set.size() > 1)
00149     {
00150         m_xPos = to_set.at(0);//x coordinate
00151         m_yPos = to_set.at(1);//y coordinate
00152     }//end if
00153 }//end void
00154 
00155 double RobotThread::getXSpeed(){ return m_speed; }
00156 double RobotThread::getASpeed(){ return m_angle; }
00157 
00158 double RobotThread::getXPos(){ return m_xPos; }
00159 double RobotThread::getYPos(){ return m_yPos; }
00160 double RobotThread::getAPos(){ return m_aPos; }
00161 }//end namespace
00162 


gui_command
Author(s): Hunter Allen
autogenerated on Mon Oct 6 2014 08:35:15