Friday, April 15, 2022

21-Lumped Components

Lumped components are resistors, inductors, and capacitors as well as combinations such as RC, RL, and RLC circuits. This post will show how to create the graphical model for these classes which derive from the Comp base class.

First, add two new public variables to the Comp.cs base class:

// public variables
public Point Pin;
public Point Pout;

These two Point variables will keep track on the input pin location and the output pin location.

Using pencil and graph paper, I determined the size of the basic lumped element components to be width = 60 pixels and height = 40 pixels. It is convenient to add some variables in the Comp.cs base class to define length of input/output leads, component size, component half size, etc. Using these variables we can eliminate the use of "Magic Numbers" or hardcoded values except in one place. This code pattern is much easier to maintain and modify.

// Protected variables - available to derived subclasses
protected int compSize = 60;
protected int halfCompSize = 30;
protected int leadL = 10;  // Length of input and output leads
protected int bodyL = 40;  // Length of the component body (without leads)
protected int compL = 60;  // Length of the component with leads

Next, add a variable to define a common pen that will be used to draw all components.

//Components draw variables
public Pen drawPen = new Pen(Color.White);

The drawPen variable is defined with a White color because we are using a dark mode on the schematicCanvas.

Resistor

Right click on Lumped folder > Add > Class, Name: RES.cs. Note that the class in created in the namespace called MicrowaveTools.Components.Lumped. Make the class public and inherit from the Comp base class.

public class RES : Comp
{
 
}

The RES class will have two constructors. One with no arguments and one that sets all of the base class parameters.

public RES()
{
 
}
 
public RES(float value, Point location, int[] nodes)
{
    Orientation = "Series";
    Type = "Res";
    Value = value;
    Loc = location;
    Nodes = nodes;
    print();
    Pin = Loc;
    Pout = new Point(Loc.X + compL, Loc.Y);
}

Note that the second constructor accepts the value of the component which will be used later in the analysis of the electrical model, the location point, and the assigned nodes. A node in this context refers arbitrary integers assigned to the input and output ports. Component nodes are connected to other component nodes using wires. This constructor calls the component print() method.

Next, add the print() and Draw() methods that override the virtual methods of the base class.

public override void print()
{
    Debug.WriteLine("Type: " + this.Type + " R: " + this.Value + "\t[" + this.Nodes[0] + ", "         + this.Nodes[1] + "]");
}

The print() method requires the System.Diagnostics C# Library to use the Debug.WriteLine() method to display debug messages on the output console. This message displays the component type, value, and nodes of the resistor component. The message is displayed whenever a resistor object is instantiated.

public override void Draw(Graphics gr)
{
    // Draw the resistor body
    for (int i = 1; i < 5; i++)
    {
        gr.DrawLine(drawPen, Loc.X + leadL * (i), Loc.Y, Loc.X + leadL * (i) + 3, Loc.Y -                leadL);
        gr.DrawLine(drawPen, Loc.X + leadL * (i) + 3, Loc.Y - leadL, Loc.X + leadL * (i) + 6,             Loc.Y + leadL);
        gr.DrawLine(drawPen, Loc.X + leadL * (i) + 6, Loc.Y + leadL, Loc.X + leadL * (i + 1),             Loc.Y);
    }
 
    // Draw the input and output leads
    gr.DrawLine(drawPen, Loc.X, Loc.Y, Loc.X + leadL, Loc.Y);           // Input lead
    gr.DrawLine(drawPen, Loc.X + bodyL + leadL, Loc.Y, Loc.X + compL, Loc.Y);   // Output lead
}

The resistor Draw() method uses the Graphics context provided by the MainForm Paint() method to draw the resistor on the window. All sections of the resistor are drawn with the graphics DrawLine() method. The resistor body is draw in a loop to produce 4 resistor sections. Then the input and output leads are drawn. The resistor is drawn relative to its location on the screen such that if the component is moved by the user, it will be drawn in the correct location.

