Home
Blog
Go Back
NEWS FROM US
|
5
Min
Updated On
September 27, 2024

PID Controller Design with STM32 Microcontrollers | Acrome

PID Controller Design with STM32 Microcontroller using Ball Balancing Table

Acrome’s Ball Balancing Table's already extensive software option now comes with the C option which enables it to be used with one of the most popular microcontrollers STM32!! Check out the below video to see how it works. Details on how to use this new option is explained in this document.

Introduction:

A feature requested from our users is using the STM32 microcontrollers and C programming language. The STM family of microcontrollers are one of the most popular microcontroller devices, and used by academia in laboratories, hobbyists and private companies as well.

In this blog document, we would like to give an example on how to use STM32 microcontroller with a classical control experiment such as Ball Balancing Experiment system using the PID control and make it possible to use the C language for PID controller design principles. If you are not familiar with the PID controller design principles, feel free to read our other blog document about PID.

Before diving into the details of the system, let's give some brief information about STM32 and IDE used to program it:

STM32 Microcontrollers:

STM32 microcontrollers are integrated circuits that STMicroelectronics produces. They are widely utilized in a wide range of applications, including those in the industrial, medical, consumer electronics sectors, research and in academia as well. They have 32-bit Arm Cortex-M processors in their foundation, and they provide a selection of peripheral ports and memory configurations.

Because of its adaptability, affordable price, and strong support from STMicroelectronics, the STM32 microcontroller series are well known. With its wide variety of peripheral interfaces, including USB, Ethernet, and CAN, a ready-to-use IDE named CubeIDE and with cross-compilers for mostly used IDEs, they are very suitable for a wide range of applications. Engineers favor it because of its real-time capabilities, large memory options, and low power consumption.

Integrated Development Environment (IDE):

The process of developing software for microcontrollers is done with integrated development environments (IDEs). A variety of IDEs, including the widespread CubeIDE created by STMicroelectronics, are supported by the STM32 microcontroller series, as well as other industry-recognized IDEs including Keil uVision, IAR Embedded Workbench, and Atollic TrueSTUDIO. Editors, compilers, debuggers, and system setup tools are all provided by these IDEs as part of their extensive toolkit for software development. Personal preference and the precise project needs are frequently determining factors in IDE selection.

STMicroelectronics' CubeIDE has a number of benefits over other IDEs when used with the STM32 microcontroller line. First off, it offers a user-friendly interface that streamlines the design process and enables engineers to effectively take advantage of the STM32's full capabilities. The CubeIDE also seamlessly combines with other STMicroelectronics development tools, such the STM32CubeMX, for an efficient development process. This accelerates the development process by removing the need to switch between various tools.

STM32CubeIDE Interface
STM32CubeIDE Interface

Since CubeIDE is created by STM electronics, it fully supports all STM32 microcontrollers including STM32F3 family which is used in our system. 1st and 3rd party libraries are included in CubeIDE which can be used to program endless things.

The hardware used is as follows:

  • STM32F303RET6 Microcontroller: STM32 is the processing unit of the system, it takes the position of the ball as input from Acrome controller then processes this data to calculate the position of Servo motors and transfers them to Acrome controller again as output.
  • Acrome Controller: This controller gets the position of the ball and transfers it to the STM32 controller; when the position of Servo motors is received; Acrome controller drives the motors to their desired position.
Model of a Acrome Controller

  • Acrome’s Ball Balancing Table System:

The ball balancing table system is a classic example of a dynamic control system that requires precise and quick control inputs to balance a ball on a platform. The input to this system can be the position and orientation of the ball relative to the platform. This input is obtained through a touchscreen that is equipped across the system's table. The output of the system would be the control signals applied to the platform motors to keep the ball in balance.

The controllable parameters of the ball balancing table system are the motor speeds and the angle of tilt of the platform. By controlling these parameters, the platform can be adjusted to keep the ball in a stable position. The PID controller takes the difference between the desired position of the ball and its actual position as input and outputs the control signals for the motors. 

          

Acrome Ball Balancing Table

For Software:

C programming language is used, as it is the major programming language for programming the ARM and STM controllers. 

