se2ez
jointpanel.cpp
Go to the documentation of this file.
1 /* Author: Constantinos Chamzas, Zachary Kingston */
2 
3 #include <se2ez/core/log.h>
4 #include <se2ez/core/io.h>
5 #include <se2ez/core/robot.h>
6 #include <se2ez/core/state.h>
7 #include <se2ez/core/frame.h>
8 #include <se2ez/core/ik.h>
10 
11 #include <se2ez/gui/renderarea.h>
12 #include <se2ez/gui/jointwidget.h>
13 #include <se2ez/gui/jointpanel.h>
14 
15 #include "ui_jointpanel.h"
16 
17 using namespace se2ez;
18 
19 gui::JointPanel::JointPanel(RobotPtr robot, std::string name, QWidget *parent)
20  : Panel(name, parent), ui_(new Ui::JointPanel), robot_(robot)
21 {
22  ui_->setupUi(this);
23 
24  // allocating space for state
25  jstate_ = std::make_shared<State>(robot_);
26 
27  for (const auto &name : robot_->getFrameNames())
28  {
29  const auto &data = robot_->getFrameDataConst(name);
30  if (data->movable)
31  ui_->ees->addItem(QString(name.c_str()));
32  }
33 
34  ui_->ees->setCurrentIndex(ui_->ees->count() - 1);
35 
36  cm_ = std::make_shared<Box2DCollisionManager>(robot_);
37  for (const auto &name : robot_->getFrameNames())
38  {
39  // We don't add jointWidgets for fixed joints
40  if (!(robot_->getFrame(name)->getJoint().type == Joint::FIXED))
41  {
42  auto jw = new gui::JointWidget(this, name, robot_, jstate_);
43 
44  ui_->vbox->insertWidget(0, jw);
45  connect(jw, SIGNAL(stateChanged()), this, SLOT(updateState()));
46 
47  joints_.emplace_back(jw);
48  }
49  };
50 
51  connect(ui_->reset, SIGNAL(clicked()), this, SLOT(reset()));
52  connect(ui_->random, SIGNAL(clicked()), this, SLOT(random()));
53  connect(ui_->randomCF, SIGNAL(clicked()), this, SLOT(randomCollisionFree()));
54  connect(ui_->frames, SIGNAL(stateChanged(int)), this, SLOT(updateState()));
55  connect(ui_->geometry, SIGNAL(stateChanged(int)), this, SLOT(updateState()));
56  connect(ui_->collide, SIGNAL(stateChanged(int)), this, SLOT(updateState()));
57  connect(ui_->sd, SIGNAL(stateChanged(int)), this, SLOT(updateState()));
58  connect(ui_->aware, SIGNAL(stateChanged(int)), this, SLOT(updateIK()));
59  connect(ui_->ees, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(updateIK()));
60 
61  updateIK();
62  reset();
63 }
64 
66 {
68 
69  ee_ = ui_->ees->currentText().toStdString();
70  if (ui_->aware->isChecked())
71  ik_ = std::make_shared<CollisionAwareChainIK>(robot_, ee_);
72  else
73  ik_ = std::make_shared<ChainIK>(robot_, ee_);
74 }
75 
77 {
79 
80  if (ui_->geometry->isChecked())
81  {
82  if (ui_->sd->isChecked())
83  minimum_ = cm_->distance(jstate_, distances_);
84 
85  else if (ui_->collide->isChecked())
86  cm_->collide(jstate_, colliding_);
87  }
88 
89  emit stateChanged();
90 }
91 
93 {
95 
96  jstate_->copy(state);
97  for (const auto &joint : joints_)
98  joint->fromState();
99 }
100 
102 {
104 
105  state->copy(jstate_);
106 }
107 
109 {
110  return robot_;
111 }
112 
114 {
115  return jstate_;
116 }
117 
119 {
120  return ee_;
121 }
122 
124 {
125  return robot_->getSignature();
126 }
127 
129 {
131 
132  for (const auto &joint : joints_)
133  joint->reset();
134 }
135 
137 {
139 
140  jstate_->setRandom(robot_);
141 
142  for (const auto &joint : joints_)
143  joint->fromState();
144 }
145 
147 {
149 
150  unsigned int tries = 0;
151  bool inCollision;
152 
153  do
154  {
155  jstate_->setRandom(robot_);
156  inCollision = cm_->collide(jstate_);
157  } while ((tries++ < 50) && inCollision);
158 
159  if (not inCollision)
160  for (const auto &joint : joints_)
161  joint->fromState();
162 }
163 
164 void gui::JointPanel::draw(QPainter &painter, gui::RenderArea *canvas)
165 {
167  jstate_->fk(robot_);
168 
169  auto pose = tf::flattenIsometry(jstate_->getPose(robot_, ee_));
170 
171  bool geo = ui_->geometry->isChecked();
172  if (geo)
173  {
174  if (ui_->sd->isChecked())
175  canvas->drawGeometry(painter, robot_, jstate_,
176  std::make_shared<RenderArea::SignedDistanceDrawer>(
177  ui_->collide->isChecked(), ui_->sd->checkState() == Qt::PartiallyChecked,
179  else
180  canvas->drawGeometry(painter, robot_, jstate_,
181  std::make_shared<RenderArea::CollideDrawer>(ui_->collide->isChecked(),
183  }
184 
185  bool frames = ui_->frames->isChecked();
186  if (frames)
187  canvas->drawAllFrames(painter, robot_, jstate_);
188 
189  if (geo || frames)
190  {
191  painter.save();
192  painter.resetTransform();
193 
194  int ax, ay;
195  canvas->toScreen(pose[0], pose[1], ax, ay);
196  painter.drawText(ax + 5, ay - 5, name);
197 
198  painter.restore();
199  }
200 }
201 
202 bool gui::JointPanel::click(QMouseEvent * /*event*/, double x, double y, int /*sx*/, int /*sy*/)
203 {
205 
206  const std::string &name = ui_->ees->currentText().toStdString();
207  if (ik_->solve(jstate_, {{name, tf::toIsometry(Eigen::Vector3d{x, y, 0})}}, {{name, IKSolver::POSITION}}))
208  for (const auto &joint : joints_)
209  joint->fromState();
210 
211  return true;
212 }
213 
214 void gui::JointPanel::update(boost::posix_time::ptime /*last*/, boost::posix_time::ptime /*current*/)
215 {
216 }
217 
219 {
220  delete ui_;
221 }
X- and Y-axis components.
Definition: ik.h:47
A shared pointer wrapper for se2ez::State.
Eigen::Vector3d flattenIsometry(const Eigen::Isometry2d &frame)
Converts a transformation frame into a vector [x, y, t] composed of a translation (x...
Definition: math.cpp:30
std::recursive_mutex mutex_
Definition: panel.h:47
Definition: cspacepanel.h:22
void setState(const StatePtr &state)
Definition: jointpanel.cpp:92
void update(boost::posix_time::ptime last, boost::posix_time::ptime current) override
Definition: jointpanel.cpp:214
std::set< std::string > colliding_
Definition: jointpanel.h:82
bool click(QMouseEvent *event, double x, double y, int sx, int sy) override
Definition: jointpanel.cpp:202
Fixed joint, just a rigid transformation.
Definition: joint.h:40
void drawGeometry(QPainter &painter, RobotPtr robot, StatePtr state, GeometryDrawerPtr drawer=nullptr)
Definition: renderarea.cpp:211
std::string getSignature()
Definition: jointpanel.cpp:123
const std::string & getEE()
Definition: jointpanel.cpp:118
void toScreen(double xin, double yin, int &xout, int &yout)
Definition: renderarea.cpp:431
CollisionManager::SignedDistance minimum_
Definition: jointpanel.h:83
CollisionManager::SignedDistanceMap distances_
Definition: jointpanel.h:84
JointPanel(RobotPtr robot, std::string name, QWidget *parent=0)
Definition: jointpanel.cpp:19
QString name
Definition: panel.h:45
std::vector< JointWidget * > joints_
Definition: jointpanel.h:86
void drawAllFrames(QPainter &painter, RobotPtr robot, StatePtr state)
Definition: renderarea.cpp:195
A shared pointer wrapper for se2ez::Robot.
T c_str(T... args)
Ui::JointPanel * ui_
Definition: jointpanel.h:74
CollisionManagerPtr cm_
Definition: jointpanel.h:81
void draw(QPainter &painter, RenderArea *canvas) override
Definition: jointpanel.cpp:164
The canvas widget. It contains all the drawing functions as well as all the general settings for draw...
Definition: renderarea.h:48
const RobotPtr & getRobot()
Definition: jointpanel.cpp:108
Main namespace.
Definition: collision.h:11
const StatePtr & getState()
Definition: jointpanel.cpp:113