gui_task_widget.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #include "gui_task_widget.hh"
18 
19 #include <tf/tf.h>
20 #include <math.h>
21 
22 #include <gazebo/msgs/msgs.hh>
23 
24 #include <sstream>
25 
26 using namespace gazebo;
27 
28 // Register this plugin with the simulator
29 GZ_REGISTER_GUI_PLUGIN(GUITaskWidget)
30 
33  : GUIPlugin(),
34  // setup pixmap and painter for wind compass
36  windPixmap(150, 150), windPainter(&(this->windPixmap)),
37  // setup pixmap and painter for contact
38  contactPixmap(150, 150), contactPainter(&(this->contactPixmap)),
39  // set the time for most recent contact
40  contactTime(ros::Time::now())
41 {
42 #if GAZEBO_MAJOR_VERSION >= 8
43  // initialize ros if that hasnt happened yet
44  if (!ros::isInitialized())
45  {
46  int argc = 0;
47  char** argv = NULL;
48  ros::init(argc, argv, "gazebo", ros::init_options::NoSigintHandler |
50  }
51 
52  // Set the frame background and foreground colors
53  this->setStyleSheet(
54  "QFrame { background-color : rgba(100, 100, 100, 255); color : white; }");
55 
56  // Create the main layout
57  QHBoxLayout *mainLayout = new QHBoxLayout;
58  mainLayout->setContentsMargins(0, 0, 0, 0);
59 
60  // Create the frame to hold all the widgets
61  QFrame *mainFrame = new QFrame();
62  // Create the layout that sits inside the frame
63  QHBoxLayout *InfoLayout = new QHBoxLayout();
64  InfoLayout->setContentsMargins(0, 0, 0, 0);
65 
66  // Task info Block
67  // Create a time label
68  QLabel *taskInfo = new QLabel;
69  // Add the label to the frame's layout
70  InfoLayout->addWidget(taskInfo);
71  connect(this, SIGNAL(SetTaskInfo(QString)),
72  taskInfo, SLOT(setText(QString)), Qt::QueuedConnection);
73 
74  // Wind direction block
75  // Create a time label
76  QLabel *windDirection = new QLabel;
77  // Add the label to the frame's layout
78  InfoLayout->addWidget(windDirection);
79  connect(this, SIGNAL(SetWindDirection(QPixmap)),
80  windDirection, SLOT(setPixmap(QPixmap)), Qt::QueuedConnection);
81 
82  // Contact block
83  // Create a time label
84  QLabel *contact = new QLabel;
85  // Add the label to the frame's layout
86  InfoLayout->addWidget(contact);
87  connect(this, SIGNAL(SetContact(QPixmap)),
88  contact, SLOT(setPixmap(QPixmap)), Qt::QueuedConnection);
89 
90  // Add frameLayout to the frame
91  mainFrame->setLayout(InfoLayout);
92  // Add the frame to the main layout
93  mainLayout->addWidget(mainFrame);
94 
95  // set position and resize this widget
96  this->setLayout(mainLayout);
97  this->move(10, 10);
98  this->resize(600, 170);
99 
100  this->node.reset(new ros::NodeHandle);
101  // Subscribe to tasks topic (ROS)
102  this->taskSub = this->node->subscribe("/vrx/task/info", 1,
104  // Subscribe to wind speed topic (ROS)
105  this->windSpeedSub = this->node->subscribe("/vrx/debug/wind/speed", 1,
107  // Subscribe to wind direction topic (ROS)
108  this->windDirectionSub = this->node->subscribe("/vrx/debug/wind/direction", 1,
110  // Subscribe to link states topic (ROS)
111  this->linkStateSub = this->node->subscribe("/gazebo/link_states", 1,
113  // Subscribe to contact topic (ROS)
114  this->contactSub = this->node->subscribe("/vrx/debug/contact", 1,
115  &GUITaskWidget::OnContact, this);
116 #endif
117 }
118 
121 {
122 }
123 
125 void GUITaskWidget::OnContact(const vrx_gazebo::Contact::ConstPtr &_msg)
126 {
127  // put red over anything preivously present
128  // as to write on a red back ground
129  this->contactPixmap.fill(Qt::red);
130  this->contactPainter.setBrush(Qt::NoBrush);
131  this->pen.setColor(Qt::black);
132  this->pen.setWidth(10);
133  this->contactPainter.setPen(this->pen);
134  // Write Contact
135  this->contactPainter.drawText(QPoint(10, 15), QString("CONTACT WITH:"));
136  // Write what the wamv is in contact with
137  this->contactPainter.drawText(QPoint(10, 30),
138  QString::fromStdString(_msg->collision2));
139  // update time of last contact
140  this->contactTime = ros::Time::now();
141  // send pixmap to gzserver
142  this->SetContact(this->contactPixmap);
143 }
145 void GUITaskWidget::OnLinkStates(const gazebo_msgs::LinkStates::ConstPtr &_msg)
146 {
147  // find wamv base_link in all the links reported
148  unsigned int c = 0;
149  for (auto& i : _msg->name)
150  {
151  if (i == "wamv::base_link")
152  break;
153  ++c;
154  }
155  // get yaw (heading) of wamv
156  tf::Quaternion q(_msg->pose[c].orientation.x,
157  _msg->pose[c].orientation.y,
158  _msg->pose[c].orientation.z,
159  _msg->pose[c].orientation.w);
160  tf::Matrix3x3 m(q);
161  double roll, pitch;
162  // update wamvHeading
163  m.getRPY(roll, pitch, this->wamvHeading);
164  // if the last collision was greater than 1 sec ago,
165  // make contact widget all gray
166  if ((ros::Time::now() - this->contactTime) > ros::Duration(1))
167  {
168  this->contactPixmap.fill(Qt::gray);
169  this->SetContact(this->contactPixmap);
170  }
171 }
172 
174 void GUITaskWidget::OnWindDirection(const std_msgs::Float64::ConstPtr &_msg)
175 {
176  // draw gray background
177  this->windPixmap.fill(Qt::gray);
178  // draw black circle
179  this->windPainter.setBrush(Qt::NoBrush);
180  this->pen.setColor(Qt::black);
181  this->pen.setWidth(10);
182  this->windPainter.setPen(this->pen);
183  this->windPainter.drawEllipse(5, 5, 140, 140);
184  // draw the N for North
185  this->windPainter.setPen(Qt::red);
186  this->windPainter.drawText(QRect(71, -2, 20, 20), 0, tr("N"), nullptr);
187 
188  // draw the wind hand
189  const double pi = 3.14159265;
190  double scale = .3*(pow(this->windSpeed, 2));
191  double x = scale*cos((pi/-180)*(_msg->data)) + 75;
192  double y = scale*sin((pi/-180)*(_msg->data)) + 75;
193  this->pen.setWidth(10);
194  this->pen.setColor(Qt::red);
195  this->windPainter.setPen(this->pen);
196  this->windPainter.drawLine(QLine(75, 75, x, y));
197  // draw the wamv hand
198  this->pen.setWidth(6);
199  this->pen.setColor(Qt::blue);
200  this->windPainter.setPen(this->pen);
201  x = (-40*cos(this->wamvHeading + pi)) + 75;
202  y = (40*sin(this->wamvHeading + pi)) + 75;
203  this->windPainter.drawLine(QLine(75, 75, x, y));
204  // send the compass to gzserver
205  this->SetWindDirection(this->windPixmap);
206 }
207 
209 void GUITaskWidget::OnWindSpeed(const std_msgs::Float64::ConstPtr &_msg)
210 {
211  // update windSpeed
212  this->windSpeed = _msg->data;
213 }
214 
216 void GUITaskWidget::OnTaskInfo(const vrx_gazebo::Task::ConstPtr &_msg)
217 {
218  std::ostringstream taskInfoStream;
219  taskInfoStream.str("");
220  taskInfoStream << "Task Info:\n";
221  taskInfoStream << "Task Name: " << _msg->name << "\n";
222  taskInfoStream << "Task Phase: " << _msg->state << "\n";
223  taskInfoStream << "Ready Time: " <<
224  this->FormatTime(_msg->ready_time.toSec()) << "\n";
225  taskInfoStream << "Running Time: " <<
226  this->FormatTime(_msg->running_time.toSec()) << "\n";
227  taskInfoStream << "Elapsed Time: " <<
228  this->FormatTime(_msg->elapsed_time.toSec()) << "\n";
229  taskInfoStream << "Remaining Time: " <<
230  this->FormatTime(_msg->remaining_time.toSec()) << "\n";
231  taskInfoStream << "Timed out: ";
232  if (_msg->timed_out)
233  taskInfoStream << "true" << "\n";
234  else
235  taskInfoStream << "false" << "\n";
236  taskInfoStream << "Score: " << _msg->score << "\n";
237  this->SetTaskInfo(QString::fromStdString(taskInfoStream.str()));
238 }
239 
241 std::string GUITaskWidget::FormatTime(unsigned int sec) const
242 {
243  std::ostringstream stream;
244  unsigned int day, hour, min;
245 
246  stream.str("");
247 
248  day = sec / 86400;
249  sec -= day * 86400;
250 
251  hour = sec / 3600;
252  sec -= hour * 3600;
253 
254  min = sec / 60;
255  sec -= min * 60;
256 
257  stream << std::setw(2) << std::setfill('0') << day << " ";
258  stream << std::setw(2) << std::setfill('0') << hour << ":";
259  stream << std::setw(2) << std::setfill('0') << min << ":";
260  stream << std::setw(2) << std::setfill('0') << sec << ".";
261 
262  return stream.str();
263 }
264 
QPen pen
general use pen
void OnWindSpeed(const std_msgs::Float64::ConstPtr &_msg)
Callback that received wind speed messages.
~GUITaskWidget()
Destructor.
double windSpeed
last reported windspeed
void OnContact(const vrx_gazebo::Contact::ConstPtr &_msg)
Callback that receives Contact messages.
void OnLinkStates(const gazebo_msgs::LinkStates::ConstPtr &_msg)
Callback that receives link state messages.
ROSCPP_DECL bool isInitialized()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::string FormatTime(unsigned int sec) const
Helper function to format time string.
void OnWindDirection(const std_msgs::Float64::ConstPtr &_msg)
Callback that received wind direction messages.
float scale
Definition: pm_sampling.py:26
TFSIMD_FORCE_INLINE const tfScalar & y() const
QPainter windPainter
Paiter for painting on the compass.
double wamvHeading
last reported wamvHeading(yaw)
void SetContact(QPixmap _pixmap)
A signal used to set the contact widget.
void SetTaskInfo(QString _string)
A signal used to set the task info line edit.
void getRPY(tfScalar &roll, tfScalar &pitch, tfScalar &yaw, unsigned int solution_number=1) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
double min(double a, double b)
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
void SetWindDirection(QPixmap _pixmap)
A signal used to set the wind and wamv direction.
void OnTaskInfo(const vrx_gazebo::Task::ConstPtr &_msg)
Callback that received task info messages.
static Time now()
QPainter contactPainter
painter for contact widget
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
ros::Time contactTime
Last time contact occurred.


vrx_gazebo
Author(s): Brian Bingham , Carlos Aguero
autogenerated on Thu May 7 2020 03:54:56