Chat with us, powered by LiveChat ME 330 SDSU Closed Loop Servo Controller Lab Report | excelpaper.org/
+1(978)310-4246 credencewriters@gmail.com
  

All of the instructions are attached, including a template that should be followed.

When you start this report please be as detailed as possible and have good amount of writing and discussion

as for the procedures;

you should be writing descriptive paragraph on the setup and debugging and not rewrite the steps. Assume the reader is familiar with the procedure but needs to be made aware your specific setup, issues, and opportunities. Some of the debugs include misplacing the the wires on the physical circuit and connections in the wires, not getting the right data.


DO NOT COPY FROM THE LAP MANUAL


ALL LAB QUESTIONS must be answered and detailed, no short answer


Follow the template and show the equations used, calucations, and all the steps

ME-330
Mechatronics – Laboratory 5
Closed Loop Servo Controller
Dr. Luis Galvis, Prof. Wield, N. A. Satterlee
Department of Mechanical Engineering
SDSU ME 330
Laboratory 5
Contents
General Guidance:
Section 1. Introduction and Objectives
Section 2. Proportional Position Controller
Section 2.1 Required Components
Section 2.2 Physical Circuit (experimentation)
Section 2.2.1 Building the Circuits
Section 2.3 Arduino Code (experimentation)
Section 2.4 Data Collection (experimentation)
Section 2.5 Questions (analyze and interpret)
Section 3. Derivative Position Controller
Section 3.1 Questions (analyze and interpret)
Section 4. System Modeling
Section 4.1 Setup Matlab (experimentation)
Section 4.2 Questions (analyze and interpret)
Section 5. Proportional Integral Position Controller
Section 5.1 System tuning (experimentation)
Section 5.2 Questions (analyze and interpret)
Section 6. Proportional Integral Derivative Position Controller
Section 6.1 System tuning (experimentation)
Section 6.2 Questions (analyze and interpret)
Appendix A: System Types
Appendix B: System Modeling
Appendix C: PID Controller
Appendix D: Analog to Digital converter – ADC
3
4
6
6
6
1
2
4
5
5
5
6
6
9
9
9
9
11
11
11
12
13
13
14
Table of Figures
Figure 1: Closed-Loop flow chart
Figure 2: Servo design diagram
Figure 3: Servo and Input Circuits
Figure 4: Potentiometers
Figure 5: L298N motor driver.
Figure 6: Code convention used for the Power Rails and Terminal Strip Blocks
Figure 7: Completed and fully assembled circuit
Figure 8: Example step input and output
Figure 9: Example derivative controller
Figure 10: Example integral controller.
Figure 11: System response types
4
5
6
7
7
8
1
4
5
10
11
2
SDSU ME 330
Laboratory 5
General Guidance:
Learning objectives:
1. An ability to develop and conduct appropriate experimentation, analyze and interpret data, and
use engineering judgment to draw conclusions.
2. An ability to function effectively on a team whose members together provide leadership, create
a collaborative and inclusive environment, establish goals, plan tasks, and meet objectives.
It is recommended that students will review the lab instructions in advance. Students should come to
the lab prepared with questions for the instructor and TA.
The labs are intended to be completed by four students cooperating as a team. Troubleshooting is an
expected part of the lab learning. Students within a group or between groups should collaborate during
the lab sessions to complete the labs and troubleshoot issues and discuss the results. Students will be
asked to provide confidential peer evaluation in the areas of leadership, creating a collaborative and
inclusive environment, establish goals, plan tasks, and meet objectives.
Physical circuit photographs are generally not included, or the details have been omitted. Physical
circuit board photographs can easily be misinterpreted. Circuit diagrams and schematics present a more
concise and accurate specification for the circuit. When breadboarding, please use the circuit diagram.
In the sections identified as “experimentation, students should discuss in the lab report any key
learnings especially from trouble shooting any issues the group encountered and how the group was
able to solve them.
In the sections identified as “analyze and interpret”, questions are presented. It is expected that the
students read the entire document and answers all questions in the lab report. Students are expected to
perform some research to answer questions. Research is to be cited in the report and documented in
the references.
3
SDSU ME 330
Laboratory 5
Section 1. Introduction and Objectives
This lab is intended to provide the student with a basic overview of Arduino programming and closedloop position control with PWM. You will be using a servo mech, input signal, Arduino, and motor driver.
You will induce a step input and measure the output response, use Matlab for system modeling, and
tune your PID controller.
In this lab you will complete the following objectives:
1.
2.
3.
4.
5.
6.
Build circuits with breadboard
Perform circuit analysis with semiconductors for digital and analog systems
Use component datasheets to analyze circuit performance
Implement closed-loop PID controller
Tune PID controller and define system response type
Determine circuit improvements for the system(s)
In addition to the objectives above, control system analysis and transfer function computation will be
performed. If the lab is presented in person, each team should determine which individual builds the
circuit and which individual writes the Arduino code. Once the work is complete, the team should
convene virtually to complete the lab report.
Be sure to take screenshots, take pictures, and save files. Anything that might be useful when
completing the lab report should be recorded for future use.
Figure 1: Closed-Loop flow chart
Figure 1 displays our basic system flowchart. In this system, the Plant is our circuit. The input to the
plant is a PWM voltage and the output is the position, C(s). The controller is our Arduino board. It takes
the position error, E(s), and outputs a voltage. The error is the setpoint position, R(s) minus the output
position from the plant, C(s). The function u(t) can be described by the equation below:
Equation 1

( ) = ( ) + ∫0
( ′ ) ′ +
( )

We can then take the Laplace transform and describe the equation as a transfer function in the Laplace
domain:
4
SDSU ME 330
Laboratory 5
( ) = + +
R(s)
E(s)

2 +
Kd
=
2 + +

Kp
K
+ i
Kd
Kd
2 +
=

G(s)

+

C(s)

