You are on page 1of 5

Private Sub FRECUENCIACONTROLADOR_Click()

Dim T1 As Single

Dim T0 As Single, Dt As Single, tf As Single

Dim w As Single, p As Single

Dim KP As Single, TR As Single

Dim funcionG As Single, funciondb As Single, funcionarg As Single, funcionz As Single

Dim n As Integer, contador As Integer

'titulos en celdas

Range("B2").Select

ActiveCell.FormulaR1C1 = "Tiempo (radianes/min)"

Range("B2").Select

Selection.Font.Bold = True

Range("C2").Select

ActiveCell.FormulaR1C1 = "Ganancia - G"

Range("C2").Select

Selection.Font.Bold = True

Range("D2").Select

ActiveCell.FormulaR1C1 = "Tiempo (radianes/min)"

Range("D2").Select

Selection.Font.Bold = True

Range("E2").Select

ActiveCell.FormulaR1C1 = "Ganancia en db"

Range("E2").Select

Selection.Font.Bold = True

Range("F2").Select

ActiveCell.FormulaR1C1 = "Desfase - grados"

Range("F2").Select

Selection.Font.Bold = True

Range("A2").Select

ActiveCell.FormulaR1C1 = "Tinicial (radianes/min)"


Range("A2").Select

Selection.Font.Bold = True

Range("A4").Select

ActiveCell.FormulaR1C1 = "Tfinal (radianes/min)"

Range("A4").Select

Selection.Font.Bold = True

Range("A6").Select

ActiveCell.FormulaR1C1 = "Incremento tiempo (radianes/min)"

Range("A6").Select

Selection.Font.Bold = True

Range("A8").Select

ActiveCell.FormulaR1C1 = "Nº de puntos"

Range("A8").Select

Selection.Font.Bold = True

Range("A10").Select

ActiveCell.FormulaR1C1 = "Valor de p -nº de periodos)"

Range("A10").Select

Selection.Font.Bold = True

Range("A12").Select

ActiveCell.FormulaR1C1 = "Ganancia de controlador (KP)"

Range("A12").Select

Selection.Font.Bold = True

Range("A14").Select

ActiveCell.FormulaR1C1 = "Tiempo de accion integral - min/rep (TR)"

Range("A14").Select

Selection.Font.Bold = True

'pedir datos

T0 = InputBox("valor inicial de t (t0) - radianes/min:"): Cells(3, 1) = T0 'Tiempo inicial

tf = InputBox("valor final de t (tf)- radianes/min:"): Cells(5, 1) = tf 'Tiempo final

Dt = InputBox("valor incremento de t (Dt)- radianes/min:"): Cells(7, 1) = Dt 'Incremento


tiempo
n = Int((tf - T0) / (Dt)) + 1: Cells(9, 1) = n 'Numero de puntos

KP = InputBox("Ganancia controlador proporcional(KP):"): Cells(13, 1) = KP

TR = InputBox("Tiempo de accion integral min/rep (TR):"): Cells(15, 1) = TR

'borra las celdas de respuestas anteriores

contador = 0

While Abs(Cells(contador + 3, 2)) Or Abs(Cells(contador + 3, 3)) Or Abs(Cells(contador + 3, 4))


Or Abs(Cells(contador + 3, 5)) Or Abs(Cells(contador + 3, 6)) > 0

Cells(contador + 3, 2) = "": Cells(contador + 3, 3) = "": Cells(contador + 3, 4) = "":


Cells(contador + 3, 5) = "": Cells(contador + 3, 6) = ""

contador = contador + 1

Wend

'tabla de valores tiempo-respuestas

For w = T0 To tf Step Dt

p = (w - T0) / Dt: Cells(11, 1) = p

Cells(p + 3, 2) = w: Cells(p + 3, 4) = w

funcionG = KP * Sqr(1 + (1 / (w * TR) ^ 2))

funciondb = 20 * Log(funcionG) / Log(10#)

funcionz = -1 / (w * TR)

funcionarg = (Atn(funcionz)) * (180 / 3.14)

Cells(p + 3, 3) = funcionG 'Ganancia (amplitud salida/entrda

Cells(p + 3, 5) = funciondb 'Ganancia en decibelios

Cells(p + 3, 6) = funcionarg 'Desfase en grados sexagedesimales

Next w

Call grafico

End Sub

'Grafico

Sub grafico()

Dim n As Single, p As Single, ChartsTemp As Object, graf As Object

Dim datos As String

n = Cells(9, 1): p = Cells(11, 1)

'Eliminar grafico anterior


Set ChartsTemp = ActiveSheet.ChartObjets

If ChartsTemp.Count > 0 Then

ChartsTemp(ChartsTemp.Count).Delete

End If

datos = Range(Cells(3, 4), Cells(p + 3, 6)).Address ' rango a graficar

Set graf = Charts.Add 'grafico y sus caracteristicas

'caracteristicas

With graf

.Name = "Grafico"

.ChartType = xlXYScatterSmoothNoMarkers

.SetSourceData Source:=Sheets("hoja1").Range(datos), PlotBy:=xlColumns

.Location where:=xlLocationAsObject, Name:="hoja1"

End With

With ActiveChart

.HasTitle = True

.ChartTitle.Characters.Text = "RESPUESTA FRECUENCIAL CONTROLADOR PROPORCIONAL"

.Axes(xlCategory, xlPrimary).HasTitle = True

.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Tiempo(radianes/min)"

.Axes(xlValue, xlPrimary).HasTitle = True

.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Ganancia - dB, Desfase - grados"

.Axes(xlValue, xlPrimary).HasTitle = True

.Axes(xlCategory).ScaleType = xlScaleLogarithmic

End With

'cambia el grafico de posicion

With ActiveChart.Parent

.Left = 370:

.Top = 20:

End With

'el programa termina en una celda vacia

Range("A17").Select

ActiveCell.FormulaR1C1 = ""
Range("A17").Select

Selection.Font.Bold = True

End Sub

You might also like