|
1 | 1 | import numpy as np |
2 | 2 | import biorbd |
| 3 | +try: |
| 4 | + import BiorbdViz |
| 5 | + biorbd_viz_found = True |
| 6 | +except ModuleNotFoundError: |
| 7 | + biorbd_viz_found = False |
| 8 | + |
3 | 9 |
|
4 | 10 | # |
5 | 11 | # This examples shows how to |
|
17 | 23 | # Load a predefined model |
18 | 24 | model = biorbd.Model("../pyomecaman.bioMod") |
19 | 25 | 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 |
23 | 37 |
|
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 |
27 | 42 |
|
| 43 | +# Dispatch markers in biorbd structure so EKF can use it |
28 | 44 | 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]) |
31 | 47 |
|
32 | | -# Create a Kalman filter |
33 | | -freq = 100 # 100 Hz |
| 48 | +# Create a Kalman filter structure |
| 49 | +freq = 100 # Hz |
34 | 50 | params = biorbd.KalmanParam(freq) |
35 | 51 | kalman = biorbd.KalmanReconsMarkers(model, params) |
36 | 52 |
|
37 | 53 | # Perform the kalman filter for each frame (the first frame is much longer than the next) |
38 | | -print(f"Target Q = {targetQ}") |
39 | 54 | Q = biorbd.GeneralizedCoordinates(model) |
40 | 55 | Qdot = biorbd.GeneralizedVelocity(model) |
41 | 56 | 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): |
43 | 59 | kalman.reconstructFrame(model, targetMarkers, Q, Qdot, Qddot) |
| 60 | + q_recons[:, i] = Q.to_array() |
44 | 61 |
|
45 | 62 | # 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 | + |
0 commit comments