ElectricPy icon indicating copy to clipboard operation
ElectricPy copied to clipboard

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

Open engineerjoe440 opened this issue 3 years ago • 3 comments

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.

  • non_inverting_vout - Image Link: https://www.basictables.com/media/non-inverting-opamp-circuit.png
  • inverting_vout - Image Link: https://www.basictables.com/media/inverting-opamp-circuit.png
  • differential_vout - Image Link: https://www.basictables.com/media/differential-opamp-circuit.png
  • inverting_summing_vout - Image Link: https://www.basictables.com/media/inverting-summing-opamp-circuit.png

engineerjoe440 avatar May 07 '22 16:05 engineerjoe440

please assign me this issue

Lakshmikanth2001 avatar Dec 28 '22 18:12 Lakshmikanth2001

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,
    }

Lakshmikanth2001 avatar Dec 30 '22 07:12 Lakshmikanth2001

Hi, @Lakshmikanth2001!

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

  • looks like there's a typo in the very first ValueError entry

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

engineerjoe440 avatar Jan 02 '23 23:01 engineerjoe440