Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <math.h>
00024 #include <QLabel>
00025 #include <QSlider>
00026 #include <QVBoxLayout>
00027
00028 #include <std_msgs/Float64.h>
00029
00030 #include "pr2_gazebo/qt_ros_slider.h"
00031
00032 QtRosSlider::QtRosSlider(const QString &text, QWidget *_parent)
00033 : QWidget(_parent)
00034 {
00035 this->ac = NULL;
00036 this->gripper_publishers.clear();
00037
00038 this->min_int = 0;
00039 this->max_int = 1000;
00040
00041 this->label = new QLabel();
00042 this->label->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
00043 this->label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00044
00045 setText(text);
00046 }
00047
00048 void QtRosSlider::initTorsoCommander()
00049 {
00050 this->lcd = new QLCDNumber(5);
00051 this->lcd->setSegmentStyle(QLCDNumber::Filled);
00052
00053 this->slider = new QSlider(Qt::Horizontal);
00054 this->slider->setRange(this->min_int, max_int);
00055
00056 connect(this->slider, SIGNAL(valueChanged(int)),
00057 this, SLOT(conversion(int)));
00058
00059 connect(this->slider, SIGNAL(valueChanged(int)),
00060 this, SIGNAL(valueChanged(int)));
00061
00062 connect(this->slider, SIGNAL(valueChanged(int)),
00063 this, SLOT(onValueChange(int)));
00064
00065 QVBoxLayout *layout = new QVBoxLayout;
00066 layout->addWidget(this->lcd);
00067 layout->addWidget(this->slider);
00068 layout->addWidget(this->label);
00069 setLayout(layout);
00070
00071 setFocusProxy(this->slider);
00072 }
00073
00074 void QtRosSlider::initGripperCommander()
00075 {
00076 this->lcd = new QLCDNumber(5);
00077 this->lcd->setSegmentStyle(QLCDNumber::Filled);
00078
00079 this->slider = new QSlider(Qt::Horizontal);
00080 this->slider->setRange(this->min_int, max_int);
00081
00082 connect(this->slider, SIGNAL(valueChanged(int)),
00083 this, SLOT(conversion(int)));
00084
00085 connect(this->slider, SIGNAL(valueChanged(int)),
00086 this, SIGNAL(valueChanged(int)));
00087
00088 connect(this->slider, SIGNAL(valueChanged(int)),
00089 this, SLOT(onValueChange(int)));
00090
00091 QVBoxLayout *layout = new QVBoxLayout;
00092 layout->addWidget(this->lcd);
00093 layout->addWidget(this->slider);
00094 layout->addWidget(this->label);
00095 setLayout(layout);
00096
00097 setFocusProxy(this->slider);
00098 }
00099
00100 void QtRosSlider::setCallback( actionlib::SimpleActionClient<pr2_controllers_msgs::SingleJointPositionAction> *_ac)
00101 {
00102 this->ac = _ac;
00103 this->initTorsoCommander();
00104 }
00105
00106 void QtRosSlider::addGripperPublisher(ros::Publisher _pub)
00107 {
00108 this->gripper_publishers.push_back(_pub);
00109 this->initGripperCommander();
00110 }
00111
00112 void QtRosSlider::onValueChange(int value)
00113 {
00114 if (this->ac)
00115 {
00116 pr2_controllers_msgs::SingleJointPositionGoal goal;
00117 goal.position = this->convert(value);
00118 ac->sendGoal(goal);
00119 }
00120
00121 if (!this->gripper_publishers.empty())
00122 {
00123 pr2_controllers_msgs::Pr2GripperCommand cmd;
00124 cmd.position = this->convert(value);
00125 cmd.max_effort = 100;
00126 for (std::vector<ros::Publisher>::iterator iter = this->gripper_publishers.begin();
00127 iter != this->gripper_publishers.end(); iter++)
00128 iter->publish(cmd);
00129 }
00130
00131 }
00132
00133 int QtRosSlider::value() const
00134 {
00135 return this->slider->value();
00136 }
00137
00138 double QtRosSlider::convert(int value)
00139 {
00140 return ((double)value - min_int)/(max_int - min_int) * (max_value - min_value) + min_value;
00141 }
00142
00143 void QtRosSlider::conversion(int value)
00144 {
00145 this->lcd->display(this->convert(value));
00146 }
00147
00148 QString QtRosSlider::text() const
00149 {
00150 return this->label->text();
00151 }
00152
00153 void QtRosSlider::setValue(double value)
00154 {
00155 int int_value = (int)((value - min_value) / (max_value - min_value) * (max_int - min_int) - min_int);
00156 this->slider->setValue(int_value);
00157 }
00158
00159 void QtRosSlider::setRange(int minValue, int maxValue)
00160 {
00161 if (minValue < 0 || maxValue > 999 || minValue > maxValue) {
00162 qWarning("QtRosSlider::setRange(%d, %d)\n"
00163 "\tRange must be 0..999\n"
00164 "\tand minValue must not be greater than maxValue",
00165 minValue, maxValue);
00166 return;
00167 }
00168 this->slider->setRange(minValue, maxValue);
00169 }
00170
00171 void QtRosSlider::setText(const QString &text)
00172 {
00173 this->label->setText(text);
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189