PTU_GUI.cpp
Go to the documentation of this file.
1 
8 #include "gui/PTU_GUI.h"
9 
10 namespace GUI_PTU {
11 
12 PTU_GUI::PTU_GUI( wxWindow* parent ) : GUIDialog( parent )
13 {
14  update_timer = new wxTimer(this);
15  update_timer->Start(16);
16 
17  immUpdate = false;
18  listenForUpdates = false;
19 
20  Connect(update_timer->GetId(), wxEVT_TIMER, wxTimerEventHandler(PTU_GUI::onUpdate), NULL, this);
21 
22  initTopicList();
23 
24 // ros::NodeHandle priv("~");
25 
26  panSlider->SetValue(0);
27  panSpinner->SetValue(0);
28  tiltSlider->SetValue(0);
29  tiltSpinner->SetValue(0);
30 
31  pan_min->SetValue(0);
32  pan_max->SetValue(0);
33  pan_base->SetValue(0);
34  pan_target->SetValue(0);
35  pan_upper->SetValue(0);
36  pan_accel->SetValue(0);
37  pan_hold->SetValue(0);
38  pan_move->SetValue(0);
39 
40  tilt_min->SetValue(0);
41  tilt_max->SetValue(0);
42  tilt_base->SetValue(0);
43  tilt_target->SetValue(0);
44  tilt_upper->SetValue(0);
45  tilt_accel->SetValue(0);
46  tilt_hold->SetValue(0);
47  tilt_move->SetValue(0);
48 
49  leftImageSubscriber = nh.subscribe<sensor_msgs::Image>("left/image_color", 1, &PTU_GUI::onLeftImage, this);
50  rightImageSubscriber = nh.subscribe<sensor_msgs::Image>("right/image_color", 1, &PTU_GUI::onRightImage, this);
51 
52  predict_client = nh.serviceClient<asr_flir_ptu_driver::Predict>("/asr_flir_ptu_driver/path_prediction");
53 
54  wxBitmap* imageLeft = new wxBitmap(wxImage(640, 480));
55  leftPanel->setImage(imageLeft);
57 
58  wxBitmap* imageRight = new wxBitmap(wxImage(640, 480));
59  rightPanel->setImage(imageRight);
61  seq_num = 0;
62  ptu.getParam("asr_flir_ptu_gui/path_prediction", use_path_prediction);
63 }
64 
66  jointStatePublisher = ptu.advertise<asr_flir_ptu_driver::State>("asr_flir_ptu_driver/state_cmd", 1);
67  jointStateSubscriber = ptu.subscribe<asr_flir_ptu_driver::State>("asr_flir_ptu_driver/ptu_state", 1, &PTU_GUI::onStateCommand, this);
68  updater = ptu.serviceClient<std_srvs::Empty>("asr_flir_ptu_driver/update_settings");
69 
71 
72  panSlider->SetValue(0);
73  panSpinner->SetValue(0);
74  tiltSlider->SetValue(0);
75  tiltSpinner->SetValue(0);
76 }
77 
79 {
80  int panBase = 0, panSpeed = 0, panUpper = 0, panAccel = 0;
81  int tiltBase = 0, tiltSpeed = 0, tiltUpper = 0, tiltAccel = 0;
82  int panHold = 0, panMove = 0;
83  int tiltHold = 0, tiltMove = 0;
84  double panMin = 0, panMax = 0;
85  double tiltMin = 0, tiltMax = 0;
86 
87  ptu.getParam("asr_flir_ptu_driver/pan_min_angle", panMin);
88  ptu.getParam("asr_flir_ptu_driver/pan_max_angle", panMax);
89  ptu.getParam("asr_flir_ptu_driver/pan_base_speed", panBase);
90  ptu.getParam("asr_flir_ptu_driver/pan_target_speed", panSpeed);
91  ptu.getParam("asr_flir_ptu_driver/pan_upper_speed", panUpper);
92  ptu.getParam("asr_flir_ptu_driver/pan_accel", panAccel);
93  ptu.getParam("asr_flir_ptu_driver/pan_hold", panHold);
94  ptu.getParam("asr_flir_ptu_driver/pan_move", panMove);
95  ptu.getParam("asr_flir_ptu_driver/tilt_min_angle", tiltMin);
96  ptu.getParam("asr_flir_ptu_driver/tilt_max_angle", tiltMax);
97  ptu.getParam("asr_flir_ptu_driver/tilt_base_speed", tiltBase);
98  ptu.getParam("asr_flir_ptu_driver/tilt_target_speed", tiltSpeed);
99  ptu.getParam("asr_flir_ptu_driver/tilt_upper_speed", tiltUpper);
100  ptu.getParam("asr_flir_ptu_driver/tilt_accel", tiltAccel);
101  ptu.getParam("asr_flir_ptu_driver/tilt_hold", tiltHold);
102  ptu.getParam("asr_flir_ptu_driver/tilt_move", tiltMove);
103 
104  pan_min->SetValue((int) panMin);
105  pan_max->SetValue((int) panMax);
106  pan_base->SetValue(panBase);
107  pan_target->SetValue(panSpeed);
108  pan_upper->SetValue(panUpper);
109  pan_accel->SetValue(panAccel);
110  pan_hold->SetValue(panHold);
111  pan_move->SetValue(panMove);
112 
113  tilt_min->SetValue((int) tiltMin);
114  tilt_max->SetValue((int) tiltMax);
115  tilt_base->SetValue(tiltBase);
116  tilt_target->SetValue(tiltSpeed);
117  tilt_upper->SetValue(tiltUpper);
118  tilt_accel->SetValue(tiltAccel);
119  tilt_hold->SetValue(tiltHold);
120  tilt_move->SetValue(tiltMove);
121 }
122 
123 
126  ros::master::getTopics(topics);
127  ros::master::V_TopicInfo::iterator it = topics.begin();
128  ros::master::V_TopicInfo::iterator end = topics.end();
129 
130  int topicCount = 0;
131 
132  for (; it != end; ++it) {
133  const ros::master::TopicInfo& ti = *it;
134 
135  if (strcmp(ti.datatype.c_str(), "sensor_msgs/Image") == 0) {
136  leftImageTopic->Append(wxString::FromUTF8(ti.name.c_str()));
137  rightImageTopic->Append(wxString::FromUTF8(ti.name.c_str()));
138  topicCount++;
139  } else if (strcmp(ti.datatype.c_str(), "asr_flir_ptu_driver/State") == 0 &&
140  ti.name.find("state") != std::string::npos) {
141  wxString entryName = wxString::FromUTF8(ti.name.substr(0, ti.name.find("state")).c_str());
142  if (ptuChoice->FindString(entryName) == wxNOT_FOUND) ptuChoice->Append(entryName);
143  }
144  }
145 
146  //these lines assume that the combo boxes are sorted, which they aren't
147  for (long int i = 0; i < topicCount; i++) {
148  if (leftImageTopic->GetString(i).Find(wxString::FromUTF8(DEFAULT_PREFFEREDLEFT)) >= 0) {
149  leftImageTopic->SetSelection(i);
150  break;
151  }
152  }
153 
154  for (long int i = 0; i < topicCount; i++) {
155  if (rightImageTopic->GetString(i).Find(wxString::FromUTF8(DEFAULT_PREFFEREDRIGHT)) >= 0) {
156  rightImageTopic->SetSelection(i);
157  break;
158  }
159  }
160 }
161 
162 void PTU_GUI::OnDialogClose( wxCloseEvent& event ) {
163  //setting the checkbox seems to suppress the bug where the cam tries to turn around 180°
164  immCheck->SetValue(false);
165  wxTheApp->Exit();
166 }
167 
168 void PTU_GUI::OnLeftTopicChoice( wxCommandEvent& event ) {
170  std::string str = std::string(leftImageTopic->GetStringSelection().mb_str());
171  leftImageSubscriber = nh.subscribe<sensor_msgs::Image>(str, 1, &PTU_GUI::onLeftImage, this);
172 }
173 
174 void PTU_GUI::OnRightTopicChoice( wxCommandEvent& event ) {
176  std::string str = std::string(rightImageTopic->GetStringSelection().mb_str());
177  rightImageSubscriber = nh.subscribe<sensor_msgs::Image>(str, 1, &PTU_GUI::onRightImage, this);
178 }
179 
180 void PTU_GUI::OnPTUChoice( wxCommandEvent& event ) {
181  std::string new_ptu_name = std::string(ptuChoice->GetStringSelection().mb_str());
182  if (new_ptu_name.size() == 0) return;
183  else ptu_name = new_ptu_name;
184  updatePTUInfo();
185 }
186 
187 void PTU_GUI::OnPanScroll( wxScrollEvent& event ) {
188  panSpinner->SetValue(panSlider->GetValue());
189 }
190 
191 void PTU_GUI::OnPanSpin( wxSpinEvent& event ) {
192  panSlider->SetValue(panSpinner->GetValue());
193 }
194 
195 void PTU_GUI::OnPanSpinText( wxCommandEvent& event ) {
196  panSlider->SetValue(panSpinner->GetValue());
197 }
198 
199 void PTU_GUI::OnTiltScroll( wxScrollEvent& event ) {
200  tiltSpinner->SetValue(tiltSlider->GetValue());
201 }
202 
203 void PTU_GUI::OnTiltSpin( wxSpinEvent& event ) {
204  tiltSlider->SetValue(tiltSpinner->GetValue());
205 }
206 
207 void PTU_GUI::OnTiltSpinText( wxCommandEvent& event ) {
208  tiltSlider->SetValue(tiltSpinner->GetValue());
209 }
210 
211 void PTU_GUI::OnImmChecked( wxCommandEvent& event ) {
212  immUpdate = immCheck->IsChecked();
213 }
214 
215 void PTU_GUI::OnListenChecked( wxCommandEvent& event ) {
216  listenForUpdates = listenCheck->IsChecked();
217  if (listenForUpdates) immCheck->SetValue(false);
218  immCheck->Enable(!listenForUpdates);
219  updateButton->Enable(!listenForUpdates);
220 }
221 
222 void PTU_GUI::OnUpdateClicked( wxCommandEvent& event ) {
223  if (ptu_name.size() == 0) return;
224  ptu.setParam("asr_flir_ptu_driver/pan_min_angle", (double) pan_min->GetValue());
225  ptu.setParam("asr_flir_ptu_driver/pan_max_angle", (double) pan_max->GetValue());
226  ptu.setParam("asr_flir_ptu_driver/pan_base_speed", pan_base->GetValue());
227  ptu.setParam("asr_flir_ptu_driver/pan_target_speed", pan_target->GetValue());
228  ptu.setParam("asr_flir_ptu_driver/pan_upper_speed", pan_upper->GetValue());
229  ptu.setParam("asr_flir_ptu_driver/pan_accel", pan_accel->GetValue());
230  ptu.setParam("asr_flir_ptu_driver/pan_hold", pan_hold->GetValue());
231  ptu.setParam("asr_flir_ptu_driver/pan_move", pan_move->GetValue());
232  ptu.setParam("asr_flir_ptu_driver/tilt_min_angle", (double) tilt_min->GetValue());
233  ptu.setParam("asr_flir_ptu_driver/tilt_max_angle", (double) tilt_max->GetValue());
234  ptu.setParam("asr_flir_ptu_driver/tilt_base_speed", tilt_base->GetValue());
235  ptu.setParam("asr_flir_ptu_driver/tilt_target_speed", tilt_target->GetValue());
236  ptu.setParam("asr_flir_ptu_driver/tilt_upper_speed", tilt_upper->GetValue());
237  ptu.setParam("asr_flir_ptu_driver/tilt_accel", tilt_accel->GetValue());
238  ptu.setParam("asr_flir_ptu_driver/tilt_hold", tilt_hold->GetValue());
239  ptu.setParam("asr_flir_ptu_driver/tilt_move", tilt_move->GetValue());
240  std_srvs::Empty empty;
241  updater.call(empty);
242 
243  //############### Überprüfen ob funktioniert, z.B. indem man Wert übergibt der außerhalb der phys. Limits liegt ###############################
245  //#############################################################################################################################################
246 
247  if (!immUpdate) {
248  sensor_msgs::JointState joint_state;
249  asr_flir_ptu_driver::State msg;
250  if(use_path_prediction) {
251  asr_flir_ptu_driver::Predict end_point_prediction;
252  end_point_prediction.request.pan = panSlider->GetValue();
253  end_point_prediction.request.tilt = tiltSlider->GetValue();
254  predict_client.call(end_point_prediction);
255  joint_state = createJointCommand(end_point_prediction.response.new_pan, end_point_prediction.response.new_tilt, 0, 0);
256  msg.no_check_forbidden_area = true;
257  }
258  else {
259  joint_state = createJointCommand(panSlider->GetValue(), tiltSlider->GetValue(), 0, 0);
260  msg.no_check_forbidden_area = false;
261  }
262  msg.state = joint_state;
263  seq_num++;
264  msg.seq_num = seq_num;
266  }
267 
268 }
269 
270 void PTU_GUI::onStateCommand(const asr_flir_ptu_driver::State::ConstPtr& msg) {
271  if (!listenForUpdates) return;
272 
273  panSpinner->SetValue(msg->state.position[0]);
274  OnPanSpin(((wxSpinEvent&)wxEVT_NULL));
275  tiltSpinner->SetValue(msg->state.position[1]);
276  OnTiltSpin(((wxSpinEvent&)wxEVT_NULL));
277 }
278 
279 void PTU_GUI::onUpdate(wxTimerEvent& evt) {
280  if (immUpdate) {
281  sensor_msgs::JointState joint_state = createJointCommand(panSlider->GetValue(), tiltSlider->GetValue(), 0, 0);
282  asr_flir_ptu_driver::State msg;
283  msg.state = joint_state;
284  seq_num++;
285  msg.seq_num = seq_num;
287  }
288 
289  ros::spinOnce();
290  if (!ros::ok())
291  {
292  Close();
293  }
294 }
295 
296 wxBitmap* PTU_GUI::createBitmap(const sensor_msgs::Image::ConstPtr& msg) {
297  //not a particularly good approach; ignores encoding/image size/etc.
298 // wxImage* img = new wxImage(msg->width / 2, msg->height / 2);
299  int sizeMod = msg->width / 640;
300  wxImage img(msg->width / sizeMod, msg->height / sizeMod);
301  int bpp = msg->step / msg->width;
302 
303  for (int i = 0; i < img.GetHeight(); i++) {
304  for (int j = 0; j < img.GetWidth(); j++) {
305  img.SetRGB(j, i,
306  msg->data[sizeMod * i * msg->width * bpp + sizeMod * bpp * j + 2],
307  msg->data[sizeMod * i * msg->width * bpp + sizeMod * bpp * j + 1],
308  msg->data[sizeMod * i * msg->width * bpp + sizeMod * bpp * j]);
309  }
310  }
311 
312  wxBitmap* image = new wxBitmap(img);
313  return image;
314 }
315 
316 void PTU_GUI::onLeftImage(const sensor_msgs::Image::ConstPtr& msg) {
318  leftPanel->paintNow();
319 }
320 
321 void PTU_GUI::onRightImage(const sensor_msgs::Image::ConstPtr& msg) {
323  rightPanel->paintNow();
324 }
325 
326 sensor_msgs::JointState PTU_GUI::createJointCommand(double pan, double tilt, double panSpeed, double tiltSpeed) {
327  sensor_msgs::JointState joint_state;
328  joint_state.header.stamp = ros::Time::now();
329  joint_state.name.push_back("pan");
330  joint_state.position.push_back(pan);
331  joint_state.velocity.push_back(panSpeed);
332  joint_state.name.push_back("tilt");
333  joint_state.position.push_back(tilt);
334  joint_state.velocity.push_back(tiltSpeed);
335  return joint_state;
336 }
337 
338 }
ros::NodeHandle nh
Definition: PTU_GUI.h:46
wxTimer * update_timer
Definition: PTU_GUI.h:54
void OnTiltSpinText(wxCommandEvent &event)
Definition: PTU_GUI.cpp:207
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
wxImagePanel * rightPanel
Definition: GUI.h:55
std::string ptu_name
Definition: PTU_GUI.h:55
wxSlider * panSlider
Definition: GUI.h:59
bool use_path_prediction
Definition: PTU_GUI.h:59
ros::ServiceClient predict_client
Definition: PTU_GUI.h:53
ros::ServiceClient updater
Definition: PTU_GUI.h:52
void publish(const boost::shared_ptr< M > &message) const
void OnPanScroll(wxScrollEvent &event)
Definition: PTU_GUI.cpp:187
wxSpinCtrl * tilt_min
Definition: GUI.h:71
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void onLeftImage(const sensor_msgs::Image::ConstPtr &msg)
Definition: PTU_GUI.cpp:316
bool call(MReq &req, MRes &res)
PTU_GUI(wxWindow *parent)
Definition: PTU_GUI.cpp:12
wxSpinCtrl * pan_hold
Definition: GUI.h:84
void OnPanSpinText(wxCommandEvent &event)
Definition: PTU_GUI.cpp:195
ROSCPP_DECL bool getTopics(V_TopicInfo &topics)
void OnTiltScroll(wxScrollEvent &event)
Definition: PTU_GUI.cpp:199
void OnListenChecked(wxCommandEvent &event)
Definition: PTU_GUI.cpp:215
wxSpinCtrl * tilt_hold
Definition: GUI.h:96
wxImagePanel * leftPanel
Definition: GUI.h:54
wxSpinCtrl * tilt_target
Definition: GUI.h:90
void OnLeftTopicChoice(wxCommandEvent &event)
Definition: PTU_GUI.cpp:168
std::vector< TopicInfo > V_TopicInfo
wxSpinCtrl * pan_move
Definition: GUI.h:86
wxSpinCtrl * tilt_accel
Definition: GUI.h:94
bool immUpdate
Definition: PTU_GUI.h:56
#define DEFAULT_PREFFEREDRIGHT
Definition: PTU_GUI.h:26
wxSpinCtrl * tilt_upper
Definition: GUI.h:92
ros::Subscriber jointStateSubscriber
Definition: PTU_GUI.h:49
wxSpinCtrl * pan_max
Definition: GUI.h:65
wxSpinCtrl * pan_min
Definition: GUI.h:63
void onRightImage(const sensor_msgs::Image::ConstPtr &msg)
Definition: PTU_GUI.cpp:321
wxButton * updateButton
Definition: GUI.h:101
bool listenForUpdates
Definition: PTU_GUI.h:57
wxSpinCtrl * panSpinner
Definition: GUI.h:60
void OnPanSpin(wxSpinEvent &event)
Definition: PTU_GUI.cpp:191
#define DEFAULT_PREFFEREDLEFT
Definition: PTU_GUI.h:25
ros::Subscriber rightImageSubscriber
Definition: PTU_GUI.h:51
ROSCPP_DECL bool ok()
ros::Subscriber leftImageSubscriber
Definition: PTU_GUI.h:50
void onStateCommand(const asr_flir_ptu_driver::State::ConstPtr &msg)
Definition: PTU_GUI.cpp:270
wxSpinCtrl * tilt_move
Definition: GUI.h:98
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
wxComboBox * ptuChoice
Definition: GUI.h:57
void setImage(wxBitmap *image)
wxBitmap * createBitmap(const sensor_msgs::Image::ConstPtr &msg)
Definition: PTU_GUI.cpp:296
void OnTiltSpin(wxSpinEvent &event)
Definition: PTU_GUI.cpp:203
sensor_msgs::JointState createJointCommand(double pan, double tilt, double panSpeed, double tiltSpeed)
Definition: PTU_GUI.cpp:326
void OnImmChecked(wxCommandEvent &event)
Definition: PTU_GUI.cpp:211
Definition: GUI.h:41
ros::Publisher jointStatePublisher
Definition: PTU_GUI.h:48
wxSpinCtrl * pan_base
Definition: GUI.h:76
ros::NodeHandle ptu
Definition: PTU_GUI.h:47
bool getParam(const std::string &key, std::string &s) const
void OnRightTopicChoice(wxCommandEvent &event)
Definition: PTU_GUI.cpp:174
wxComboBox * leftImageTopic
Definition: GUI.h:52
wxSpinCtrl * pan_target
Definition: GUI.h:78
wxCheckBox * immCheck
Definition: GUI.h:99
void updatePTUInfo()
Definition: PTU_GUI.cpp:65
wxSpinCtrl * tiltSpinner
Definition: GUI.h:68
static Time now()
wxSlider * tiltSlider
Definition: GUI.h:67
wxSpinCtrl * tilt_max
Definition: GUI.h:73
void OnPTUChoice(wxCommandEvent &event)
Definition: PTU_GUI.cpp:180
wxSpinCtrl * tilt_base
Definition: GUI.h:88
void OnDialogClose(wxCloseEvent &event)
Definition: PTU_GUI.cpp:162
void OnUpdateClicked(wxCommandEvent &event)
Definition: PTU_GUI.cpp:222
wxComboBox * rightImageTopic
Definition: GUI.h:53
void onUpdate(wxTimerEvent &evt)
Definition: PTU_GUI.cpp:279
ROSCPP_DECL void spinOnce()
wxSpinCtrl * pan_accel
Definition: GUI.h:82
void updateSlidersFromParamServer()
Definition: PTU_GUI.cpp:78
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
Class GUIDialog.
Definition: GUI.h:46
wxCheckBox * listenCheck
Definition: GUI.h:100
wxSpinCtrl * pan_upper
Definition: GUI.h:80
void initTopicList()
Definition: PTU_GUI.cpp:124


asr_flir_ptu_driver
Author(s): Valerij Wittenbeck, Joachim Gehrung, Pascal Meißner, Patrick Schlosser
autogenerated on Mon Dec 2 2019 03:15:17