You are on page 1of 10

PID controller for DC motor

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

Intrigal gain I_output += double.[PID CONTROLLER FOR DC MOTOR] PID controller for DC motor Implementation of PID controller Proportional gain P_output =double. Mechatronics & System Automation Applications |EE4320 1 .Text)*omega_error*dt.Text) * omega_error. Derivative gain A low pass filter is added to prevent the system from becoming unstable.Parse(numKp.Parse(numKi.

Implementation of motor model ‫ܭ‬௠ ∶ ‫ݐ݊݁ݐݏ݊݋ܿ ݎ݋ݐ݋ܯ‬ ܴ ∶ ‫ ݊݅ ݁ܿ݊ܽݐݏ݅ݏ݁ݎ ݎ݋ݐ݋ܯ‬Ω ‫ ݊݅ ݁ܿ݊݁ݐܿݑ݀݊݅ ݎ݋ݐ݋ܯ ∶ ܮ‬H ‫ݎ݋ݐ݋ܯ‬௜௡ ∶ ݅݊‫ ݎ݋ݐ݋݉ ݋ݐ ݐݑ݌‬ൌ ‫ݔ‬ ‫ݎ݋ݐ݋ܯ‬௢௨௧ ∶ ‫ ݎ݋ݐ݋݉ ݂݋ ݐݑ݌ݐݑ݋‬ൌ ‫ݕ‬ ‫ܭ‬௠ ‫ݕ‬ൌ ൬ ൰‫ݔ‬ ‫ ݏܮ‬൅ ܴ ‫ݕ‬ሺ‫ ݏܮ‬൅ ܴሻ ൌ ‫ܭݔ‬௠ ‫ݕ‬ൌ ‫ ݕ‬ൌ ‫׬‬ቀ ‫ܭݔ‬௠ ‫ܴݕ‬ െ ‫ݏܮ‬ ‫ݏܮ‬ ௫௄೘ ቁ ݀‫ݐ‬ ௅ െ ‫ ׬‬ቀ ቁ ݀‫ݐ‬ ௅ ௬ோ ‫ ݕ‬ൌ ‫݌݉݁ݐ‬1 െ ‫݌݉݁ݐ‬2 implementation: temp1 += (motor_input * Km * dt) / L.Text) *omega_error) .temp2.Parse(numKd. motor_output = temp1 . temp2 += (motor_output * R * dt) / L.tempD.[PID CONTROLLER FOR DC MOTOR] ‫߱ ∶ ݔ‬௘௥௥௢௥ ‫ݐ݌ݐݑ݋_ܦ ∶ ݕ‬ ‫ݏ‬ ‫ݕ‬ൌ ൬ ൰‫ݔ‬ ‫ݏ‬൅݃ ‫ݕ‬ሺ‫ ݏ‬൅ ݃ሻ ൌ ‫ݏݔ‬ ‫ݕ‬ൌ ‫ݔ‬െ ‫݃ݕ‬ ‫ݏ‬ ‫ ݕ‬ൌ ‫ ݔ‬െ න ‫ݐ݀ ݃ݕ‬ tempD += D_output * dt. D_output = (double. Mechatronics & System Automation Applications |EE4320 2 .

RollingPointPairList speed_desired = new RollingPointPairList(1200). System. System. double P_output. System. Mechatronics & System Automation Applications |EE4320 3 . dt. System. temp2. tempD.[PID CONTROLLER FOR DC MOTOR] Implementation of load model ‫ ݊݅ ܽ݅ݐݎ݁݊݊݅ ݀ܽ݋ܮ ∶ ܬ‬Kgmଶ s ିଶ ܾ ∶ ‫ ݊݅݊݋݅ݐ݂ܿ݅ ݀ܽ݋ܮ‬Nm ‫ݎ݋ݐ݋ܯ‬௢௨௧ ∶ ‫ ݎ݋ݐ݋݉ ݂݋ ݐݑ݌ݐݑ݋‬ൌ ‫ݔ‬ ߱௢௨௧ ∶ ‫ ݀݁݁݌ݏ ݐ݌ݐݑ݋‬ൌ ‫ݕ‬ 1 ‫ݕ‬ൌ ൬ ൰‫ݔ‬ ‫ ݏܬ‬൅ ܾ ‫ݕ‬ሺ‫ ݏܬ‬൅ ܾሻ ൌ ‫ݔ‬ ‫ݕ‬ൌ ௫ ‫ݔ‬ ‫ܾݕ‬ െ ‫ݏܬ‬ ‫ݏܬ‬ ‫ ݕ‬ൌ ‫ ׬‬ቀ ቁ ݀‫ ݐ‬െ ‫ ׬‬ቀ ቁ ݀‫ݐ‬ ௃ ௃ ௬௕ ‫ ݕ‬ൌ ‫݌݉݁ݐ‬3 െ ‫݌݉݁ݐ‬4 implementation: temp3 += (load_input * dt) / J.motor_output. Code using using using using using using using using using System. System. int tickStart = 0. temp4 += (load_output * b * dt) / J. namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(). temp1. ZedGraph.Text.temp4. temp3. temp4.Drawing. } //Graph curves RollingPointPairList speed_actual = new RollingPointPairList(1200).Generic.Forms.ComponentModel.Windows. System.Collections. D_output.Data. System. I_output.Linq.omega_out. load_output = temp3 .

Parse(numKp.Text). //Load Inertia double b = double. //Motor Resistance temp1 += (motor_input * Km * dt) / L.Parse(numR. P_output = double. return PID_output.Parse(numB. } /* ************************************************************************************************** * 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.Text) * omega_error) .Text).Text) * omega_error * dt.Parse(numVBase. //Load Friction temp3 += (load_input * dt) / J. double motor_output) { double Km = double. tempD += D_output * dt. I_output += double.Parse(numL. } /* ************************************************************************************************** * 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. return motor_output.Text).Text). //Motor Constant double L = double.Text) * omega_error. motor_output = temp1 .Parse(numKm.Text). double load_output) { double J = double.Parse(numKd. temp2 += (motor_output * R * dt) / L.Parse(numJ.Parse(numKi. Mechatronics & System Automation Applications |EE4320 4 .temp2.[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. //Proportional Gain //Differential Gain //Integral Gain PID_output = P_output+Constrain(D_output) + Constrain(I_output) + double.tempD. D_output = (double.Text). //Motor Inductance double R = double.

load_output = temp3 .TickCount . } /* ************************************************************************************************** * Timer #1 Interrupt handler * * Description : Scan user inputs and populate outputs * * Arguments : windows form objects * * Returns : none ************************************************************************************************** */ private void timer1_Tick(object sender.tickStart) / 1000. } /* ************************************************************************************************** * 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. Mechatronics & System Automation Applications |EE4320 5 .temp4. double Vemf = back_EMF(omega_out). EventArgs e) { double time = (Environment. return Vemf. double omega_error = omega_desired .[PID CONTROLLER FOR DC MOTOR] temp4 += (load_output * b * dt) / J. double Va = PID_Controller_output(omega_error). //Desired Speed dt = timer1. } return value. int max = 1000.0.Vemf. 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. omega_out). if (value > max) { value = max.omega_out. } if (value < min) { value = min.0.Text).Parse(numKb. double motor_input = Va .Interval / 1000. motor_output = motor(motor_input. double omega_desired = double. omega_out = motor_load(motor_output. //Back EMF Constant double Vemf = omega_out* Kb. motor_output).Parse(txtOmegaInput.Text).

Invalidate(). omega_desired).CurveList.GraphPane.Scale.Points does not // support IPointListEdit. } /* ************************************************************************************************** * Timer #2 Interrupt handler * * Description : Update Graph * * Arguments : windows form objects * * Returns : none ************************************************************************************************** */ private void timer2_Tick(object sender.Points as IPointListEdit.AxisChange(). // Get the PointPairList IPointListEdit list = curve.0.XAxis. if (time > xScale. so we won't be able to modify it if (list == null) return. it means the reference at curve.TickCount . with one // major step between the max X value and the end of the axis Scale xScale = zg1.Text = string. } try { // Make sure the Y axis is rescaled to accommodate actual data zg1.CurveList[0] as LineItem.0. // Keep the X scale at a rolling 30 second interval.MajorStep) { xScale.[PID CONTROLLER FOR DC MOTOR] txtOmegaOut.Add(time.MajorStep. omega_out). EventArgs e) { if (zg1. speed_desired. if (curve == null) return.xScale. // If this is null. //Plot curves----------------------------------------------------------speed_actual.00}".30. } catch (Exception) { } } /* ************************************************************************************************** * Timer #2 Interrupt handler * * Description : Initialize Graph * * Arguments : windows form objects * Mechatronics & System Automation Applications |EE4320 6 .Max . xScale. // Time is measured in seconds double time = (Environment.Format("{0:0.Min = xScale.GraphPane.Count <= 0) return.Max .Add(time.tickStart) / 1000.Max = time + xScale. omega_out). // Force a redraw zg1. // Get the first Curve Item in the graph LineItem curve = zg1.GraphPane.

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

KD = 30 Mechatronics & System Automation Applications |EE4320 8 . KI = 5. KD = 20 KP = 20. KI = 50. KI = 10. KD = 10 KP = 10. KI = 50.[PID CONTROLLER FOR DC MOTOR] Outputs KP = 10. KD = 10 KP = 10.

[PID CONTROLLER FOR DC C MOTOR] Implementing PID controller using OpAmps Proportional roportional Derivative erivative Summer Inverter Integral Mechatronics & System Automation Applications |EE4320 9 .