See Appendix A to determine what system type G(s) is anticipated to be. Based on the theoretical
system, we can decide what type of controller we need. In this lab, we will test these assumptions to
determine the theoretical system that should work vs the system that is ultimately selected.
In this lab, we will build and program a servo motor. A servo is simply a gear motor attached to a
potentiometer and controlled via a circuit. A diagram for a servo motor is displayed in Figure 2 below.
There are typically 3 types: Analog, Digital, and Continuous (PWM). Analog and Continuous require
PWM signals. But continuous is missing a gear that connects the motor to the sensor and can only turn
forwards and reverse. A Digital servo requires a library or function and only accepts a binary signal from
a microcontroller. The signal is then interpreted, and the onboard controller moves the motor to the
correct position.
Figure 2: Servo design diagram
5
SDSU ME 330
Laboratory 5
Section 2. Proportional Position Controller
Section 2.1 Required Components
1x Arduino Board
1x ELVIS Board
1x Potentiometer
1x Gearmotor + Potentiometer Board (Servo)
1x LM298N Motor Controller
Section 2.2 Physical Circuit (experimentation)
The DC motor is coupled to a potentiometer identified as . This potentiometer is used to sense
the angular position of the motor shaft. There is another potentiometer used for position setpoint
identified as . Both the servo and setup potentiometers are connected as voltage dividers as
shown in Figure 3. These circuits will use the voltage supplied by the Arduino. The location and terminal
identification of the potentiometers is shown in Figure 4.
The Arduino will control the DC motor via the L298N motor driver board shown in Figure 5. The L298N
motor driver board will be powered by the +5V on the Elvis board. This is needed to get more current to
the motor which will enable the higher torque required for the step response.
Recall the control connections on the motor driver: Enable A is PWM control motor voltage (speed) of
motor A. Input 1 and Input 2 are logic control for direction of rotation of motor A. Since were are going
to connect the board to an external 5V supply, you must remove the jumper of the L298N (see figure 5).
You are going to use the ELVIS II Breadboard following the code convention shown in Figure 6.
Reminder: Be sure to connect the Arduino ground to the Elvis ground.
Figure 3: Servo and Input Circuits
6
SDSU ME 330
Laboratory 5
Figure 4: Potentiometers
(a) Setpoint potentiometer terminals. (b) Location and terminals of the servo potentiometer.
Figure 5: L298N motor driver.
7
SDSU ME 330
Laboratory 5
Figure 6: Code convention used for the Power Rails and Terminal Strip Blocks
on the ELVIS II Breadboard.
8
Section 2.2.1 Building the Circuits
Assemble each of the circuits following the diagram in Figure 7.
The circuit shows the suggested location of jumper wires identified as (i = 1…22). The jumper connection details are shown in Table 1.
Figure 7: Completed and fully assembled circuit
Table 1: Detail of breadboard connections.
Jumper
From
To
J1
M(+)
L298N(OUT1)
J2
M(-)
L298N(OUT2)
J3
Rservo(3)
BB-C2-(1A)
J4
Rservo(2)
BB-C2-(2A)
J5
Rservo(1)
BB-C2-(3A)
J6
BB-C2-(1E)
PR-C3 (+)
J7
BB-C2-(2E)
Arduino(A0)
J8
BB-C2-(3E)
PR-C3 (-)
J9
Arduino(5V)
PR-C3 (+)
J10
Arduino(GND)
PR-C3 (-)
N/A
Rpot(1)
BB-C3 (5J)
N/A
Rpot(2)
BB-C3 (3J)
N/A
Rpot(3)
BB-C3 (1J)
J11
BB-C3 (5F)
PR-C3 (-)
J12
BB-C3 (3F)
Arduino(A1)
J13
BB-C3 (1F)
PR-C3 (+)
J14
Arduino(D9)
L298N(ENA)
J15
Arduino(D8)
L298N(IN1)
J16
Arduino(D7)
L298N(IN2)
J17
L298N(VCC)
PR-C2 (+)
J18
L298N(GND)
PR-C2 (-)
J19
L298N(5V)
PR-C2 (+)
J20
ELVIS(GROUND) PR-C2 (-)
J21
ELVIS(+5V)
PR-C2 (+)
J22
PR-C2 (-)
PR-C3 (-)
Note: Columns 2 and 3 of Table 1 show the component followed by its terminal tag between
parentheses.
SDSU ME 330
Laboratory 5
Section 2.3 Arduino Code (experimentation)
We will define the pins used by the Arduino. Pin A0 and A1 are used for our potentiometer pins, and
pins 7, 8, and 9 are used for the motor direction and speed. Note that the proportional constant should
be modified so the output is smooth.
#define
#define
#define
#define
#define
POT1 A0 //servo potentiometer pin
POT2 A1 //input potentiometer pin
DIRA 7 //Direction pin
DIRB 8 //Direction pin
ENABLE 9 //Enable pin
Now the global variables used throughout the program are defined in this section.
float
float
float
float
float
float
float
float
float
float
float
float
error=0;
previous_error=0;
integral=0;
derivative=0;
proportional=0;
dt=0;
tic=0;
toc=0;
Kd=0.0;
Kp=10;
Ki=0.0;
PWM=0;
//Define
//Define
//Define
//Define
error variable
previous error for derivative
integral variable
derivative variable
//Derivative Constant
//Proportional Constant
//Integral Constant
The input and output pins are defined in the setup function.
void setup() {
// put your setup code here, to run once:
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
pinMode(ENABLE, OUTPUT);
pinMode(POT1, INPUT);
pinMode(POT2, INPUT);
Serial.begin(9600);
TCCR1B = TCCR1B & B11111000 | B00000001; // Changes the PWM to 31kHz
}
2
SDSU ME 330
Laboratory 5
void loop() {
// put your main code here, to run repeatedly:
previous_error=error;
int position_servo = analogRead(POT1);
int position_input = analogRead(POT2);
error=position_input-position_servo;
//compute position error
integral=integral+Ki*error*dt;
// compute integral
derivative=(error-previous_error)*Kd/dt; // compute derivative
proportional=Kp*error;
// compute proportional
PWM=proportional+derivative+integral;
// input to plant computed from
PID controller
dt=(tic-toc)/1000.0;
//time in seconds
toc=tic;
tic=millis();
if (PWM>255)
// limit PWM output
PWM=255;
if (PWM0){
// Set motor direction to motor
controller
digitalWrite(DIRA,1);
digitalWrite(DIRB,0);
analogWrite(ENABLE,PWM);
}
else{
digitalWrite(DIRA,0);
digitalWrite(DIRB,1);
analogWrite(ENABLE,-PWM);
}
// Print relevant data
Serial.print(error);
Serial.print(” “);
Serial.print(previous_error);
Serial.print(” “);
Serial.print(integral);
Serial.print(” “);
Serial.print(derivative);
Serial.print(” “);
Serial.print(PWM);
Serial.print(” “);
Serial.print(dt);
Serial.print(” “);
Serial.print(position_servo);
Serial.print(” “);
Serial.println(position_input);
}
3
SDSU ME 330
Laboratory 5
Section 2.4 Data Collection (experimentation)
1)
2)
3)
4)
Turn on your Arduino and Elvis board.
Run the serial monitor.
Clear your serial monitor output.
Set the setpoint potentiometer to its “zero” position. You can check the zero position as 0 on
the serial monitor.
5) Create a step input by quickly turning your potentiometer as shown in Figure 8 (Note that this is
a tuned controller so your data will not look the same). The knob does not need to be turned all
the way, we simply want to high slope for a brief time as shown in the figure. The slope should
be as steep as possible. Note: Do not turn the input to the maximum (1023) try to keep it
around 500-700.
6) Uncheck the auto scroll line and copy the output
7) Paste it into MS Excel. Note that the data is not pasted into columns. You will need to navigate
to the data tab and select text to columns. Select delimited, select next, then check the “space”
box and click finish. The data will now be in tabs.
8) Open the MATLAB script lab5_plotter.m and run SECTION 1 of this script.
9) In MATLAB, double-click on variable results. You should see a table with two rows of zero.
10) Copy the Excel data obtained in step 7 and paste it on the MATLAB “table” from step 9. You will
overwrite the content of results.
11) Run SECTION 2 of script lab5_plotter.m. You should see the plot of the input and output
signals. The plot is similar to the graph shown in Figure 8 but not equal because you are using
MATLAB.
12) Go to SECTION 3 of script lab5_plotter.m. You will save the data to file. Type the file name
by replacing XXXX on the following line
my_filename = ‘XXXX’;
Run the script section afterwards.
Figure 8: Example step input and output
4
SDSU ME 330
Laboratory 5
Section 2.5 Questions (analyze and interpret)
1) Increase and decrease the proportional constant (e.g. 1.5, 2.5, 5.5, 15.0, 20). Follow the same
steps 2-12 of section 2.4 and discuss what you observe.
2) Find the proportional constant threshold at which the output is oscillatory. Save the data for this
case.
3) Based on the results of question 1, what system type is this?
4) Review Appendix A, theoretically what system type is this?
5) If the actual and theoretical systems are not the same explain why the discrepancy exists.
Section 3. Derivative Position Controller
For this section simply modify the code so:


