# 开源 **Repository Path**: cccccciki/open-source ## Basic Information - **Project Name**: 开源 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-23 - **Last Updated**: 2026-01-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # PID Controller Test Project (pid_test) This project is a PID controller test system based on the STM32F103 microcontroller, implementing position and speed loop PID control for a motor, with real-time system status displayed on an OLED screen. ## Project Overview This project provides a complete embedded PID control solution suitable for motor control, robotic joint control, and similar applications. The system adopts a layered architecture comprising a motor driver layer, PID control layer, and application logic layer. ## Key Features - **Dual-loop PID Control**: Simultaneously supports independent control of position loop (PID position) and speed loop (PID speed) - **Motor Control**: Supports DC motor PWM speed control - **Encoder Reading**: Real-time acquisition of motor encoder data for position and speed feedback - **OLED Display**: Real-time system status display via I2C-connected OLED screen - **Graphics Drawing**: Supports basic shapes such as points, lines, rectangles, circles, and triangles - **Chinese Character Display**: Built-in font library supporting multiple sizes of Chinese and English characters ## Hardware Requirements - STM32F103xB series microcontroller - I2C interface OLED display (SSD1306 compatible) - DC motor with encoder feedback - Motor driver module (e.g., L298N, TB6612, etc.) - ST-Link or J-Link debugger ## Software Architecture ``` pid_test/ ├── Core/ # STM32CubeMX-generated low-level drivers │ ├── Inc/ # Header files │ └── Src/ # Source files (gpio, i2c, tim, usart, oled, font, etc.) ├── application/ # Application layer code │ ├── controller.c/h # PID controller implementation │ ├── motor.c/h # Motor control │ ├── pid.c/h # PID algorithm implementation │ ├── read.c/h # Encoder data reading │ └── test.c # OLED display test ├── Drivers/ # STM32 HAL and CMSIS libraries ├── CMakeLists.txt # CMake build configuration └── pid_test.ioc # STM32CubeMX configuration file ``` ## Core Module Descriptions ### PID Controller (`application/pid.c/h`) ```c // PID initialization void PID_init(pid_type_def *pid, uint8_t mode, const float PID[3], float max_out, float max_iout); // PID calculation float PID_calc(pid_type_def *pid, float ref, float set); // PID parameters: [Kp, Ki, Kd] ``` ### Controller Logic (`application/controller.c/h`) - `controller_init()`: Initializes controller state - `HAL_TIM_PeriodElapsedCallback()`: Timer period callback for PID computation ### Motor Control (`application/motor.c/h`) - `motor_init()`: Initializes motor PWM - `SetPower()`: Sets motor power ### Data Reading (`application/read.c/h`) - `Read_init()`: Initializes encoder - `Read_Encoder()`: Reads encoder value - `R_Speed()`: Calculates rotational speed - `R_Angle()`: Calculates angle ### OLED Driver (`Core/Src/oled.c/h`) - `OLED_Init()`: Initializes OLED display - `OLED_SetPixel()`: Sets pixel - `OLED_DrawLine/DrawRectangle/DrawCircle()`: Draws shapes - `OLED_PrintString()`: Displays string - `OLED_NewFrame()/OLED_ShowFrame()`: Updates frame buffer ## Build Instructions ### Environment Requirements - CMake 3.15+ - ARM GCC toolchain (gcc-arm-none-eabi) - STM32CubeMX ### Build Steps ```bash # 1. Use STM32CubeMX to open pid_test.ioc and regenerate code # 2. Build the project mkdir build && cd build cmake .. make ``` ### Firmware Flashing Using ST-Link: ```bash st-flash --reset write build/pid_test.bin 0x8000000 ``` ## Usage Guide 1. **Hardware Connections** - Connect the OLED screen to I2C2 - Connect the motor encoder to timer encoder mode pins - Connect the motor driver PWM signal to timer output pins 2. **Parameter Configuration** Modify target angle and PID parameters in `controller.c`: ```c const float position[3] = {Kp, Ki, Kd}; // Position loop parameters const float speed[3] = {Kp, Ki, Kd}; // Speed loop parameters float target_angle = 90; // Target angle ``` 3. **Debugging & Monitoring** - OLED screen displays current system status - Serial output for debugging (baud rate: 115200) ## API Reference ### OLED Display API | Function | Description | |----------|-------------| | `OLED_Init()` | Initializes OLED | | `OLED_FillRectangle()` | Fills rectangular area | | `OLED_PrintString()` | Displays string | | `OLED_DrawImage()` | Displays image | | `OLED_NewFrame()` | Begins new frame | | `OLED_ShowFrame()` | Refreshes display frame | ### PID Control API | Function | Description | |----------|-------------| | `PID_init()` | Initializes PID controller | | `PID_calc()` | Executes PID calculation | | `PID_clear()` | Resets PID state | ## Notes 1. Ensure power supply voltage and current meet motor requirements 2. PID parameters must be tuned according to actual load conditions 3. Encoder wiring must respect A/B phase sequence 4. For first-time use, verify OLED display functionality first ## License This project is released under an open-source license.