% mcb_host_mode_control_script.m
% Initializes the parameters for mcb_host_mode_control.slx

% Copyright 2020 The MathWorks, Inc.

block='mcb_host_mode_control/Serial Communication/Dashboard control/';
block_first='mcb_host_mode_control/Serial Communication/Dashboard control/Change detect';
block_last='/Unit Delay';

host_init.mode=0;
host_init.signal1=7;
host_init.signal2=8;

% open loop run
host_init.Vpk=0.2;
host_init.N_rpm=10;

% Torque mode
host_init.Id_ref=0;
host_init.Iq_ref=0;
host_init.Nmode=0; % 0 is locked rotor, 1 is free run

% Speed mode
host_init.Nref=0.2; 


set_param([block 'Operating Mode'],'value',num2str(host_init.mode));
set_param([block 'Signal Sel 1'],'value',num2str(host_init.signal1));
set_param([block 'Signal Sel 2'],'value',num2str(host_init.signal2));
set_param([block 'Id Ctrl Kp Gain'],'value',num2str(PI_params.Kp_i));
set_param([block 'Id Ctrl Ki Gain'],'value',num2str(PI_params.Ki_i));
set_param([block 'Iq Ctrl Kp Gain'],'value',num2str(PI_params.Kp_i));
set_param([block 'Iq Ctrl Ki Gain'],'value',num2str(PI_params.Ki_i));
set_param([block 'Speed Ctrl Kp Gain'],'value',num2str(PI_params.Kp_speed));
set_param([block 'Speed Ctrl Ki Gain'],'value',num2str(PI_params.Ki_speed));
set_param([block 'Voltage (peak)'],'value',num2str(host_init.Vpk));
set_param([block 'Speed in open loop'],'value',num2str(host_init.N_rpm));
set_param([block 'Id Ref'],'value',num2str(host_init.Id_ref));
set_param([block 'Iq Ref'],'value',num2str(host_init.Iq_ref));
set_param([block 'Pos lock Input (torque mode)'],'value',num2str(host_init.Nmode));
set_param([block 'Speed Ref (speed mode)'],'value',num2str(host_init.Nref));

set_param([block_first,block_last],'InitialCondition',num2str(host_init.mode));
set_param([block_first,'1',block_last],'InitialCondition',num2str(host_init.signal1));
set_param([block_first,'2',block_last],'InitialCondition',num2str(host_init.signal2));
set_param([block_first,'3',block_last],'InitialCondition',num2str(PI_params.Kp_i));
set_param([block_first,'4',block_last],'InitialCondition',num2str(PI_params.Ki_i));
set_param([block_first,'5',block_last],'InitialCondition',num2str(PI_params.Kp_i));
set_param([block_first,'6',block_last],'InitialCondition',num2str(PI_params.Ki_i));
set_param([block_first,'7',block_last],'InitialCondition',num2str(PI_params.Kp_speed));
set_param([block_first,'8',block_last],'InitialCondition',num2str(PI_params.Ki_speed));
set_param([block_first,'9',block_last],'InitialCondition',num2str(host_init.Vpk));
set_param([block_first,'10',block_last],'InitialCondition',num2str(host_init.N_rpm));
set_param([block_first,'11',block_last],'InitialCondition',num2str(host_init.Id_ref));
set_param([block_first,'12',block_last],'InitialCondition',num2str(host_init.Iq_ref));
set_param([block_first,'13',block_last],'InitialCondition',num2str(host_init.Nmode));
set_param([block_first,'14',block_last],'InitialCondition',num2str(host_init.Nref));

msgId=[162, 164, 165, 171, 172, 173, 174 175, 176, 191, 192, 201, 202, 203, 181];

clear block;
clear block_first;
clear block_last;
clear host_init;
