Вы находитесь на странице: 1из 10

PID controller for DC motor

E G N Karunaratne
080224 L
Mechatronics & System Automation Applications EE 4320

[PID CONTROLLER FOR DC MOTOR]

PID controller for DC motor

Implementation of PID controller


Proportional gain
P_output =double.Parse(numKp.Text) * omega_error;

Intrigal gain
I_output += double.Parse(numKi.Text)*omega_error*dt;

Derivative gain

A low pass filter is added to prevent the system from becoming unstable. Mechatronics & System Automation Applications |EE4320 1

[PID CONTROLLER FOR DC MOTOR]


_


tempD += D_output * dt; D_output = (double.Parse(numKd.Text) *omega_error) - tempD;

Implementation of motor model


H

1 2 implementation:
temp1 += (motor_input * Km * dt) / L; temp2 += (motor_output * R * dt) / L; motor_output = temp1 - temp2;

Mechatronics & System Automation Applications |EE4320

[PID CONTROLLER FOR DC MOTOR] Implementation of load model


Kgm s Nm 1

3 4 implementation:
temp3 += (load_input * dt) / J; temp4 += (load_output * b * dt) / J; load_output = temp3 - temp4;

Code
using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; ZedGraph;

namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //Graph curves RollingPointPairList speed_actual = new RollingPointPairList(1200); RollingPointPairList speed_desired = new RollingPointPairList(1200); int tickStart = 0; double P_output, D_output, I_output, tempD, temp1, temp2, temp3, temp4, dt,motor_output,omega_out;

Mechatronics & System Automation Applications |EE4320

[PID CONTROLLER FOR DC MOTOR]


/* ************************************************************************************************** * Generate PID controller output * * Description : This function impliments the PID controller * * Arguments : omega_error is the error input to the PID controller * * Returns : PID_output is the output of the PID controller ************************************************************************************************** */ private double PID_Controller_output(double omega_error) { double PID_output; P_output = double.Parse(numKp.Text) * omega_error; tempD += D_output * dt; D_output = (double.Parse(numKd.Text) * omega_error) - tempD; I_output += double.Parse(numKi.Text) * omega_error * dt; //Proportional Gain //Differential Gain //Integral Gain

PID_output = P_output+Constrain(D_output) + Constrain(I_output) + double.Parse(numVBase.Text); return PID_output; }

