RcsPySim
A robot control and simulation library
OMJointState.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #define _USE_MATH_DEFINES
32 
33 #include "OMJointState.h"
34 #include "OMCombined.h"
35 
36 #include <Rcs_typedef.h>
37 #include <Rcs_joint.h>
38 #include <Rcs_math.h>
39 
40 #include <sstream>
41 #include <stdexcept>
42 #include <cmath>
43 
44 namespace Rcs
45 {
46 
47 
48 static bool defaultWrapJointAngle(RcsJoint* joint)
49 {
50  // Wrap if it models one full rotation
51  return RcsJoint_isRotation(joint) && joint->q_min == -M_PI && joint->q_max == M_PI;
52 }
53 
54 OMJointState::OMJointState(RcsGraph* graph, const char* jointName, bool wrapJointAngle) :
55  graph(graph), wrapJointAngle(wrapJointAngle)
56 {
57  joint = RcsGraph_getJointByName(graph, jointName);
58  if (!joint) {
59  std::ostringstream os;
60  os << "Unable to find joint " << jointName << " in graph.";
61  throw std::invalid_argument(os.str());
62  }
63  if (wrapJointAngle && !RcsJoint_isRotation(joint)) {
64  std::ostringstream os;
65  os << "Joint " << jointName << " is not a rotation joint, so we cannot wrap the joint angle.";
66  throw std::invalid_argument(os.str());
67  }
68 }
69 
70 OMJointState::OMJointState(RcsGraph* graph, const char* jointName) : OMJointState(graph, jointName, false)
71 {
73 }
74 
75 OMJointState::OMJointState(RcsGraph* graph, RcsJoint* joint) :
76  graph(graph),
77  joint(joint),
79 {
80 }
81 
83 {
84  // Nothing else to destroy
85 }
86 
87 
88 unsigned int OMJointState::getStateDim() const
89 {
90  return 1;
91 }
92 
93 unsigned int OMJointState::getVelocityDim() const
94 {
95  return 1;
96 }
97 
98 void OMJointState::computeObservation(double* state, double* velocity, const MatNd* currentAction, double dt) const
99 {
100  double q = graph->q->ele[joint->jointIndex];
101  if (wrapJointAngle) {
102  q = Math_fmodAngle(q);
103  }
104  *state = q;
105  *velocity = graph->q_dot->ele[joint->jointIndex];
106 }
107 
108 void OMJointState::getLimits(double* minState, double* maxState, double* maxVelocity) const
109 {
110  // Use joint limits from graph (in contrast to the other observation models)
111  *minState = joint->q_min;
112  *maxState = joint->q_max;
113  *maxVelocity = joint->speedLimit;
114 }
115 
116 std::vector<std::string> OMJointState::getStateNames() const
117 {
118  return {joint->name};
119 }
120 
122 {
123  auto combined = new OMCombined();
124  RCSGRAPH_TRAVERSE_JOINTS(graph) {
125  combined->addPart(new OMJointState(graph, JNT));
126  }
127  return combined;
128 }
129 
131 {
132  auto combined = new OMCombined();
133  RCSGRAPH_TRAVERSE_JOINTS(graph) {
134  if (!JNT->constrained) {
135  combined->addPart(new OMJointState(graph, JNT));
136  }
137  }
138  return combined;
139 }
140 
141 // Use the base class constructor
142 OMJointStatePositions::OMJointStatePositions(RcsGraph* graph, const char* jointName, bool wrapJointAngle) :
143  OMJointState(graph, jointName, wrapJointAngle) {}
144 
146 {
147  return 0;
148 }
149 
150 } /* namespace Rcs */
virtual unsigned int getVelocityDim() const
RcsGraph * graph
Definition: OMJointState.h:85
OMJointStatePositions(RcsGraph *graph, const char *jointName, bool wrapJointAngle)
static ObservationModel * observeUnconstrainedJoints(RcsGraph *graph)
virtual std::vector< std::string > getStateNames() const
static bool defaultWrapJointAngle(RcsJoint *joint)
static ObservationModel * observeAllJoints(RcsGraph *graph)
virtual unsigned int getStateDim() const
virtual void computeObservation(double *state, double *velocity, const MatNd *currentAction, double dt) const
RcsJoint * joint
Definition: OMJointState.h:87
virtual void getLimits(double *minState, double *maxState, double *maxVelocity) const
virtual ~OMJointState()
OMJointState(RcsGraph *graph, const char *jointName, bool wrapJointAngle)
virtual unsigned int getVelocityDim() const