# 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 .
```

## 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. 