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 .

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.1.tar.gz (14.7 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.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydrodynamics-1.0.1.tar.gz
  • Upload date:
  • Size: 14.7 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.1.tar.gz
Algorithm Hash digest
SHA256 d3cdc548f320530099126e3e543bbe4552dab68e747f0c1e080349c46ef4e63d
MD5 d719ca7ba9e59e922fa070af4441e9c1
BLAKE2b-256 7d2fdb52614f855adb56e5e874206df5d522034acb7e9875d54e37c3a7b71c58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydrodynamics-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 507aad4c9bdeb41825024682dc3d6cde1db54a1bd46d60d4a4c0c2da3f9a160d
MD5 fa91d6b3f1762532d16ad53b6007ed3b
BLAKE2b-256 5b358de428109638a4bc2bcdf8e5f1969126b4109e10ee6dabc0163f526dcc78

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