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 <cmath>
28 
29 #include <VirtualRobot/Robot.h>
30 #include <VirtualRobot/RobotNodeSet.h>
31 
33 
35 
36 #include <RobotSkillTemplates/statecharts/ForceControlGroup/ForceControlGroupStatechartContext.generated.h>
37 
38 
39 using namespace armarx;
40 using namespace ForceControlGroup;
41 
42 // DO NOT EDIT NEXT LINE
43 ZeroForceControlVelocityCalcuation::SubClassRegistry ZeroForceControlVelocityCalcuation::Registry(
44  ZeroForceControlVelocityCalcuation::GetName(),
46 
48  XMLStateConstructorParams stateData) :
50  ZeroForceControlVelocityCalcuationGeneratedBase<ZeroForceControlVelocityCalcuation>(stateData)
51 {
52 }
53 
54 void
56 {
57  // put your user code for the enter-point here
58  // execution time should be short (<100ms)
59  setTimeoutEvent(in.gettimeout(), createEventTimeout());
60 
61 
62  ForceControlGroupStatechartContext* c = getContext<ForceControlGroupStatechartContext>();
63  std::string tcpName;
64 
65  if (in.istcpNameSet())
66  {
67  tcpName = in.gettcpName();
68  }
69  else
70  {
71  tcpName = c->getRobot()->getRobotNodeSet(in.getrobotNodeSetName())->getTCP()->getName();
72  }
73 
74  DatafieldRefPtr forceRef = in.getforceRef();
75  DatafieldRefPtr torqueRef = in.gettorqueRef();
76  // ARMARX_INFO << deactivateSpam(2) << "ForceRef: " << forceRef->getDataFieldIdentifier()->getIdentifierStr();
77  Literal update(forceRef->getDataFieldIdentifier(), "updated", Literal::createParameterList());
78  Literal update_torque(
79  torqueRef->getDataFieldIdentifier(), "updated", Literal::createParameterList());
80 
81  installConditionForSensorUpdate(update && update_torque);
82  auto now = TimeUtil::GetTime();
83  IceUtil::Time duration;
84 
85  if (in.istimestampSet())
86  {
87  duration = now - in.gettimestamp()->toTime();
88  }
89  else
90  {
91  duration = IceUtil::Time::milliSecondsDouble(0);
92  }
93 
94  //ARMARX_IMPORTANT << deactivateSpam(1) << "DURATION: " << duration.toMilliSecondsDouble();
95  std::string robotNodeSetName = in.getrobotNodeSetName();
96 
97 
98  //*************Force Calc****************
99  FramedDirectionPtr vel = in.getcurrentVelocity();
100  // FramedDirectionPtr curAcc = in.getcurrentAcc();
101  FramedDirectionPtr curForce = forceRef->getDataField()->get<FramedDirection>();
102  vel = FramedDirection::ChangeFrame(c->getRobot(), *vel, curForce->frame);
103  // ARMARX_CHECK_EXPRESSION(vel->frame == curAcc->frame);
104  ARMARX_CHECK_EXPRESSION(vel->frame == curForce->frame);
105  float forceThreshold = in.getforceThreshold();
106  //float maxSensitivity = 2.0f;
107 
108  Eigen::Vector3f newVel(3);
109  Eigen::Vector3f newAcc(3);
110  ARMARX_INFO << deactivateSpam(1) << "force magnitude: " << curForce->toEigen().norm();
111 
112  if (curForce->toEigen().norm() > forceThreshold)
113  {
114  newAcc = curForce->toEigen() * in.getAccGain();
115  }
116  else if (vel->toEigen().norm() > 0)
117  {
118  ARMARX_IMPORTANT << deactivateSpam(1) << "Deccelerating";
119  newAcc = -in.getDecGain() * vel->toEigen().normalized();
120  }
121  else
122  {
123  newAcc = Eigen::Vector3f::Zero();
124  }
125 
126  ARMARX_IMPORTANT << deactivateSpam(1) << "current curForce: " << curForce->output();
127  ARMARX_IMPORTANT << deactivateSpam(1) << "current velocity: " << vel->output();
128  newVel = vel->toEigen() + newAcc * duration.toMilliSecondsDouble() * 0.001;
129 
130  if (newVel.norm() > in.getmaxVelocity())
131  {
132  newVel = newVel.normalized() * in.getmaxVelocity();
133  }
134 
135  ARMARX_IMPORTANT << deactivateSpam(1) << "current newVel: " << newVel;
136  // vel->x = 0;
137  // vel->y = 0;
138 
139  //*************Torque Calc****************
140  FramedDirectionPtr velRot = in.getcurrentRotVelocity();
141  // FramedDirectionPtr curAcc = in.getcurrentRotAcc();
142  FramedDirectionPtr curTorque = torqueRef->getDataField()->get<FramedDirection>();
143  velRot = FramedDirection::ChangeFrame(c->getRobot(), *velRot, curTorque->frame);
144  // ARMARX_CHECK_EXPRESSION(vel->frame == curAcc->frame);
145  ARMARX_CHECK_EXPRESSION(velRot->frame == curTorque->frame);
146  float torqueThreshold = in.gettorqueThreshold();
147  //float maxSensitivity = 2.0f;
148 
149  Eigen::Vector3f newRotVel(3);
150  Eigen::Vector3f newRotAcc(3);
151 
152  //ARMARX_INFO << deactivateSpam(1) << "force magnitude: " << curToruqe->toEigen().norm();
153  if (curTorque->toEigen().norm() > torqueThreshold)
154  {
155  newRotAcc = curTorque->toEigen() * in.getRotAccGain();
156  }
157  else if (velRot->toEigen().norm() > 0)
158  {
159  //ARMARX_IMPORTANT << deactivateSpam(1) << "Deccelerating";
160  newRotAcc = -in.getRotDccGain() * velRot->toEigen().normalized();
161  }
162  else
163  {
164  newRotAcc = Eigen::Vector3f::Zero();
165  }
166 
167  //ARMARX_IMPORTANT << deactivateSpam(1) << "current curTorque: " << curTorque->output();
168  //ARMARX_IMPORTANT << deactivateSpam(1) << "current velocity: " << velRot->output();
169  Eigen::Vector3f velRotDelta = newRotAcc * duration.toMilliSecondsDouble() * 0.001;
170 
171  if (velRotDelta.norm() > in.getmaxRotAcc())
172  {
173  velRotDelta = velRotDelta.normalized() * in.getmaxRotAcc();
174  }
175 
176  newRotVel = velRot->toEigen() + velRotDelta;
177 
178  if (newRotVel.norm() > in.getmaxVelocity())
179  {
180  newRotVel = newRotVel.normalized() * in.getmaxRotVelocity();
181  }
182 
183  // ARMARX_IMPORTANT << deactivateSpam(1) << "current newVel: " << newRotVel;
184  // vel->x = 0;
185  // vel->y = 0;
186 
187 
188  //*********************Output**********************
189  out.settimestamp(new TimestampVariant(now));
190  //out.setcurrentSensitivity(in.getcurrentSensitivity());
191  out.setcurrentVelocity(new FramedDirection(newVel, vel->frame, vel->agent));
192  out.setcurrentRotVelocity(new FramedDirection(newRotVel, velRot->frame, velRot->agent));
193 
194 
195  FramedDirectionPtr newFramedVel = new FramedDirection(newVel, vel->frame, vel->agent);
196  FramedDirectionPtr newFramedRotVel =
197  new FramedDirection(newRotVel, velRot->frame, velRot->agent);
198 
199 
200  newFramedVel = FramedDirection::ChangeFrame(
201  c->getRobot(), *newFramedVel, c->getRobot()->getRootNode()->getName());
203  << "current newVel in platform: " << newFramedVel->output();
204  newFramedRotVel = FramedDirection::ChangeFrame(
205  c->getRobot(), *newFramedRotVel, c->getRobot()->getRootNode()->getName());
207  << "current newVel in platform: " << newFramedRotVel->output();
208 
209 
210  c->getTCPControlUnit()->setTCPVelocity(
211  robotNodeSetName, tcpName, newFramedVel, newFramedRotVel);
212 }
213 
214 void
216 {
217  // put your user code for the exit point here
218  // execution time should be short (<100ms)
219 }
220 
221 // DO NOT EDIT NEXT FUNCTION
224 {
226 }
armarx::FramedDirection::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: FramedPose.cpp:306
ZeroForceControlVelocityCalcuation.h
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::onEnter
void onEnter() override
Definition: ZeroForceControlVelocityCalcuation.cpp:55
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation
Definition: ZeroForceControlVelocityCalcuation.h:31
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::TimestampVariant
Definition: TimestampVariant.h:54
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:142
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: ZeroForceControlVelocityCalcuation.cpp:223
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::ZeroForceControlVelocityCalcuation
ZeroForceControlVelocityCalcuation(XMLStateConstructorParams stateData)
Definition: ZeroForceControlVelocityCalcuation.cpp:47
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< DatafieldRef >
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::FramedDirection::ChangeFrame
static FramedDirectionPtr ChangeFrame(const VirtualRobot::RobotPtr &robot, const FramedDirection &framedVec, const std::string &newFrame)
Definition: FramedPose.cpp:91
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
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:68
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
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:86
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:37
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:27
armarx::ForceControlGroup::ZeroForceControlVelocityCalcuation::onExit
void onExit() override
Definition: ZeroForceControlVelocityCalcuation.cpp:215