File: KinematicEngines.cpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (249 lines) | stat: -rw-r--r-- 7,626 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

#include <lib/high-precision/Constants.hpp>
#include <lib/smoothing/LinearInterpolate.hpp>
#include <core/Scene.hpp>
#include <pkg/common/KinematicEngines.hpp>
#include <preprocessing/dem/Shop.hpp>

namespace yade { // Cannot have #include directive inside.

YADE_PLUGIN((KinematicEngine)(CombinedKinematicEngine)(TranslationEngine)(HarmonicMotionEngine)(RotationEngine)(HelixEngine)(InterpolatingHelixEngine)(
        HarmonicRotationEngine)(ServoPIDController)(BicyclePedalEngine));

CREATE_LOGGER(KinematicEngine);
CREATE_LOGGER(ServoPIDController);

void KinematicEngine::action()
{
	if (ids.size() > 0) {
		FOREACH(Body::id_t id, ids)
		{
			assert(id < (Body::id_t)scene->bodies->size());
			Body* b = Body::byId(id, scene).get();
			if (b) b->state->vel = b->state->angVel = Vector3r::Zero();
		}
		apply(ids);
	} else {
		LOG_WARN("The list of ids is empty! Can't move any body.");
	}
}

void CombinedKinematicEngine::action()
{
	if (ids.size() > 0) {
		// reset first
		FOREACH(Body::id_t id, ids)
		{
			assert(id < (Body::id_t)scene->bodies->size());
			Body* b = Body::byId(id, scene).get();
			if (b) b->state->vel = b->state->angVel = Vector3r::Zero();
		}
		// apply one engine after another
		FOREACH(const shared_ptr<KinematicEngine>& e, comb)
		{
			if (e->dead) continue;
			e->scene = scene;
			e->apply(ids);
		}
	} else {
		LOG_WARN("The list of ids is empty! Can't move any body.");
	}
}

const shared_ptr<CombinedKinematicEngine> CombinedKinematicEngine::fromTwo(const shared_ptr<KinematicEngine>& first, const shared_ptr<KinematicEngine>& second)
{
	shared_ptr<CombinedKinematicEngine> ret(new CombinedKinematicEngine);
	ret->ids = first->ids;
	ret->comb.push_back(first);
	ret->comb.push_back(second);
	return ret;
}


void TranslationEngine::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	if (ids2.size() > 0) {
#ifdef YADE_OPENMP
		const long size = ids2.size();
#pragma omp parallel for schedule(static)
		for (long i = 0; i < size; i++) {
			const Body::id_t& id = ids2[i];
#else
		FOREACH(Body::id_t id, ids2)
		{
#endif
			assert(id < (Body::id_t)scene->bodies->size());
			Body* b = Body::byId(id, scene).get();
			if (!b) continue;
			b->state->vel += velocity * translationAxis;
		}
	} else {
		LOG_WARN("The list of ids is empty! Can't move any body.");
	}
}

void HarmonicMotionEngine::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	if (ids2.size() > 0) {
		Vector3r w        = f * 2.0 * Mathr::PI; //Angular frequency
		Vector3r velocity = (((w * scene->time + fi).array().sin()) * (-1.0));
		velocity          = velocity.cwiseProduct(A);
		velocity          = velocity.cwiseProduct(w);
		FOREACH(Body::id_t id, ids2)
		{
			assert(id < (Body::id_t)scene->bodies->size());
			Body* b = Body::byId(id, scene).get();
			if (!b) continue;
			b->state->vel += velocity;
		}
	} else {
		LOG_WARN("The list of ids is empty! Can't move any body.");
	}
}


void InterpolatingHelixEngine::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	Real virtTime   = wrap ? Shop::periodicWrap(scene->time, *times.begin(), *times.rbegin()) : scene->time;
	angularVelocity = linearInterpolate<Real, Real>(virtTime, times, angularVelocities, _pos);
	linearVelocity  = angularVelocity * slope;
	HelixEngine::apply(ids2);
}

