This repository contains a Python-based Field-Oriented Control (FOC) simulation for a Brushless DC (BLDC)/PMSM motor, along with a Tkinter GUI that allows you to edit parameters and plot the simulation results.
Parameters Tab Plot Tab
- Clone or download this repository.
- In a terminal (or cmd prompt) navigate to the project folder
- Run: python gui.py
- Parameters Tab:
- You’ll see four columns:
- Motor & Load: Resistance, inductance, pole pairs, flux, inertia, friction, etc.
- Controller Gains: Position loop (Kp, Ki, Kd), speed loop, and dq current loops.
- Simulation: Total simulation time, time-step, DC bus voltage.
- Trajectory: Final angle (deg), max velocity, max acceleration, gear ratio.
- Hover over any label to see a tooltip with more details about that parameter.
- You’ll see four columns:
- Run Simulation Button:
- After editing parameters, click “Run Simulation”.
- The script collects all parameter values, calls the FOC simulation function, and then plots the results in the Plot tab.
- Plot Tab:
- Displays eight subplots (arranged in 4×2) showing:
- Load angle vs. reference
- Speeds
- dq currents
- dq voltages
- Electromagnetic torque
- Three-phase voltages
- PWM duty cycles
- One spare plot (placeholder)
- You can switch back and forth between tabs.
- Displays eight subplots (arranged in 4×2) showing:
- Trapezoidal Velocity Profile: The motor’s position reference is generated by a trapezoid (or triangular if the distance is too short) motion profile up to the final angle (in degrees).
- FOC Loops:
- Position Loop (outer) → speed reference
- Speed Loop (middle) → torque/current reference
- Current Loops (inner, dq-axis) → voltage commands
- Space-Vector PWM: The dq voltage commands are transformed back into three-phase voltages and duty cycles.
- Electrical & Mechanical Equations: The motor’s stator equations are integrated each time-step, as well as the rotor/load inertia and friction.
- Motor/Load Parameters: Adjust default values in foc_sim.py to match your hardware.
- Controller Gains: Tune Kp/Ki/Kd for position/speed/current loops to see different control responses.
- Plot Layout: Modify the draw_plots_in_figure method in gui.py to add or remove subplots.
- Tooltips: You can adjust tooltip text or position in the create_tooltip function.
-
foc_sim.py- Holds all the motor/load parameters, controller gains, and the simulation logic (including a trapezoidal velocity profile, the electrical/mechanical equations, and space-vector PWM calculations).
- Returns a dictionary of time-series results (currents, voltages, torques, etc.) after running the simulation.
-
gui.py- Provides a Tkinter interface with two tabs:
- Parameters: All motor, load, controller, simulation, and trajectory parameters in one tab, arranged in columns. You can edit them directly in text fields.
- Plot: Shows embedded Matplotlib subplots after the simulation run.
- Includes tooltips on each parameter label, displaying short explanations when you hover the mouse over them.
- Provides a Tkinter interface with two tabs:
- Python 3.7+ (or newer)
- NumPy (for numeric arrays)
- Matplotlib (for plotting)
- Tkinter (typically comes with Python on most platforms)
- (Optional) venv or conda environment recommended for clean dependency management.
You can install dependencies via:
pip install numpy matplotlib