PTUController.cpp
Go to the documentation of this file.
3 {
4  const double PTUController::RAD_TO_DEG = 180.0 / M_PI;
5 
6  PTUController::PTUController(ros::NodeHandle& node_handle, std::string name):
7  nodeHandle(node_handle),
8  simpleActionServer(name, false) {
9  //register the goal and feeback callbacks
12 
13  ROS_DEBUG("PTUController, %s", ros::this_node::getName().c_str());
15  stateCommandPublisher = nodeHandle.advertise<asr_flir_ptu_driver::State>(getDefaultStateCmdTopicName(), 100);
17 
18  stateCommandMessage.header.seq = 0;
19  stateCommandMessage.name.push_back("pan");
20  stateCommandMessage.name.push_back("tilt");
21  stateCommandMessage.velocity.push_back(0);
22  stateCommandMessage.velocity.push_back(0);
25  validate_client= nodeHandle.serviceClient<asr_flir_ptu_driver::Validate>(validation_service);
26  alive = nodeHandle.serviceClient<std_srvs::Empty>(alive_service, true);
27  seq_num = 0;
28  }
29 
31  }
32  void PTUController::currentStateArrived(const asr_flir_ptu_driver::State::ConstPtr& msg) {
33  if (msg->seq_num == seq_num) {
34  current_pan = msg->state.position[0];
35  current_tilt = msg->state.position[1];
38  //update joint state
39  sensor_msgs::JointState state;
40  state.header.stamp = lastStateTime;
41  state.name.resize(2);
42  state.position.resize(2);
43  state.velocity.resize(2);
44  state.name[0] = "ptu_pan";
45  state.position[0] = getCurrentPan();
46  state.velocity[0] = 0;
47  state.name[1] = "ptu_tilt";
48  state.position[1] = getCurrentTilt();
49  state.velocity[1] = 0;
50  simpleActionServerFeedback.base_joint = state;
51 
52  double squaredSums = pow( desired_pan - current_pan , 2.0) + pow(desired_tilt - current_tilt, 2.0);
53  double currentDistance = sqrt(squaredSums);
54  simpleActionServerFeedback.percentage = 1.0 - currentDistance / startDistance;
55  ROS_DEBUG_STREAM("Covered " << simpleActionServerFeedback.percentage << "% of the way to my goal.");
57 
58  //if ((fabs(getCurrentPan() - desired_pan) < getToleranceValue() ) &&
59  // (fabs(getCurrentTilt() - desired_tilt) < getToleranceValue() )) {
60 
61  //This might cause a problem when a new goal is sent but the variable is still true from the previouse one - not sure atm if this can happen
62  //bool reached_goal_and_halted;
63  if(msg->finished) {
64  simpleActionServerResult.end_joint = state;
65  ROS_INFO("Succeeded");
67  } else {
68  count++;
69  if (count > getMaxSteps()) {
70  simpleActionServerResult.end_joint = state;
71  ROS_ERROR("Aborted: More steps taken than allowed. MaxSteps: %d", getMaxSteps());
73  }
74  }
75  }
76  }
77  }
78 
80  ROS_DEBUG("Goal callback");
81  if(!alive)
82  {
83  ROS_ERROR("The PTU does not responde. Please check the PTU-Node");
85  }
86 
88  double pan_candidate = target_joint.position[0];
89  double tilt_candidate = target_joint.position[1];
90  double pan_speed = target_joint.velocity[0];
91  double tilt_speed = target_joint.velocity[1];
92  //seems it only uses non-speed mode; not sure why this is needed
93  if (pan_speed != 0 || tilt_speed != 0)
94  {
95  ROS_ERROR("The speed is not 0,0!");
97  }
98 
99  asr_flir_ptu_driver::Validate values_for_validation;
100  values_for_validation.request.pan = pan_candidate;
101  values_for_validation.request.tilt = tilt_candidate;
102  values_for_validation.request.margin = getMargin();
103  if(validate_client.call(values_for_validation)) {
104  if(values_for_validation.response.is_valid)
105  {
106  desired_pan = values_for_validation.response.new_pan;
107  desired_tilt = values_for_validation.response.new_tilt;
108  }
109  else {
110  ROS_ERROR("Values for pan %f and tilt %f do not lie within the working area of the ptu, neither do they lie within the margin %f out of the working area"
111  , pan_candidate, tilt_candidate, getMargin());
113  }
114  }
115  else {
116  ROS_ERROR("Failed to contact Validate service");
118  }
119 
120 
121  //ros::Time current_time = ros::Time::now();
122  //double time_difference = current_time.toSec() - lastStateTime.toSec();
123 
124  //Falsch: Hier bitte separaten watchdog einbauen
125  //if (time_difference > getTimeToWait()) {
126 
127 
128  //Alive is a service client with steady connection to the supplying PTUNode. A check with "if" of this client indicates if the PTUNode instance is still alive or not
129  //For more information please check http://wiki.ros.org/roscpp/Overview/Services point 2.1
130 
131  seq_num++;
132  asr_flir_ptu_driver::State msg;
133  stateCommandMessage.position.clear();
134  stateCommandMessage.position.push_back(desired_pan);
135  stateCommandMessage.position.push_back(desired_tilt);
136  //Wenn panSpeed und tiltSpeed sowieso nur übergeben wird wenn beide = 0 sind, warum werden sie dann überhaupt übergeben?
137  stateCommandMessage.velocity.push_back(pan_speed);
138  stateCommandMessage.velocity.push_back(tilt_speed);
139  stateCommandMessage.header.stamp = ros::Time::now();
140  msg.state = stateCommandMessage;
141  msg.seq_num = seq_num;
142  msg.no_check_forbidden_area = false;
144  ROS_DEBUG("pan:%f , tilt:%f, panSpeed:%f, tiltSpeed:%f", desired_pan, desired_tilt, pan_speed, tilt_speed);
145  count = 0;
146  double squared_sums = pow( desired_pan - current_pan , 2.0) + pow(desired_tilt - current_tilt, 2.0);
147  startDistance = sqrt(squared_sums);
148  }
149 
151  ROS_INFO("Preemted");
153  }
158  nodeHandle.getParam("timeToWait", timeToWait);
159  nodeHandle.getParam("margin", margin);
160  nodeHandle.getParam("maxSteps", maxSteps);
161  nodeHandle.getParam("tolerance", tolerance);
162  nodeHandle.getParam("topicName", topicName);
163  nodeHandle.getParam("commandTopicName", commandTopicName);
164  nodeHandle.getParam("serviceValidation", validation_service);
165  nodeHandle.getParam("serviceAlive", alive_service);
166  }
168  return max_pan;
169  }
171  return min_pan;
172  }
173 
175  return max_tilt;
176  }
178  return min_tilt;
179  }
180 
182  return current_pan;
183  }
185  return current_tilt;
186  }
187 
189  return topicName;
190  }
192  return commandTopicName;
193  }
195  return tolerance;
196  }
198  return timeToWait;
199  }
201  return maxSteps;
202  }
204  {
205  return margin;
206  }
207 
208  void PTUController::setMargin(double value)
209  {
210  margin = value;
211  }
212 }
213 
214 int main(int argc, char** argv)
215 {
216  ros::init(argc, argv, "ptu_controller_actionlib");
217  ros::NodeHandle n("~");
218  std::string actionServerName;
219  n.getParam("actionServerName", actionServerName);
220  asr_flir_ptu_controller::PTUController * nodeActionlib = new asr_flir_ptu_controller::PTUController(n, actionServerName);
221  ros::spin();
222  return 0;
223 }
asr_flir_ptu_controller::PTUMovementResult simpleActionServerResult
Definition: PTUController.h:79
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
boost::shared_ptr< const Goal > acceptNewGoal()
void publishFeedback(const FeedbackConstPtr &feedback)
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
sensor_msgs::JointState stateCommandMessage
Definition: PTUController.h:53
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
ROSCPP_DECL const std::string & getName()
int main(int argc, char **argv)
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
#define ROS_INFO(...)
void registerPreemptCallback(boost::function< void()> cb)
asr_flir_ptu_controller::PTUMovementFeedback simpleActionServerFeedback
Definition: PTUController.h:78
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
#define ROS_DEBUG_STREAM(args)
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
sensor_msgs::JointState target_joint
Definition: PTUController.h:45
void currentStateArrived(const asr_flir_ptu_driver::State::ConstPtr &msg)
bool getParam(const std::string &key, std::string &s) const
static Time now()
actionlib::SimpleActionServer< asr_flir_ptu_controller::PTUMovementAction > simpleActionServer
Definition: PTUController.h:77
PTUController(ros::NodeHandle &n, std::string name)
void registerGoalCallback(boost::function< void()> cb)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


asr_flir_ptu_controller
Author(s): Ralph Schleicher, Patrick Schlosser
autogenerated on Sun Nov 24 2019 03:28:47