Coder Social home page Coder Social logo

edxmorgan / diff_uv Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 2.13 MB

A 6D differentiable underwater vehicle dynamics in body, ned and quaternion coordinate.

License: MIT License

Python 14.06% Jupyter Notebook 85.94%
euler marine optimization python quaternions robotics underwater differentiable-programming casadi symbolic-computation sympy cpp code-generation jacobian

diff_uv's Introduction

Diff_UV (Differentiable Underwater Vehicle System)

A differentiable Underwater vehicles dynamic model in 6 DOFs based on casadi operations.

The matrices ๐‘€, ๐ถ(๐œˆ) and ๐ท(๐œˆ), and vector ๐‘”(๐œ‚) in the dynamics contain more than 300 unknown parameters in total. As a result, estimation of all parameters is infeasible. Yet, based on the features and operating speeds of the vehicle, several assumptions can be made to simplify the dynamic model and reduce the number of unknown parameters in the model.

The assumptions that have been made for the dynamics of a lightweight underwater vehicle are listed in the following:

  • Operates at relative low speeds (i.e. less than 2 m/s), lift forces can be neglected.
  • Assumed to have port-starboard symmetry and fore-aft symmetry; and the centre of gravity (CG) is assumed to be located in the symmetry planes.
  • Assumed to be hydrodynamically symmetrical about 6-DoF. Accordingly, the motions between DoFs of the vehicle in hydrodynamic can be decoupled.
  • Assumed to operate below the wave-affected zone. As a result, disturbances of waves on the vehicle are negligible.

Getting Started

To use Diff_UV in your own project, simply clone this repository to your workspace:

cd path/to/src
git clone https://github.com/Eddy-Morgan/Diff_UV.git

All kinematics & hydrodynamic terms implemented in this project have been defined using Fossen's formulations. The terms implemented include:

  • Kinematics : Rotation & Coordinate Transformation Matrices
  • Mass: rigid body inertia and added mass in body, ned and quaternion.
  • Coriolis: centripetal, coriolis, and added coriolis in body, ned and quaternion.
  • Damping: linear and quadratic damping in body, ned and quaternion.
  • Restoring forces: buoyancy and gravitational forces in body, ned and quaternion.
  • Forward dynamics
  • Inverse dynamics

Each of the aforementioned terms provide their own distinct data methods for independent use and are managed altogether within the diffUV class.

from diffUV import dyn_body,dyned_eul, kin
uv_dyn = dyn_body()
uv_dyned = dyned_eul()

inertia_mat = uv_dyn.body_inertia_matrix()
coriolis_mat = uv_dyn.body_coriolis_centripetal_matrix()
restoring_vec = uv_dyn.body_restoring_vector()
dampn_mat = uv_dyn.body_damping_matrix()

v_dot = uv_body.body_forward_dynamics()

For detailed usage examples of the Diff_UV, see Jupyter notebook.

Extending with CasADi Capabilities

All expressions obtained from the diffUV methods are of CasADi type. This allows them to be integrated with CasADi's advanced functionalities for optimization, symbolic computations, and numerical integrations.

Symbolic Differentiation

Utilize CasADi's automatic differentiation to compute derivatives:

from casadi import jacobian
accel_jacobian = jacobian(v_dot, uv_dyn.body_state_vector)

Code Generation

Expressions can be directly exported to MATLAB and C++ formats, for integration with external systems and applications.

import os
from casadi import Function
from diffUV.utils.symbols import *

I_o = vertcat(I_x, I_y, I_z,I_xz) # rigid body inertia wrt body origin
decoupled_added_m = vertcat(X_du, Y_dv, Z_dw, K_dp, M_dq, N_dr) # added mass in diagonals
coupled_added_m =  vertcat(X_dq, Y_dp, N_dp, M_du, K_dv) # effective added mass in non diagonals 


M_func = Function('M_b', [m, I_o, z_g, decoupled_added_m, coupled_added_m], [inertia_mat]) # for both numerical & symbolic use
M_func.generate("M_b.c")
os.system(f"gcc -fPIC -shared M_b.c -o libM_b.so")
// C++ (and CasADi)
#include <casadi/casadi.hpp>
using namespace casadi;

void diffuv_usage_cplusplus(){
  std::cout << "---" << std::endl;
  std::cout << "Usage from CasADi C++:" << std::endl;
  std::cout << std::endl;

  // Use CasADi's "external" to load the compiled function
  Function f = external("M_b", "libM_b.so");

  // Use like any other CasADi function
  double m = 11.5;
  std::vector<double> Io = {0.16, 0.16, 0.16, 0};
  double z_g = 0.02;
  std::vector<double> added_m = {-5.5 , -12.7 , -14.57,  -0.12,  -0.12,  -0.12};
  std::vector<double> coupl_added_m = {0, 0, 0, 0, 0}; // assuming decoupling motion
  std::vector<DM> arg = {m, Io, z_g, added_m, coupl_added_m};
  std::vector<DM> res = f(arg);

  std::cout << "result (0): " << res.at(0) << std::endl;
  std::cout << "result (1): " << res.at(1) << std::endl;
}

int main()
{
    diffuv_usage_cplusplus();
    return 0;
}

References

Fossen, T.I. (2011) Handbook of Marine Craft Hydrodynamics and Motion Control. John Wiley & Sons, Inc., Chichester, UK. https://doi.org/10.1002/9781119994138

diff_uv's People

Contributors

edxmorgan avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.