se2ez
state.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #include <iostream>
4 
5 #include <se2ez/core/math.h>
6 #include <se2ez/core/log.h>
7 #include <se2ez/core/robot.h>
8 #include <se2ez/core/state.h>
9 #include <se2ez/core/frame.h>
10 
11 using namespace se2ez;
12 
13 State::State(const Robot &robot)
14  : data(Eigen::VectorXd::Zero(robot.getNumJoints())), frames(std::make_shared<FrameMap>(robot))
15 {
16  // Setting the state to 0 could be out of bounds
17  for (const auto &pair : robot.getJoints())
18  {
19  const auto &frame = pair.second;
20  const auto &joint = frame->frame->getJoint();
21 
22  joint.enforceBounds(data);
23  }
24 }
25 
26 State::State(const RobotPtr &robot)
27  : data(Eigen::VectorXd::Zero(robot->getNumJoints())), frames(std::make_shared<FrameMap>(robot))
28 {
29  // Setting the state to 0 could be out of bounds
30  for (const auto &pair : robot->getJoints())
31  {
32  const auto &frame = pair.second;
33  const auto &joint = frame->frame->getJoint();
34 
35  joint.enforceBounds(data);
36  }
37 }
38 
39 void State::setJoint(const RobotPtr &robot, const std::string &name, double value)
40 {
41  robot->getFrameValues(name, data)[0] = value;
42  dirty = true;
43 }
44 
45 void State::setTranslateJoint(const RobotPtr &robot, const std::string &name, const Eigen::Vector2d &value)
46 {
47  robot->getFrameValues(name, data) = value;
48  dirty = true;
49 }
50 
51 void State::setFloatJoint(const RobotPtr &robot, const std::string &name, const Eigen::Isometry2d &value)
52 {
53  setFloatJoint(robot, name, tf::flattenIsometry(value));
54  dirty = true;
55 }
56 
57 void State::setFloatJoint(const RobotPtr &robot, const std::string &name, const Eigen::Vector3d &value)
58 {
59  robot->getFrameValues(name, data) = value;
60  dirty = true;
61 }
62 
63 void State::setRandom(const RobotPtr &robot)
64 {
65  for (const auto &fname : robot->getFrameNames())
66  {
67  const auto &frameData = robot->getFrameDataConst(fname);
68  frameData->frame->getJoint().setRandom(frameData->getValues(data));
69  }
70 
71  dirty = true;
72 }
73 
74 void State::copy(const StatePtr &other)
75 {
76  data = other->data;
77  dirty = true;
78 }
79 
80 void State::fk(const RobotPtr &robot)
81 {
82  if (!dirty)
83  return;
84 
85  frames->dirty();
86  robot->fk(data, frames);
87 
88  dirty = false;
89 }
90 
91 const Eigen::Isometry2d &State::getPose(const RobotPtr &robot, const std::string &frame)
92 {
93  if (dirty)
94  fk(robot);
95 
96  return frames->getEntryConst(frame).pose;
97 }
98 
99 const Eigen::MatrixXd &State::getJacobian(const RobotPtr &robot, const std::string &frame)
100 {
101  return robot->getJacobian(frame, data, frames).jacobian;
102 }
103 
105  const std::string &frame)
106 {
107  if (dirty)
108  fk(robot);
109 
110  return frames->getEntryConst(frame).geometry;
111 }
112 
114 {
115  return frames->printFrames(robot);
116 }
const tf::EigenVector< Eigen::Isometry2d > & getGeometryPoses(const RobotPtr &robot, const std::string &frame)
Get the poses of geometry attached to a frame of the robot. fk() must be called before calling this f...
Definition: state.cpp:104
FrameMapPtr frames
Frame poses.
Definition: state.h:141
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
const Eigen::Isometry2d & getPose(const RobotPtr &robot, const std::string &frame)
Get the pose of a frame of the robot. fk() must be called before calling this function.
Definition: state.cpp:91
void setTranslateJoint(const RobotPtr &robot, const std::string &name, const Eigen::Vector2d &value)
Sets the value of a frame with a translation joint.
Definition: state.cpp:45
std::string printFrames(const RobotPtr &robot) const
Print out the current value of all frames on the robot.
Definition: state.cpp:113
void copy(const StatePtr &other)
Copies the configuration from another state.
Definition: state.cpp:74
Map of frame name to a pair of that describes if the transform is "dirty" (needs updating) and its cu...
Definition: robot.h:39
A representation of a robot (in this case, a kinematic tree in the plane).
Definition: robot.h:119
bool dirty
Definition: state.h:139
void fk(const RobotPtr &robot)
Compute the forward kinematics for all frames in the robot.
Definition: state.cpp:80
State(const Robot &robot)
Constructor.
Definition: state.cpp:13
A shared pointer wrapper for se2ez::Robot.
const std::vector< std::pair< std::string, FrameDataPtr > > & getJoints() const
Get the ordered list of active joints in this robot with their associated frame information.
Definition: robot.cpp:508
Main namespace.
Definition: collision.h:11
const Eigen::MatrixXd & getJacobian(const RobotPtr &robot, const std::string &frame)
Get the jacobian of a frame of the robot.
Definition: state.cpp:99
void setRandom(const RobotPtr &robot)
Sets a random value to the state.
Definition: state.cpp:63
Eigen::VectorXd data
Configuration vector.
Definition: state.h:140
void setJoint(const RobotPtr &robot, const std::string &name, double value)
Sets the value of a frame with one joint parameter.
Definition: state.cpp:39
void setFloatJoint(const RobotPtr &robot, const std::string &name, const Eigen::Isometry2d &value)
Sets the value of a frame with a floating joint.
Definition: state.cpp:51