Skip to content

Commit 96ec00c

Browse files
authored
Merge pull request #157 from pariterre/master
Fixed dimensions of GeneralizedTorque when using quaternions
2 parents 6b7eb5c + dc9bda4 commit 96ec00c

File tree

5 files changed

+104
-45
lines changed

5 files changed

+104
-45
lines changed

examples/python3/forwardKinematics.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import numpy as np
22
import biorbd
33

4+
try:
5+
from BiorbdViz.biorbd_vtk import VtkModel, VtkWindow
6+
import pyomeca
7+
biorbd_viz_found = True
8+
except ModuleNotFoundError:
9+
biorbd_viz_found = False
10+
411
#
512
# This examples shows how to
613
# 1. Load a model
@@ -15,13 +22,32 @@
1522
# Load a predefined model
1623
model = biorbd.Model("../pyomecaman.bioMod")
1724
nq = model.nbQ()
25+
n_mark = model.nbMarkers()
26+
n_frames = 20
1827

19-
# Choose a position to get the markers from
20-
Q = np.zeros((nq,))
28+
# Generate clapping gesture data
29+
qinit = np.array([0, 0, -.3, 0.35, 1.15, -0.35, 1.15, 0, 0, 0, 0, 0, 0])
30+
qmid = np.array([0, 0, -.3, 0.5, 1.15, -0.5, 1.15, 0, 0, 0, 0, 0, 0])
31+
qfinal = np.array([0, 0, -.3, 0.35, 1.15, -0.35, 1.15, 0, 0, 0, 0, 0, 0])
32+
q = np.concatenate((np.linspace(qinit, qmid, n_frames).T, np.linspace(qmid, qfinal, n_frames).T), axis=1)
2133

2234
# Proceed with the forward kinematics
23-
markers = model.markers(Q)
35+
markers = np.ndarray((3, model.nbMarkers(), 2*n_frames))
36+
for i, q in enumerate(q.T):
37+
markers[:, :, i] = np.array([mark.to_array() for mark in model.markers(q)]).T
38+
39+
# Print the first frame in the console
40+
print(markers[:, :, 0])
41+
42+
# Animate the markers
43+
if biorbd_viz_found:
44+
vtkWindow = VtkWindow(background_color=(0.5, 0.5, 0.5))
45+
vtkModel = VtkModel(vtkWindow, markers_color=(0, 0, 1), markers_size=0.01)
46+
i = 0
47+
markers = pyomeca.Markers(markers)
48+
while vtkWindow.is_active:
49+
# Update the graph
50+
vtkModel.update_markers(markers[:, :, [i]])
51+
vtkWindow.update_frame()
52+
i = (i + 1) % markers.shape[2]
2453

25-
# Print them to the console
26-
for marker in markers:
27-
print(marker.to_array())
Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import numpy as np
22
import biorbd
3+
try:
4+
import BiorbdViz
5+
biorbd_viz_found = True
6+
except ModuleNotFoundError:
7+
biorbd_viz_found = False
8+
39

410
#
511
# This examples shows how to
@@ -17,30 +23,48 @@
1723
# Load a predefined model
1824
model = biorbd.Model("../pyomecaman.bioMod")
1925
nq = model.nbQ()
20-
# nqdot = model.nbQdot()
21-
# nqddot = model.nbQddot()
22-
n_frames = 3
26+
nb_mus = model.nbMuscles()
27+
n_frames = 20
28+
29+
# Generate clapping gesture data
30+
qinit = np.array([0, 0, -.3, 0.35, 1.15, -0.35, 1.15, 0, 0, 0, 0, 0, 0])
31+
qmid = np.array([0, 0, -.3, 0.5, 1.15, -0.5, 1.15, 0, 0, 0, 0, 0, 0])
32+
qfinal = np.array([0, 0, -.3, 0.35, 1.15, -0.35, 1.15, 0, 0, 0, 0, 0, 0])
33+
target_q = np.concatenate((np.linspace(qinit, qmid, n_frames).T, np.linspace(qmid, qfinal, n_frames).T), axis=1)
34+
markers = np.ndarray((3, model.nbMarkers(), 2*n_frames))
35+
for i, q in enumerate(target_q.T):
36+
markers[:, :, i] = np.array([mark.to_array() for mark in model.markers(q)]).T
2337

24-
# Generate random data (3 frames)
25-
targetQ = np.random.random((nq,))
26-
targetMarkers = model.markers(targetQ)
38+
# If ones was using c3d data opened using ezc3d
39+
# import ezc3d
40+
# c3d = ezc3d.c3d(data_filename)
41+
# markers = c3d['data']['points'][:3, :, :] # XYZ1 x markers x time_frame
2742

