target.cpp
Go to the documentation of this file.
1 #include "lib/target.h"
2 
4 {
5 }
6 
7 target::target (const target& t) : target(t.id, t.state, t.pose, t.stamp)
8 {
9 }
10 
11 target::target (unsigned int id, target_state_t state) : target(id, state, geometry_msgs::Pose())
12 {
13 }
14 
15 target::target (unsigned int id, target_state_t state, geometry_msgs::Pose pose) : target(id, state, pose, Time::now())
16 {
17 }
18 
19 target::target (unsigned int id, target_state_t state, geometry_msgs::Pose pose, Time stamp) : id(id), state(state), pose(pose), stamp(stamp)
20 {
21  // read parameters
22  double loop_rate;
23  nh.param(this_node::getName() + "/loop_rate", loop_rate, 5.0);
24  int queue_size;
25  nh.param(this_node::getName() + "/queue_size", queue_size, 10);
26  double timeout;
27  nh.param(this_node::getName() + "/tracking_timeout", timeout, 5.0);
28  this->timeout = Duration(timeout);
29 
30  // init loop rate
31  rate = new Rate(loop_rate);
32 
33  ROS_INFO("Added target %d in state %d", id, state);
34 }
35 
37 {
38  delete rate;
39 }
40 
41 geometry_msgs::Pose target::get_pose ()
42 {
43  return pose;
44 }
45 
47 {
48  return state;
49 }
50 
51 bool target::lost ()
52 {
53  // target is being tracked
55  // no updates received within timeout
56  if (stamp + timeout < Time::now()) {
57  if (state == TARGET_TRACKED)
58  ROS_INFO("Target %d: tracked --> lost", id);
59  else if (state == TARGET_ASSIGNED)
60  ROS_INFO("Target %d: assigned --> lost", id);
61  else
62  ROS_INFO("Target %d: lost", id);
63 
64  // update target information
66 
67  return true;
68  }
69  }
70 
71  return false;
72 }
73 
74 void target::operator= (const target& t)
75 {
76  id = t.id;
77  update(t.state, t.pose, t.stamp);
78 }
79 
80 void target::update (target_state_t state, geometry_msgs::Pose pose, Time stamp)
81 {
82  // this target has been completed already, nothing to do
83  if (this->state == TARGET_DONE) {
84  ROS_DEBUG("Target %d: already done", id);
85  return;
86  }
87 
88  // this target has been assigned to another cps for completion but is still tracked
89  else if (this->state == TARGET_ASSIGNED) {
90  // update target information
91  this->pose = pose;
92  this->stamp = stamp;
93 
94  // target completed by the other cps
95  if (state == TARGET_DONE) {
96  ROS_DEBUG("Target %d: assigned --> done", id);
97 
98  this->state = state;
99 
100  // store target id in parameter server
101  vector<int> done;
102  nh.getParam(this_node::getNamespace() + "/targets_done", done);
103  done.push_back(id);
104  nh.setParam(this_node::getNamespace() + "/targets_done", done);
105  }
106 
107  return;
108  }
109 
110  // target is being tracked
111  else if (this->state == TARGET_TRACKED) {
112  // send update
113  if (state == TARGET_TRACKED) {
114  ROS_DEBUG("Target %d: tracked, update", id);
115 
116  // update target information
117  this->state = state;
118  this->pose = pose;
119  this->stamp = stamp;
120  }
121 
122  // target has been assigned to another cps
123  else if (state == TARGET_ASSIGNED) {
124  ROS_DEBUG("Target %d: tracked --> assigned", id);
125 
126  // update target information
127  this->state = state;
128  this->stamp = stamp;
129  }
130 
131  return;
132  }
133 
134  // target has been found by another cps
135  else if (this->state == TARGET_KNOWN) {
136  // target now found by this cps
137  if (state == TARGET_TRACKED) {
138  ROS_DEBUG("Target %d: known --> tracked, position (%.2f,%.2f)", id, pose.position.x, pose.position.y);
139 
140  // update target information
141  this->state = state;
142  this->pose = pose;
143  this->stamp = stamp;
144  }
145 
146  // target has been assigned to another cps
147  else if (state == TARGET_ASSIGNED) {
148  ROS_DEBUG("Target %d: known --> assigned", id);
149 
150  // update target information
151  this->state = state;
152  this->stamp = stamp;
153  }
154 
155  // target has been lost by another cps
156  else if (state == TARGET_LOST) {
157  ROS_DEBUG("Target %d: known --> lost", id);
158 
159  // update target information
160  this->state = state;
161  this->pose = pose;
162  this->stamp = stamp;
163  }
164 
165  return;
166  }
167 
168  // target has been lost by another cps
169  else if (this->state == TARGET_LOST) {
170  // update target information
171  this->state = state;
172  this->pose = pose;
173  this->stamp = stamp;
174 
175  // target has been found by another cps
176  if (state == TARGET_KNOWN) {
177  ROS_DEBUG("Target %d: lost --> known", id);
178  }
179 
180  // target has been found by this cps
181  else if (state == TARGET_TRACKED) {
182  ROS_DEBUG("Target %d: lost --> tracked", id);
183  }
184 
185  return;
186  }
187 
188  else {
189  ROS_ERROR("Target %d: invalid state transition %d --> %d!", id, this->state, state);
190  }
191 }
~target()
Destructor that destroys all objects.
Definition: target.cpp:36
int id
The ID of this target. Negative IDs are invalid.
Definition: target.h:108
Time stamp
Time stamp of latest update of the target.
Definition: target.h:123
Duration timeout
The time in seconds after which a target transitions into the state TARGET_LOST when no target update...
Definition: target.h:128
void update(target_state_t state, geometry_msgs::Pose pose, Time stamp)
Update the information about a target.
Definition: target.cpp:80
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
target_state_t state
State of the target.
Definition: target.h:113
bool lost()
Check whether the target tracked by this CPS has been lost. Switch state from TARGET_TRACKED to TARGE...
Definition: target.cpp:51
geometry_msgs::Pose get_pose()
Get the position of the target.
Definition: target.cpp:41
void operator=(const target &t)
Assignment operator.
Definition: target.cpp:74
geometry_msgs::Pose pose
Position of the target.
Definition: target.h:118
A target that is monitored by the CPSs.
Definition: target.h:25
bool getParam(const std::string &key, std::string &s) const
target_state_t get_state()
Get the state of the target.
Definition: target.cpp:46
Rate * rate
The loop rate object for running the behavior control loops at a specific frequency.
Definition: target.h:133
target_state_t
An enumeration for the state of a target.
Definition: target.h:13
NodeHandle nh
A node handle for the main ROS node.
Definition: target.h:103
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
target()
Constructor.
Definition: target.cpp:3
#define ROS_DEBUG(...)


target_monitor
Author(s): Micha Sende
autogenerated on Tue Jan 19 2021 03:30:07