Our sample code written in C language mainly consists of 3 parts: Main file that contains all necessary functions to run the system, PID controller file and communication file. The most important key features included in the code are:

  • PID Controller which can be tuned manually by the user.
  • Auto calibration function which calibrates the system by pressing 1 button only on the STM32 controller. This button also comes with a button debouncing feature. More details provided below
  •  Protocol to initialize communication between Acrome controller and STM32 controller.

The example code in the video consists of 3 files:

1- Main file (main.c)

As the name suggests, the main file contains the functions and commands that make the system work. 

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
void start_usart_communication();
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeD

2- PID Controller file (pid.c):

This is where the PID controller’s Kp, Ki and Kd gains can be adjusted manually.

A PID controller is a type of feedback control system that uses a set of equations to calculate and manipulate the output of a system in order to control a process and maintain it at a desired setpoint. The controller uses the error between the setpoint and the current process value, as well as the historical values of the error, to calculate the necessary corrective action. This action is then applied to the system in the form of a control signal, which can be used to adjust the system's output and bring it closer to the desired setpoint.

//PID input
	servo->uk[1] = servo->uk[0];
	servo->uk[0] = servo->error_sum;
	servo->uk_sum = servo->uk[1] / (sizeof(servo->uk)/sizeof(*(servo->uk)));
	servo->uk_sum += servo->uk[0] / (sizeof(servo->uk)/sizeof(*(servo->uk)));

//Proportional gains
	servo->P = servo->kp * servo->uk[0];
// integral gain
	servo->I = servo->ki * servo->uk_sum;
	servo->I = (servo->I > 800) ? 800 : (servo->I < -800) ? -800 : servo->I; // saturate integral gain
	servo->ts = 0.04;
//Derivate gain
	servo->D = servo->kd * (servo->uk[0] - servo->uk[1]) / (servo->ts);
//PID
	servo->yk = servo->P + servo->I + servo->D; //PID
	servo->yk = (servo->yk < -1000) ? -1000 :
				(servo->yk > 1000) ? 1000 : servo->yk; 
 //saturate PID signal because our motor can work in between -1000 and 1000

3- Communication protocol file (bbt.c):

For the communication between Acrome controller and STM32 controller; USART protocol is used.

USART, or Universal Synchronous/Asynchronous Receiver/Transmitter, is a type of serial communication protocol that allows devices to transmit and receive data over a single communication line. 

In synchronous mode, the transmission of data is synchronized to a fixed clock signal, which allows for faster transmission speeds but requires that both devices operate at the same clock frequency.

void generate_transmit_data(struct BBT *bbt_transmit_data) {
	memcpy(bbt_transmit_data->usart_sending_data, &bbt_transmit_data->hdr,
			sizeof(bbt_transmit_data->hdr));
	memcpy(bbt_transmit_data->usart_sending_data + 1,
			&bbt_transmit_data->device_id,
			sizeof(bbt_transmit_data->device_id));
	memcpy(bbt_transmit_data->usart_sending_data + 2,
			&bbt_transmit_data->servo[0], sizeof(bbt_transmit_data->servo[0]));
	memcpy(bbt_transmit_data->usart_sending_data + 4,
			&bbt_transmit_data->servo[1], sizeof(bbt_transmit_data->servo[1]));
	bbt_transmit_data->crc32 = HAL_CRC_Calculate(&hcrc,
			(uint32_t*) bbt_transmit_data->usart_sending_data, 6);
	memcpy(bbt_transmit_data->usart_sending_data + 6, &bbt_transmit_data->crc32,
			sizeof(bbt_transmit_data->crc32));
}

In addition; CRC32 algorithm is used in the communication to prevent any error in the data to be transmitted.

Important details about some features:

CRC32 Algorithm:

Cyclic Redundancy Check 32, is a type of checksum algorithm that is used to detect errors in data transmission or storage. It is called a "cyclic redundancy check" because it uses a series of calculations based on the binary data being transmitted or stored, and the resulting checksum value is determined by a cyclic algorithm. The CRC32 algorithm produces a 32-bit checksum value, which can be used to detect errors in the transmitted or stored data. If the data being transmitted or stored is changed in any way, the resulting checksum value will be different, allowing the receiving device to detect the error and take appropriate action. The CRC32 algorithm is widely used in many different applications, including data transmission and storage, error correction, and data integrity verification.

