Skip to content

Commit

Permalink
Merge pull request #15 from ami-iit/add_IK
Browse files Browse the repository at this point in the history
Add Human inverse kinematic class
  • Loading branch information
davidegorbani authored Jan 9, 2024
2 parents e4bbf7a + 29e4b36 commit f4cb926
Show file tree
Hide file tree
Showing 11 changed files with 487 additions and 72 deletions.
86 changes: 28 additions & 58 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ on:
- cron: '0 2 * * *'

env:
BipedalLocomotionFramework_TAG: v0.12.0
BipedalLocomotionFramework_TAG: v0.16.1
LieGroupController_TAG: v0.2.0
action-restore-cache: 'true'
jobs:
build:
Expand All @@ -32,18 +33,18 @@ jobs:
run: |
echo "DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
# Use conda for main dependencies
- uses: conda-incubator/setup-miniconda@v2
# Use mamba for dependencies
- uses: mamba-org/setup-micromamba@v1
with:
miniforge-variant: Mambaforge
miniforge-version: latest
environment-file: ci_env.yml
channel-priority: true

# Print the environment variables to simplify development and debugging
- name: Environment Variables
# Use bash in order to have same basic commands in all OSs
shell: bash
run: env

# Remove apt repos on Ubuntu that are known to break from time to time
# See https://github.com/actions/virtual-environments/issues/323
- name: Remove broken apt repos [Ubuntu]
Expand All @@ -54,68 +55,24 @@ jobs:
# ============
# DEPENDENCIES
# ============
- name: Restore cached conda based dependencies
if: ${{ env.action-restore-cache == 'true' }}
uses: actions/cache/restore@v3
with:
path: ${{ env.CONDA }}/envs/test
key: ${{ matrix.os }}-conda-${{ hashFiles('.github/workflows/conda-deps.yml') }}-${{ env.DATE }}
id: cache-restore-conda-deps