the derivative constant is greater than zero (e.g. 0.5, 1.0, 2.0, 5.0 ) and,
the proportional constant is zero.
An example of a response is provided in Figure 9. Hint: If you’re having trouble getting reasonable
results, try using a smaller Kd value.
Section 3.1 Questions (analyze and interpret)
1) Increase and decrease the derivative constant. Follow the same steps 2-12 of section 2.4 and
discuss what you observe.
2) Based on the results of question 1, what system type is this?
3) Review Appendix A, theoretically what system type is this?
4) If the actual and theoretical systems are not the same explain why the discrepancy exists.
Figure 9: Example derivative controller
ADC – Analog to Digital Converter. See Appendix D
5
SDSU ME 330
Laboratory 5
Section 4. System Modeling
In this section, you will take your derivative controller data and use MATLAB to suggest proportional and
integral gains that you can then try on your physical circuit. Set the proportional and integral constants
to zero and only use the derivative constant. What we are doing is eliminating the forward integral in
Equation 1 so we can estimate it more easily. Since this was done in the previous section, simply use the
data from that section.
Section 4.1 Setup Matlab (experimentation)
1) Open the data file you want to use. In MATLAB, double click on the file you want to open.
2) Find the transfer function using the tfest function. You only need to run SECTION 4 of script
lab5_plotter.m.
Note: if you get an error, check Manage add ons to see if you have the Control System Toolbox
loaded. If not, load it in.
3) Note that in
g
= tfest(data,2,0);
, the numbers 2 and 0 are used to estimate for a system with 2 poles (denominator) and no
zeroes (numerator). These are specified since we are trying to recreate the transfer function.
MATLAB should now output an estimated transfer function like the one shown below:
Notice the fit estimation is high here (96%). If you obtain a low estimation (>85%) try repeating the
process beginning with the physical data collection.
4) Now we will add back in the forward integral we removed by respecifying the transfer function
using the coefficients of the transfer function with “tf” command as shown in the following
example
G = tf([349],[1 32.7 846 0]);
(example only)
Here, you will
• place the scalar of the transfer function numerator in the first term between square
brackets ( e.g. [349])
6
SDSU ME 330
Laboratory 5

