def print_model(): model = osim.Model() model.setName('model') # Create a body with name 'body', mass of 1 kg, center of mass at the # origin of the body, and unit inertia (Ixx = Iyy = Izz = 1 kg-m^2). body = osim.Body('body', 1.0, osim.Vec3(0), osim.Inertia(1)) # Create a free joint (all 6 degrees of freedom) with Ground as the parent # body and 'body' as the child body. joint = osim.FreeJoint('joint', model.getGround(), body) # Add the body and joint to the model. model.addComponent(body) model.addComponent(joint) # Create a TableReporter to save quantities to a file after simulating. reporter = osim.TableReporterVec3() reporter.setName('reporter') reporter.set_report_time_interval(0.1) # Report the position of the origin of the body. reporter.addToReport(body.getOutput('position')) # For comparison, we will also get the center of mass position from the # Model, and we can check that the two outputs are the same for our # one-body system. The (optional) second argument is an alias for the name # of the output; it is used as the column label in the table. reporter.addToReport(model.getOutput('com_position'), 'com_pos') # Display what input-output connections look like in XML (in .osim files). print("Reporter input-output connections in XML:\n" + reporter.dump()) model.addComponent(reporter) model.printToXML(model_filename)
def print_model(): model = osim.Model() model.setName('model') # Create a body with name 'body', mass of 1 kg, center of mass at # the origin of the body, and unit inertia # (Ixx = Iyy = Izz = 1 kg-m^2). body = osim.Body('body', 1.0, osim.Vec3(0), osim.Inertia(1)) # Create a free joint (all 6 degrees of freedom) with Ground as # the parent body and 'body' as the child body. joint = osim.FreeJoint('joint', model.getGround(), body) # Add the body and joint to the model. model.addComponent(body) model.addComponent(joint) # Create a TableReporter to save quantities to a file after # simulating. reporter = osim.TableReporterVec3() reporter.setName('reporter') reporter.set_report_time_interval(0.1) reporter.addToReport(model.getOutput('com_position')) reporter.getInput('inputs').setAlias(0, 'com_pos') # Display what input-output connections look like in XML # (in .osim files). print("Reporter input-output connections in XML:\n" + \ reporter.dump()) model.addComponent(reporter) model.printToXML(model_filename)
def animate_iterations(modelIn, traj_prefix): xmin = np.inf ymin = np.inf zmin = np.inf xmax = -np.inf ymax = -np.inf zmax = -np.inf data = list() dir, prefix = os.path.split(traj_prefix) files = sorted(os.listdir(dir)) for file in files: if file.startswith(prefix): model = osim.Model(modelIn) trajectory = osim.MocoTrajectory(os.path.join(dir, file)) statesTab = trajectory.exportToStatesTable() osim.STOFileAdapter.write(statesTab, 'statestabTODO.sto') states_traj = osim.StatesTrajectory.createFromStatesStorage( model, 'statestabTODO.sto') reporter = osim.TableReporterVec3() model.finalizeFromProperties() for comp in model.getComponentsList(): marker = osim.Marker.safeDownCast(comp) if marker: reporter.addToReport(marker.getOutput('location')) model.addComponent(reporter) model.initSystem() for state in states_traj: model.realizeReport(state) table = reporter.getTable().flatten(['_x', '_y', '_z']) filepath = "TODO.sto" osim.STOFileAdapterVec3.write(reporter.getTable(), 'TODO3.sto') osim.STOFileAdapter.write(table, filepath) num_header_rows = 1 with open(filepath) as f: for line in f: if not line.startswith('endheader'): num_header_rows += 1 else: break this_data = pd.read_csv(filepath, delimiter='\t', index_col=0, header=num_header_rows) assert (len(this_data.columns) % 3 == 0) xmin = min( xmin, np.min(this_data.iloc[:, 0::3].to_numpy(), keepdims=False)) xmax = max( xmax, np.max(this_data.iloc[:, 0::3].to_numpy(), keepdims=False)) ymin = min( ymin, np.min(this_data.iloc[:, 1::3].to_numpy(), keepdims=False)) ymax = max( ymax, np.max(this_data.iloc[:, 1::3].to_numpy(), keepdims=False)) zmin = min( zmin, np.min(this_data.iloc[:, 2::3].to_numpy(), keepdims=False)) zmax = max( zmax, np.max(this_data.iloc[:, 2::3].to_numpy(), keepdims=False)) data.append(this_data) assert (len(data) > 0) # TODO uniform time sampling for interpretable results!!! fig = pl.figure() ax = fig.add_subplot(111, projection='3d') # graph, = ax.plot([], [], [], color='k', # linestyle=' ', # marker='o') len_data = float(len(data)) def init(): ax.set_axis_off() ax.set_xlim(xmin, xmax) ax.set_ylim(zmin, zmax) ax.set_zlim(ymin, ymax) def update(frame): # graph.set_data(data[frame].iloc[:, 0::3].values.flatten(), # data[frame].iloc[:, 1::3].values.flatten()) # graph.set_3d_properties(data[frame].iloc[:, 2::3].values.flatten()) pl.cla() ax.set_axis_off() ax.set_xlim(xmin, xmax) ax.set_ylim(zmin, zmax) ax.set_zlim(ymin, ymax) for i in range(frame - 1, frame): for j in range(int(len(data[i].columns) / 3)): ax.plot( data[i].iloc[:, 3 * j].values.flatten(), data[i].iloc[:, 3 * j + 2].values.flatten(), data[i].iloc[:, 3 * j + 1].values.flatten(), #color=(1 - i / float(frame), # 1 - i / float(frame), # 1 - i / float(frame)), linestyle='-', ) # ax.plot(data[i].iloc[:, 0::3].values.flatten(), # data[i].iloc[:, 2::3].values.flatten(), # data[i].iloc[:, 1::3].values.flatten(), # color=(1 - i / float(frame), # 1 - i / float(frame), # 1 - i / float(frame)), # linestyle=' ', # marker='o', # markersize=1, # ) ax.set_title(f'iteration {frame}') # return graph, animation = FuncAnimation(fig, update, init_func=init, frames=range(len(data))) pl.show()