| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % This file is part of the hoverboard-new-firmware-hack-FOC project
- % Compared to previouse commutation method, this project implements
- % FOC (Field Oriented Control) for BLDC motors with Hall sensors.
- % The new control methods offers superior performanace
- % compared to previous method featuring:
- % >> reduced noise and vibrations
- % >> smooth torque output
- % >> improved motor efficiency -> lower energy consumption
- %
- % Author: Emanuel FERU
- % Copyright � 2019-2021 Emanuel FERU <aerdronix@gmail.com>
- %
- % This program is free software: you can redistribute it and/or modify
- % it under the terms of the GNU General Public License as published by
- % the Free Software Foundation, either version 3 of the License, or
- % (at your option) any later version.
- %
- % This program is distributed in the hope that it will be useful,
- % but WITHOUT ANY WARRANTY; without even the implied warranty of
- % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- % GNU General Public License for more details.
- %
- % You should have received a copy of the GNU General Public License
- % along with this program. If not, see <http://www.gnu.org/licenses/>.
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Clear workspace
- close all
- clear
- clc
- % Load model parameters
- Ts = 6.2e-6; % [s] Model sampling time (1 MHz)
- Ts_ctrl = 62e-6; % [s] Controller sampling time (~16 kHz)
- f_ctrl = 16e3; % [Hz] Controller frequency = 1/Ts_ctrl (16 kHz)
- Ts_speed = Ts_ctrl * 50;
- i_pwm_count = 4000;
- i_dead = 10;
- i_adc = 10;
- i_sample_before = 10;
- i_Udc = 300;
- i_half_pwm_count = i_pwm_count;
- i_sample_min = (i_dead + i_adc + i_sample_before);
- i_hall_count_max = 1/Ts;
- % Motor Paramaters
- Rs = 0.0918;
- Ld = 0.0009262;
- Lq = 0.001024;
- band_current = 3.14 * 2 * 4000;
- % Kp = 0.0009262 * 3.14 * 8000 = 26
- % Ki = 0.0918/0.0009262 = 99.11
- % Kserial = 99.11 / 0.5816 = 170.42
- % Current sample parameters
- n_adc_max = 4096;
- n_resistance = 0.0005;
- n_ref_vol = 3.3;
- n_gain = 17.1;
- %VBUS sample parameters
- b_start_with_commutation = 0;
- % Sine/Cosine wave look-up table
- res_elecAngle = 1;
- a_elecAngle_XA = 0:res_elecAngle:360; % [deg] Electrical angle grid
- r_sin_M1 = sin((a_elecAngle_XA)*(pi/180));
- r_cos_M1 = cos((a_elecAngle_XA)*(pi/180));
- % Speed limitations
- n_max = 5000; % [rpm] Maximum motor speed: [-5000, 5000]
- % open loop speed -> voltage lookup table
- res_rpm_openloop = 100;
- delta_rpm_openloop = 100; % delta rpm that when speed decreased, enter open loop
- min_start_voltage_openloop = 100;
- a_rpm_step = i_Udc / (n_max / res_rpm_openloop);
- % Motor parameters
- n_polePairs = 4; % [-] Number of motor pole pairs
- a_elecPeriod = 360; % [deg] Electrical angle period
- a_elecDeltaAngle = 60; % [deg] Electrical angle between two Hall sensor changing events
- a_mechAngle = a_elecDeltaAngle / n_polePairs; % [deg] Mechanical angle between two Hall sensor changing events
- r_whl = 6.5 * 2.54 * 1e-2 / 2; % [m] Wheel radius. Diameter = 6.5 inch (1 inch = 2.54 cm): Speed[kph] = rpm*(pi/30)*r_whl*3.6
- f_lpf_coeff = 0.12;
- %hall, [4,6,2,3,1,5,4] [ 3,2,6,4,5,1]
- vec_hallToPos = [7 5 1 0 3 4 2 7]; % [-] Mapping Hall signal to position
- i_hall_offset = -30;
- % Speed Calculation Parameters
- cf_speedCoef = (i_hall_count_max * a_mechAngle * (pi/180) * (30/pi)); % [-] Speed calculation coefficient (factors are due to conversions rpm <-> rad/s)
- z_maxCntRst = i_hall_count_max; % [-] Maximum counter value for reset (works also as time-out to detect standing still)
- n_commDeacvHi = 30; % [rpm] Commutation method deactivation speed high
- n_commAcvLo = 15; % [rpm] Commutation method activation speed low
- dz_cntTrnsDetHi = 140; % [-] Counter gradient High for transient behavior detection (used for speed estimation)
- dz_cntTrnsDetLo = 100; % [-] Counter gradient Low for steady state detection (used for speed estimation)
- n_stdStillDet = 3; % [rpm] Speed threshold for Stand still detection
- % Motor Angle Measurement (e.g. using an encoder)
- b_angleMeasEna = 0; % [-] Enable flag for external mechanical motor angle sensor: 0 = estimated (default), 1 = measured
- % Control model request
- OPEN_MODE = 0; % [-] Open mode
- SPD_MODE = 1; % [-] Speed mode
- TRQ_MODE = 2; % [-] Torque mode
- z_ctrlModReq = TRQ_MODE; % [-] Control Mode Request (default)
- % Cruise control
- b_cruiseCtrlEna = 0; % [-] Cruise control enable flag: 0 = disable (default), 1 = enable
- n_cruiseMotTgt = 0; % [-] Cruise control motor speed target
- %% F04_Field_Weakening
- b_fieldWeakEna = 0; % [-] Field weakening enable flag: 0 = disable (default), 1 = enable
- r_fieldWeakHi = 1000; % [1000, 1500] Input target High threshold for reaching maximum Field Weakening / Phase Advance
- r_fieldWeakLo = 750; % [ 500, 1000] Input target Low threshold for starting Field Weakening / Phase Advance
- n_fieldWeakAuthHi = 400; % [rpm] Motor speed High for field weakening authorization
- n_fieldWeakAuthLo = 300; % [rpm] Motor speed Low for field weakening authorization
- % FOC method
- id_fieldWeakMax = 5; % [A] Field weakening maximum current
- %% F05_Field_Oriented_Control
- z_selPhaCurMeasABC = 0; % [-] Select measured current phases: {iA,iB} = 0; {iB,iC} = 1; {iA,iC} = 2
- % Motor Limitations Calibratables
- speed_pi_time = 1e-3;
- speed_pi_count = speed_pi_time/Ts_ctrl;
- cf_iqKiLimProt = 60 / (f_ctrl/3); % [-] Current limit protection integral gain (only used in VLT_MODE and SPD_MODE)
- cf_nKiLimProt = 20 / (f_ctrl/3); % [-] Speed limit protection integral gain (only used in VLT_MODE and TRQ_MODE)
- cf_KbLimProt = 1000 / (f_ctrl/3);% [-] Back calculation gain for integral anti-windup
- % Voltage Limitations
- V_margin = 0.95; % [-] Voltage margin to make sure that there is a sufficiently wide pulse for a good phase current measurement
- Vd_max = i_Udc * V_margin;
- Vq_max_XA = 0:1:Vd_max;
- Vq_max_M1 = sqrt(Vd_max^2 - Vq_max_XA.^2); % Circle limitations look-up table
- i_sca = 1; % [-] [not tunable] Scalling factor A to int16 (50 = 1/0.02)
- % Current Limitations
- i_max = 45; % [A] Maximum allowed motor current (continuous)
- i_max = i_max * i_sca;
- iq_maxSca_XA = 0:0.02:0.99;
- iq_maxSca_XA = fixpt_evenspace_cleanup(iq_maxSca_XA, ufix(16), 2^-16); % Make sure the data is evely spaced up to the last bit
- iq_maxSca_M1 = sqrt(1 - iq_maxSca_XA.^2); % Current circle limitations map
- %% F06_Control_Type_Management
- % Commutation method
- z_commutMap_M1 = [1 0 -1 -1 0 1; % Phase A
- 0 1 1 0 -1 -1; % Phase B
- -1 -1 0 1 1 0]; % Phase C [-] Commutation method map
- % Q axis control gains
- cf_iqKp = 0.3; % [-] P gain
- cf_iqKi = 100 / (f_ctrl/3); % [-] I gain
- % D axis control gains
- cf_idKp = 0.9; % [-] P gain
- cf_idKi = 1.071; % [-] I gain
- % Speed control gains
- cf_nKp = 1.18; % [-] P gain
- cf_nKi = 20.4 / (f_ctrl/3);% [-] I gain
|