cyclic redundancy check diagram

Button debouncing:

Button debouncing is a technique used to prevent a single press of a button or switch from being registered multiple times by a computer or other electronic device. When a button is pressed, it can sometimes bounce, causing the electrical contacts inside the switch to make and break multiple times in rapid succession. This can cause the device to register multiple presses of the button, even though the user only intended to press it once. Button debouncing involves adding a small delay to the processing of the button press, allowing the electrical contacts to settle and ensuring that only a single press is

registered. This can improve the reliability and accuracy of the device and prevent unintended multiple presses of the button.

Auto-calibration button:

When this button is pressed on the STM32 controller, the system starts calibrating the ball until the error becomes lower than Deadband.

if (state == 1) {
offset_data.calibrate_x += (offset_data.error_x > 0) ? -0.03 :
(offset_data.error_x < 0) ? +0.03 : 0;
offset_data.calibrate_y += (offset_data.error_y > 0) ? -0.03 :
(offset_data.error_y < 0) ? +0.03 : 0;
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1);
		}
// if error is  smaller than dead band , system will stop calibration.
if (fabs(offset_data.error_x) < offset_data.dead_band
&& fabs(offset_data.error_y) < offset_data.dead_band) {
// We must use a timer because the ball has momentum.The timer help to wait until stop the ball.
			HAL_TIM_Base_Start_IT(&htim2);
		}

PID Controller Design:

PID control stands for Proportional-Integral-Derivative control, so its the combination of 3 controllers with each having its own function. Because each controller has its own advantages and disadvantages, combining the 3 of them in 1 controller eliminates the disadvantages of using each one individually.

PID control diagram
  • Proportional Controller:

A proportional controller modifies the control signal proportionally to the difference between the desired setpoint and the actual process variable. In other words, it calculates the control signal by multiplying the mistake by a gain factor. Due to the control signal's direct relationship to the error's magnitude, the process is subsequently more precisely controlled. They are easy to implement, have a quick response time, and require little tuning. In some systems, however, proportional controllers might be unable to deliver the requisite level of stability and control, and additional control elements like derivative or integral controllers can be needed to achieve the desired performance.

  • Integral Controller:

An integral controller calculates the integral of the error, which is the sum of all past errors, and multiplies it by a gain factor to determine the control signal. This helps to eliminate steady-state error, which occurs when a proportional controller is unable to drive the process variable to its desired setpoint. Integral controllers are commonly used in conjunction with proportional controllers to provide improved performance and stability in control systems. They have the ability to drive the process variable to the setpoint and maintain it there, even in the presence of disturbances. However, integral controllers can cause overshoot and oscillations if not properly designed and tuned. They also have a slower response time compared to proportional controllers and may be less-suitable for dynamic systems.

  • Derivative Controller:

A derivative controller adjusts the control signal based on the rate of change of the error. It calculates the derivative of the error, which is the rate at which the error is changing, and multiplies it by a gain factor to determine the control signal. This helps to improve the response time and stability of the control system by anticipating the future behavior of the process variable and reducing overshoot. Derivative controllers are commonly used in conjunction with proportional and integral controllers to provide a more complete control solution. They are particularly useful in high-dynamic systems where a fast response time is required. However, derivative controllers can also amplify noise in the measurement signal and cause instability if not properly designed and tuned.

Whether you are a seasoned programmer or just starting out with C, we encourage you to explore the full potential of this new option and see what you can accomplish with the Acrome ball balancing table system.

You can check Ball Balancing Table and its other software options and our other products by checking our website ! together we shape the engineers of the future. 

Useful Links:

Author

Serhan Argun
R&D Manager

Discover Acrome

Acrome was founded in 2013. Our name stands for ACcessible RObotics MEchatronics. Acrome is a worldwide provider of robotic experience with software & hardware for academia, research and industry.