/* ************************************************************************************************** * Generate motor simulation model output * * Description : This function impliments the DC motor model * * Arguments : motor_input is the input to the DC motor model * : motor_output is the prvious output of the motor model * * Returns : motor_output is the new output of the motor model ************************************************************************************************** */ private double motor(double motor_input, double motor_output) { double Km = double.Parse(numKm.Text); //Motor Constant double L = double.Parse(numL.Text); //Motor Inductance double R = double.Parse(numR.Text); //Motor Resistance temp1 += (motor_input * Km * dt) / L; temp2 += (motor_output * R * dt) / L; motor_output = temp1 - temp2; return motor_output; }

/* ************************************************************************************************** * Generate load model output * * Description : This function impliments the load model * * Arguments : load_input is the input to the load model * : load_output is the prvious output of the load model * * Returns : load_output is the new output of the load model ************************************************************************************************** */ private double motor_load(double load_input, double load_output) { double J = double.Parse(numJ.Text); //Load Inertia double b = double.Parse(numB.Text); //Load Friction temp3 += (load_input * dt) / J;

Mechatronics & System Automation Applications |EE4320

[PID CONTROLLER FOR DC MOTOR]


temp4 += (load_output * b * dt) / J; load_output = temp3 - temp4; return load_output; }

/* ************************************************************************************************** * Generate Back EMF of the motor model * * Description : This function impliments the Back EMF of the motor model * * Arguments : omega_out is the output speed of the motor model in rad/s * * Returns : Vemf is the Back EMF of the motor model ************************************************************************************************** */ private double back_EMF(double omega_out) { double Kb = double.Parse(numKb.Text); //Back EMF Constant double Vemf = omega_out* Kb; return Vemf; }

/* ************************************************************************************************** * Constrain output * * Description : Used to bound values to prevent system from been unstable * * Arguments : value is the input value to be checked * * Returns : bounded value ************************************************************************************************** */ private double Constrain(double value) { int min = -1000; int max = 1000; if (value > max) { value = max; } if (value < min) { value = min; } return value; } /* ************************************************************************************************** * Timer #1 Interrupt handler * * Description : Scan user inputs and populate outputs * * Arguments : windows form objects * * Returns : none ************************************************************************************************** */ private void timer1_Tick(object sender, EventArgs e) { double time = (Environment.TickCount - tickStart) / 1000.0; double omega_desired = double.Parse(txtOmegaInput.Text); //Desired Speed dt = timer1.Interval / 1000.0; double omega_error = omega_desired - omega_out; double Va = PID_Controller_output(omega_error); double Vemf = back_EMF(omega_out); double motor_input = Va - Vemf; motor_output = motor(motor_input, motor_output); omega_out = motor_load(motor_output, omega_out);

Mechatronics & System Automation Applications |EE4320

[PID CONTROLLER FOR DC MOTOR]


txtOmegaOut.Text = string.Format("{0:0.00}", omega_out); //Plot curves----------------------------------------------------------speed_actual.Add(time, omega_out); speed_desired.Add(time, omega_desired); }

/* ************************************************************************************************** * Timer #2 Interrupt handler * * Description : Update Graph * * Arguments : windows form objects * * Returns : none ************************************************************************************************** */ private void timer2_Tick(object sender, EventArgs e) { if (zg1.GraphPane.CurveList.Count <= 0) return; // Get the first Curve Item in the graph LineItem curve = zg1.GraphPane.CurveList[0] as LineItem; if (curve == null) return; // Get the PointPairList IPointListEdit list = curve.Points as IPointListEdit; // If this is null, it means the reference at curve.Points does not // support IPointListEdit, so we won't be able to modify it if (list == null) return; // Time is measured in seconds double time = (Environment.TickCount - tickStart) / 1000.0; // Keep the X scale at a rolling 30 second interval, with one // major step between the max X value and the end of the axis Scale xScale = zg1.GraphPane.XAxis.Scale; if (time > xScale.Max - xScale.MajorStep) { xScale.Max = time + xScale.MajorStep; xScale.Min = xScale.Max - 30.0; } try { // Make sure the Y axis is rescaled to accommodate actual data zg1.AxisChange(); // Force a redraw zg1.Invalidate(); } catch (Exception) { } }

/* ************************************************************************************************** * Timer #2 Interrupt handler * * Description : Initialize Graph * * Arguments : windows form objects *

Mechatronics & System Automation Applications |EE4320

[PID CONTROLLER FOR DC MOTOR]


* Returns : none ************************************************************************************************** */ public void CreateChart(ZedGraphControl zgc) { GraphPane graph = zgc.GraphPane; // Set the titles and axis labels graph.XAxis.Title.Text = "Time (s)"; graph.YAxis.Title.Text = "Speed (rad/s)"; graph.XAxis.Title.FontSpec.Size = 20; graph.YAxis.Title.FontSpec.Size = 20; LineItem curve; curve = graph.AddCurve("Actual Speed", speed_actual, Color.Red, SymbolType.None); curve = graph.AddCurve("Desired Speed", speed_desired, Color.Blue, SymbolType.None); // Show the x axis grid graph.XAxis.MajorGrid.IsVisible = true; graph.YAxis.MajorGrid.IsVisible = true; graph.XAxis.Scale.Min = 0; graph.XAxis.Scale.Max = 5; // turn off the opposite tics so the Y tics don't show up on the Y2 axis graph.YAxis.MajorTic.IsOpposite = false; graph.YAxis.MinorTic.IsOpposite = false; // Don't display the Y zero line graph.YAxis.MajorGrid.IsZeroLine = true; // Align the Y axis labels so they are flush to the axis graph.YAxis.Scale.Align = AlignP.Inside; // Fill the axis background with a gradient graph.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); // Sample at 50ms intervals timer1.Interval = 50; timer1.Enabled = true; timer1.Start(); // Calculate the Axis Scale Ranges try { zgc.AxisChange(); } catch (Exception er) { } tickStart = Environment.TickCount; } private void Form1_Load(object sender, EventArgs e) { zg1.Visible = true; CreateChart(zg1); } private void zg1_Load(object sender, EventArgs e) { } } }

Mechatronics & System Automation Applications |EE4320

[PID CONTROLLER FOR DC MOTOR]


Outputs KP = 10, KI = 5, KD = 10

KP = 10, KI = 10, KD = 10

KP = 10, KI = 50, KD = 20

KP = 20, KI = 50, KD = 30

Mechatronics & System Automation Applications |EE4320

[PID CONTROLLER FOR DC C MOTOR] Implementing PID controller using OpAmps


Proportional roportional

Derivative erivative

Summer

Inverter

Integral

Mechatronics & System Automation Applications |EE4320

Вам также может понравиться