Professional Documents
Culture Documents
SCHOOL OF ENGINEERING
PROJECT C REPORT
DESIGN AND PROGRAM SILK TENSILE
TESTING SYSTEM
Date: ………./………../……………
Signature
REMARKS OF COMMITEES
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
Date: ………./………../……………
Signature
TABLE OF CONTENT
ABBREVIATION…………………………………………………………………………….i
ACKNOWLEDGEMENT.......................................................................................................ii
ABSTRACT.............................................................................................................................iii
CHAPTER 1. INTRODUCTION...........................................................................................1
1.1 Project purpose..........................................................................................................1
1.2 The necessary of strength testing in manufacture industry..................................1
1.3 Academic factors.......................................................................................................2
CHAPTER 2: LITERATURE REVIEW...............................................................................3
2.1 General about Arduino..................................................................................................3
2.2 Arduino Mega 2560........................................................................................................4
2.3 Mechanical power transmission....................................................................................5
2.4 Control and Program technique...................................................................................7
2.4.1 Tensile calculation...................................................................................................7
2.4.2 Motor speed control................................................................................................8
2.4.3 PID controller..........................................................................................................9
CHAPTER 3: HARDWARE AND MODEL.......................................................................11
3.1 Hardware desgin..........................................................................................................11
3.2 Hardware specifications..............................................................................................12
3.2.1 Nisca DC servo motor...........................................................................................12
3.2.2 DC motor driver BTS7960 43A...........................................................................12
3.2.3 Encoder..................................................................................................................13
3.2.4 Hall effect current sensor ACS712......................................................................14
3.2.5 Power supply..........................................................................................................14
3.2.6 Data transfer module UART................................................................................14
3.3 Designing and Modelling.............................................................................................15
3.3.1 Designing................................................................................................................15
3.3.2 Manufacture method............................................................................................16
3.3.3 Real model..............................................................................................................16
CHAPTER 4: SOFTWARE..................................................................................................18
4.1 Model design – SOLIDWORKS...................................................................................18
4.2 Programming – ARDUINO IDE.................................................................................18
4.3 Control panel desgin and PID adaptive chart – VISUAL STUDIO.........................19
CHAPTER 5: PROGRAMMING........................................................................................20
5.1 System work flow.........................................................................................................20
5.2 I/O table.........................................................................................................................20
5.3 Flow chart.....................................................................................................................21
5.4 Control panel and windows.........................................................................................21
CHAPTER 6: CONCLUSION..............................................................................................22
BIBLIOGRAPHY..................................................................................................................24
APPENDIX.............................................................................................................................25
TABLE OF FIGURES
Fig 1.1 Tensile Testing Machine…………………………………………………….…………
1
Fig 1.2 Silk/paper rolling system………………………………………………………………………
2
Fig 2.1 Arduino Uno board…………………………………………………………………………….3
Fig 2.2 Arduino Nano board……………………………………………………………………………
3
Fig 2.3 Arduino Gemma board………………………...………………………………………
3
Fig 2.4 Arduino Mega 2560 board…………………………………………………………….4
Fig 2.5 Arduino Mega 2560 specification………………………………..
…………………….4
Fig 2.6 Arduino Mega 2560 Pinout……………………………………………………………
5
Fig 2.7 Arduino boards digital pins usable for interrupt………………………………………
5
Fig 2.8 The mechanical power transmission on the model……………………………………6
Fig 2.9 DC motor movement theory……………………………………………………………
6
Fig 2.10 The PID formula………………………...……………………………...
…………….9
Fig 3.1 Nisca NF5475 DC servo motor………………………………………………………
12
Fig 3.2 Motor specification……………………………………………………………...……
12
Fig 3.3 DC motor driver
BTS7960…………………………………………………………...12
Fig 3.4 BTS 7960 DC motor pinout………………………………………………………..
….13
Fig 3.5 600ppr rotary
encoder………………………………………………………………..13
Fig 3.6 Encoder theory……………………………………………………………………….13
Fig 3.7 ACS712
Pinout………………………………………………………………………..14
Fig 3.8 24VDC switching mode power
supply………………………………………………..14
Fig 3.9 UART CP2102 USB
Module………………………………………………………….15
Fig 4.1 Solidworks icon………………………………………..……………………………..18
Fig 4.2 Solidworks 2017 interface……………………………………………………………
18
Fig 4.3 Arduino IDE interface …………………………………………………………….
….19
Fig 4.4 Visual Studio
icon……………………………………………………………………………..19
Fig 4.5 Visual Studio
interface………………………………………………………………………..19
Fig 5.1 PID chart of silk speed and
tension………………………………………………………….21
Fig 5.2 PID adjust window for Tension and Speed…………………………………………………
22
Fig 5.3 Set point input window for Tension and
Speed…………………………………………….22
Fig 5.4 PID chart of silk speed and
tension…………………………………………………...22
ABBREVIATIONS
PID: Proportional–Integral–Derivative
Besides, I want to thank my lecturer, he is also my advisor in this project – Mr. Nguyen Ngoc
Hay for spending time to give me useful advises in testing solution, power transmission,
programming C language and apply controller to the real model in the most effective way.
Last but not least, I also want to say thanks to all committees for examining and supervising
my project report, program and hardware. Your comments will be very necessary for my last
project, the capstone, and job in the future.
ABSTRACT
The achievement of 4.0 industry revolution has set huge changes in factories around the
world. These positive change happen the same in many aspects of a modern factory, from
importation, manufacture process, packaging, storage system until exportation. This project
modelling a tensile testing system that applied mostly in textile industry or metallurgy
industry with the same structure of system. Furthermore, we drive this project to be a
functional project, by applying with PID controller with user control interface by C#,
controller adaptive graph, power transmission design and calculate, data transfer via wifi with
basic step of cloud data transfer, this is the basic object of the forth industry revoluotion.
Arduino is our choice, this is not a complicated microcontroller board as PIC, ARM or PLC,
but it is useable and popular nowadays, this board allows us to be more familiar with data
transfer by internet to get the most important goal: understand how technology work in the
era of IOT.
Working area:
- Control speed of DC servo motors with encoder read.
- Read data from current meters.
- UART and Arduino Mega 2560 connection.
- Mechanical design.
- User control inteface design.
1
In weaving industry and rolled steel: this situation shows another type of strength
testing while the motors need to run at the right speed to keep the material at the right
shape, also prevent it from torn.
2
CHAPTER 2: LITERATURE REVIEW
2.1 General about Arduino.
Arduino is an electronic platform invented since 2005 that works mainly for testing,
experience and work easily on hardware and software. For many years, Arduino becomes
the controller of million projects, from daily activities to professional projects, which can
read input data from sensors or button and talk to the actuators like valves, motors,…The
important factor that makes Arduino becomes popular is the open – source environment,
where Arduino users can connect with others, they can be students, makers or engineers.
At the beginning, Arduino was born in Ivrea Interaction Design Institute as a tool for fast
prototyping, especially for people who have no engineering background. Since its easy to
use for beginer coders, but still flexible to adapt with advance projects in different areas,
Arduino reaches a huge community of user.
Unlike other controllers or processors like PIC, ARM, or much more complex like PLC,
Arduino is a simple circuit board that includes the microcontroller of ATMega series and
other pinout to connect with different devices.
3
In this project, we use Arduino Mega 2560 board as the controller, plus other
peripherals like current sensors, encoders and data transfer module via UART
protocol.
4
Fig 2.5 Arduino Mega 2560 specification. Source: arduino.cc
Fig 2.7 Arduino boards digital pins usable for interrupt. Source: arduino.cc
5
R1
N=
R2
Where:
-N: ratio
-R1: number of teeth/diameter of gear, pulley 1
-R2: number of teeth/diameter of gear, pulley 2
- Undirect power transfer: in some cases, the position of motor shaft is not directed to
the rotary objects. For that reason, mechanism structure work as a power transfer,
such as linkage, cam shaft,…
In this project, we apply XL belt and XL pulley to for the power transfer from DC
servo motor to the two main rollers. The ratio between motor and rollers speed is 1:5
(the motor take one revolution, the main roller rotate five revolution).
6
2.4 Control and Program technique.
2.4.1 Tensile calculation.
Tensile calculation is a combination between three different numbers from the
motor: torque, current, voltage. The table below shows the range of current in
two motors after the manual test with the power supply.
Voltage(V)
DC Motor 1 DC Motor 2
Current (A)
2.64 24 24
2.61 24 0
2.71 0 24
The torque formula is:
F .D
T=
2
Where:
-T: torque (Nm)
-F: Force (N)
-D: Roller diameter (m)
The total torque of two DC motor to stretch the silk is calculated base on the
formula below:
0.5 x (Current of motor 1+Current of motor 2)
T =
7.5
Where:
7
Where:
pulled encoder
n=
active encoder
8
direction (information about the motor driver is used in this project mentioned
in Chapter 3). A definition called PWM (Pulse Width Moderation) is created
by the microcontroller and feed to the motor driver. It is involves application
of varying width pulses to the motor driver to control the supply voltage. This
method proves to be very efficient as the power loss is kept at minimum, and it
doesn’t involve the use of any complex equipment.
2.4.3 PID controller.
PID (Proportional-Intsssssssegral-Derivative) controller is applied in many
industrial control engineering areas. Unlike open-loop control process, the
main key point of PID controller is closed-loop control, where the feedback of
output signal from system must follow the set point as order on each
manufacture process, this number proved that the system is working at the
right condition.
9
P - Propotional
I – Integral
Because P - controller always exists an offset between the process variable
and set point, I-controller provides necessary action to eliminate the steady
state error. It integrates the error over a period of time until error value
reaches to zero. It holds the value to final control device at which error
becomes zero.
D – Derivative
I-controller doesn’t have the capability to predict the future behavior of
error. So it does nothing when the set point is changed. D-controller solves
this problem by anticipating future behavior of the error. Its output
depends on rate of change of error with respect to time, multiplied by
derivative constant.
Normally, a PID controller when run will be looked as a continuous line graph
that present the response from system, this graph will autmatically do the real
– time compare with the set point line, troubleshoot all data appear during the
process.
10
CHAPTER 3: HARDWARE AND MODEL
3.1 Hardware desgin.
The hardware is designed base on the real model in industry with parts are listed in
the table below:
5 19 – 10 hat bearing 10
6 26 – 10 bearing 2
7 58 – 45 bearing 2
8 Roller 5
10 Encoder stand 1
11 Limit switch 2
11
3.2 Hardware specifications
3.2.1 Nisca DC servo motor.
Nisca NF5475 DC servo motor is a 24VDC motor that includes a 200ppr encoder.
12
Specification:
- Power supply: 6 – 27 VDC
- Shutdown automatically if the voltage is lower than 5.5V to avoid motor run
on low voltage.
- Maximum control frequency: 25kHz
13
Fig 3.6 Encoder theory. Source: orentialmotor.com
3.2.4 Hall effect current sensor ACS712.
Hall effect current sensor ACS712 is a type of current sensor that apply Hall Effect to
record current signal from AC/DC power supply. The analog output signal is linear
with the current so it easy to collect data in high accuracy by controllers.
14
Fig 3.8 24VDC switching mode power supply.
3.2.6 Data transfer module UART
Universal Asynchronous Receiver Transmitter (UART) is a serial communication
device that performs parallel to serial data conversion at the transmitter side and serial
to parallel data at the receiver side. The transfer speed and data speed are
configurable.
15
3.3 Designing and Modelling
3.3.1 Designing
16
3.3.2 Manufacture method.
The hardware is manufactured with several method that applied at EIU
Fablab:
3D printing.
Laser cutting and engraving.
Lathe.
3.3.3 Real model.
17
Note: The real model and the design is quite different because errors happen during
manufacture and some points need to be changed to adapt the program easily.
18
CHAPTER 4: SOFTWARE
During the working process, we applied different software in each step of the project.
19
Fig 4.3 Arduino IDE interface
4.3 Control panel desgin and PID adaptive chart – VISUAL STUDIO.
Visual Studio is a popular intergrated development environment (IDE) created by
Microsoft for programming app, web service, web apps or offline apps also. This
software applies many platforms such as Windows Form, Windows API, Windows
Presentation Foundation,… to support users in 36 programming languages with
usable tools.
20
CHAPTER 5: PROGRAMMING
5.1 System work flow.
Two DC servo motor linked by a system of roller, transfer power via belt and pulley
to run a silk stretching system. There are two 200ppr encoders already attached on
motors that will collect data of speed and send back to Arduino as the feedback signal
for the PID controller that control stretch force, while the middle encoder that
attached on the middle roller collect data of silk speed when run between rollers.
There are two separate PID controller for two motors, one will control the stretch
force while the other go with silk speed. All control element and chart display will
work through a control panel designed by C# programming, the chart will show the
condition of response signal base on the set point value.
21
5.3 Flow chart.
22
Fig 5.2 PID adjust window for Tension and Speed.
Fig 5.3 Set point input window for Tension and Speed..
23
CHAPTER 6: CONCLUSION
In conclusion, this project is a chance for us to practice programming a close-loop system to
research and do study about a truthly automatic system. Furthermore, we can apply
knowledge of some important subjects: Microcontroller and Measurement – Control in data
collecting by sensor and data transfer via UART module, while the knownledge of Basic
Mechanism Transmission help us in calculate and design the mechanical power transmission
system that is suitable for tensile testing. With all the knowledge and experiences, this project
will be the first step for us to understand the fundamental points of the system and prepare for
the capstone, where we will make a bigger model, stronger motor and new solution for data
transfer.
In this project, we pay attention mostly on automation control, data collecting and processing.
Mostly using encoders with different resolution for three points of speed control, current
sensor that have different ways of signal processing. We can practice and being flexible in
choosing solution of coding, also the solution for searching and analysing documents.
After microcontroller project, this is our third project during the education program. We
achieved our main goals of the project: study about sensors, automation and control
engineering via PID controller, and the best thing is researching and document collecting, it
will be our most valuable experience for us to prepare for the future job in engineering area.
24
BIBLIOGRAPHY
- Tension Control Systems for Light, Medium and Heavy-Duty Tensioning, Warner
Electric.
- What is Arduino ? https://www.arduino.cc/en/guide/introduction
- a t t a c h I n t e r r u p t ( ) , https://www.arduino.cc/reference/en/language/functions/
external-interrupts/attachinterrupt/
25
APPENDIX
Arduino code:
#define RPWM_Output_Left 7 // left motor
#define LPWM_Output_Right 9 // right motor
int encoder = 0;
int encoder1 = 0;
int encoder_mid = 0;
//----------------truyen nhan---------------
String mySt = "",myPara ="",myPara2="",
kp_string, ki_string, kd_string,
F_set_speed_string, stop_string,
kp_speed_string, ki_speed_string,
kd_speed_string, set_speed_string;
byte sky,oi,son,tung,em,ti,py,ne;
//--------------------PID Force-------------
boolean motor_start = false;
boolean goto_Left = false ;
boolean goto_Right = false;
float F_pv_speed = 0;
float F_set_speed = 0;// unit N
float F_e_speed = 0; //error of speed = set_speed - pv_speed
26
float F_e_speed_pre = 0; //last error of speed
float F_e_speed_sum = 0; //sum error of speed
float F_pwm_pulse = 0; //this value is 0~255
float pv_speed = 0;
float actual_pv_speed = 0;
float set_speed = 0;
float e_speed = 0; //error of speed = set_speed - pv_speed
float e_speed_pre = 0; //last error of speed
float e_speed_sum = 0; //sum error of speed
float pwm_pulse = 0; //this value is 0~255
double n=0;
int i=0;
double Torque = 0;
float Left_Current = 0.0;
float Right_Current = 0.0;
27
const int kenhA = 2; //encoder left
const int kenhB = 3;
void setup()
{
pinMode(10, OUTPUT);
pinMode(5, INPUT_PULLUP);
Serial.begin(9600); //Starts the serial connecemon with 115200 Buad Rate
28
pinMode(RPWM_Output_Left,OUTPUT);
pinMode(LPWM_Output_Right,OUTPUT);
//--------------------------timmer setup
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
timer1_counter = 64911;//64911;
// gia tri TCNT1 = timmer1_counter = 65535 - T*(F/n)
// F: tan so vi dieu khien = 16 Mhz
// n: bo chia tan so emmer1 = 256
// T: thoi gian can dinh thoi = 0.01s
29
}
for (int i = 0; i < myPara.length(); i++) //2
{
if (myPara.charAt(i) == '|') {oi = i;}
}
for (int i = 0; i < myPara.length(); i++) //3
{
if (myPara.charAt(i) == '?') {son = i;}
}
for (int i = 0; i < myPara.length(); i++) //3
{
if (myPara.charAt(i) == '%') {tung = i;}
}
for (int i = 0; i < myPara.length(); i++) //3
{
if (myPara.charAt(i) == '^') {em = i;}
}
Serial.println("!");
}
kp_string/*sky*/= ki_string/*sky+1*/= kd_string/*oi*/
= kp_speed_string /*son*/= ki_speed_string /*tung*/= kd_speed_string
= myPara;
30
F_kp = kp_string.toFloat();
F_ki = ki_string.toFloat();
F_kd = kd_string.toFloat();
kp = kp_speed_string.toFloat();
ki = ki_speed_string.toFloat();
kd = kd_speed_string.toFloat();
if (mySt.startsWith("B"))
{
myPara2 = mySt.substring(1);
for (int i = 0; i < myPara2.length(); i++)
{
if (myPara2.charAt(i) == ';') {ti = i;}
}
F_set_speed_string = set_speed_string = myPara2;
F_set_speed_string.remove(ti);
set_speed_string.remove(0, ti+1);
F_set_speed = F_set_speed_string.toFloat();
set_speed = set_speed_string.toFloat();
Serial.println("=");
}
if (mySt.startsWith("S")) { analogWrite(RPWM_Output_Left,0);
analogWrite(LPWM_Output_Right,0);
motor_start = false;}
if (mySt.startsWith("L")) { goto_Left = true;
31
goto_Right = false;
Serial.print("[");}
if (mySt.startsWith("N")) { goto_Left = false;
goto_Right = true;
Serial.print("]");}
}
if (digitalRead(5) == LOW)
{
motor_start = false ;
analogWrite(LPWM_Output_Right,0);
analogWrite(RPWM_Output_Left,0);
digitalWrite(10, HIGH);
}
else{digitalWrite(10, LOW);}
//-----------------------cam bien dong left------------------//
float AcsValue_Left=0.0,Samples_Left=0.0,AvgAcs_Left=0.0,AcsValueF_Left=0.0;
for (int x = 0; x < 100; x++){AcsValue_Left = analogRead(A0)+1; //Read
current sensor values
Samples_Left = Samples_Left + AcsValue_Left; //Sum
delay (1);}
AvgAcs_Left=Samples_Left/100; //TaF_king Average
of Samples
AvgAcs_Left = AvgAcs_Left * vpp;
AvgAcs_Left = AvgAcs_Left - 2.5;
AcsValue_Left = AvgAcs_Left / sensitivity;
Left_Current = AcsValue_Left;
//-----------------------cam bien dong right------------------//
float
AcsValue_Right=0.0,Samples_Right=0.0,AvgAcs_Right=0.0,AcsValueF_Right=0.0;
for (int x = 0; x < 100; x++){AcsValue_Right = analogRead(A1)+1; //Read
current sensor values
32
Samples_Right = Samples_Right + AcsValue_Right; //Sum
delay (1);}
AvgAcs_Right=Samples_Right/100; //TaF_king
Average of Samples
AvgAcs_Right = AvgAcs_Right * vpp;
AvgAcs_Right = AvgAcs_Right - 2.5;
AcsValue_Right = AvgAcs_Right / sensitivity;
Right_Current = AcsValue_Right;
//--------------phan 2 : gui du lieu len c#--------------
pv_speed = pv_speed *0.09424;
Serial.println("K"+ String(pv_speed));
delay(30);
Serial.println("D"+ String(F_pv_speed));
}
//-------------------end loop---------------------//
void updateEncoder0() {encoder+=1;} //increasing encoder at new pulse
void updateEncoder1() {encoder1+=1;} //increasing encoder at new pulse
void updateEncoder2() { encoder_mid+=1; } //increasing encoder at new pulse
33
if (n < 0.9) n = 0.95;
F_pv_speed = (Torque*2)*n*(1/0.045); //calculate motor speed, unit is rpm
pv_speed = 60.0*(encoder_mid/600.0)/0.01;
encoder = 0;
encoder1 = 0;
encoder_mid = 0;
//PID program
if (motor_start)
{
F_e_speed = F_set_speed - F_pv_speed;
Torque = 0.5*(Left_Current*2)*(1/7.5);
34
{
F_e_speed = 0;
F_e_speed_pre = 0;
F_e_speed_sum = 0;
F_pwm_pulse = 0;
e_speed = 0;
e_speed_pre = 0;
e_speed_sum = 0;
pwm_pulse = 0;
}
if (F_pwm_pulse <255 & F_pwm_pulse >0)
{analogWrite(LPWM_Output_Right,F_pwm_pulse);} //set motor speed
else{
if (F_pwm_pulse>255){F_pwm_pulse=255;
analogWrite(LPWM_Output_Right,F_pwm_pulse);}
else {F_pwm_pulse=0; analogWrite(LPWM_Output_Right,
F_pwm_pulse);}
}
if (pwm_pulse <255 & pwm_pulse >0)
{analogWrite(RPWM_Output_Left,pwm_pulse);} //set motor speed
else{
if (pwm_pulse>255){pwm_pulse=255;
analogWrite(RPWM_Output_Left,pwm_pulse);}
else {pwm_pulse=0; analogWrite(RPWM_Output_Left, pwm_pulse);}
}
}
void ToRight()
{
n = encoder1/encoder;
if (n > 1.1) n = 1.05;
35
if (n < 0.9) n = 0.95;
F_pv_speed = (Torque*2)*n*(1/0.045); //calculate motor speed, unit is rpm
pv_speed = 60.0*(encoder_mid/600.0)/0.01;
encoder = 0;
encoder1 = 0;
encoder_mid = 0;
//PID program
if (motor_start)
{
F_e_speed = F_set_speed - F_pv_speed;
Torque = 0.5*(Right_Current*2)*(1/7.5);
36
F_e_speed = 0;
F_e_speed_pre = 0;
F_e_speed_sum = 0;
F_pwm_pulse = 0;
e_speed = 0;
e_speed_pre = 0;
e_speed_sum = 0;
pwm_pulse = 0;
}
if (F_pwm_pulse <255 & F_pwm_pulse >0)
{analogWrite(RPWM_Output_Left,F_pwm_pulse);} //set motor speed
else
{
if (F_pwm_pulse>255){F_pwm_pulse=255;
analogWrite(RPWM_Output_Left,F_pwm_pulse);}
else {F_pwm_pulse=0; analogWrite(RPWM_Output_Left,
F_pwm_pulse);}
}
if (pwm_pulse <255 & pwm_pulse >0)
{analogWrite(LPWM_Output_Right,pwm_pulse);} //set motor speed
else{
if (pwm_pulse>255){pwm_pulse=255;
analogWrite(LPWM_Output_Right,pwm_pulse);}
else {pwm_pulse=0; analogWrite(LPWM_Output_Right, pwm_pulse);}}
}
37
Code C#:
FROM 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (!serialPort1.IsOpen)
{
serialPort1.Open();
textBox1.Text = "Opened";
textBox1.ForeColor = Color.MediumSeaGreen;
}
else { textBox1.Text = "Busy"; textBox1.ForeColor = Color.Gray; }
}
int[] recieve = new int[10];
int i = 0;
string[] String;
double temp;
38
private void label1_Click(object sender, EventArgs e)
{
}
private string rxString;
string inputString;
private void displayText(object o, EventArgs e)
{
//hien thi len c#
if (rxString.StartsWith("K"))
{
rxString = rxString.Substring(1);
rpm_textBox.Text = rxString;
}
if (rxString.StartsWith("D"))
{
rxString = rxString.Substring(1);
tension_textBox.Text = rxString;
FROM 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
39
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Boolean txt = true;
private void Form2_Load(object sender, EventArgs e)
{
if (txt)
{
Kp_textBox_F2.Text = "1.0";
Ki_textBox_F2.Text = "0.45";
Kd_textBox_F2.Text = "0";
Kp_TB_speed_F2.Text = "0.12";
Ki_TB_speed_F2.Text = "0.19";
Kd_TB_speed_F2.Text = "0";
txt = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}}
40
PID CHART
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;
namespace WindowsFormsApp1
{
public partial class TensionChart : Form
{
int tickStart = 0;
public TensionChart()
{
InitializeComponent();
}
double tension, tesion_setpoint;
Boolean scroll = true;
public void draw(double tension, double tesion_setpoint) // Ở ví dụ này chúng ta
có 2 đường
{
tension = (Convert.ToDouble(textBox2.Text));
tesion_setpoint = (Convert.ToDouble(tension_label.Text));
if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
return;
41
// Kiểm tra việc khởi tạo các đường curve
// Đưa về điểm xuất phát
LineItem curve1 = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
LineItem curve2 = zedGraphControl1.GraphPane.CurveList[1] as LineItem;
if (curve1 == null)
return;
if (curve2 == null)
return;
// list chứa các điểm.
// Get the PointPairList
IPointListEdit list1 = curve1.Points as IPointListEdit;
IPointListEdit list2 = curve2.Points as IPointListEdit;
if (list1 == null)
return;
if (list2 == null)
return;
// Time được tính bằng ms
double time = (Environment.TickCount - tickStart) / 1000.0;
// Tính toán giá trị hiện thị
// Muốn setpoint1hiện thị cái gì thì chỉ việc thay vào setpointx
list1.Add(time, tension); // Đây chính là hàm hiển thị dữ liệu của mình lên đồ
thị
list2.Add(time, tesion_setpoint); // Đây chính là hàm hiển thị dữ liệu của
mình lên đồ thị
// Ko vẽ setpoint2 mà thử vẽ đồ thị hình sin với 3 seconds per cycle
//list2.Add(time, Math.Sin(2.0 * Math.PI* time / 3.0));
// đoạn chương trình thực hiện vẽ đồ thị
Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
if (time > xScale.Max - xScale.MajorStep)
{
42
//xScale.Max = time + xScale.MajorStep;
// xScale.Min = xScale.Max - 30.0; // Timer chạy qua 30 sẽ tự động dịch
chuyển tịnh tiến sang trái
// Nếu ko muốn dịch chuyển mà chạy bắt đầu từ 0 thì :
xScale.Min = 0;
if (scroll)
{
xScale.Max = time + xScale.MajorStep;
xScale.Min = xScale.Max - 30.0;
}
else
{
xScale.Max = time + xScale.MajorStep;
xScale.Min = 0;
}
}
// Vẽ đồ thị
zedGraphControl1.AxisChange();
// Force a redraw
zedGraphControl1.Invalidate();}
public void draw2(double speed, double speed_setpoint) // Ở ví dụ này chúng ta
có 2 đường
{
speed = (Convert.ToDouble(rpm_textBox.Text));
speed_setpoint = (Convert.ToDouble(speed_label.Text));
if (zedGraphControl2.GraphPane.CurveList.Count <= 0)
return;
// Kiểm tra việc khởi tạo các đường curve
// Đưa về điểm xuất phát
LineItem curve_1 = zedGraphControl2.GraphPane.CurveList[0] as LineItem;
43
LineItem curve_2 = zedGraphControl2.GraphPane.CurveList[1] as LineItem;
if (curve_1 == null)
return;
if (curve_2 == null)
return;
// list chứa các điểm.
// Get the PointPairList
IPointListEdit list_1 = curve_1.Points as IPointListEdit;
IPointListEdit list_2 = curve_2.Points as IPointListEdit;
if (list_1 == null)
return;
if (list_2 == null)
return;
// Time được tính bằng ms
double time = (Environment.TickCount - tickStart) / 1000.0;
// Tính toán giá trị hiện thị
list_1.Add(time, speed); // Đây chính là hàm hiển thị dữ liệu của mình lên đồ
thị
list_2.Add(time, speed_setpoint); // Đây chính là hàm hiển thị dữ liệu của
mình lên đồ thị
Scale x_Scale = zedGraphControl2.GraphPane.XAxis.Scale;
if (time > x_Scale.Max - x_Scale.MajorStep)
{
//xScale.Max = time + xScale.MajorStep;
// xScale.Min = xScale.Max - 30.0; // Timer chạy qua 30 sẽ tự động dịch
chuyển tịnh tiến sang trái
// Nếu ko muốn dịch chuyển mà chạy bắt đầu từ 0 thì :
x_Scale.Min = 0;
if (scroll)
{
x_Scale.Max = time + x_Scale.MajorStep;
44
x_Scale.Min = x_Scale.Max - 30.0;
}
else
{
x_Scale.Max = time + x_Scale.MajorStep;
x_Scale.Min = 0;
} }
// Vẽ đồ thị
zedGraphControl2.AxisChange();
// Force a redraw
zedGraphControl2.Invalidate();
}
45
//timer1.Start(); // Chạy Timer1
// Định hiện thị cho trục thời gian (Trục X)
myPane.XAxis.Scale.Min = 0; // Min = 0;
myPane.XAxis.Scale.Max = 30; // Mã = 30;
myPane.XAxis.Scale.MinorStep = 1; // Đơn vị chia nhỏ nhất 1
myPane.XAxis.Scale.MajorStep = 5; // Đơn vị chia lớn 5
// Gọi hàm xác định cỡ trục
zedGraphControl1.AxisChange();
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.Max = 70;
GraphPane myPane2 = zedGraphControl2.GraphPane;
// Khai báo sửa dụng Graph loại GraphPane;
myPane2.Title.Text = "Speed PID Graph";
myPane2.XAxis.Title.Text = "Time";
myPane2.YAxis.Title.Text = "Speed";
//myPane2.XAxis.Title.Text.bo
//myPane2.YAxis.Title.Text = "Speed";
// Định nghĩa list để vẽ đồ thị. Để các bạn hiểu rõ cơ chế làm việc ở đây khai
báo 2 list điểm <=> 2 đường đồ thị
RollingPointPairList list_1 = new RollingPointPairList(1000);
RollingPointPairList list_2 = new RollingPointPairList(1000);
LineItem curve_1 = myPane2.AddCurve("actual speed", list_1, Color.Red,
SymbolType.None); // Color màu đỏ, đặc trưng cho đường 1
LineItem curve_2 = myPane2.AddCurve("speed set point", list_2, Color.Blue,
SymbolType.None); // Color màu Xanh, đặc trưng cho đường 2
// ví dụ khoảng cách là 50ms 1 lần
timer1.Interval = 50;
//timer1.Enabled = true; // Kích hoạt cho timer1
//timer1.Start(); // Chạy Timer1
// Định hiện thị cho trục thời gian (Trục X)
myPane2.XAxis.Scale.Min = 0; // Min = 0;
46
myPane2.XAxis.Scale.Max = 30; // Mã = 30;
myPane2.XAxis.Scale.MinorStep = 1; // Đơn vị chia nhỏ nhất 1
myPane2.XAxis.Scale.MajorStep = 5; // Đơn vị chia lớn 5
// Gọi hàm xác định cỡ trục
zedGraphControl1.AxisChange();
myPane2.YAxis.Scale.Min = 0;
myPane2.YAxis.Scale.Max = 50;
47
// Khởi động timer về vị trí ban đầu
if (check == 0) { tickStart = Environment.TickCount; check = 1; }
}
else
{
timer1.Enabled = false;
button1.Text = "START";
button1.BackColor = Color.LightSeaGreen;
}}
private void rpm_textBox_TextChanged(object sender, EventArgs e)
{}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
zedGraphControl1.GraphPane.CurveList.Clear();
zedGraphControl1.GraphPane.GraphObjList.Clear();
zedGraphControl2.GraphPane.CurveList.Clear();
zedGraphControl2.GraphPane.GraphObjList.Clear();
this.Close();
} }}
INPUT FORM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
48
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class InputForm : Form
{
public InputForm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{ this.Close();}
private void InputForm_Load(object sender, EventArgs e)
{
tension_TB.Text = "8";
speed_TB.Text = "120";
} }}
49