Sim_data for a custom hardware RL PMSM
古いコメントを表示
Hello im train to train a TD3 agent for a FOc PMSM
but im creating the Sim_data for my real hardware but im not sure if is working
this is my code
%% Model : PMSM Field Oriented Control (Sensorless, RL compatible)
% Description : sim_data for mcb_pmsm_foc_sim_RL
% Motor : MCAT-identified PMSM (sensorless)
% Purpose : Stable simulation + RL training
%
% IMPORTANT:
% This file follows MathWorks MCB naming conventions.
% Do NOT change field names unless you also edit Simulink blocks.
%% =========================================================
% PWM Switching frequency
%% =========================================================
PWM_frequency = 5e3; % Hz
T_pwm = 1/PWM_frequency; % s
%% =========================================================
% Sample times
%% =========================================================
Ts = T_pwm; % Controller sample time
Ts_simulink = T_pwm/2; % Plant simulation
Ts_motor = T_pwm/2;
Ts_inverter = T_pwm/2;
Ts_speed = 10*Ts; % Speed loop
%% =========================================================
% Data type
%% =========================================================
dataType = 'single';
%% =========================================================
% PMSM PARAMETERS (MCB-compatible)
%% =========================================================
pmsm = struct();
% -------- Electrical --------
pmsm.p = 4; % Pole pairs
pmsm.Rs = 0.54; % Ohm
pmsm.Ld = 235e-6; % H
pmsm.Lq = 218e-6; % H
% -------- Flux (from MCAT Kt) --------
% Kt = (3/2)*p*FluxPM -> FluxPM = Kt / (1.5*p)
pmsm.FluxPM = 0.0548 / (1.5 * pmsm.p); % Wb
% -------- Mechanical --------
pmsm.J = 1e-4; % kg*m^2
pmsm.B = 1e-4; % viscous friction (MUST exist)
% -------- Ratings --------
pmsm.I_rated = 6; % A
pmsm.V_rated = 15; % V
pmsm.N_rated = 4000; % rpm
% -------- Base speed (REQUIRED by model) --------
pmsm.N_base = 4000; % rpm
% -------- Encoder placeholders (REQUIRED even if sensorless) --------
pmsm.QEPSlits = 2048; % Dummy value (never used in sensorless)
%% =========================================================
% INVERTER PARAMETERS
%% =========================================================
inverter = struct();
inverter.Vdc = 24; % DC bus voltage
inverter.ISenseMax = 8.25; % A (hardware scale)
inverter.Rds_on = 0.05; % Ohm
inverter.DeadTime = 500e-9; % s
%% =========================================================
% TARGET PARAMETERS (generic)
%% =========================================================
target = struct();
target.PWM_frequency = PWM_frequency;
target.ADC_Vref = 3.3;
%% =========================================================
% PER-UNIT SYSTEM (used by scaling blocks)
%% =========================================================
PU_System = struct();
PU_System.I_base = pmsm.I_rated;
PU_System.V_base = pmsm.V_rated / sqrt(3);
PU_System.W_base = pmsm.N_base * 2*pi/60;
PU_System.T_base = (3/2) * pmsm.p * pmsm.FluxPM * PU_System.I_base;
%% =========================================================
% CURRENT PI CONTROLLER PARAMETERS
% (from your MCAT tuning)
%% =========================================================
PI_params = struct();
% Current loop gains
PI_params.Kp_d = 0.6746;
PI_params.Ki_d = 0.03434;
PI_params.Kp_q = 0.2490;
PI_params.Ki_q = 0.02231;
% Limits
PI_params.CurrentLimit = 0.9; % 90 %
% Delays (simulation)
PI_params.delay_Currents = 1;
PI_params.delay_Position = 1;
%% =========================================================
% SPEED LOOP PARAMETERS
%% =========================================================
Speed_params = struct();
Speed_params.Kp = 0.002368;
Speed_params.Ki = 0.0002252;
Speed_params.UpperLimit = 2; % A
Speed_params.LowerLimit = -2; % A
Speed_params.RampUp = 5000; % rpm/s
Speed_params.RampDown = 5000; % rpm/s
%% =========================================================
% SENSORLESS OBSERVER PARAMETERS
%% =========================================================
Observer = struct();
% BEMF observer
Observer.BEMF_F0 = 300; % Hz
Observer.BEMF_xi = 1;
% Tracking observer
Observer.TO_F0 = 70; % Hz
Observer.TO_xi = 1;
% Startup & merging
Observer.StartupRamp = 3000; % rpm/s
Observer.StartupCurrent = 0.65; % A
Observer.MergeSpeed = 500; % rpm
Observer.MergeCoeff = 1.0; % 100 %
%% =========================================================
% DISPLAY (debug)
%% =========================================================
disp("=== PMSM sim_data loaded successfully ===");
disp(pmsm);
``
but i need it to work like this one
% Model : PMSM Field Oriented Control
% Description : Set Parameters for Motor, Inverter and Controllers
% : for simulating the FOC control algorithm for PMSM
% File name : mcb_pmsm_foc_sim_data.m
% Copyright 2020-2021 The MathWorks, Inc.
%% Set PWM Switching frequency
PWM_frequency = 5e3; %Hz // converter s/w freq 5e3
T_pwm = 1/PWM_frequency; %s // PWM switching time period
%% Set Sample Times
Ts = T_pwm; %sec // simulation time step for controller
Ts_simulink = T_pwm/2; %sec // simulation time step for model simulation
Ts_motor = T_pwm/2; %sec // Simulation sample time
Ts_inverter = T_pwm/2; %sec // simulation time step for average value inverter
Ts_speed = 10*Ts; %Sec // Sample time for speed controller
%% Set data type for controller & code-gen
dataType = 'single'; % Floating point code-generation
%% System Parameters // Hardware parameters
pmsm = mcb.getPMSMParameters('Maxon_EC_Speedgoat');
pmsm.PositionOffset = 0.165;
%% Target & Inverter Parameters
target = mcb.getProcessorParameters('F28379D',PWM_frequency);
inverter = mcb.getInverterParameters('BoostXL-DRV8305');
% Update ISenseMax that is measurable by target ADC
inverter.ISenseMax = inverter.ISenseMax * target.ADC_Vref / inverter.ISenseVref;
%% Derive Characteristics
pmsm.N_base = mcb.getMotorBaseSpeed(pmsm,inverter); %rpm // Base speed of motor at given Vdc
%% PU System details // Set base values for pu conversion
PU_System = mcb.getPUSystemParameters(pmsm,inverter);
%% Controller design // Get ballpark values!
PI_params = mcb.getPIControllerParameters(pmsm,inverter,PU_System,T_pwm,Ts,Ts_speed);
%Updating delays for simulation
PI_params.delay_Currents = 1;
PI_params.delay_Position = 1;
%% Displaying model variables
disp(pmsm);
disp(inverter);
disp(target);
disp(PU_System);
https://es.mathworks.com/help/reinforcement-learning/ug/train-td3-agent-for-pmsm-control.html#TrainTD3AgentForPMSMControlExample-3
回答 (1 件)
Simran
約11時間 前
0 投票
I understand you're trying to create a custom sim_data script for training a TD3 agent to control a FOC PMSM with your real hardware, and you want to ensure your manually defined parameters work similarly to the MathWorks MCB example that uses utility functions like mcb.getPMSMParameters().
Assuming you're using MATLAB with Reinforcement Learning Toolbox and Simscape Electrical (Specialized Power Systems) for PMSM modeling on MATLAB R2025b using the latest Windows version:
Key differences in your approach:
The reference example uses Motor Control Blockset utility functions to automatically calculate parameters:
pmsm = mcb.getPMSMParameters('Maxon_EC_Speedgoat');
PI_params = mcb.getPIControllerParameters(pmsm,inverter,PU_System,T_pwm,Ts,Ts_speed);
Your script defines these manually, which is fine if your calculations are correct and match what the Simscape PMSM block expects.
I would suggest checking:
- Verify your parameter calculations match Simscape conventions:
- Check if your FluxPM calculation from Kt is correct
- Verify your PU_System base values match what the PMSM block expects
- Ensure pmsm.N_base is correctly defined
2. Confirm your script provides all required fields:
- The Simulink model expects specific field names (e.g., pmsm.Rs, pmsm.Ld, PI_params.Kp_d, etc.)
- Missing or incorrectly named fields will cause simulation errors
Please refer to these documentations for more infomation:
- Train TD3 Agent for PMSM Control - MATLAB & Simulink - relevant to your complete workflow
- PMSM Field-Oriented Control - Permanent magnet synchronous machine field-oriented control - Simulink - Motor drive control examples
- Simscape Electrical Documentation- Reference implementations
Hope this helps!
カテゴリ
ヘルプ センター および File Exchange で Motor Drives についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!