Here''''s an explanation of each term: Proportional P Term: The proportional term is based on the current error, which is the difference between the desired setpoint and the actual output..
Trang 1HANOI UNIVERSITY OF SCIENCE AND
TECHNOLOGY
PROJECT Microprocessor (HMI) ME4162 Subject: Encoder motor speed control by PID
Group 15 Students ID Class/Year Jobs
Code, report
Hardware setup, content prepares
Code, material prepares
Instructure: TS Dương Văn Lạ c
HÀ N I, 01/2022 Ộ
Trang 2- Relatively many typos
- Clear layout
- The content is quitcomplete, accurate
- Relatively good presentation
- Few typos
- The layout of the report
is clear
- Complete, accurate content
- Good presentation
- There are full members
to coordinate the implementation
Self-assessment score: 8.5/10 Signature: ……… ……… ………
is limited
- Limited aesthetics
- Active products
- The product is relatively complete, and the function is quite complete
- Aesthetically pleasing products
- The product workswell
- The product is quite complete, fully functional
- Aesthetically pleasing products
- Reliable operation products
- There are full members
to coordinate the implementation
Self-assessment score: 8/10 Signature: ……… ……… ………
3) Project defense Score
- Understanding anapplying learned swords is limited
- Answers questionrelatively well
- Understand and apply the learned swords relatively well
- Correct and completeanswers to all question
- Understand and makegood use of the knowledge learned
- Ability to analyze, evaluate, create
II OFFICIAL RATING SCORE
Report Book Score
ĐBC
(10%)
Product Score
ĐSP
(40%)
DefenseScore *
ĐBV
(50%)
AVERAGE SCORE
ĐKQ= 10%Đ +40%Đ +50%ĐBC SP BV
2 Nguyễn Duy Thảo 20206089
*Noted: ĐBV ≤ min {Đ , ĐBC SP}
Trang 3Table of Contents
Introduction 4
Hardware Components 5
Components 5
Circuit Diagram 8
PID principle 8
Software Programing 10
Dictionaries 10
Main file 12
Code 14
Conclusion 30
Trang 4The utilization of Atmega16 brings to the forefront its capability to interface seamlessly with peripherals and sensors, making it an ideal choice for projects demanding precision control The integration of encoder feedback into the control loop enables real-time adjustments, ensuring that the motor responds promptly to changes in the desired speed
Throughout this essay, we will explore the intricacies of the project, from the theoretical foundations of motor control to the practical implementation of algorithms on the Atmega16 microcontroller The significance of encoder motors in various applications and the role of precise speed control will be discussed, shedding light on the project's relevance in the broader context of mechatronics
As we navigate through the technical details, it becomes evident that this project not only serves
as a valuable learning experience for students in the field of electronics and robotics but also has the potential to contribute to advancements in automation and control systems By the end of this essay, readers will gain insights into the complexities of encoder motor speed control and the proficiency of Atmega16 in tackling such challenges, inspiring further exploration and innovation
in the ever-evolving landscape of mechatronics
Trang 5Hardware Info
1 Components:
Microprocessor Atmaga16:
- 8-bit AVR RISC architecture
- Modified Harvard architecture
- 16 KB Flash program memory
Trang 66
LCD LM016M:
- 16x2 character LCD
- Alphanumeric characters and symbols
- 5x8 dot matrix for each character
- Parallel interface with microcontrollers
- Requires several I/O pins
- Power: Operates on low voltage (usually 5V)
- Compact and suitable for space-limited projects
Trang 7Encoder Motor: 50RPM (12V)
- Transmission ratio 171:1
- No-load current: 60mA
- Maximum current under load: 1.3A
- No-load speed: 50RPM (50 rounds per minute)
- Maximum speed under load: 27RPM (27 rounds per minute)
- Rated pulling force Moment: 4KG.CM
- Maximum Moment Force: 9KG.CM
- Maximum current for each H-bridge is: 2A
- Logic voltage level: Low -0.3V~1.5V, High: 2.3V~Vss
- Dimensions: 43x43x27mm
Trang 8Proportional (P) Term:
The proportional term is based on the current error, which is the difference between the desired setpoint and the actual output It produces an output that is proportional to the magnitude of the error
The proportional term helps the system respond to the current error, but it alone may result in steady-state error, where the system settles with a non-zero difference between the setpoint and the actual output
Trang 9Mathematically, the proportional term (P) is given by:
Integral (I) Term:
The integral term addresses the accumulation of past errors over time It helps eliminate state error by integrating the error over time This term is useful for systems with inherent biases
Control Output:
The overall control output, often denoted as U(t), is the sum of the proportional, integral, and derivative terms
U(t)=P+I+D
determine the sensitivity of each term and need to be tuned to achieve the desired system response PID controller adjusts the motor input voltage (or current) to maintain the motor speed close to the desired setpoint The feedback signal often comes from a sensor, such as an encoder, providing information about the motor speed The PID controller continuously adjusts the motor input to minimize the difference between the setpoint and the actual speed
Trang 1010
Software Programing
We implement a speed control system using a PID (Proportional-Integral-Derivative) controller
on an ATmega16 microcontroller The system continuously monitors the encoder input, calculates the current speed, and adjusts the motor's PWM signal to maintain the desired speed The user can
alcd.h: This header file provides a set of functions and macros for interacting with an alphanumeric LCD, and it is meant to be included in other source files to enable LCD functionality in an embedded system
Trang 11mega16_bits.h: bit definitions for the ATmega16 microcontroller These definitions are used to manipulate specific bits in the microcontroller's registers for various peripherals such as timers, interrupts, ADC, etc
stdarg.h: This file is used to define macros for accessing a variable number of arguments in functions like printf and scanf
stdbool.h: This file provides a boolean data type and the constants true and false
Trang 12Header Files: The necessary header files are included, such as <mega16.h> (for specific definitions), <delay.h> (for delay functions), <alcd.h> (for alphanumeric LCD functions),
ATmega16-<stdbool.h> (for boolean type), <stdio.h>, <stdlib.h>, and <math.h>
Pin and Parameter Definitions:
Various macro definitions are used to define pins and parameters For example, pin definitions for up_sw, down_sw, start_sw, mode_sw, enable_pin, encoder_pin, encoder_revolution, gear_ratio, etc
Global Variables:
Global variables are declared to store various parameters and states, such as set_mode, run_mode, mode_press, start_press, speed_set, encoder, speed_target, output, error values, and PID coefficients (ki, kp, kd)
Interrupt Service Routines:
External Interrupt (ext_int0_isr):
Triggered on external interrupt 0 (used for encoder input)
Detects the direction of encoder rotation (Clockwise or Counterclockwise) and updates the encoder variable accordingly
Timer 2 Overflow (timer2_ovf_isr):
Triggered on Timer 2 overflow
Controls the main timing of the system, updating variables and triggering the PID controller periodically
Main Function:
Port Configuration: Configures the input/output ports of the microcontroller
Trang 13Timer Configurations:
Initialization: Initializes LCD display and other peripherals
Main Loop:
Infinite loop (while(1)) where the main control logic resides
Setting Function:
PID Speed Computation and PWM Speed Output Functions:
The pid_speed_compute function computes the PID control output based on the current speed error, integral, and derivative terms
The pwm_speed_out function adjusts the PWM output to control the motor speed
PID Computation Function (pid_speed_compute):
PID Algorithm: Computes the PID control output based on the error (difference between set speed and actual speed), integral of error, and derivative of error
PWM Output Function (pwm_speed_out):
PWM Mapping: Maps the PID control output to PWM signals for controlling the motor speed Main Loop:
adjust settings
PID Control Algorithm:
The PID gains (Kp, Ki, Kd) are set dynamically based on user-set values (kp_set, ki_set, kd_set) PWM Control:
The pwm_speed_out function adjusts the motor speed by mapping the PID control output to PWM EEPROM Usage:
Uses EEPROM to store and retrieve PID gains (ki, kp, kd) between program runs
Trang 1414
Code pid_speed.c:
/******************************************************* Chip type: ATmega16
Program type: Application
AVR Core Clock frequency: 16.000000 MHz Memory model: Small
External RAM size: 0
Data Stack size: 256
#define up_sw PINB.0
#define down_sw PINB.1
#define start_sw PINB.2
#define mode_sw PINB.3
#define enable_pin PORTD.3
#define encoder_pin PIND.6
#define encoder_revolution 11
#define gear_ratio 171
// Declare your global variables here
bool set_mode=false;
Trang 15void pwm_speed_out(float pwm_value);
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void) {
// Place your code here
TCNT2=99;
Trang 16// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1)
| (0<<DDA0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
// Port B initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=P Bit2=P Bit1=P Bit0=P
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (1<<PORTB3) | (1<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
Trang 17// Port C initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
// Port D initialization
// Function: Bit7=In Bit6=In Bit5=Out Bit4=Out Bit3=out Bit2=In Bit1=In Bit0=In
DDRD=(0<<DDD7) | (0<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (0<<DDD2) | (0<<DDD1)
| (0<<DDD0);
// State: Bit7=T Bit6=P Bit5=0 Bit4=0 Bit3=T Bit2=T Bit1=T Bit0=T
PORTD=(0<<PORTD7) | (1<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 15.625.000 kHz
// Mode: Normal top=0xFF
// OC0 output: Disconnected
// Timer Period: 10 ms
TCCR2=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (1<<CS02) | (1<<CS01) | (1<<CS00);
TCNT2=99;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 16000.000 kHz
// Mode: Fast PWM top=ICR1
// OC1A output: Non-Inverted PWM
// OC1B output: Non-Inverted PWM
Trang 1818
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 0.0625 us
// Output Pulse(s):
// OC1A Period: 0.0625 us
// OC1B Period: 0.0625 us
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (0<<COM1B0) | (1<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (1<<WGM12) | (0<<CS12) | (0<<CS11) | (1<<CS10);
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (1<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
Trang 19// INT2: Off
GICR|=(0<<INT1) | (1<<INT0) | (0<<INT2);
MCUCR=(0<<ISC11) | (0<<ISC10) | (1<<ISC01) | (0<<ISC00); MCUCSR=(0<<ISC2);
GIFR=(0<<INTF1) | (1<<INTF0) | (0<<INTF2);
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTA Bit 0
TCCR1A&=0x7f; // disable pwm A out
TCCR1A&=0xdf; // enable pwm B out
enable_pin=0;
TIMSK&=0xbf;//disable timer0 interupt
lcd_printfxy(0,0," SPEED PID ");
Trang 20TCCR1A&=0x7f; // disable pwm A out
TCCR1A|=0x20; // enable pwm B out
while (1)
{
// Place your code here
if(mode_sw==0 && mode_press==0){ mode_press=1;
TIMSK&=0xbf;//disable timer2 interupt setting();
}
Trang 21TIMSK&=0xbf;//disable timer2 interupt lcd_printfxy(0,0," SET:%02d RPM ",speed_set); lcd_printfxy(0,1," STOP ");
lcd_puts(str);
Trang 24lcd_puts(str);
if(i<2)
Trang 27lcd_printfxy(0,0," SET:%02d RPM ",speed_set);
sprintf(str,"Kpid: %1.0f %1.0f %1.2f",kp_set,ki_set,kd_set); lcd_gotoxy(0,1);
Trang 29// TCCR1A&=0x7f; // disable pwm A out
// TCCR1A|=0x20; // enable pwm B out
Trang 3030
Conclusion The integration of PID control principles allows the system to achieve and maintain a desired setpoint with high accuracy The Atmega16 microcontroller, with its seamless interfacing capabilities and processing power, emerges as a robust choice for projects demanding precision control The inclusion of encoder feedback in the control loop facilitates real-time adjustments, ensuring prompt responses to changes in desired speed
As we delved into the technical details, the significance of encoder motors in various applications became evident From robotics to industrial automation, the ability to control speed and position precisely is paramount The project not only serves as a valuable learning experience for students
in electronics and robotics but also holds potential for contributing to advancements in automation and control systems
In conclusion, this project stands at the intersection of theoretical knowledge and practical implementation, offering a holistic understanding of encoder motor speed control By leveraging the proficiency of Atmega16, students and enthusiasts alike can explore the complexities of mechatronics, inspiring further innovation in this ever-evolving landscape The quest for precision and efficiency continues, and projects like these pave the way for a future where mechatronic systems seamlessly navigate the intricacies of real-world challenges
Pros:
motor speed The proportional, integral, and derivative terms work together to minimize the difference between the desired setpoint and the actual motor speed
system to make real-time adjustments This ensures that the motor promptly responds to changes in the desired speed, contributing to a dynamic and adaptable control system
potential to contribute to advancements in automation and control systems The principles demonstrated here can be extended to more complex systems and applications, influencing the broader landscape of mechatronics
Cons:
Tuning the PID coefficients (Kp, Ki, Kd) requires expertise, and an improperly tuned system may exhibit instability or suboptimal performance
troubleshooting the system could be challenging, especially for users who are not versed in control theory and microcontroller programming