Home > Kubuntu, python > Python: current trace calculator

Python: current trace calculator

la formula approssimata per il calcolo della corrente massima su una pista di spessore ‘thickness’, larghezza ‘width’, tenendo conto del delta termico ‘t_diff’ è:

I = k_layer * t_diff^0.44 * A^0.725

dove k_layer assume i valori di 0.024 per gli inner layer e 0.048 per i layer esterni.

Il codice python è il seguente:

import Tkinter as Tk
import ttk
import tkMessageBox as Mb

CENTER = Tk.N + Tk.S + Tk.W + Tk.E


class View(object):
    def __init__(self):
        self.root = Tk.Tk()
        self.root.title('Current trace calculator')
        self.rb_var = Tk.IntVar()
        self.thickness_var = Tk.StringVar()
        self.current_var = Tk.StringVar()
        self.t_diff_var = Tk.StringVar()
        self.width_var = Tk.StringVar()

    def build_ui(self):
        """Build the UI with all widgets"""
        frame = Tk.Frame(self.root)
        frame.pack(fill=Tk.BOTH)

        ##### Radio buttons
        rb_inner = Tk.Radiobutton(frame, text='Inner', variable=self.rb_var,
                                  value=1)
        rb_inner.grid(sticky=CENTER, row=0, column=0, padx=5)
        rb_ext = Tk.Radiobutton(frame, text='External', variable=self.rb_var,
                                value=2)
        rb_ext.grid(sticky=CENTER, row=0, column=1, padx=5)

        ttk.Separator(frame).grid(sticky=CENTER, row=1, column=0,
                                  columnspan=2, padx=2, pady=2)
        ##### Thickness
        Tk.Label(frame, text='* Cu thickness (um)').grid(
            sticky=CENTER, row=2, column=0, padx=2, pady=2)
        en_thickness = Tk.Entry(frame, textvariable=self.thickness_var)
        en_thickness.grid(sticky=CENTER, row=2, column=1, padx=2, pady=2)
        ttk.Separator(frame).grid(sticky=CENTER, row=3, column=0,
                                  columnspan=2, padx=2, pady=2)

        ##### T_diff
        Tk.Label(frame, text='* delta temp.(max - Ta)', width=25).grid(
            sticky=CENTER, row=4, column=0, padx=2, pady=2)
        en_t_diff = Tk.Entry(frame, width=5, textvariable=self.t_diff_var)
        en_t_diff.grid(sticky=CENTER, row=4, column=1, padx=2, pady=2)

        ttk.Separator(frame).grid(sticky=CENTER, row=5, column=0,
                                  columnspan=2, padx=2, pady=2)
        ##### width box
        Tk.Label(frame, text='* Trace width (mils)', width=25).grid(
            sticky=CENTER, row=6, column=0, padx=2, pady=2)
        en_width = Tk.Entry(frame, textvariable=self.width_var)
        en_width.grid(sticky=CENTER, row=6, column=1, padx=2, pady=2)
        ttk.Separator(frame).grid(sticky=CENTER, row=7, column=0,
                                  columnspan=2, padx=2, pady=2)

        ##### current box
        Tk.Label(frame, text='Current max (A)', width=25).grid(
            sticky=CENTER, row=8, column=0, padx=2, pady=2)
        en_current = Tk.Label(frame, bg='yellow',
                              textvariable=self.current_var, anchor='w')
        en_current.grid(sticky=CENTER, row=8, column=1, padx=2, pady=2)
        ttk.Separator(frame).grid(sticky=CENTER, row=9, column=0,
                                  columnspan=2, padx=2, pady=2)

        ##### buttons
        self.btn_gen = Tk.Button(frame, text='Calculate!')
        self.btn_gen.grid(sticky=CENTER, row=10, column=0, padx=2, pady=2)
        btn_quit = Tk.Button(frame, text='Quit', command=self.root.destroy)
        btn_quit.grid(sticky=CENTER, row=10, column=1, padx=2, pady=2)
        self.rb_var.set(2)

    def start(self):
        """Start the mainloop"""
        self.root.mainloop()


class Controller(object):
    def __init__(self):
        self.view = View()
        self.view.build_ui()
        self.view.thickness_var.set('35')
        self.view.t_diff_var.set('15')

    def run_app(self):
        """Start the View (UI) mainloop"""
        self.view.start()

    def bind_widgets(self):
        """Bind the view widgets to the callbacks"""
        self.view.btn_gen.configure(command=self.on_calculate)

    def on_calculate(self):
        """Calculate Button callback, calculate the max current"""
        try:
            t_diff = float(self.view.t_diff_var.get())
            thickness = float(self.view.thickness_var.get())
            width = float(self.view.width_var.get())
        except (TypeError, ValueError):
            alert("Fill [*] with numbers not literals!")
        else:
            k_layer = 0.024 if self.view.rb_var.get() == 1 else 0.048
            current = calculate_current(width, thickness, t_diff, k_layer)
            self.view.current_var.set(str(current))


def calculate_current(width, thickness, t_diff, k_layer):
    """Calculate the max current for a trace of width=width.
       formula: I = k_layer * t_diff^0.44 * A^0.725
       A is measured in mils^2
    """
    section = thickness / 25.4 * width
    return k_layer * t_diff**0.44 * section**0.725


def alert(message):
    """Return a MessageBox with "string" message passed as argument"""
    Mb.showinfo(title='Alert!', message=message, icon=Mb.INFO)


def main():
    c = Controller()
    c.bind_widgets()
    c.run_app()


if __name__ == '__main__':
    main()

curtracecalc

Categorie:Kubuntu, python
  1. Non c'è ancora nessun commento.
  1. No trackbacks yet.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: