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  nh.param(this_node::getName() + "/target_tolerance", target_tolerance, 0.1);
30 
31  // initialize publishers
32  target_found_pub = nh.advertise<cpswarm_msgs::TargetPositionEvent>("target_found", queue_size, true);
33  target_update_pub = nh.advertise<cpswarm_msgs::TargetPositionEvent>("target_update", queue_size, true);
34  target_lost_pub = nh.advertise<cpswarm_msgs::TargetPositionEvent>("target_lost", queue_size, true);
35  target_done_pub = nh.advertise<cpswarm_msgs::TargetPositionEvent>("target_done", queue_size, true);
36 
37  // init loop rate
38  rate = new Rate(loop_rate);
39 
40  // inform others about newly found target
41  if (state == TARGET_TRACKED) {
42  // wait until subscriber is connected
43  while (ok() && target_found_pub.getNumSubscribers() <= 0)
44  rate->sleep();
45 
46  ROS_DEBUG("Found target %d at (%.2f,%.2f)", id, pose.position.x, pose.position.y);
47 
48  // publish event
49  cpswarm_msgs::TargetPositionEvent target;
50  geometry_msgs::PoseStamped ps;
51  ps.pose = pose;
52  ps.header.frame_id = "local_origin_ned";
53  target.pose = ps;
54  target.header.stamp = Time::now();
55  target.swarmio.name = "target_found";
56  target.id = id;
57  target_found_pub.publish(target);
58  }
59 }
60 
62 {
63  delete rate;
64 }
65 
66 geometry_msgs::Pose target::get_pose ()
67 {
68  return pose;
69 }
70 
71 void target::lost ()
72 {
73  // target is being tracked
74  if (state == TARGET_TRACKED) {
75  // no updates received within timeout
76  if (stamp + timeout < Time::now()) {
77  // update target information
79 
80  // wait until subscriber is connected
81  while (ok() && target_lost_pub.getNumSubscribers() <= 0)
82  rate->sleep();
83 
84  // publish event
85  cpswarm_msgs::TargetPositionEvent target;
86  geometry_msgs::PoseStamped ps;
87  ps.pose = pose;
88  ps.header.frame_id = "local_origin_ned";
89  target.pose = ps;
90  target.header.stamp = Time::now();
91  target.swarmio.name = "target_lost";
92  target.id = id;
93  target_lost_pub.publish(target);
94  }
95  }
96 }
97 
98 void target::operator= (const target& t)
99 {
100  id = t.id;
101  update(t.state, t.pose, t.stamp);
102 }
103 
104 void target::update (target_state_t state, geometry_msgs::Pose pose, Time stamp)
105 {
106  // this target has been completed already, nothing to do
107  if (this->state == TARGET_DONE) {
108  ROS_DEBUG("Target %d already done", id);
109  return;
110  }
111 
112  // target completed
113  if (state == TARGET_DONE) {
114  // store target id in parameter server
115  vector<int> done;
116  nh.getParam(this_node::getNamespace() + "/targets_done", done);
117  done.push_back(id);
118  nh.setParam(this_node::getNamespace() + "/targets_done", done);
119 
120  // publish event
121  cpswarm_msgs::TargetPositionEvent target;
122  geometry_msgs::PoseStamped ps;
123  ps.pose = pose;
124  ps.header.frame_id = "local_origin_ned";
125  target.pose = ps;
126  target.header.stamp = Time::now();
127  target.swarmio.name = "target_done";
128  target.id = id;
129  target_done_pub.publish(target);
130 
131  // update target information
132  this->state = state;
133  this->pose = pose;
134  this->stamp = stamp;
135 
136  ROS_DEBUG("Target %d done", id);
137 
138  return;
139  }
140 
141  // this target has been assigned already, nothing to do except setting it to done
142  if (this->state == TARGET_ASSIGNED) {
143  ROS_DEBUG("Target %d already assigned", id);
144  return;
145  }
146 
147  // target has been assigned to a cps, stop tracking it
148  if (state == TARGET_ASSIGNED) {
149  // update target information
150  this->state = state;
151  this->stamp = stamp;
152 
153  // not necessary to publish event, since assignment is a swarm scope event already
154 
155  ROS_DEBUG("Target %d assigned", id);
156 
157  return;
158  }
159 
160  // target is being tracked, update for already known target
161  if (state == TARGET_TRACKED) {
162  // compute distance that target moved
163  double moved = hypot(last_pose.position.x - pose.position.x, last_pose.position.y - pose.position.y);
164 
165  // new target found
166  if (this->state != TARGET_TRACKED) {
167  // wait until subscriber is connected
168  while (ok() && target_update_pub.getNumSubscribers() <= 0)
169  rate->sleep();
170 
171  ROS_DEBUG("Found target %d at (%.2f,%.2f)", id, pose.position.x, pose.position.y);
172 
173  // publish event
174  cpswarm_msgs::TargetPositionEvent target;
175  geometry_msgs::PoseStamped ps;
176  ps.pose = pose;
177  ps.header.frame_id = "local_origin_ned";
178  target.pose = ps;
179  target.header.stamp = Time::now();
180  target.swarmio.name = "target_found";
181  target.id = id;
182  target_found_pub.publish(target);
183  }
184 
185  // target has moved enough for update
186  if (moved > target_tolerance) {
187  // wait until subscriber is connected
188  while (ok() && target_update_pub.getNumSubscribers() <= 0)
189  rate->sleep();
190 
191  // publish event
192  cpswarm_msgs::TargetPositionEvent target;
193  geometry_msgs::PoseStamped ps;
194  ps.pose = pose;
195  ps.header.frame_id = "local_origin_ned";
196  target.pose = ps;
197  target.header.stamp = Time::now();
198  target.swarmio.name = "target_update";
199  target.id = id;
200  target_update_pub.publish(target);
201 
202  ROS_DEBUG("Target %d update", id);
203  }
204 
205  // store current pose
206  last_pose = pose;
207 
208  // update target information
209  this->state = state;
210  this->pose = pose;
211  this->stamp = stamp;
212 
213  return;
214  }
215 }
~target()
Destructor that destroys all objects.
Definition: target.cpp:61
void lost()
Check whether the target tracked by this CPS has been lost. Switch state from TARGET_TRACKED to TARGE...
Definition: target.cpp:71
int id
The ID of this target. Negative IDs are invalid.
Definition: target.h:102
Time stamp
Time stamp of latest update of the target.
Definition: target.h:122
Publisher target_update_pub
Publisher for transmitting updated information about targets to other CPSs.
Definition: target.h:147
double target_tolerance
Minimum distance between two consecutive target positions such that a target update event is publishe...
Definition: target.h:137
Duration timeout
The time in seconds after which a target transitions into the state TARGET_LOST when no target update...
Definition: target.h:127
Publisher target_lost_pub
Publisher for transmitting information about lost targets to other CPSs.
Definition: target.h:152
Publisher target_done_pub
Publisher for transmitting information about completed targets to other CPSs.
Definition: target.h:157
void update(target_state_t state, geometry_msgs::Pose pose, Time stamp)
Update the information about a target.
Definition: target.cpp:104
target_state_t state
State of the target.
Definition: target.h:107
geometry_msgs::Pose get_pose()
Get the position of the target.
Definition: target.cpp:66
geometry_msgs::Pose last_pose
The position of the target the last time it was published as target position event.
Definition: target.h:117
void operator=(const target &t)
Assignment operator.
Definition: target.cpp:98
geometry_msgs::Pose pose
Position of the target.
Definition: target.h:112
A target that is monitored by the CPSs.
Definition: target.h:26
Rate * rate
The loop rate object for running the behavior control loops at a specific frequency.
Definition: target.h:132
target_state_t
An enumeration for the state of a target.
Definition: target.h:14
NodeHandle nh
A node handle for the main ROS node.
Definition: target.h:97
target()
Constructor.
Definition: target.cpp:3
Publisher target_found_pub
Publisher for transmitting information about found targets to other CPSs.
Definition: target.h:142


target_monitor
Author(s): Micha Sende
autogenerated on Thu Oct 31 2019 15:47:40