Robot Game  v1.0
Research Track part 1 - Assignment
rg_services.cpp
Go to the documentation of this file.
1 
21 #include "ros/ros.h"
22 #include "ros/console.h"
23 #include "geometry_msgs/Point.h"
24 #include "geometry_msgs/Vector3.h"
25 #include "geometry_msgs/Twist.h"
26 #include "robot_game/rg_check_srv.h"
27 #include "robot_game/rg_get_target_srv.h"
28 #include "robot_game/rg_get_vel_srv.h"
29 
30 #include <cmath>
31 #include <cstdlib>
32 #include <ctime>
33 
35 #define TOLERANCE 0.1f
36 
38 #define K (1.f)
39 
40 
41 
42 
43 
51 double compute_eucledian_distance( geometry_msgs::Point P1, geometry_msgs::Point P2 )
52 {
53  return sqrt( pow(P2.x-P1.x, 2.0) + pow(P2.y-P1.y, 2.0) + pow(P2.z-P1.z, 2.0) );
54 }
55 
64 double rand_range( double min, double max )
65 {
66  return min + (max - min)*( ((double)rand()) / RAND_MAX );
67 }
68 
69 
70 
71 
72 
73 
74 
88 bool rg_check_target_callback( robot_game::rg_check_srv::Request &initpos, robot_game::rg_check_srv::Response &r )
89 {
90  //ROS_INFO( "rg_check_target_callback( X(%f, %f, %f) Xt(%f, %f, %f) )", initpos.x.x, initpos.x.y, initpos.x.z, initpos.xt.x, initpos.xt.y, initpos.xt.z );
91 
92  double dist = compute_eucledian_distance( initpos.x, initpos.xt );
93  //ROS_INFO( "computed distance: %f", dist );
94 
95  return ( dist <= 0.1f );
96 }
97 
104 bool rg_get_target_callback( robot_game::rg_get_target_srv::Request &limits, robot_game::rg_get_target_srv::Response &target )
105 {
106  /*
107  * Content of the request:
108  * xmin, xmax
109  * ymin, ymsx
110  *
111  * Point: x, y, z
112  */
113 
114  //ROS_INFO( "rg_get_target_callback( %f, %f, %f, %f )", limits.xmin, limits.xmax, limits.ymin, limits.ymax );
115 
116  target.xt.z = 0.f;
117  target.xt.x = rand_range( limits.xmin, limits.xmax );
118  target.xt.y = rand_range( limits.ymin, limits.ymax );
119 
120  ROS_INFO( "new target is ( %f, %f, %f )", target.xt.x, target.xt.y, target.xt.z );
121 
122  return true;
123 }
124 
131 bool rg_get_velocity_callback( robot_game::rg_get_vel_srv::Request &initpos, robot_game::rg_get_vel_srv::Response &vel )
132 {
133  //ROS_INFO( "rg_get_velocity_callback( X(%f, %f, %f) Xt(%f, %f, %f) )", initpos.x.x, initpos.x.y, initpos.x.z, initpos.xt.x, initpos.xt.y, initpos.xt.z );
134 
135  vel.vel.linear.x = K * ( initpos.xt.x - initpos.x.x );
136  vel.vel.linear.y = K * ( initpos.xt.y - initpos.x.y );
137  vel.vel.linear.z = 0.f;
138 
139  vel.vel.angular.x = 0.f;
140  vel.vel.angular.y = 0.f;
141  vel.vel.angular.z = 0.f;
142 
143  //ROS_INFO( "output Twist( v(%f, %f, %f), w(%f, %f, %f) )", vel.vel.linear.x, vel.vel.linear.y, vel.vel.linear.z, vel.vel.angular.x, vel.vel.angular.y, vel.vel.angular.z);
144 
145  return true;
146 }
147 
148 
149 
150 
151 
152 
153 
154 
155 
159 int main( int argc, char** argv )
160 {
161  ros::init( argc, argv, "rg_services" );
162  ros::NodeHandle h;
163 
164  ros::ServiceServer srv_check_target = h.advertiseService( "rg_check_target", rg_check_target_callback );
165  ros::ServiceServer srv_get_target = h.advertiseService( "rg_get_target", rg_get_target_callback );
166  ros::ServiceServer srv_get_velocity = h.advertiseService( "rg_get_velocity", rg_get_velocity_callback );
167 
168  ros::spin();
169 
170  return 0;
171 }
srv_check_target
Service entry point for checking if the target is reached.
srv_get_target
Service entry point for requiring a nwe random target.
geometry_msgs::Point target
int main(int argc, char **argv)
Ask for services, topics, and initialise the node.
double compute_eucledian_distance(geometry_msgs::Point P1, geometry_msgs::Point P2)
Eucledian distance between two points.
Definition: rg_services.cpp:51
bool rg_get_velocity_callback(robot_game::rg_get_vel_srv::Request &initpos, robot_game::rg_get_vel_srv::Response &vel)
Service - generate a twist command.
double rand_range(double min, double max)
Generate a random value within a given range.
Definition: rg_services.cpp:64
#define K
Definition: rg_services.cpp:38
bool rg_get_target_callback(robot_game::rg_get_target_srv::Request &limits, robot_game::rg_get_target_srv::Response &target)
Service - get a new random target.
bool rg_check_target_callback(robot_game::rg_check_srv::Request &initpos, robot_game::rg_check_srv::Response &r)
Service - check target.
Definition: rg_services.cpp:88