place the polynomial coefficients on the transfer function denominator followed by zero
in the second term between square brackets (e.g. [1 32.7 846 0]).
5) The output for the transfer function above is below:
6) Now type in controlSystemDesigner(G) and a window like the following will open:
7) Navigate to tuning methods and click PID Tuning
8) Under controller type select PI and move the response time down to around 1.5s and update
the compensator
7
SDSU ME 330
Laboratory 5
9) Now observe that the window below has been updated.
10) Try using the compensator results in the physical circuit and compare. Note that for above, the
integral constant is 1.2 and the proportional constant is 1.2*1.8 (More information on this can
be found in Appendix B). The results for the reference circuit are below. (Note that your step
input should stop around 500-600 so there is room for an overshoot. If the step input goes to
1023, the potentiometer attached to the motor cannot exceed 1023; therefore, no overshoot
will occur. The step input should have as steep a slope as possible. The action should be similar
to snapping your fingers.
8
SDSU ME 330
Laboratory 5
11) The overshoot for the reference circuit is around 30% as opposed to 25% for the prediction, but
it does cross the setpoint at around 7 seconds as predicted. Overall, the prediction worked very
well. It is okay if yours does not come out this well. This took some iterations and fine-tuning.
Section 4.2 Questions (analyze and interpret)
1) Report on the results you obtained.
Section 5. Proportional Integral Position Controller
Section 5.1 System tuning (experimentation)
For this section simply modify the code so the integral and proportional constants are greater than zero
and the derivative is zero. An example of a response is provided in Figure 10. Increase and decrease the
proportional and integral constants and note your observations.
Section 5.2 Questions (analyze and interpret)
1)
2)
3)
4)
Discuss and interpret your observations from changing the PI gains.
Based on the results of question 1, what system type is this?
Review Appendix A, theoretically what system type is this?
If the actual and theoretical systems are not the same explain why the discrepancy exists.
9
SDSU ME 330
Laboratory 5
Figure 10: Example integral controller.
Note that the proportional constant was set very low to achieve the results above.
10
SDSU ME 330
Laboratory 5
Section 6. Proportional Integral Derivative Position Controller
Section 6.1 System tuning (experimentation)
Now use all three controllers by increasing them to be greater than zero and tune the system to the
specifications below. (Note these only need to be roughly tuned). You can try using the results of section
4 to help with the tuning. Also see Appendix C for tuning techniques.
1) Tune the system so that is overdamped
2) Tune the system so that it is underdamped
3) Tune the system so that it is critically damped
Figure 11: System response types
Source: https://www.motioncontroltips.com/how-to-address-overshoot-in-servo-control/
Section 6.2 Questions (analyze and interpret)
1) Discuss the challenges and results from your tuning experimentation.
2) What tuning methodology do you recommend for this system?
11
SDSU ME 330
Laboratory 5
Appendix A: System Types
Source: https://onlinelibrary.wiley.com/doi/pdf/10.1002/9781118403211.app1
Additional information on servo types:
https://support.controltechnologycorp.com/index.php?option=com_content&view=article&id=190&Ite
mid=null
12
SDSU ME 330
Laboratory 5
Appendix B: System Modeling
Appendix C: PID Controller
PID controller tuning
• YouTube: https://youtu.be/dZ8lzDi3cXY (18:13)
• Website: https://pidexplained.com/how-to-tune-a-pid-controller/
PID controller (paraphrased from: https://en.wikipedia.org/wiki/PID_controller)



Term P is proportional to the current value of the SP − PV error. If the error is large and positive,
the control output will be proportionately large and positive, considering the gain factor “K”.
Using proportional control alone will result in an error between the setpoint and the actual
process value because it requires an error to generate the proportional response. If there is no
error (or not enough error), there is no corrective response. [The P term kicks off the response
but then gets lazy.]
Term I accounts for past values of the SP − PV error and integrates them over time to produce
the I term. For example, if there is a residual SP − PV error after the application of proportional
control, the integral term seeks to eliminate the residual error by adding a control effect due to
the historic cumulative value of the error. When the error is eliminated, the integral term will
cease to grow. This will result in the proportional effect diminishing as the error decreases, but
this is compensated for by the growing integral effect. [The I term has no patience for laziness.
After a time, it takes over.]
Term D does not consider the error (meaning it cannot bring it to zero) but tries to bring the rate
of change of error to zero. It aims at flattening the error trajectory especially when there is an
oscillatory problem. [The D term just wants to keep things moving in the right direction. If P and
I are moving to minimize error, it helps. If not, it will slow them down until they turn around.]
13
SDSU ME 330
Laboratory 5
Appendix D: Analog to Digital converter – ADC
Arduino Analog pins take an input (0-5V) converts to digital values (0-1023 bits).
The Arduino takes in an analog voltage, samples the amplitude every few milliseconds, and then outputs
a digital value.
Most microcontrollers cannot output an analog voltage and must output a PWM signal that corresponds
to equivalent (choppy) analog voltage. Then a low pass filter is used to convert to an analog voltage.
14
ME-330
Mechatronics – Laboratory 0
Lab Report Example
First Name Last Name, (List remaining team members)
Department of Mechanical Engineering
Spring 2021
Abstract:
The objective of this lab report is to demonstrate to students how to write a lab report. The
abstract includes the purpose of the lab conducted, as well as a brief overview of the
procedures and results. Abstracts are typically a paragraph long.
Note that any technical details in this report are provided for formatting reference only and
should not be used for technical references.
Note that use of this template/procedure not mandatory.
Table of Contents
Section 1. Introduction ………………………………………………………………………………………………… 4
Section 2. Circuit 1 (Figure Example) …………………………………………………………………………….. 5
Section 2.1 Simulated Circuit (Table Example) …………………………………………………………….. 6
Section 2.2 Physical Circuit (Equation Example) ………………………………………………………….. 6
Section 2.3 Questions ……………………………………………………………………………………………… 6
Section 3. Circuit 2 ……………………………………………………………………………………………………… 7
Section 3.1 Simulated Circuit …………………………………………………………………………………….. 7
Section 3.2 Physical Circuit ………………………………………………………………………………………. 7
Section 3.3 Questions ……………………………………………………………………………………………… 7
Section 4. Circuit 3 ……………………………………………………………………………………………………… 7
Section 4.1 Simulated Circuit …………………………………………………………………………………….. 7
Section 4.2 Physical Circuit ………………………………………………………………………………………. 7
Section 4.3 Questions ……………………………………………………………………………………………… 7
Section 5. Conclusion …………………………………………………………………………………………………. 7
Section 5.1 Questions ……………………………………………………………………………………………… 7
Section 6. References …………………………………………………………………………………………………. 8
Appendix A – Writing Tips ……………………………………………………………………………………………. 9
Appendix B – Arduino Data ………………………………………………………………………………………….10
Appendix C – Raw Data Output ……………………………………………………………………………………12
Table of Figures
Figure 1: ESP32 Development Board …………………………………………………………………………… 4
Figure 2: Example Circuit …………………………………………………………………………………………….. 5
Figure 3: Example of Figure Captions ……………………………………………………………………………. 5
Table of Tables
Table 1: Made-up Data for Table Example ……………………………………………………………………… 6
Table of Equations
Equation 1:
Equation 2:
= + ………………………………………………………………………………………….. 6
2 = 2 + 2………………………………………………………………………………………… 9
Section 1. Introduction
This report was created to provide a template for students to use when writing lab reports as
well as procedure for writing lab reports. Simply delete the content of each section and use the
remaining sectional layout as a template. The table of contents, figures, tables, and equations
can be updated by right clicking on the table and clicking “Update Field”. Appropriate captions
must be used for figures, tables, and equations to update properly.
The introduction provides a brief overview of the objectives of the lab and how those objectives
were fulfilled. Any major issues encountered during the lab should also be described. Any new
components introduced during the lab should be described in the introduction (e.g. resistors,
capacitors, Arduino, breadboard, potentiometer, LED, etc. for Lab 1). Typically, the introduction
is 1-10 paragraphs, depending on the length of the lab and number of new components. Notice
that the sectional layout of the lab report is the same as the lab manual.
An example description of a new component is an ESP32 microcontroller as seen in Figure 1.
The ESP32 contains dual-core 32-bit LX6 processors operating at 160 or 240 MHz. Its memory
consists of 520 KiB SRAM, and features built-in 802.11 b/g/n Wi-Fi capabilities. Of particular
interest are the 18 channel (maximum) 12-bit SAR ADCs and two 8-bit DACs. The ESP32 is
compatible with a multitude of popular programming languages such as the Arduino IDE and
MicroPython. IoT (Internet of Things) OSes (Operating Systems), such as Mongoose OS, can
also be installed on the ESP32 [1]. Notice the use of the reference after stating technical details.
Figure 1: ESP32 Development Board
Section 2. Circuit 1 (Figure Example)
Provide a brief overview (typically 1 paragraph) of the circuit(s) built and analyzed in this
section. It may be useful to provide a diagram or figure like the one seen in Figure 2. This figure
can then be referenced throughout the sub-sections, as necessary.
Figure 2: Example Circuit
To add a caption to a figure, click on the figure so it selected, navigate to references at the top
of the window, then select Insert Caption, and under Options change the Label to Figure as
shown in Figure 3. To insert a caption into the report text, select Cross-reference (right next to
Insert Caption), select Figure for Reference Type, and select the appropriate figure to crossreference.
Figure 3: Example of Figure Captions
Section 2.1 Simulated Circuit (Table Example)
Each subsequent section should discuss the procedure and results for each circuit.
The procedure should not be copied and pasted from the manual. It should be summarized with
emphasis placed on any issues encountered or deviations from the procedure in the manual. If
there are multiple subsections in the lab manual, there should also be multiple sections in the
lab report.
Often it will be helpful to display data in the form of a table. To create a table, go to Insert at the
top of the window, select Table, then move your mouse cursor to select the appropriate table
size. A table example is displayed in Table 1. Creating captions for a table is the same as for a
figure, except “Table” is selected when a caption is inserted or cross-referenced.
Table 1: Made-up Data for Table Example
Data 1
17.5 V
3.2 V
9V
Data 2
2.0 A
0.9 A
1.1 A
Data 3
Blue
Red
Green
Section 2.2 Physical Circuit (Equation Example)
As with the previous section, describe the procedure and results of the corresponding section in
the manual. If equations are required, they can be inserted by selecting Insert at the top, then
clicking on Equation. Creating captions for an equation is the same as for a figure, except
“Equation” is selected when a caption is inserted or cross-referenced. An example equation is
shown in Equation 1 below.
Equation 1:
y = mx + b
Section 2.3 Questions
Each section contains a list of questions at the end. Restate each question then provide an
answer.
1) Was convergence obtained in the control system within the required time? If not,
what could be done to achieve convergence?
Convergence did not occur in the control system within the required time. It was
determined that ultimately the power supply was unable to provide the motor the
required torque to meet the requirements.
2) After completing the circuit and coupling the mechanical system via a
microcontroller and control system, what was the maximum torque measured?
How did this compare to the theoretical model?
The torque fell short of the simulation by 10%. This can be attributed to contact
resistances and part variances.
Section 3. Circuit 2
Repeat the process described in Section 2 for the next circuit.
Section 3.1 Simulated Circuit
Provided for template use.
Section 3.2 Physical Circuit
Provided for template use.
Section 3.3 Questions
Provided for template use.
Section 4. Circuit 3
Repeat the process described in Section 2 for the next circuit.
Section 4.1 Simulated Circuit
Provided for template use.
Section 4.2 Physical Circuit
Provided for template use.
Section 4.3 Questions
Provided for template use.
Section 5. Conclusion
Provide a 1-5 paragraph conclusion. Were the objectives met? State what went well and what
did not. Were there software or hardware issues? Were there any improvements that could be
made to the circuits or lab in general?
Section 5.1 Questions
Provide the conclusion questions the same way as the questions from each section.
Section 6. References
References from notable publishers only should be used. This includes manufacturer websites
and publications, websites where an author is listed with appropriate credentials, any publication
or article obtained from the SDSU library OneSearch. Avoid citing enthusiast sites and do not
cite how-to sites. While Wikipedia might be a good location to find references, Wikipedia itself
should not be cited due to its non-static nature and general lack of depth on many core
engineering topics. Below are some example references.
1. “Third-Party Platforms That Support Espressif Hardware”. Espressif Systems. Retrieved
from https://www.espressif.com/en/ecosystem/third-party-resources/third-party-sdks on
01/30/2021.
2. Fuentes, M, Vivar, M, Burgos, J.M, Aguilera, J, and Vacas, J.A. “Design of an Accurate,
Low-cost Autonomous Data Logger for PV System Monitoring Using Arduino™ That
Complies with IEC Standards.” Solar Energy Materials and Solar Cells 130 (2014): 52943. Web.
3. Alciatore, David G., and Michael B. Histand. Introduction to Mechatronics
and Measurement Systems. Fourth ed., McGraw-Hill, 2012.
4. Upgrading the WiFi shield firmware. Retrieved from
https://www.arduino.cc/en/Hacking/WiFiShieldFirmwareUpgrading on 01/30/2021.
Appendix A – Writing Tips