43+
# Dispatch markers in biorbd structure so EKF can use it
2844
markersOverFrames = []
29-
for i in range(n_frames):
30-
markersOverFrames.append(targetMarkers)
45+
for i in range(markers.shape[2]):
46+
markersOverFrames.append([biorbd.NodeSegment(m) for m in markers[:, :, i].T])
3147

32-
# Create a Kalman filter
33-
freq = 100 # 100 Hz
48+
# Create a Kalman filter structure
49+
freq = 100 # Hz
3450
params = biorbd.KalmanParam(freq)
3551
kalman = biorbd.KalmanReconsMarkers(model, params)
3652

3753
# Perform the kalman filter for each frame (the first frame is much longer than the next)
38-
print(f"Target Q = {targetQ}")
3954
Q = biorbd.GeneralizedCoordinates(model)
4055
Qdot = biorbd.GeneralizedVelocity(model)
4156
Qddot = biorbd.GeneralizedAcceleration(model)
42-
for targetMarkers in markersOverFrames:
57+
q_recons = np.ndarray((model.nbQ(), len(markersOverFrames)))
58+
for i, targetMarkers in enumerate(markersOverFrames):
4359
kalman.reconstructFrame(model, targetMarkers, Q, Qdot, Qddot)
60+
q_recons[:, i] = Q.to_array()
4461

4562
# Print the kinematics to the console
46-
print(Q.to_array())
63+
print(f"Frame {i}\nExpected Q = {target_q[:, i]}\nCurrent Q = {q_recons[:, i]}\n")
64+
65+
# Animate the results if biorbd viz is installed
66+
if biorbd_viz_found:
67+
b = BiorbdViz.BiorbdViz(loaded_model=model)
68+
b.load_movement(q_recons)
69+
b.exec()
70+

src/RigidBody/Joints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void biorbd::rigidbody::Joints::DeepCopy(const biorbd::rigidbody::Joints &other)
8383
}
8484

8585
unsigned int biorbd::rigidbody::Joints::nbGeneralizedTorque() const {
86-
return nbQ();
86+
return nbQddot();
8787
}
8888
unsigned int biorbd::rigidbody::Joints::nbDof() const {
8989
return *m_nbDof;

test/models/cube.bioMod

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ version 4
33
// DEFINITION DU SEGMENTS
44

55
// Seg1
6-
segment Seg1
6+
segment Seg1
77
translations xz
88
rotations y
99
rangesQ -0.70 2.30
10-
-3 3
11-
-pi pi
12-
mass 1
13-
inertia
14-
1 0 0
15-
0 1 0
16-
0 0 1
17-
com 0 0 0
18-
mesh 0 -1 -1
19-
mesh 0 0 -1
20-
mesh 0 0 0
21-
mesh 0 -1 0
22-
mesh 0 -1 -1
23-
mesh 1 -1 -1
24-
mesh 1 0 -1
25-
mesh 0 0 -1
26-
mesh 1 0 -1
27-
mesh 1 0 0
28-
mesh 0 0 0
29-
mesh 1 0 0
30-
mesh 1 -1 0
31-
mesh 0 -1 0
32-
mesh 1 -1 0
33-
mesh 1 -1 -1
10+
-3 3
11+
-pi pi
12+
mass 1
13+
inertia
14+
1 0 0
15+
0 1 0
16+
0 0 1
17+
com 0 0 0
18+
mesh 0 -1 -1
19+
mesh 0 0 -1
20+
mesh 0 0 0
21+
mesh 0 -1 0
22+
mesh 0 -1 -1
23+
mesh 1 -1 -1
24+
mesh 1 0 -1
25+
mesh 0 0 -1
26+
mesh 1 0 -1
27+
mesh 1 0 0
28+
mesh 0 0 0
29+
mesh 1 0 0
30+
mesh 1 -1 0
31+
mesh 0 -1 0
32+
mesh 1 -1 0
33+
mesh 1 -1 -1
3434
endsegment
3535

3636
// Marker on Seg1

test/test_rigidbody.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,15 @@ TEST(Dynamics, ForwardAccelerationConstraint){
10611061
}
10621062
}
10631063

1064+
TEST(QuaternionInModel, sizes)
1065+
{
1066+
biorbd::Model m("models/simple_quat.bioMod");
1067+
EXPECT_EQ(m.nbQ(), 4);
1068+
EXPECT_EQ(m.nbQdot(), 3);
1069+
EXPECT_EQ(m.nbQddot(), 3);
1070+
EXPECT_EQ(m.nbGeneralizedTorque(), 3);
1071+
}
1072+
10641073
TEST(Kinematics, computeQdot)
10651074
{
10661075
biorbd::Model m("models/simple_quat.bioMod");

0 commit comments

Comments
 (0)