obstacle_avoidance.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 #include "std_msgs/Float32.h"
3 #include "geometry_msgs/Twist.h"
6 
7 int main(int argc, char **argv) {
8  // Initial Variables
9  std::string pheeno_name;
10 
11  // Parse inputs
12  CommandLineParser cml_parser(argc, argv);
13 
14  // Parse input arguments for Pheeno name.
15  if (cml_parser["-n"])
16  {
17  std::string pheeno_number = cml_parser("-n");
18  pheeno_name = "/pheeno_" + pheeno_number;
19  }
20  else
21  {
22  ROS_ERROR("Need to provide Pheeno number!");
23  }
24 
25 
26  // Initializing ROS node
27  ros::init(argc, argv, "obstacle_avoidance_node");
28 
29  // Crate PheenoRobot Object
30  PheenoRobot pheeno = PheenoRobot(pheeno_name);
31 
32  // ROS Rate loop
33  ros::Rate loop_rate(10);
34 
35  // Variables before loop
36  double saved_time = ros::Time::now().toSec();
37  double current_duration;
38  double turn_direction = pheeno.randomTurn(0.07);
39  double linear = 0.0;
40  double angular = 0.0;
41  geometry_msgs::Twist cmd_vel_msg;
42 
43  while (ros::ok())
44  {
45  // Find current duration of motion.
46  current_duration = ros::Time::now().toSec() - saved_time;
47 
48  if (current_duration <= 2.0) {
49  pheeno.avoidObstaclesLinear(linear, angular, turn_direction);
50  cmd_vel_msg.linear.x = linear;
51  cmd_vel_msg.angular.z = angular;
52 
53  } else if (current_duration < 5.0) {
54  pheeno.avoidObstaclesAngular(angular, turn_direction);
55  cmd_vel_msg.linear.x = linear;
56  cmd_vel_msg.angular.z = angular;
57 
58  } else {
59  // Reset Variables
60  saved_time = ros::Time::now().toSec();
61  turn_direction = pheeno.randomTurn(0.07);
62  }
63 
64  // Publish, Spin, and Sleep
65  pheeno.publish(cmd_vel_msg);
66  ros::spinOnce();
67  loop_rate.sleep();
68  }
69 }
void publish(geometry_msgs::Twist velocity)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
ROSCPP_DECL bool ok()
bool sleep()
void avoidObstaclesAngular(double &angular, double &random_turn_value, float angular_velocity=1.2, double range_to_avoid=20.0)
static Time now()
double randomTurn(float angular=0.06)
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
void avoidObstaclesLinear(double &linear, double &angular, float angular_velocity=1.2, float linear_velocity=0.08, double range_to_avoid=20.0)


pheeno_ros
Author(s): Zahi Kakish, Sean Wilson
autogenerated on Mon Jun 10 2019 14:10:48