Take notes during your lab. Any values you measure should be recorded. If any issues
were encountered, make a note on what the issue was and what was done to solve the
issue. This will make writing the lab report much easier.
It is okay if the circuit build does not work as intended. As long as root cause analysis
was performed and documented, full credit will be awarded.
For equations, tables, and figure captions, old captions can simply be copied and pasted
to the area of interest. To update the caption, jut right click and select update field. The
equation below was updated with this method.
Equation 2:





2 = 2 + 2
Use first person passive when writing a lab report. Generally, past tense should be used
during writing. Make sure all discussions are concise. Avoid using “flowery” language
that distracts from the experiment and analysis.
If applicable, be sure to discuss the differences between your team members’ results,
and what might account for those differences. If all the results were very similar, just
state so.
Notice that the captions for figures are below the figure, captions for tables are above
the table and captions for equations are to the left of the equation.
Remember to take many pictures during the lab to help record your results. It is much
easier to delete pictures than rebuild a circuit to take pictures.
Do not leave captions hanging on a separate page from Figure or Tables. Adjust the
spacing so they are on the same page.
Appendix B – Arduino Data
Sometimes data should be documented, but it will take up too much space to document it in the
body of the report. Simply create an Appendix and reference the appendix in the report.
Typically, Arduino code that takes up more than a page should be included in the appendix
instead of the body. Some example Arduino code is provided below.
#define HEAT 6
#define THERM A5
#define POT A4
#define maxTemp 120
#define maxPWM 125
int tic;
int toc=0;
int dt;
void setup()
{
Serial.begin(9600);
pinMode(THERM, INPUT);
pinMode(HEAT, OUTPUT);
pinMode(POT, INPUT);
}
float tempF=0;
float tempSet=100;
int average=0;
float error=0;
float proportional=0;
float integral=0; float igain=0.5;
int iter=200;
float pGain=10;
void loop()
{
tic=millis(); //define tic as a global variable
dt+=tic-toc; //define toc as a global variable initialized to zero
int tempReading = analogRead(THERM);
float gain=analogRead(POT)/10.;
double tempK = log(10000.0 * ((1024.0 / tempReading – 1)));
tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK );
Temp Kelvin
float tempC = tempK – 273.15;
// Convert Kelvin to Celcius
tempF+= (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
if (average==iter){
tempF=tempF/iter;
error=(tempSet-tempF);
integral+=error;
proportional=(error*pGain+integral*igain);
if (proportional>0)
{ if (proportional>255)proportional=255;
analogWrite(HEAT, proportional);}
else
{ analogWrite(HEAT,0);}
Serial.print(“Time: “);
Serial.print(dt);
Serial.print(” F: “);
Serial.print(tempF);
Serial.print(” TempSet: “);
Serial.print(tempSet);
//
Serial.print(” Gain: “);
Serial.print(gain);
Serial.print(” error: “);
Serial.print(error);
Serial.print(” P: “);
Serial.println(proportional);
average=0;
tempF=0;
dt=0;
}
average+=1;
toc=tic;
}
Appendix C – Raw Data Output
Sometimes it is useful to display the raw data output as well as a graph. Just like the Arduino
code, if it is taking up more than a page, it should be included in the appendix. An example of
some recorded data output is shown below.
waveform
Ch0
t0
9/8/2020 17:54:57.274928
delta t 0.000200
time[0] Ch0
9/8/2020 17:54:57.274928
9/8/2020 17:54:57.275128
9/8/2020 17:54:57.275328
9/8/2020 17:54:57.275528
9/8/2020 17:54:57.275728
9/8/2020 17:54:57.275928
9/8/2020 17:54:57.276128
9/8/2020 17:54:57.276328
9/8/2020 17:54:57.276528
9/8/2020 17:54:57.276728
9/8/2020 17:54:57.276928
9/8/2020 17:54:57.277128
9/8/2020 17:54:57.277328
9/8/2020 17:54:57.277528
9/8/2020 17:54:57.277728
9/8/2020 17:54:57.277928
9/8/2020 17:54:57.278128
9/8/2020 17:54:57.278328
9/8/2020 17:54:57.278528
9/8/2020 17:54:57.278728
9/8/2020 17:54:57.278928
9/8/2020 17:54:57.279128
9/8/2020 17:54:57.279328
9/8/2020 17:54:57.279528
9/8/2020 17:54:57.279728
9/8/2020 17:54:57.279928
9/8/2020 17:54:57.280128
9/8/2020 17:54:57.280328
9/8/2020 17:54:57.280528
9/8/2020 17:54:57.280728
9/8/2020 17:54:57.280928
9/8/2020 17:54:57.281128
9/8/2020 17:54:57.281328
9/8/2020 17:54:57.281528
9/8/2020 17:54:57.281728
9/8/2020 17:54:57.281928
9/8/2020 17:54:57.282128
9/8/2020 17:54:57.282328
9/8/2020 17:54:57.282528
9/8/2020 17:54:57.282728
9/8/2020 17:54:57.282928
9/8/2020 17:54:57.283128
9/8/2020 17:54:57.283328
9/8/2020 17:54:57.283528
9/8/2020 17:54:57.283728
9/8/2020 17:54:57.283928
9/8/2020 17:54:57.284128
9/8/2020 17:54:57.284328
9/8/2020 17:54:57.284528
1.131461E-3
9.703603E-4
1.292562E-3
1.131461E-3
1.131461E-3
9.703603E-4
1.131461E-3
9.703603E-4
1.131461E-3
9.703603E-4
9.703603E-4
1.131461E-3
6.481582E-4
1.131461E-3
9.703603E-4
1.131461E-3
1.292562E-3
1.131461E-3
1.131461E-3
8.092593E-4
8.092593E-4
8.092593E-4
9.703603E-4
1.292562E-3
9.703603E-4
9.703603E-4
9.703603E-4
1.131461E-3
9.703603E-4
8.092593E-4
1.292562E-3
1.292562E-3
1.131461E-3
1.131461E-3
9.703603E-4
9.703603E-4
9.703603E-4
9.703603E-4
1.131461E-3
8.092593E-4
1.131461E-3
1.292562E-3
1.131461E-3
9.703603E-4
9.703603E-4
1.131461E-3
9.703603E-4
9.703603E-4
9.703603E-4
9/8/2020
9/8/2020
9/8/2020
9/8/2020
9/8/2020
9/8/2020
9/8/2020
9/8/2020
17:54:57.284728
17:54:57.284928
17:54:57.285128
17:54:57.285328
17:54:57.285528
17:54:57.285728
17:54:57.285928
17:54:57.286128
9.703603E-4
8.092593E-4
9.703603E-4
1.292562E-3
9.703603E-4
9.703603E-4
9.703603E-4
1.292562E-3
ME 330
Mechatronics
Mechatronics Labs
Week
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Section 1 – Tuesday
1/25/2022
02/01/22
2/8/2022
02/15/22
2/22/2022
03/01/22
3/8/2022
03/15/22
3/22/2022
3/29/2022
4/5/2022
4/12/2022
4/19/2022
4/26/2022
5/3/2022
Section 2 – Thursday
01/20/22
1/27/2022
02/03/22
02/10/22
02/17/22
02/24/22
03/03/22
03/10/22
03/17/22
03/24/22
03/31/22
04/07/22
04/14/22
04/21/22
04/28/22
05/05/22
Lab 1 Manual Basic Circuits
Lab 2 Analog Open Loop Motor Driver
Lab 3 Digital Open Loop Motor Driver
Lab 4 Closed-loop Temp Controller
Lab 5 Closed-loop Servo Controller
Lab 6 Sonic car project
Lab
Lab Topic
Merged with lecture
Lab1
Lab 1
Lab 2
Lab 2
Lab 3
Lab 3
Lab 4
Lab 4
Lab 5
Lab 5
Lab 5
Lab 6
Lab 6
Manual Basic Circuits (simulations)
Manual Basic Circuits
Analog Open Loop Motor Driver
Analog Open Loop Motor Driver
Digital Open Loop Motor Driver
Digital Open Loop Motor Driver
Closed-loop Temperature Controller
Closed-loop Temperature Controller
Spring Break (3/28 – 4/1)
Closed-loop Servo Controller
Closed-loop Servo Controller
Closed-loop Servo Controller
Sonic car project
Sonic car project
Voltage Divider, Halfwave Rectifier, Capacitor
Charging and Discharging
BJT controller, MOSFET controller, op amp
Digital BJT controller, Digital MOSFET controller, Bidirectional Motor Drive Controller, Arduino
Bang-Bang controller, Proportional controller,
Proportional Integral controller, Arduino
PID controller and tuning, Arduino
PID controller, Arduino programming
Lab 5 Closed-loop Servo Controller
Tasks …
• Build circuits with breadboard




