Metadata-Version: 2.4
Name: pydrodynamics
Version: 1.0.2
Summary: A Python package for simulating underwater hydrodynamics
Author-email: Muhammad Fauzan <muhammadfauzanaristyaputra@gmail.com>
License: MIT License
        
        Copyright (c) [year] [fullname]
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Source, https://github.com/MFauzanAP/pydrodynamics
Keywords: hydrodynamics,mathematical,model,simulation,underwater,fossen
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: pyyaml
Requires-Dist: dataclasses
Dynamic: license-file

# 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](https://www.robosub.org/), 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](https://fossen.biz/).
- **`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:

```bash
pip install pydrodynamics
```

Create a new Python file and import the necessary classes:

```python
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:

``` python
# 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:

```yaml
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:

```bash
python -m pip install -e .
```

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

```bash
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:

```bash
python -m build
twine check dist/*
```

Then, upload the package to PyPI using:

```bash
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](https://fossen.biz/) in his model for marine vehicles. 
