engineerjoe440 / ElectricPy

Electrical Engineering Formulas in Python
https://electricpy.readthedocs.io/en/latest/
MIT License
86 stars 16 forks source link

Add Operational Amplifier (OpAmp) Module and Start with a Few Basic Formulas #70

Open engineerjoe440 opened 2 years ago

engineerjoe440 commented 2 years ago

It'll be good to add a submodule electricpy.opamp to contain a variety of op-amp related formulas. We can start with the formulas laid out in BasicTables. We should make a unique function for each formula and include an example and copy of the image for each.

Lakshmikanth2001 commented 1 year ago

please assign me this issue

Lakshmikanth2001 commented 1 year ago

let me know if like the code structure for electricpy.opamp

def non_inverting(Vin = None, Vout = None, Rg = None, Rf = None):
    """
    """
    if not (Vin or Vout or Rg or Rf):
        raise ValueError(f"Atleasr 3 out of (Vin, Vout, Rg, Rf) => \
        {(Vin, Vout, Rg, Rf)} should be supplied to find out the other one")

    if Rg < 0 or Rf < 0:
        raise ValueError("Rg and Rg both must be positive")

    if Vout == None:
        try:
            Vout = Vin*(Rg + Rf)/(Rg)
        except TypeError:
            raise ValueError(f"To find out Vout (Vin, Rg, Rf) => {(Vin, Rg, Rf)} \
            should not be None")
        except ZeroDivisionError:
            raise ValueError("Rg should be non zero")

    if Vin == None:
        try:
            Vin = Vout*(Rg)/(Rg + Rf)
        except TypeError:
            raise ValueError(f"To find out Vin (Vout, Rg, Rf) => {(Vout, Rg, Rf)} \
            should not be None")
        except ZeroDivisionError:
            raise ValueError("Rg+Rf should be non zero")

    if Rg == None:
        try:
            Rg = (Vin)*Rf/(Vout -Vin)
        except TypeError:
            raise ValueError(f"To find out Rg (Vout, Vin, Rf) => {(Vout, Vin, Rf)}  \
            should not be None")
        except ZeroDivisionError:
            raise ValueError("Vin and Vout should not be equal \
            if Rf and Rg are present")

    if Rf == None:
        try:
            Rf = Rg*(Vin - Vout)/Vin
        except TypeError:
            raise ValueError(f"To find out Rf (Vout, Vin, Rg) => {(Vout, Vin, Rg)}  \
            should not be None")
        except ZeroDivisionError:
            raise ValueError("Vin cannot be found if Rf = 0")

    return {
        "Vin": Vin,
        "Vout": Vout,
        "Rg": Rg,
        "Rf": Rf,
    }
engineerjoe440 commented 1 year ago

Hi, @Lakshmikanth2001!

Looks like it's off to a great start! :smile: I've only got small suggestions about wording.

Let me give an effort with the docstring... (feel free to modify, we'll work out the details in your the pull request)

def non_inverting(Vin = None, Vout = None, Rg = None, Rf = None):
    """
    Non-Inverting Op-Amp Solver

    This function will accept three inputs for the common operational amplifier
    formulas related to a non-inverting operational amplifier (op-amp) and will
    solve for the fourth value. All inputs default to `None`, to solve for any
    value, leave its input as `None` and set all other inputs.

    .. image:: https://www.basictables.com/media/non-inverting-opamp-circuit.png

    Examples
    --------
    >>> from electriclpy.opamp import non_inverting
    >>> non_inverting(Vin=10, Rg=10, Rf=10) # Solve for Vout
    {...}
    >>> non_inverting(Vin=10, Vout=10, Rg=10) # Solve for Rf
    {...}

    Parameters
    ----------
    Vin:    [int, float]
            Input voltage, in volts. Default is None.
    Vout:   [int, float]
            Output voltage, in volts. Default is None.
    Rg:     [int, float]
            Ground-resistor value, in ohms. Default is None.
    Rf:     [int, float]
            Forward-resistor value, in ohms. Default is None.

    Returns
    -------
    dict:   Dictionary of the resultant values, includes the following:

            - Vin
            - Vout
            - Rg
            - Rf
    """
    ...