servo mech
input signal
Arduino
motor controller
• Induce step input and measure output response
• Matlab for system modeling
• Tuning experimentation closed-loop control systems
overdamped, underdamped, critically damped
✓ proportional controller
Heater assembly including fan, heater, thermistor
✓ derivative controller
✓ proportional integral controller
Servo mech
1.
2.
3.
4.
5.
gear motor
coupler
output rotational position 0-5v (potentiometer)
input rotational position 0-5v (potentiometer)
support structure
Lab 5
Background
Suppose you wanted to go from 0 to 65,
What would you do?
How would you program your cruise
control to do that?
A controlled system
• We will analyze systems of single input/
single output.
• We need to control the output so that it
reaches a desired value known as
Setpoint. The output is the Controlled
Variable
• For the Controlled Variable to change, the
system input must change via a
controller
• The controller output has effect on a
Manipulated Variable which goes as
system input
Input
R(s)
System
G(s)
Output
C(s)
• Setpoint
• Controlled Variable
• Manipulated Variable
• Feedback
• Closed-Loop Control
• Feedback is necessary to implement
Closed-Loop Control
ME 330
6
A controlled system
M(s)
Input
Q(s)
System
C(s)
Output
We have a system or plant that follows a mathematical model and can
be represented by the transfer function Q(s)
ME 330
7
A controlled system
P(s)
M(s)
Controller Input
Q(s)
System
C(s)
Output
Now we add a controller which can modify the system input identified
as M(s) and therefore change the output C(s). In this figure, the
controller has no knowledge about C(s) and is acting in OPEN-LOOP.
ME 330
8
A controlled system
M(s)
P(s)
Controller
Input
Q(s)
C(s)
System Output
H(s)
Feedback gain
With the addition of a Feedback block H(s), the status of system output
C(s) can be sent to the controller for a CLOSED-LOOP control. However,
the current status of C(s) must be compared to a desired value.
ME 330
9
A controlled system
Summer
E(s)
R(s) +
Setpoint
– Error
P(s)
Controller
M(s)
Input
Q(s)
C(s)
System Output
H(s)
Feedback gain
This desired value is known as Setpoint R(s) and the comparison is the
difference R(s) – H(s)C(s) known as Error E(s). The error E(s) is used by
the controller for a proper corrective action.
This Block Diagram known as a Feedback Form can be further simplified.
ME 330
10
A controlled system
R(s)
+
Setpoint
E(s)
– Error
M(s)
P(s)
Controller
Input
Q(s)
System
C(s)
Output
H(s)
Feedback gain
P(s)
Series Blocks can be represented
as one that results of multiplying
the gains in series.
Q(s)
G(s) = P(s)Q(s)
G(s)
ME 330
11
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
C(s)
Output
H(s)
Feedback gain
The feedback form can be simplified as
one equivalent block using some algebra.
ME 330
12
A controlled system
R(s)
E(s)
+
Setpoint