We can test that the resistor is drawn correctly by updating the MainForm.cs as follows:
  • Add Microwave Tools libraries for circuits, components, and lumped components

        // Microwave Tools Libraries
        using MicrowaveTools.Circuits;
        using MicrowaveTools.Components;
        using MicrowaveTools.Components.Lumped;

  • Create a Circuit called ckt which includes a component list called comps

        private Circuit ckt = new Circuit()

  • Add a Paint event to the schematicCanvas that iterates over all the component list using anti-aliasing to smooth the 2D graphics

        private void schematicCanvas_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
 
            foreach (Comp comp in ckt.comps)
            {
                comp.Draw(e.Graphics);
                schematicCanvas.Invalidate();
            }
        }

  • Create a resistor in a resistor button handler and add it to the circuit component list. Ensure that the handler is attached to the btnRES click event.

       private void btnRES_Click(object sender, EventArgs e)
        {
            RES res = new RES(75.0f, new Point(400, 300), new int[] { 0, 0 });
            ckt.comps.Add(res);
            schematicCanvas.Invalidate();
        }

Run the program > select Lumped > select RES. A resistor will be drawn on the window. This is the basic procedure for drawing all components in Microwave Tools.


Now follow the same pattern to create an Inductor and a Capacitor.

Inductor

  • Create a new class in the Lumped folder called IND.cs
  • Make it a public class that inherits from Comp
  • Add two constructors: 1 with no arguments and 1 that initializes the component parameters
  • Add the print() method
  • Add the Draw() method
  • Modify MainForm.cs by adding an Inductor button handler to create the inductor

        public override void Draw(Graphics gr)
        {
            // Draw the inductor body curves
            for (int i = 1; i < 5; i++)
            {
                float startAngle = 180;
                float sweepAngle = 180;
                Rectangle rect = new Rectangle(Loc.X + leadL * (i), Loc.Y - leadL - 5, leadL,                     leadL);
                gr.DrawArc(drawPen, rect, startAngle, sweepAngle);
            }
 
            // Draw the inductor body vertical lines
            for (int i = 1; i < 6; i++)
            {
                gr.DrawLine(drawPen, Loc.X + leadL * (i), Loc.Y, Loc.X + leadL * (i), Loc.Y -                     leadL);
            }
 
            // Draw the input and output leads
            gr.DrawLine(drawPen, Loc.X, Loc.Y, Loc.X + leadL, Loc.Y);                   //                     Input lead
            gr.DrawLine(drawPen, Loc.X + bodyL + leadL, Loc.Y, Loc.X + compL, Loc.Y);   //                     Output lead
        }

Capacitor

  • Create a new class in the Lumped folder called CAP.cs
  • Make it a public class that inherits from Comp
  • Add two constructors: 1 with no arguments and 1 that initializes the component parameters
  • Add the print() method
  • Add the Draw() method
  • Modify MainForm.cs by adding an Capacitor button handler to create the capacitor

       // Let the Capacitor draw itself called from the canvas paint event

        public override void Draw(Graphics gr)
        {
            // Draw the capacitor body curves
            float startAngle = 90;
            float sweepAngle = 180;
            Rectangle rect = new Rectangle(Loc.X + 3 * leadL, Loc.Y - 10, leadL, leadL * 2);
            gr.DrawArc(drawPen, rect, startAngle, sweepAngle);
 
            // Draw the capacitor body vertical line
            gr.DrawLine(drawPen, Loc.X + 2 * leadL + 5, Loc.Y - leadL, Loc.X + 2 * leadL + 5,                 Loc.Y + leadL);
 
            // Draw the input and output leads
            gr.DrawLine(drawPen, Loc.X, Loc.Y, Loc.X + 2 * leadL + 5,                                         Loc.Y);                   // Input lead
            gr.DrawLine(drawPen, Loc.X + 3 * leadL, Loc.Y, Loc.X + compL, Loc.Y);   // Output                 lead
        }

Run the program and create a resistor, inductor, and capacitor. The source code for this post is available on GitHub.



No comments:

Post a Comment

34-Microwave Tools with Analysis (Series Final Post)

In this final blog post, I have integrated Y-Matrix analysis into the Microwave Tools project. This version has addition Lumped, Ideal, Micr...