Skip to main content

A Python package for simulating underwater hydrodynamics

Project description

Pydrodynamics

Pydrodynamics (Python Hydrodynamics) is a simple Python package for simulating 6DOF rigid body dynamics with a focus on Remotely Operated Vehicles (ROVs) or Autonomous Underwater Vehicles (AUVs) using thrusters.

The reason behind this package is to provide a simple but powerful way to calculate the motion of an underwater vehicle when subjected to both thruster (control) forces and environmental forces (like buoyancy and drag).

It was built to support the development of a custom underwater vehicle simulator for the RoboSub competition, with Reinforcement Learning (RL) used for both the control system and mission planning.

Overview of Classes

  • vehicle.py: The main class that represents the vehicle. It ties the other classes together and is the main interface for the user to simulate the vehicle's motion.
  • thrusters.py: Based on PWMs given, calculates the total forces and moments generated by the thrusters on the vehicle. It uses a CSV file to look up thrust values based on PWM and voltage.
  • environment.py: This class calculates the forces acting on the vehicle due to the external environment, such as buoyancy, gravity, drag, added mass, and Coriolis forces.
  • dynamics.py: Given the external forces acting on the vehicle, this class calculates the next state of the vehicle using the equations of motion formulated by Fossen.
  • params.py: Imports the vehicle configuration from the provided YAML file and provides utility functions to access deeply nested parameters.
  • utils.py: Contains type definitions and shared functions such as calculating the rotation matrix to convert from body to world, unpacking the state object, etc.

Getting Started

Install the package using pip:

pip install pydrodynamics

Create a new Python file and import the necessary classes:

from pydrodynamics import Vehicle, State

# Provide the path to the vehicle configuration file
path_to_vehicle_config = "path/to/vehicle_config.yaml"

# Optional: Define the initial state of the vehicle
optional_initial_state = State(
	position=Position(0, 0, 0),
    orientation=Orientation(0, 0, 0),
    linear_velocity=LinearVelocity(0, 0, 0),
    angular_velocity=AngularVelocity(0, 0, 0),
    voltage=16.8,
)

# Create a new Vehicle object
auv = Vehicle(path_to_vehicle_config)

In the main loop, call the step function and pass delta time (time passed) with thruster PWM values to obtain the next state of the vehicle:

# Call the step function to simulate the vehicle's motion at every time step
thruster_pwm = [1900, 1601, 1601, 1100, 1900, 1601, 1601, 1100]
next_state = auv.step(dt, thruster_pwm)

Make sure to provide a valid path to the vehicle configuration file. The configuration file should be in YAML format and contain the following information:

name: 'New Vehicle'

env:
  gravity: 9.81
  density: 1000

physical:
  mass: 0.0

  # Total fluid volume occupied by the vehicle, used for buoyant force
  volume: 0.0

  # Center of mass from origin coordinate
  com:
    x: 0.0
    y: 0.0
    z: 0.0

  # Center of buoyancy from origin coordinate
  cob:
    x: 0.0
    y: 0.0
    z: 0.0

  # Inertia matrix, assuming off diagonal elements are 0
  inertia:
    x: 0.0
    y: 0.0
    z: 0.0

  # Projected area of the vehicle in x, y, z directions
  projected_area:
    x: 0.0
    y: 0.0
    z: 0.0

  # Drag coefficients
  drag:
    x:
      # Drag coefficient in x direction when subjected to surge velocity in x axis (u)
      u: 0.0
      v: 0.0
      w: 0.0
      p: 0.0
      q: 0.0
      r: 0.0
    y:
      ...
    z:
      ...
    k:
      ...
    m:
      ...
    n:
      ...

  # Added mass coefficients (unused for now)
  added_mass:
    x:
      u: 0.0
      v: 0.0
      w: 0.0
      p: 0.0
      q: 0.0
      r: 0.0
    y:
      ...
    z:
      ...
    k:
      ...
    m:
      ...
    n:
      ...

# Will be changing soon to accomodate for simulation of voltage drop and draining battery
electrical:
  voltage: 0.0
  capacity: 0.0

thrusters:
  # Path to the thruster data file, containing thrust at each voltage and PWM
  data: 'thruster_data.csv'

  # List of thrusters on board
  list:
    - name: thruster1
      # Position of the thruster relative to the center of mass (COM)
      pos:
        x: 0.0
        y: 0.0
        z: 0.0

      # Direction of the thruster, will be normalized later
      dir:
        x: 0.0
        y: 0.0
        z: 0.0

Building the Package

To build the package for local development, you can use the following command:

python -m pip install -e .

Once you're happy with the changes, commit them and bump the version number:

git ...

# Update the patch number
bumpver update --patch

# Or update the minor number
bumpver update --minor

# Or update the major number
bumpver update --major

Then build the package using:

python -m build
twine check dist/*

Then, upload the package to PyPI using:

twine upload dist/*

Future Work

  • Implement added mass and coriolis forces
  • Simulate draining battery and voltage drop

References

This work is mostly based on the work done by Thor I. Fossen in his model for marine vehicles.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydrodynamics-1.0.2.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydrodynamics-1.0.2-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file pydrodynamics-1.0.2.tar.gz.

File metadata

  • Download URL: pydrodynamics-1.0.2.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for pydrodynamics-1.0.2.tar.gz
Algorithm Hash digest
SHA256 66c6ae99bb7406c1b8536e9f312d13291e9bc05e02542c279330b29501c97eee
MD5 ca88df70b3253fae9c8dcbb4bdb614ed
BLAKE2b-256 e328ccc62a273a89b8a7bb71814492e87c45fda0d2965aa86173b590af90526a

See more details on using hashes here.

File details

Details for the file pydrodynamics-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pydrodynamics-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for pydrodynamics-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 df1b7f989ad5da95ce232f8ecca8f5f4deff53bbe7208cb9570892970e73ab51
MD5 8a9d70792b058eac6c325416fb58bf00
BLAKE2b-256 1b13105af1363db1a6eee7bf6648e4b26673bd261f968c31756bccd7ab2e42cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page