C(s)
G(s)
Error
Output
H(s)C(s)
H(s)
Feedback gain
Let’s find the equation at the summer
block. This equation relates E(s) to R(s)
and the output of H(s) which is H(s)C(s)
ME 330
= − ( )
13
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
H(s)C(s)
C(s)
Output
H(s)
Feedback gain
= − ( )
Now let’s find the equation that
relates C(s) to E(s)
= ( )
ME 330
14
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
C(s)
Output
H(s)C(s)
H(s)
Feedback gain
Let’s combine the two equations
and find the transfer function
C(s)/R(s)
= − ( )
= ( )
ME 330
15
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
C(s)
Output
H(s)C(s)
H(s)
Feedback gain
Let’s combine the two equations
and find the transfer function
C(s)/R(s)
ME 330
16
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
C(s)
Output
H(s)C(s)
H(s)
Feedback gain
Let’s combine the two equations
and find the transfer function
C(s)/R(s)
= −
ME 330
17
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
H(s)C(s)
C(s)
Output
H(s)
Feedback gain
Let’s do some algebra to find
C(s)/R(s)
= −
+ =
( 1 + ) =
ME 330
18
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
H(s)C(s)
C(s)
Output
H(s)
Feedback gain
Let’s do some algebra to find
C(s)/R(s)
( 1 + ) =

