Blender V4.3
MovingFrame.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Benoit Bolsee
2 *
3 * SPDX-License-Identifier: LGPL-2.1-or-later */
4
9#include "MovingFrame.hpp"
10#include <string.h>
11namespace iTaSC{
12
13static const unsigned int frameCacheSize = (sizeof(((Frame*)0)->p.data)+sizeof(((Frame*)0)->M.data))/sizeof(double);
14
16 m_function(NULL), m_param(NULL), m_velocity(), m_poseCCh(-1), m_poseCTs(0)
17{
18 m_internalPose = m_nextPose = frame;
19 initialize(6, 1);
20 e_matrix& Ju = m_JuArray[0];
21 Ju = e_identity_matrix(6,6);
22}
23
27
29{
31 return true;
32}
33
35{
36 m_cache = _cache;
37 m_poseCCh = -1;
38 if (m_cache) {
39 m_poseCCh = m_cache->addChannel(this,"pose",frameCacheSize*sizeof(double));
40 // don't store the initial pose, it's causing unnecessary large velocity on the first step
41 //pushInternalFrame(0);
42 }
43}
44
45void MovingFrame::pushInternalFrame(CacheTS timestamp)
46{
47 if (m_poseCCh >= 0) {
48 double buf[frameCacheSize];
49 memcpy(buf, m_internalPose.p.data, sizeof(m_internalPose.p.data));
50 memcpy(&buf[sizeof(m_internalPose.p.data)/sizeof(double)], m_internalPose.M.data, sizeof(m_internalPose.M.data));
51
52 m_cache->addCacheVectorIfDifferent(this, m_poseCCh, timestamp, buf, frameCacheSize, KDL::epsilon);
53 m_poseCTs = timestamp;
54 }
55}
56
57// load pose just preceding timestamp
58// return false if no cache position was found
59bool MovingFrame::popInternalFrame(CacheTS timestamp)
60{
61 if (m_poseCCh >= 0) {
62 char *item;
63 item = (char *)m_cache->getPreviousCacheItem(this, m_poseCCh, &timestamp);
64 if (item && m_poseCTs != timestamp) {
65 memcpy(m_internalPose.p.data, item, sizeof(m_internalPose.p.data));
66 item += sizeof(m_internalPose.p.data);
67 memcpy(m_internalPose.M.data, item, sizeof(m_internalPose.M.data));
68 m_poseCTs = timestamp;
69 // changing the starting pose, recompute the jacobian
71 }
72 return (item) ? true : false;
73 }
74 // in case of no cache, there is always a previous position
75 return true;
76}
77
78bool MovingFrame::setFrame(const Frame& frame)
79{
80 m_internalPose = m_nextPose = frame;
81 return true;
82}
83
84bool MovingFrame::setCallback(MovingFrameCallback _function, void* _param)
85{
86 m_function = _function;
87 m_param = _param;
88 return true;
89}
90
92{
93 // don't compute the velocity during substepping, it is assumed constant.
94 if (!timestamp.substep) {
95 bool cacheAvail = true;
96 if (!timestamp.reiterate) {
97 cacheAvail = popInternalFrame(timestamp.cacheTimestamp);
98 if (m_function)
99 (*m_function)(timestamp, m_internalPose, m_nextPose, m_param);
100 }
101 // only compute velocity if we have a previous pose
102 if (cacheAvail && timestamp.interpolate) {
103 unsigned int iXu;
104 m_velocity = diff(m_internalPose, m_nextPose, timestamp.realTimestep);
105 for (iXu=0; iXu<6; iXu++)
106 m_xudot(iXu) = m_velocity(iXu);
107 } else if (!timestamp.reiterate) {
108 // new position is forced, no velocity as we cannot interpolate
109 m_internalPose = m_nextPose;
110 m_velocity = Twist::Zero();
112 // recompute the jacobian
114 }
115 }
116}
117
118void MovingFrame::pushCache(const Timestamp& timestamp)
119{
120 if (!timestamp.substep && timestamp.cache)
121 pushInternalFrame(timestamp.cacheTimestamp);
122}
123
125{
126 if (timestamp.interpolate) {
127 if (timestamp.substep) {
128 // during substepping, update the internal pose from velocity information
129 Twist localvel = m_internalPose.M.Inverse(m_velocity);
130 m_internalPose.Integrate(localvel, 1.0/timestamp.realTimestep);
131 } else {
132 m_internalPose = m_nextPose;
133 }
134 // m_internalPose is updated, recompute the jacobian
136 }
137 pushCache(timestamp);
138}
139
141{
142 Twist m_jac;
143 e_matrix& Ju = m_JuArray[0];
144
145 //Jacobian is always identity at position on the object,
146 //we ust change the reference to the world.
147 //instead of going through complicated jacobian operation, implemented direct formula
148 Ju(1,3) = m_internalPose.p.z();
149 Ju(2,3) = -m_internalPose.p.y();
150 Ju(0,4) = -m_internalPose.p.z();
151 Ju(2,4) = m_internalPose.p.x();
152 Ju(0,5) = m_internalPose.p.y();
153 Ju(1,5) = -m_internalPose.p.x();
154 // remember that this object has moved
155 m_updated = true;
156}
157
158}
void initialize()
represents a frame transformation in 3D space (rotation + translation)
Definition frames.hpp:526
Rotation M
Orientation of the Frame.
Definition frames.hpp:529
void Integrate(const Twist &t_this, double frequency)
Definition frames.inl:628
Vector p
origine of the Frame
Definition frames.hpp:528
Rotation Inverse() const
Gives back the inverse rotation matrix of *this.
Definition frames.inl:641
double data[9]
Definition frames.hpp:301
represents both translational and rotational velocities.
Definition frames.hpp:679
static Twist Zero()
Definition frames.inl:310
double z() const
Definition frames.inl:85
double y() const
Definition frames.inl:84
double data[3]
Definition frames.hpp:145
double x() const
Definition frames.inl:83
int addChannel(const void *device, const char *name, unsigned int maxItemSize)
Definition Cache.cpp:202
double * addCacheVectorIfDifferent(const void *device, int channel, CacheTS timestamp, double *data, unsigned int length, double threshold)
Definition Cache.cpp:602
const void * getPreviousCacheItem(const void *device, int channel, CacheTS *timestamp)
Definition Cache.cpp:546
virtual void updateKinematics(const Timestamp &timestamp)
virtual void updateJacobian()
MovingFrame(const Frame &frame=F_identity)
virtual void updateCoordinates(const Timestamp &timestamp)
bool setCallback(MovingFrameCallback _function, void *_param)
virtual void pushCache(const Timestamp &timestamp)
virtual void initCache(Cache *_cache)
virtual bool finalize()
bool setFrame(const Frame &frame)
Cache * m_cache
Definition Object.hpp:28
KDL::Frame m_internalPose
Definition Object.hpp:29
bool m_updated
Definition Object.hpp:30
std::vector< e_matrix > m_JuArray
#define NULL
#define e_zero_vector
#define e_identity_matrix
#define e_matrix
#define M
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt=1)
double epsilon
default precision while comparing with Equal(..,..) functions. Initialized at 0.0000001.
Definition utility.cpp:22
bool(* MovingFrameCallback)(const Timestamp &timestamp, const Frame &_current, Frame &_next, void *param)
unsigned int CacheTS
Definition Cache.hpp:33
static const unsigned int frameCacheSize
unsigned int interpolate
Definition Cache.hpp:45
double realTimestep
Definition Cache.hpp:38
unsigned int cache
Definition Cache.hpp:43
CacheTS cacheTimestamp
Definition Cache.hpp:39
unsigned int reiterate
Definition Cache.hpp:42
unsigned int substep
Definition Cache.hpp:41