uav_simple_tracking.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <ros/console.h>
4 #include <cpswarm_msgs/TrackingAction.h>
5 #include <cpswarm_msgs/TargetPositionEvent.h>
7 
8 using namespace ros;
9 
14 
18 cpswarm_msgs::TargetPositionEvent target;
19 
24 
30 void ActionCallback(const cpswarm_msgs::TrackingGoalConstPtr& goal, action_server_t* as)
31 {
32  NodeHandle nh;
33 
34  // target id
35  target.id = goal->target;
36 
37  // set loop rate
38  double loop_rate;
39  nh.param(this_node::getName() + "/loop_rate", loop_rate, 5.0);
40  Rate rate(loop_rate);
41 
42  ROS_INFO("Executing tracking");
43 
44  // tracking library
45  uav_simple_tracking uav_tracking(target, goal->altitude);
46 
47  // execute tracking until state changes
49  while (ok() && !as->isPreemptRequested() && state == STATE_ACTIVE) {
50  ROS_DEBUG("Tracking step");
51  behavior_state_t result = uav_tracking.step(target);
52  if (state == STATE_ACTIVE)
53  state = result;
54  rate.sleep();
55  spinOnce();
56  }
57 
58  // stop moving
59  uav_tracking.stop();
60 
61  // tracking succeeded
62  if (state == STATE_SUCCEEDED) {
63  ROS_INFO("Tracking succeeded");
64  as->setSucceeded();
65  }
66 
67  // tracking aborted
68  else if (state == STATE_ABORTED) {
69  ROS_INFO("Tracking aborted");
70  as->setAborted();
71  }
72 
73  // tracking was preempted
74  else{
75  ROS_INFO("Tracking preempted");
76  as->setPreempted();
77  }
78 }
79 
84 void update_callback (const cpswarm_msgs::TargetPositionEvent::ConstPtr& msg)
85 {
86  // update information for this target
87  if (msg->id == target.id)
88  target = *msg;
89 }
90 
95 void lost_callback (const cpswarm_msgs::TargetPositionEvent::ConstPtr& msg)
96 {
97  if (msg->id == target.id)
99 }
100 
105 void done_callback (const cpswarm_msgs::TargetPositionEvent::ConstPtr& msg)
106 {
107  if (msg->id == target.id)
109 }
110 
117 int main (int argc, char** argv)
118 {
119  // init ros node
120  init(argc, argv, "uav_tracking");
121  NodeHandle nh;
122 
123  // define which log messages are shown
126  }
127  else{
128  ROS_ERROR("Could not set logger level!");
129  }
130 
131  // initially, no targets being tracked
132  target.id = -1;
133 
134  // subscribers
135  int queue_size;
136  nh.param(this_node::getName() + "/queue_size", queue_size, 1);
137  Subscriber update_sub = nh.subscribe("target_update", queue_size, update_callback);
138  Subscriber lost_sub = nh.subscribe("target_lost", queue_size, lost_callback);
139  Subscriber done_sub = nh.subscribe("target_done", queue_size, done_callback);
140 
141  // start action server
142  action_server_t as(nh, "uav_tracking", boost::bind(&ActionCallback, _1, &as), false);
143  as.start();
144 
145  // wait for action client
146  spin();
147 
148  return 0;
149 }
150 
An implementation that allows to track a target by minimizing the offset between the cyber physical s...
void update_callback(const cpswarm_msgs::TargetPositionEvent::ConstPtr &msg)
Callback function to receive target update events.
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
behavior_state_t state
The state of the behavior algorithm.
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
void lost_callback(const cpswarm_msgs::TargetPositionEvent::ConstPtr &msg)
Callback function to receive event that target has been lost.
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
behavior_state_t step(cpswarm_msgs::TargetPositionEvent target)
Execute one cycle of the tracking algorithm.
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
int main(int argc, char **argv)
Main function to be executed by ROS.
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
ROSCPP_DECL void spin()
actionlib::SimpleActionServer< cpswarm_msgs::TrackingAction > action_server_t
An action server type that allows to start and stop the tracking task.
cpswarm_msgs::TargetPositionEvent target
The target being tracked.
bool sleep()
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
void done_callback(const cpswarm_msgs::TargetPositionEvent::ConstPtr &msg)
Callback function to receive event that target has been done.
ROSCONSOLE_DECL bool set_logger_level(const std::string &name, levels::Level level)
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
#define ROSCONSOLE_DEFAULT_NAME
behavior_state_t
An enumeration for the state of the behavior algorithm.
void ActionCallback(const cpswarm_msgs::TrackingGoalConstPtr &goal, action_server_t *as)
Callback of the action server which executes the tracking task until it is preempted or finished...
#define ROS_DEBUG(...)


uav_simple_tracking
Author(s): Micha Sende
autogenerated on Sat Feb 6 2021 03:11:44