=
( )
1+
ME 330
= ( )
19
A controlled system
R(s)
E(s)
+
Setpoint

G(s)
Error
H(s)C(s)
C(s)
Output
H(s)
Feedback gain
Let’s do some algebra to find
C(s)/R(s)
( 1 + ) =

=
( )
1+
ME 330
= ( )
20
A controlled system
How will the output of the plant respond to the desired setpoint?
Setpoint
R(s)
Geq(s)
Output
C(s)
Controller Plant

( ) =
1+
G(s) = P(s)Q(s)
Here is the equivalent block diagram
ME 330
21
The PID controller
E(s)
Error
M(s)
P(s)
Controller
Input
The PID control relates the time
output m(t) to the error input
e(t) following the equation.
Derivative

= + න +

−∞
Proportional
Integral
What can you visualize from this relationship?
ME 330
22
The PID controller
E(s)
Error
M(s)
P(s)
Controller
Input
If we apply the Laplace
Transform to the time equation
the expression is
1
= + ( ) + ( )

1
= + + ( )

ME 330
23
The PID controller
E(s)
Error
P(s)
Controller
E(s)
M(s)
Input

+ +

M(s)
If we apply the Laplace
Transform to the time equation
the expression is
1
= + ( ) + ( )

1
= + + ( )

ME 330
24
The PID controller

E(s)
Error
P(s)
Controller
M(s)
E(s)
Input

+
+
M(s)
+

: Proportional Constant
In the lab we are going to
analyze the system response
of P(s) to changes on
, and
: Integral Constant
: Derivative Constant
Tune the controller for the desired response by adjusting , ,
ME 330
25
Lab 4,5 Closed-loop Controller
PID controller
• Term P is proportional to the current value of the SP − PV error. If the error is large and
positive, the control output will be proportionately large and positive, considering the
gain factor “K”. Using proportional control alone will result in an error between the
setpoint and the actual process value because it requires an error to generate the
proportional response. If there is no error (or not enough error), there is no corrective
response. [The P term kicks off the response but then gets lazy.]
• Term I accounts for past values of the SP − PV error and integrates them over time to
produce the I term. For example, if there is a residual SP − PV error after the application
of proportional control, the integral term seeks to eliminate the residual error by
adding a control effect due to the historic cumulative value of the error. When the error
is eliminated, the integral term will cease to grow. This will result in the proportional
effect diminishing as the error decreases, but this is compensated for by the growing
integral effect. [The I term has no patience for laziness. After a time, it takes over.]
• Term D does not consider the error (meaning it cannot bring it to zero), but tries to
bring the rate of change of error to zero. It aims at flattening the error trajectory
especially when there is an oscillatory problem. [The D term just wants to keep things
moving in the right direction. If P and I are moving to minimize error, it helps out. If not,
it will slow them down until they turn around.]
paraphrased from: https://en.wikipedia.org/wiki/PID_controller
Lab 5 Closed-loop Servo Controller
Clarifications and Tips
• Looking for a step response. Turn the input knob with a short quick movement that
doesn’t go all the way to the stop. This will allow for servo overshoot.
• Potentiometers get dirty. That is, there can be some oxidation on the windings and
brush that leads to “noise” in the signal. You can spray contact cleaner on them and do
a few back-and-forth rotations to clean them up. (Electric guitar players are well aware.)
• ADC is analog to digital converter. That’s the reading from the input and response
potentiometers.
• Why connect +5v to both +12v and +5v on the motor driver? This is needed to get more
current to the motor. Step response from motor requires higher torque which takes
more current.
• Recall the control connections to the motor: Enable A is PWM control motor voltage
(speed) of A. Input 1 and Input 2 are logic control for direction of rotation.
Lab 5 Closed-loop Servo Controller
Clarifications and Tips
• Check your hardware. The potentiometers should measure a 0-5v from the wire
coming from the center terminal or, outside the circuit, they should measure 0 to 10k
ohms. Also check that the gearbox on the motor isn’t stripped.
• Your controller gains for tuning derivative, proportional, and integral are Kd, Kp, Ki
• If you’re having trouble getting reasonable results in Section 3: Derivative Position
Controller, try using Kd = 0.1
• The intent of “Section 4 – System Modeling”, is to take your derivative controller data
and use Matlab to suggest proportional and integral gains that you can then try on
your physical circuit. See next sheet.
Lab 5 Closed-loop Servo Controller
Clarifications and Tips
Section 4: System Modeling
• Use the derivative controller data from Section 3
• Follow the lab instructions for Matlab to input your data.
• If you get an error, check Manage add ons to see if you have the Control System
Toolbox loaded. If not, load it.
• After selecting tuning methods, PID tuning, and moving the response time to ~ 1.5s,
update compensator and you’ll see a window like this that will indicate new gains.
Kp = 1.2 x 1.8
If you’re not able get to
1.5s, check that you’re using
a reasonable dt value from
your data such as 0.05.
Ki = 1.2
Use these gains from the Matlab
controller modeling in your Arduino
program to use in your physical circuit.
Redo the step and response from your
circuit. Graph your input output data.
Lab 5 Closed-loop Servo Controller
Clarifications and Tips
Section 6: Conclusion
Source: https://www.motioncontroltips.com/how-to-address-overshoot-in-servo-control/
Lab 5 Closed-loop Servo Controller
Clarifications and Tips
Arduino Analog pins
take an input (0-5V)
and converts to digital
values 0-1023 (bits)
Thank you for your help
improving the lab instructions!
Input (0-5v)
The Arduino takes in an analog voltage, samples the amplitude
every few milliseconds, and then outputs a digital value.
Output analog voltage (0-5v)
MOST microcontrollers cannot output an analog voltage and must
output a PWM signal that corresponds to equivalent (choppy) analog
voltage. Then a low pass filter is used to convert to an analog voltage.
Lab 4,5 Closed-loop Controller
Further learning suggestions:
PID controller tuning
• YouTube: https://youtu.be/dZ8lzDi3cXY (18:13)
• Website: https://pidexplained.com/how-to-tune-a-pid-controller/
Arduino programming
• YouTube: Tutorial 1: For beginners
• Many more on the same channel

(23:49)

Purchase answer to see full
attachment

error: Content is protected !!