qt_ros_slider.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the example classes of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
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 


pr2_gazebo
Author(s): John Hsu
autogenerated on Thu Jan 2 2014 11:45:25