- name: Dependencies (using conda)
shell: bash -l {0}
if: steps.cache-restore-conda-deps.outputs.cache-hit != 'true'
run: |
mamba env update -f .github/workflows/conda-deps.yml
- name: Cache conda based dependencies
if: ${{ steps.cache-restore-conda-deps.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v3
with:
path: ${{ env.CONDA }}/envs/test
key: ${{ matrix.os }}-conda-${{ hashFiles('.github/workflows/conda-deps.yml') }}-${{ env.DATE }}
id: cache-save-conda-deps

- name: Restore cached source-based dependencies
if: ${{ env.action-restore-cache == 'true' && steps.cache-restore-conda-deps.outputs.cache-hit == 'true' }}
uses: actions/cache/restore@v3
with:
path: ${{ github.workspace }}/install/deps
key: ${{ matrix.os }}-${{ matrix.build_type }}-source-${{env.BipedalLocomotionFramework_TAG}}-${{ env.DATE }}
id: cache-restore-source-deps

- name: Source-based dependencies
if: ${{ steps.cache-restore-conda-deps.outputs.cache-hit != 'true' || steps.cache-restore-source-deps.outputs.cache-hit != 'true' }}
# Additional dependencies useful only on Linux
- name: Dependencies [Ubuntu]
if: matrix.os == 'ubuntu-latest'
shell: bash -l {0}
run: |
# bipedal-locomotion-framework
cd ${GITHUB_WORKSPACE}
git clone --depth 1 --single-branch --branch ${BipedalLocomotionFramework_TAG} https://github.com/ami-iit/bipedal-locomotion-framework blf
cd blf
mkdir -p build
cd build
cmake -GNinja .. \
-DCMAKE_PREFIX_PATH=${GITHUB_WORKSPACE}/install/deps \
-DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install/deps
cmake --build . --config ${{matrix.build_type}} --target install
- name: Cache source-based dependencies
if: ${{ steps.cache-restore-source-deps.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v3
with:
path: ${{ github.workspace }}/install/deps
key: ${{ matrix.os }}-${{ matrix.build_type }}-source-${{env.BipedalLocomotionFramework_TAG}}-${{ env.DATE }}
id: cache-save-source-deps

# Additional dependencies only useful on Linux
# See https://github.com/robotology/robotology-superbuild/issues/477
micromamba install expat-cos6-x86_64 libselinux-cos6-x86_64 libxau-cos6-x86_64 libxcb-cos6-x86_64 libxdamage-cos6-x86_64 libxext-cos6-x86_64 libxfixes-cos6-x86_64 libxxf86vm-cos6-x86_64 mesalib mesa-libgl-cos6-x86_64 mesa-libgl-devel-cos6-x86_64
# ===================
# CMAKE-BASED PROJECT
# ===================
# We will just configure and build the project now. Further modifications and tests can be added
# Configure step
- name: Configure
- name: Configure [Ubuntu, macOS]
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macOS-latest'
shell: bash -l {0}
run: |
mkdir -p build
Expand All @@ -126,6 +83,19 @@ jobs:
-DFRAMEWORK_COMPILE_YarpImplementation=ON \
-DBUILD_TESTING:BOOL=ON \
-DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install
- name: Configure [Windows]
if: matrix.os == 'windows-latest'
shell: bash -l {0}
run: |
mkdir -p build
cd build
cmake -G"Visual Studio 17 2022" .. \
-DCMAKE_PREFIX_PATH=${GITHUB_WORKSPACE}/install/deps \
-DCMAKE_BUILD_TYPE=${{matrix.build_type}} \
-DFRAMEWORK_COMPILE_YarpImplementation=ON \
-DBUILD_TESTING:BOOL=ON \
-DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install
# Build step
- name: Build
Expand Down
13 changes: 0 additions & 13 deletions .github/workflows/conda-deps.yml

This file was deleted.

6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# build folder
build/*

# VS Code
.vscode/*

.cache
# MacOS
.DS_Store
.DS_Store
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- The HumanIK class (https://github.com/ami-iit/biomechanical-analysis-framework/pull/15)
- The `CHANGELOG.md` file
- The `Logging` feature (https://github.com/ami-iit/biomechanical-analysis-framework/pull/10)
15 changes: 15 additions & 0 deletions ci_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: biomechanical-analysis-framework
channel:
- conda-forge
dependencies:
- cmake
- compilers
- make
- ninja
- pkg-config
- yarp
- idyntree
- spdlog
- catch2>=3.3.0
- manif
- bipedal-locomotion-framework
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

add_subdirectory(IK)
add_subdirectory(Logging)
8 changes: 8 additions & 0 deletions src/IK/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

add_biomechanical_analysis_library(
NAME IK
PUBLIC_HEADERS include/BiomechanicalAnalysis/IK/InverseKinematics.h
SOURCES src/InverseKinematics.cpp
PUBLIC_LINK_LIBRARIES BipedalLocomotion::IK BipedalLocomotion::ParametersHandler BipedalLocomotion::ContinuousDynamicalSystem BipedalLocomotion::CommonConversions
PRIVATE_LINK_LIBRARIES BiomechanicalAnalysis::Logging
SUBDIRECTORIES tests)
177 changes: 177 additions & 0 deletions src/IK/include/BiomechanicalAnalysis/IK/InverseKinematics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/**
* @file InverseKinematic.h
* @authors Davide Gorbani <davide.gorbani@iit.it>
*/

#ifndef BIOMECHANICAL_ANALYSIS_INVERSE_KINEMATIC_H
#define BIOMECHANICAL_ANALYSIS_INVERSE_KINEMATIC_H

// iDynTree
#include <iDynTree/KinDynComputations.h>

// BipedalLocomotion
#include <BipedalLocomotion/IK/QPInverseKinematics.h>
#include <BipedalLocomotion/ParametersHandler/IParametersHandler.h>
#include <BipedalLocomotion/System/VariablesHandler.h>
#include <BipedalLocomotion/IK/SO3Task.h>
#include <BipedalLocomotion/ContinuousDynamicalSystem/FloatingBaseSystemKinematics.h>
#include <BipedalLocomotion/ContinuousDynamicalSystem/ForwardEuler.h>

namespace BiomechanicalAnalysis
{

namespace IK
{

class HumanIK
{
private:
// Integration time step in nanoseconds
std::chrono::nanoseconds m_dtIntegration;

// Struct to integrate the base and joint velocities
struct System
{
std::shared_ptr<BipedalLocomotion::ContinuousDynamicalSystem::ForwardEuler<BipedalLocomotion::ContinuousDynamicalSystem::FloatingBaseSystemKinematics>> integrator;
std::shared_ptr<BipedalLocomotion::ContinuousDynamicalSystem::FloatingBaseSystemKinematics> dynamics;
};

// System to integrate the base and joint velocities
System m_system;

// Joint positions and velocities
Eigen::VectorXd m_jointPositions;
Eigen::VectorXd m_jointVelocities;
Eigen::Matrix4d m_basePose;
Eigen::Matrix<double, 6, 1> m_baseVelocity;
Eigen::Vector3d m_gravity;

// tasks
std::shared_ptr<BipedalLocomotion::IK::SO3Task> m_link1OrientationTask;

// pointer to the KinDynComputations object
std::shared_ptr<iDynTree::KinDynComputations> m_kinDyn;

// Number of Joint Degrees of Freedom
int m_nrDoFs;

BipedalLocomotion::IK::QPInverseKinematics m_qpIK;
BipedalLocomotion::System::VariablesHandler m_variableHandler;

public:

/**
* Constructor
*/
HumanIK(){};

/**
* Destructor
*/
~HumanIK(){};

/**
* initialize all the task and the inverse kinematics solver
* @param handler pointer to the parameters handler
* @param kinDyn pointer to the KinDynComputations object
* @return true if all the tasks are initialized correctly
*/
bool initialize(std::weak_ptr<const BipedalLocomotion::ParametersHandler::IParametersHandler> handler,
std::shared_ptr<iDynTree::KinDynComputations> kinDyn);

/**
* set the integration time step
* @param dt integration time step in seconds
* @return true if the integration time step is set correctly
*/
bool setDt(const double dt);

/**
* get the integration time step
* @return integration time step in seconds
*/
double getDt() const;

/**
* set the number of DoFs
* @param nrDoFs number of DoFs
* @return true if the number of DoFs is set correctly
*/
bool setDoFsNumber(const int nrDoFs);

/**
* get the number of DoFs
* @return number of DoFs
*/
int getDoFsNumber() const;

/**
* set the initial joint positions
* @param qInitial initial joint positions
* @return true if the initial joint positions are set correctly
*/
bool setInitialJointPositions(const Eigen::Ref<const Eigen::VectorXd> qInitial);

/**
* set the orientation and angular velocity for the link 1 task
* @param link1Orientation link 1 orientation
* @param link1AngularVelocity link 1 angular velocity
* @return true if the link 1 orientation and angular velocity are set correctly
*/
bool setLink1OrientationAndAngVel(const manif::SO3d &link1Orientation,
const manif::SO3Tangentd &link1AngularVelocity);

/**
* advance the inverse kinematics solver
* @return true if the inverse kinematics solver is advanced correctly
*/
bool advance();

/**
* get the joint positions
* @param jointPositions joint positions
* @return true if the joint positions are retrieved correctly
*/
bool getJointPositions(Eigen::Ref<Eigen::VectorXd> jointPositions) const;

/**
* get the joint velocities
* @param jointVelocities joint velocities
* @return true if the joint velocities are retrieved correctly
*/
bool getJointVelocities(Eigen::Ref<Eigen::VectorXd> jointVelocities) const;

/**
* get the base position
* @param basePosition base position
* @return true if the base position is retrieved correctly
*/
bool getBasePosition(Eigen::Ref<Eigen::Vector3d> basePosition) const;

/**
* get the base linear velocity
* @param baseVelocity base linear velocity
* @return true if the base linear velocity is retrieved correctly
*/
bool getBaseLinearVelocity(Eigen::Ref<Eigen::Vector3d> baseVelocity) const;

/**
* get the base orientation
* @param baseOrientation base orientation
* @return true if the base orientation is retrieved correctly
*/
bool getBaseOrientation(Eigen::Ref<Eigen::Matrix3d> baseOrientation) const;

/**
* get the base angular velocity
* @param baseAngularVelocity base angular velocity
* @return true if the base angular velocity is retrieved correctly
*/
bool getBaseAngularVelocity(Eigen::Ref<Eigen::Vector3d> baseAngularVelocity) const;
};

} // namespace IK
} // namespace BiomechanicalAnalysis


#endif // BIOMECHANICAL_ANALYSIS_INVERSE_KINEMATIC_H
Loading

0 comments on commit f4cb926

Please sign in to comment.