ZeroForceControlVelocityCalcuation.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2014-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotSkillTemplates::ForceControlGroup
19  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
27 #include <RobotSkillTemplates/statecharts/ForceControlGroup/ForceControlGroupStatechartContext.generated.h>
28 
31 #include <VirtualRobot/Robot.h>
32 #include <VirtualRobot/RobotNodeSet.h>
33 #include <cmath>
34 
35 
36 using namespace armarx;
37 using namespace ForceControlGroup;
38 
39 // DO NOT EDIT NEXT LINE
40 ZeroForceControlVelocityCalcuation::SubClassRegistry ZeroForceControlVelocityCalcuation::Registry(ZeroForceControlVelocityCalcuation::GetName(), &ZeroForceControlVelocityCalcuation::CreateInstance);
41 
42 
43 
45  XMLStateTemplate<ZeroForceControlVelocityCalcuation>(stateData), ZeroForceControlVelocityCalcuationGeneratedBase<ZeroForceControlVelocityCalcuation>(stateData)
46 {
47 }
48 
50 {
51  // put your user code for the enter-point here
52  // execution time should be short (<100ms)
53  setTimeoutEvent(in.gettimeout(), createEventTimeout());
54 
55 
56 
57  ForceControlGroupStatechartContext* c = getContext<ForceControlGroupStatechartContext>();
58  std::string tcpName;
59 
60  if (in.istcpNameSet())
61  {
62  tcpName = in.gettcpName();
63  }
64  else
65  {
66  tcpName = c->getRobot()->getRobotNodeSet(in.getrobotNodeSetName())->getTCP()->getName();
67  }
68 
69  DatafieldRefPtr forceRef = in.getforceRef();
70  DatafieldRefPtr torqueRef = in.gettorqueRef();
71  // ARMARX_INFO << deactivateSpam(2) << "ForceRef: " << forceRef->getDataFieldIdentifier()->getIdentifierStr();
72  Literal update(forceRef->getDataFieldIdentifier(), "updated", Literal::createParameterList());
73  Literal update_torque(torqueRef->getDataFieldIdentifier(), "updated", Literal::createParameterList());
74 
75  installConditionForSensorUpdate(update && update_torque);
76  auto now = TimeUtil::GetTime();
77  IceUtil::Time duration;
78 
79  if (in.istimestampSet())
80  {
81  duration = now - in.gettimestamp()->toTime();
82  }
83  else
84  {
85  duration = IceUtil::Time::milliSecondsDouble(0);
86  }
87 
88  //ARMARX_IMPORTANT << deactivateSpam(1) << "DURATION: " << duration.toMilliSecondsDouble();
89  std::string robotNodeSetName = in.getrobotNodeSetName();
90 
91 
92  //*************Force Calc****************
93  FramedDirectionPtr vel = in.getcurrentVelocity();
94  // FramedDirectionPtr curAcc = in.getcurrentAcc();
95  FramedDirectionPtr curForce = forceRef->getDataField()->get<FramedDirection>();
96  vel = FramedDirection::ChangeFrame(c->getRobot(), *vel, curForce->frame);
97  // ARMARX_CHECK_EXPRESSION(vel->frame == curAcc->frame);
98  ARMARX_CHECK_EXPRESSION(vel->frame == curForce->frame);
99  float forceThreshold = in.getforceThreshold();
100  //float maxSensitivity = 2.0f;
101 
102  Eigen::Vector3f newVel(3);
103  Eigen::Vector3f newAcc(3);
104  ARMARX_INFO << deactivateSpam(1) << "force magnitude: " << curForce->toEigen().norm();
105 
106  if (curForce->toEigen().norm() > forceThreshold)
107  {
108  newAcc = curForce->toEigen() * in.getAccGain();
109  }
110  else if (vel->toEigen().norm() > 0)
111  {
112  ARMARX_IMPORTANT << deactivateSpam(1) << "Deccelerating";
113  newAcc = -in.getDecGain() * vel->toEigen().normalized();
114  }
115  else
116  {
117  newAcc = Eigen::Vector3f::Zero();
118  }
119 
120  ARMARX_IMPORTANT << deactivateSpam(1) << "current curForce: " << curForce->output();
121  ARMARX_IMPORTANT << deactivateSpam(1) << "current velocity: " << vel->output();
122  newVel = vel->toEigen() + newAcc * duration.toMilliSecondsDouble() * 0.001;
123 
124  if (newVel.norm() > in.getmaxVelocity())
125  {
126  newVel = newVel.normalized() * in.getmaxVelocity();
127  }
128 
129  ARMARX_IMPORTANT << deactivateSpam(1) << "current newVel: " << newVel;
130  // vel->x = 0;
131  // vel->y = 0;
132 
133  //*************Torque Calc****************
134  FramedDirectionPtr velRot = in.getcurrentRotVelocity();
135  // FramedDirectionPtr curAcc = in.getcurrentRotAcc();
136  FramedDirectionPtr curTorque = torqueRef->getDataField()->get<FramedDirection>();
137  velRot = FramedDirection::ChangeFrame(c->getRobot(), *velRot, curTorque->frame);
138  // ARMARX_CHECK_EXPRESSION(vel->frame == curAcc->frame);
139  ARMARX_CHECK_EXPRESSION(velRot->frame == curTorque->frame);
140  float torqueThreshold = in.gettorqueThreshold();
141  //float maxSensitivity = 2.0f;
142 
143  Eigen::Vector3f newRotVel(3);
144  Eigen::Vector3f newRotAcc(3);
145 
146  //ARMARX_INFO << deactivateSpam(1) << "force magnitude: " << curToruqe->toEigen().norm();
147  if (curTorque->toEigen().norm() > torqueThreshold)
148  {
149  newRotAcc = curTorque->toEigen() * in.getRotAccGain();
150  }
151  else if (velRot->toEigen().norm() > 0)
152  {
153  //ARMARX_IMPORTANT << deactivateSpam(1) << "Deccelerating";
154  newRotAcc = -in.getRotDccGain() * velRot->toEigen().normalized();
155  }
156  else
157  {
158  newRotAcc = Eigen::Vector3f::Zero();
159  }
160 
161  //ARMARX_IMPORTANT << deactivateSpam(1) << "current curTorque: " << curTorque->output();
162  //ARMARX_IMPORTANT << deactivateSpam(1) << "current velocity: " << velRot->output();
163  Eigen::Vector3f velRotDelta = newRotAcc * duration.toMilliSecondsDouble() * 0.001;
164 
165  if (velRotDelta.norm() > in.getmaxRotAcc())
166  {
167  velRotDelta = velRotDelta.normalized() * in.getmaxRotAcc();
168  }
169 
170  newRotVel = velRot->toEigen() + velRotDelta;
171 
172  if (newRotVel.norm() > in.getmaxVelocity())
173  {
174  newRotVel = newRotVel.normalized() * in.getmaxRotVelocity();
175  }
176 
177  // ARMARX_IMPORTANT << deactivateSpam(1) << "current newVel: " << newRotVel;
178  // vel->x = 0;
179  // vel->y = 0;
180 
181 
182 
183 
184  //*********************Output**********************
185  out.settimestamp(new TimestampVariant(now));
186  //out.setcurrentSensitivity(in.getcurrentSensitivity());
187  out.setcurrentVelocity(new FramedDirection(newVel, vel->frame, vel->agent));
188  out.setcurrentRotVelocity(new FramedDirection(newRotVel, velRot->frame, velRot->agent));
189 
190 
191  FramedDirectionPtr newFramedVel = new FramedDirection(newVel, vel->frame, vel->agent);
192  FramedDirectionPtr newFramedRotVel = new FramedDirection(newRotVel, velRot->frame, velRot->agent);
193 
194 
195  newFramedVel = FramedDirection::ChangeFrame(c->getRobot(), *newFramedVel, c->getRobot()->getRootNode()->getName());
196  ARMARX_IMPORTANT << deactivateSpam(1) << "current newVel in platform: " << newFramedVel->output();
197  newFramedRotVel = FramedDirection::ChangeFrame(c->getRobot(), *newFramedRotVel, c->getRobot()->getRootNode()->getName());
198  ARMARX_IMPORTANT << deactivateSpam(1) << "current newVel in platform: " << newFramedRotVel->output();
199 
200 
201  c->getTCPControlUnit()->setTCPVelocity(robotNodeSetName, tcpName, newFramedVel,
202  newFramedRotVel);
203 
204 
205 
206 }
207 
208 
210 {
211  // put your user code for the exit point here
212  // execution time should be short (<100ms)
213 
214 }
215 
216 
217 // DO NOT EDIT NEXT FUNCTION
219 {
221 }
222 
armarx::FramedDirection::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: FramedPose.cpp:265
ZeroForceControlVelocityCalcuation.h
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::onEnter
void onEnter() override
Definition: ZeroForceControlVelocityCalcuation.cpp:49
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation
Definition: ZeroForceControlVelocityCalcuation.h:31
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::TimestampVariant
Definition: TimestampVariant.h:54
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:129
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: ZeroForceControlVelocityCalcuation.cpp:218
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::ZeroForceControlVelocityCalcuation
ZeroForceControlVelocityCalcuation(XMLStateConstructorParams stateData)
Definition: ZeroForceControlVelocityCalcuation.cpp:44
IceInternal::Handle< DatafieldRef >
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::FramedDirection::ChangeFrame
static FramedDirectionPtr ChangeFrame(const VirtualRobot::RobotPtr &robot, const FramedDirection &framedVec, const std::string &newFrame)
Definition: FramedPose.cpp:86
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::Registry
static SubClassRegistry Registry
Definition: ZeroForceControlVelocityCalcuation.h:43
FramedPose.h
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
TimeUtil.h
armarx::FramedDirection
FramedDirection is a 3 dimensional direction vector with a reference frame. The reference frame can b...
Definition: FramedPose.h:83
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:38
armarx::Vector3::toEigen
virtual Eigen::Vector3f toEigen() const
Definition: Pose.cpp:134
armarx::Literal
Definition: Term.h:208
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::onExit
void onExit() override
Definition: ZeroForceControlVelocityCalcuation.cpp:209