void HelixEngine::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	if (ids2.size() > 0) {
		const Real& dt = scene->dt;
		angleTurned += angularVelocity * dt;
		shared_ptr<BodyContainer> bodies = scene->bodies;
		FOREACH(Body::id_t id, ids2)
		{
			assert(id < (Body::id_t)bodies->size());
			Body* b = Body::byId(id, scene).get();
			if (!b) continue;
			b->state->vel += linearVelocity * rotationAxis;
		}
		rotateAroundZero = true;
		RotationEngine::apply(ids2);
	} else {
		LOG_WARN("The list of ids is empty! Can't move any body.");
	}
}

void RotationEngine::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	if (ids2.size() > 0) {
#ifdef YADE_OPENMP
		const long size = ids2.size();
#pragma omp parallel for schedule(static)
		for (long i = 0; i < size; i++) {
			const Body::id_t& id = ids2[i];
#else
		FOREACH(Body::id_t id, ids2)
		{
#endif
			assert(id < (Body::id_t)scene->bodies->size());
			Body* b = Body::byId(id, scene).get();
			if (!b) continue;
			b->state->angVel += rotationAxis * angularVelocity;
			if (rotateAroundZero) {
				const Vector3r l = b->state->pos - zeroPoint;
				Quaternionr    q(AngleAxisr(angularVelocity * scene->dt, rotationAxis));
				Vector3r       newPos = q * l + zeroPoint;
				b->state->vel += Vector3r(newPos - b->state->pos) / scene->dt;
			}
		}
	} else {
		LOG_WARN("The list of ids is empty! Can't move any body.");
	}
}

void HarmonicRotationEngine::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	const Real& time = scene->time;
	Real        w    = f * 2.0 * Mathr::PI; //Angular frequency
	angularVelocity  = -1.0 * A * w * sin(w * time + fi);
	RotationEngine::apply(ids2);
}

void ServoPIDController::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	if (iterPrevStart < 0 or ((scene->iter - iterPrevStart) >= iterPeriod)) {
		Vector3r tmpForce = Vector3r::Zero();

		if (ids2.size() > 0) {
			FOREACH(Body::id_t id, ids2)
			{
				assert(id < (Body::id_t)scene->bodies->size());
				tmpForce += scene->forces.getForce(id);
			}
		} else {
			LOG_WARN("The list of ids is empty!");
		}

		axis.normalize();
		tmpForce = tmpForce.cwiseProduct(axis); // Take into account given axis
		errorCur = tmpForce.norm() - target;    // Find error

		const Real pTerm = errorCur * kP;               // Calculate proportional term
		iTerm += errorCur * kI;                         // Calculate integral term
		const Real dTerm = (errorCur - errorPrev) * kD; // Calculate derivative term

		errorPrev = errorCur; // Save the current value of the error

		curVel = (pTerm + iTerm + dTerm); // Calculate current velocity

		if (math::abs(curVel) > math::abs(maxVelocity)) { curVel *= math::abs(maxVelocity) / math::abs(curVel); }

		iterPrevStart = scene->iter;
		current       = tmpForce;
	}

	translationAxis = axis;
	velocity        = curVel;

	TranslationEngine::apply(ids2);
}


void BicyclePedalEngine::apply(const vector<Body::id_t>& ids2)
{
	// ‘ids’ shadows a member of ‘yade::TranslationEngine’ [-Werror=shadow]
	if (ids2.size() > 0) {
		Quaternionr qRotateZVec(Quaternionr().setFromTwoVectors(Vector3r(0, 0, 1), rotationAxis));

		Vector3r newPos = Vector3r(cos(fi + angularVelocity * scene->dt) * radius, sin(fi + angularVelocity * scene->dt) * radius, 0.0);
		Vector3r oldPos = Vector3r(cos(fi) * radius, sin(fi) * radius, 0.0);

		Vector3r newVel = (oldPos - newPos) / scene->dt;

		fi += angularVelocity * scene->dt;
		newVel = qRotateZVec * newVel;

#ifdef YADE_OPENMP
		const long size = ids2.size();
#pragma omp parallel for schedule(static)
		for (long i = 0; i < size; i++) {
			const Body::id_t& id = ids2[i];
#else
		FOREACH(Body::id_t id, ids2)
		{
#endif
			assert(id < (Body::id_t)scene->bodies->size());
			Body* b = Body::byId(id, scene).get();
			if (!b) continue;
			b->state->vel += newVel;
		}
	} else {
		LOG_WARN("The list of ids is empty! Can't move any body.");
	}
}

} // namespace yade