% This file demonstrates how to add external exoskeleton to the model using a custom
% object.
%
% The model will look for .object files in two locations
% 1) The model's local directory
% 2) The Models directory in the OpenSim Installation Directory
% e.g. (<OpenSim_Home>\Models)
%
% The allowed object extensions are .vtp, .stl, .obj
% -----------------------------------------------------------------------
% The OpenSim API is a toolkit for musculoskeletal modeling and
% simulation. See http://opensim.stanford.edu and the NOTICE file
% for more information. OpenSim is developed at Stanford University
% and supported by the US National Institutes of Health (U54 GM072970,
% R24 HD065690) and by DARPA through the Warrior Web program.
%
% Copyright (c) 2005-2019 Stanford University and the Authors
% Author(s): Daniel A. Jacobs
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
% http://www.apache.org/licenses/LICENSE-2.0.
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
% implied. See the License for the specific language governing
% permissions and limitations under the License.
% -----------------------------------------------------------------------
%% Import OpenSim Libraries
import org.opensim.modeling.*
%% Define the Model File Path.
% The default is a relative path from the working directory for the example
model_path = 'leg6dof9musc.osim';
%% Instantiate the Model
geoModel = Model(model_path);
% Change the name
geoModel.setName('leg6dof9musc_addexo');
%% Define the foot mesh name
meshPath = 'Exo_sample.stl';
%% Add Bodies and joints for two feet
% Make feet Bodies
% 以下变量可以在三维建模软件里查看,此处使用的是UG的测量体功能
mass = 1.0218694020000001;
massCenter = Vec3(0.0050601788940000001,-0.10817267374599999,0.01);
inertia = Inertia(0.066192308971390001,0.0019169830833100001, 0.067428045786759996, -0.0030000000000000001, 0, 0);
upexo = Body('upexo', mass , massCenter, inertia);%输入依次为:body name, mass, masscenter, inertia
lwexo = Body('lwexo', mass , massCenter, inertia );
% Add visual objects of the feet
upgeo = Mesh(meshPath);% 导入几何
upgeo.setColor(Vec3(0.5));% 更改颜色,默认值白色
upexo.attachGeometry(upgeo);
upexo.scaleAttachedGeometry(Vec3(0.013, 0.013, 0.013));% 更改导入的几何模型的大小
lwgeo = Mesh(meshPath);
lwgeo.setColor(Vec3(1,1,0.6));
lwexo.attachGeometry(lwgeo);
lwexo.scaleAttachedGeometry(Vec3(0.01, 0.01, 0.01));
% Get a reference to the shank bodies
femur_r= geoModel.getBodySet().get('femur_r');
tibia_r = geoModel.getBodySet().get('tibia_r');
% Make Weld Joints for the feet
locationInParent = Vec3(0,0,0.08);
orientationInParent = Vec3(0);
locationInChild = Vec3(0);
orientationInChild = Vec3(0);
exo_femur = WeldJoint('exo_femur',femur_r, locationInParent, orientationInParent, upexo, locationInChild, orientationInChild);
exo_tibia = WeldJoint('exo_tibia',tibia_r, locationInParent,orientationInParent, lwexo, locationInChild, orientationInChild);
% Add the bodies and joints to the model
geoModel.addBody(upexo);
geoModel.addBody(lwexo);
geoModel.addJoint(exo_femur);
geoModel.addJoint(exo_tibia);
% Make a contact mesh for each foot
contact_up = ContactMesh(meshPath,Vec3(0,0,0), Vec3(0,0,0), upexo, 'contact_up');
contact_lw = ContactMesh(meshPath,Vec3(0,0,0), Vec3(0,0,0), lwexo, 'contact_lw');
% Add contact geometry to the model
geoModel.addContactGeometry(contact_up);
geoModel.addContactGeometry(contact_lw);
%% finalize connections
geoModel.finalizeConnections()
%% Print the model to file.
newFilename = strrep(model_path, '.osim', '_addexo.osim');
isSuccessful = geoModel.print(newFilename);
if (~isSuccessful), error('Model printed to file failed'); end
fprintf('Model printed to file successfully\n');
评论0