jog_joint_panel.cpp
Go to the documentation of this file.
1 #include <boost/thread.hpp>
2 #include <rviz/config.h>
4 #include <ros/package.h>
6 #include <QVBoxLayout>
7 #include <QHBoxLayout>
8 #include <QSignalMapper>
9 #include <QTimer>
10 #include "jog_joint_panel.h"
11 #include "jog_msgs/JogJoint.h"
12 
13 namespace jog_controller
14 {
15 
17  : rviz::Panel(parent)
18 {
19  controller_name_ = "jog_joint_node";
20  // Parameters
21  ros::NodeHandle nh;
22 
23  nh.getParam("/" + controller_name_ + "/joint_names", joint_name_);
24  jog_joint_num_ = joint_name_.size();
25  jog_value_.resize(jog_joint_num_);
26 
27  //ros::param::param<std::vector<std::string> >(, joint_name_);
28 
29  // ROS_INFO_STREAM("joint_name_:" << joint_name_);
30 
31  QHBoxLayout* enable_layout = new QHBoxLayout;
32  enable_layout->addWidget( new QLabel( "Jog Enable:" ));
33  jog_button_ = new QPushButton("OFF");
34  jog_button_->setStyleSheet("QPushButton:checked { background-color: red; }\n");
35  jog_button_->setCheckable(true);
36  jog_button_->setChecked(false);
37  enable_layout->addWidget(jog_button_);
38 
39  QVBoxLayout* jog_layout = new QVBoxLayout;
40 
43  for (int i=0; i<jog_joint_num_; i++)
44  {
45  QHBoxLayout* layout = new QHBoxLayout;
46 
47  joint_label_[i] = new QLabel;
48  joint_label_[i]->setText(QString::fromStdString(joint_name_[i]));
49  layout->addWidget(joint_label_[i]);
50 
51  jog_slider_[i] = new JogSlider(Qt::Horizontal);
52  jog_slider_[i]->setTickPosition(QSlider::TicksBelow);
53  jog_slider_[i]->setTickInterval(500);
54  jog_slider_[i]->setMinimum(-1000);
55  jog_slider_[i]->setMaximum( 1000);
56  jog_slider_[i]->setTracking(true);
57  jog_slider_[i]->setSingleStep(0);
58  jog_slider_[i]->setPageStep(0);
59  // How can I change the slider style more properly??
60  jog_slider_[i]->setStyleSheet("QSlider::handle {"
61  "background: white;"
62  "border: 3px solid black;"
63  "width: 60px;"
64  "margin: -30px 0;"
65  "} "
66  "QSlider::sub-page {"
67  "background: rgb(164, 192, 2);"
68  "} "
69  "QSlider::add-page {"
70  "background: rgb(223, 70, 70);"
71  "} ");
72  layout->addWidget(jog_slider_[i]);
73 
74  jog_layout->addLayout(layout);
75  }
76 
77  QVBoxLayout* layout = new QVBoxLayout;
78  layout->addLayout(enable_layout);
79  layout->addLayout(jog_layout);
80  setLayout(layout);
81 
82  connect(jog_button_, SIGNAL(toggled(bool)), this, SLOT(respondEnable(bool)));
83 
84  // Slider
85  for (int i=0; i<jog_slider_.size(); i++)
86  {
87  connect(jog_slider_[i],
88  SIGNAL(sliderReleased()), jog_slider_[i],
89  SLOT(respondSliderReleased()));
90  }
91 
92  // Timer for publish
93  QTimer* output_timer = new QTimer( this );
94  connect( output_timer, SIGNAL( timeout() ), this, SLOT( publish() ));
95  output_timer->start(100);
96 
97  jog_joint_pub_ = nh.advertise<jog_msgs::JogJoint>( "jog_joint", 1);
98 }
99 
101 {
102  connect( vis_manager_, SIGNAL( preUpdate() ), this, SLOT( update() ));
103  updateJoint();
104 }
105 
107 {
108 }
109 
111 {
112 }
113 
115 {
116  // publish
117  jog_msgs::JogJoint msg;
118  msg.header.stamp = ros::Time::now();
119  msg.header.frame_id = frame_id_;
120 
121  msg.joint_names.resize(jog_joint_num_);
122  for (int i=0; i<jog_joint_num_; i++)
123  {
124  msg.joint_names[i] = joint_label_[i]->text().toUtf8().constData();
125  }
126 
127  msg.deltas.resize(jog_slider_.size());
128  for (int i=0; i<jog_slider_.size(); i++)
129  {
130  msg.deltas[i] = 0.1 * jog_slider_[i]->value() / jog_slider_[i]->maximum();
131  }
132  // Publish if the button is enabled
133  if(jog_button_->isChecked())
134  {
135  // Publish only if the all value are not equal zero
136  bool flag = false;
137  for (int i=0; i<jog_slider_.size(); i++)
138  {
139  if (jog_slider_[i]->value() != 0)
140  {
141  flag = true;
142  break;
143  }
144  }
145  if (flag)
146  {
147  jog_joint_pub_.publish(msg);
148  }
149  }
150 }
151 
153 {
154  if (checked)
155  {
156  jog_button_->setText("ON");
157  }
158  else
159  {
160  jog_button_->setText("OFF");
161  }
162 }
163 
165 {
166  rviz::Panel::save(config);
167 }
168 
170 {
171  rviz::Panel::load(config);
172 }
173 
175 {
176  QLineEdit* label = new QLineEdit;
177  label->setReadOnly( true );
178  return label;
179 }
180 
181 void JogJointPanel::fillNumericLabel( QLineEdit* label, double value )
182 {
183  label->setText( QString::number( value, 'f', 2 ));
184 }
185 
187 {
188  this->setValue(0);
189 }
190 
191 } // namespace jog_controller
192 
193 
msg
virtual void save(rviz::Config config) const
VisualizationManager * vis_manager_
void publish(const boost::shared_ptr< M > &message) const
TFSIMD_FORCE_INLINE void setValue(const tfScalar &x, const tfScalar &y, const tfScalar &z)
std::vector< QLabel * > joint_label_
PLUGINLIB_EXPORT_CLASS(test_moveit_controller_manager::TestMoveItControllerManager, moveit_controller_manager::MoveItControllerManager)
std::vector< JogSlider * > jog_slider_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
JogJointPanel(QWidget *parent=0)
bool getParam(const std::string &key, std::string &s) const
virtual void load(const rviz::Config &config)
static Time now()
std::vector< double > jog_value_
virtual void save(Config config) const
virtual void load(const Config &config)
void fillNumericLabel(QLineEdit *label, double value)
std::vector< std::string > joint_name_


jog_controller
Author(s): Ryosuke Tajima
autogenerated on Sun May 17 2020 03:25:01