Skip to content

Commit

Permalink
Squashed 'motor_control' changes from f1ca2b2..3ec6431 (#914)
Browse files Browse the repository at this point in the history
3ec6431 Updating main by next-2: fixing coding style
0add76f Updating main by next
5f8b514 create main branch from next branch

Co-authored-by: sdausr <[email protected]>
  • Loading branch information
2 people authored and GitHub Enterprise committed Sep 7, 2023
1 parent 2e6c66f commit 3c4f7b0
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 75 deletions.
4 changes: 2 additions & 2 deletions motor_control/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@Library('pipeline-library')_
VitisLibPipeline (branch: 'next', libname: 'xf_motorcontrol', TARGETS: 'hls_csim:hls_csynth:hls_cosim:vitis_sw_emu:vitis_hw_emu:vitis_hw_build',
VitisLibPipeline (branch: 'main', libname: 'xf_motorcontrol', TARGETS: 'hls_csim:hls_csynth:hls_cosim:vitis_sw_emu:vitis_hw_emu:vitis_hw_build',
upstream_dependencies: '',
devtest: 'RunDeploy.sh', TOOLVERSION: '2023.1_stable_latest', 'os_types' : 'centos7:rhel7')
devtest: 'RunDeploy.sh', TOOLVERSION: '2023.1_released', 'os_types' : 'centos7:rhel7')
4 changes: 1 addition & 3 deletions motor_control/L1/include/hw/clarke_2p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ void Clarke_Inverse_2p_ap_fixed(T_IO& va_out,
T_MID Valpha = valpha_in;
T_MID Vbeta = vbeta_in;
va_out = Valpha;
#pragma HLS BIND_OP variable = vb_out op = mul impl = dsp
#pragma HLS BIND_OP variable = vc_out op = mul impl = dsp
vb_out = (0 - Valpha + Vbeta * sqrt3) / 2;
vc_out = (0 - Valpha - Vbeta * sqrt3) / 2;
};

#endif
#endif
5 changes: 4 additions & 1 deletion motor_control/L1/include/hw/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ from Advanced Micro Devices, Inc.

#define COMM_MOTOR_PARA_TL_TH (0)

#define COMM_MOTOR_PARA_DT_SIM (0.0000001)
#define COMM_MOTOR_PARA_DT_SIM (0.0000005)

#define COMM_MOTOR_PARA_UMAX (24) // to be uniformed with MAX_VAL_PWM

Expand All @@ -87,6 +87,9 @@ typedef ap_uint<32> t_glb_speed_theta;
/// Data Type for the data buffer in calculation.
typedef ap_fixed<32, 16> t_glb_q15q16;

/// Data Type for the smo discrete sample time
typedef ap_fixed<32, 1> t_glb_smo_time;

template <class T>
struct RangeDef {
T min;
Expand Down
8 changes: 1 addition & 7 deletions motor_control/L1/include/hw/field_weakening.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ static float Sqrt2(float x) {
#pragma HLS INLINE

float xhalf, res, x2, x3, x4;
#pragma HLS BIND_OP variable = xhalf op = fmul impl = dsp
#pragma HLS BIND_OP variable = x2 op = fmul impl = dsp
#pragma HLS BIND_OP variable = x3 op = fmul impl = dsp
#pragma HLS BIND_OP variable = x4 op = sub impl = dsp
#pragma HLS BIND_OP variable = res op = frecip impl = dsp

xhalf = 0.5f * x;
int i = *(int*)&x; // get bits for floating VALUE
Expand Down Expand Up @@ -142,7 +137,6 @@ void Field_Weakening_T(

ap_fixed<48, 32> squareSum = Vd_decoup * Vd_decoup + Vq_decoup * Vq_decoup;
float temp;
#pragma HLS BIND_OP variable = temp op = fsqrt impl = dsp
temp = std::sqrt(squareSum.to_float());
Modulation = temp;

Expand All @@ -167,4 +161,4 @@ void Field_Weakening_T(
// pid has move outside
}

#endif // _FIELD_WEAKENING_HPP_
#endif // _FIELD_WEAKENING_HPP_
30 changes: 24 additions & 6 deletions motor_control/L1/include/hw/foc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ enum FOC_Mode {
MOD_MANUAL_TORQUE_FLUX_FIXED_SPEED,
MOD_MANUAL_TORQUE_FLUX,
MOD_MANUAL_TORQUE,
MOD_MANUAL_FLUX
MOD_MANUAL_FLUX,
MOD_MANUAL_TORQUE_FLUX_FIXED_ANGLE,
MOD_TOTAL_NUM
};

} // namespace xf
Expand Down Expand Up @@ -424,6 +426,12 @@ void Control_foc_ap_fixed(T_IO& Vd,
cos_out = cos_gen_angle; // Generated angle cos
sin_out = sin_gen_angle; // Generated angle sin
break;
case MOD_MANUAL_TORQUE_FLUX_FIXED_ANGLE:
Vd = args_vd; // Sorce Vd from register
Vq = args_vq; // Sorce Vq from register
cos_out = cos_in; // angle cos from aix register
sin_out = sin_in; // angle sin from aix register
break;
case MOD_MANUAL_FLUX:
Vd = args_vd;
Vq = Torque_out;
Expand Down Expand Up @@ -498,7 +506,8 @@ void foc_core_ap_fixed(
volatile int& angle_stts,
volatile int& Ialpha_stts,
volatile int& Ibeta_stts,
volatile int& Ihomopolar_stts) {
volatile int& Ihomopolar_stts,
volatile int& fixed_angle_args) {
#pragma HLS INLINE off
#pragma HLS BIND_STORAGE variable = sin_table type = RAM_2P impl = BRAM
#pragma HLS BIND_STORAGE variable = cos_table type = RAM_2P impl = BRAM
Expand Down Expand Up @@ -583,6 +592,9 @@ void foc_core_ap_fixed(
apx_fw_kp_args(31, 0) = fw_kp_args;
t_glb_q15q16 apx_fw_ki_args;
apx_fw_ki_args(31, 0) = fw_ki_args;
t_glb_q15q16 apx_fixed_angle_args;
apx_fixed_angle_args(31, 0) = fixed_angle_args;
short fixed_angle_args_short = apx_fixed_angle_args;
// clang-format on

// static T_Vabc SVM_inv_index = MAX_LIM >> 1;
Expand Down Expand Up @@ -659,7 +671,10 @@ void foc_core_ap_fixed(
// t_sincos sin_gen_angle; sin_gen_angle(15, 0) = sin_table[gen_angle];

t_angle Theta = Angle - angle_sh_args; // Apply angle correction
Theta = (FOC_mode == MOD_MANUAL_TORQUE_FLUX_FIXED_SPEED) ? gen_angle : Theta;
// Theta = (FOC_mode == MOD_MANUAL_TORQUE_FLUX_FIXED_SPEED) ? gen_angle : Theta;
Theta = (FOC_mode == MOD_MANUAL_TORQUE_FLUX_FIXED_SPEED)
? gen_angle
: (FOC_mode == MOD_MANUAL_TORQUE_FLUX_FIXED_ANGLE) ? fixed_angle_args_short : Theta;
Theta = (Theta < 0) ? (short)(Theta + VALUE_CPR) : Theta; // Correct negative angle
Theta = (Theta >= VALUE_CPR) ? (short)(Theta - VALUE_CPR) : Theta; // Correct angle overload to (0, CPR)
t_angle Q = (Theta / cpr_div_ppr); // Correct angle overload round to (0. cpr_div_ppr)
Expand Down Expand Up @@ -907,7 +922,7 @@ void foc_core_ap_fixed(
t_glb_q15q16 apx_speed_Ihomopolar_stts = Ihomopolar;

t_glb_q15q16 apx_speed_stts = RPM;
t_glb_q15q16 apx_angle_stts = Theta;
t_glb_q15q16 apx_angle_stts = Angle;

speed_stts = apx_speed_stts.range(31, 0);
angle_stts = apx_angle_stts.range(31, 0);
Expand Down Expand Up @@ -989,6 +1004,7 @@ void foc_core_ap_fixed(
* @param Ialpha_stts Output status to monitor Ialpha (output of Clarke_Direct)
* @param Ibeta_stts Output status to monitor Ibeta (output of Clarke_Direct)
* @param Ihomopolar_stts Output status to monitor Ihomopolar (output of Clarke_Direct)
* @param fixed_angle_args Input Args for MOD_MANUAL_TORQUE_FLUX_FIXED_ANGLE
* @param trip_cnt Input Args to set the trip count of foc loop
*/
// clang-format on
Expand Down Expand Up @@ -1044,6 +1060,7 @@ void hls_foc_strm_ap_fixed(
volatile int& Ialpha_stts,
volatile int& Ibeta_stts,
volatile int& Ihomopolar_stts,
volatile int& fixed_angle_args,
volatile long& trip_cnt) {
short cpr_div_ppr = VALUE_CPR / ppr_args;
unsigned int tab_map_factor = ((COMM_MACRO_TLB_LENTH * (unsigned int)ppr_args) << 16) / VALUE_CPR;
Expand Down Expand Up @@ -1082,7 +1099,7 @@ void hls_foc_strm_ap_fixed(
//
id_stts, flux_acc_stts, flux_err_stts, flux_out_stts, iq_stts, torque_acc_stts, torque_err_stts,
torque_out_stts, speed_stts, speed_acc_stts, speed_err_stts, speed_out_stts, angle_stts, Ialpha_stts,
Ibeta_stts, Ihomopolar_stts);
Ibeta_stts, Ihomopolar_stts, fixed_angle_args);

T_IO va = Va_out;
T_IO vb = Vb_out;
Expand Down Expand Up @@ -1154,6 +1171,7 @@ void hls_foc_strm_int(
volatile int& Ialpha_stts,
volatile int& Ibeta_stts,
volatile int& Ihomopolar_stts,
volatile int& fixed_angle_args,
volatile long& trip_cnt) {
short cpr_div_ppr = VALUE_CPR / ppr_args;
unsigned int tab_map_factor = ((COMM_MACRO_TLB_LENTH * (unsigned int)ppr_args) << 16) / VALUE_CPR;
Expand Down Expand Up @@ -1201,7 +1219,7 @@ void hls_foc_strm_int(
//
id_stts, flux_acc_stts, flux_err_stts, flux_out_stts, iq_stts, torque_acc_stts, torque_err_stts,
torque_out_stts, speed_stts, speed_acc_stts, speed_err_stts, speed_out_stts, angle_stts, Ialpha_stts,
Ibeta_stts, Ihomopolar_stts);
Ibeta_stts, Ihomopolar_stts, fixed_angle_args);

t_glb_q15q16 apx_va = Va_out;
t_glb_q15q16 apx_vb = Vb_out;
Expand Down
12 changes: 0 additions & 12 deletions motor_control/L1/include/hw/park.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ void Park_Direct_ap_fixed(
T_MID cos_theta = (T_MID)cos_theta_in;
T_MID sin_theta = (T_MID)sin_theta_in;
T_MID Ia_cos, Ib_sin, Ib_cos, Ia_sin, Id, Iq;
#pragma HLS BIND_OP variable = Ia_cos op = mul impl = dsp
#pragma HLS BIND_OP variable = Ib_sin op = mul impl = dsp
#pragma HLS BIND_OP variable = Ib_cos op = mul impl = dsp
#pragma HLS BIND_OP variable = Ia_sin op = mul impl = dsp
#pragma HLS BIND_OP variable = Id op = add impl = dsp
#pragma HLS BIND_OP variable = Iq op = sub impl = dsp

Ia_cos = (T_MID)Ialpha * cos_theta;
Ib_sin = (T_MID)Ibeta * sin_theta;
Expand Down Expand Up @@ -92,12 +86,6 @@ void Park_Inverse_ap_fixed(
T_MID cos_theta = (T_MID)cos_theta_in;
T_MID sin_theta = (T_MID)sin_theta_in;
T_MID Vd_cos, Vq_sin, Vq_cos, Vd_sin, va_inv, vb_inv;
#pragma HLS BIND_OP variable = Vd_cos op = mul impl = dsp
#pragma HLS BIND_OP variable = Vq_sin op = mul impl = dsp
#pragma HLS BIND_OP variable = Vq_cos op = mul impl = dsp
#pragma HLS BIND_OP variable = Vd_sin op = mul impl = dsp
#pragma HLS BIND_OP variable = va_inv op = sub impl = dsp
#pragma HLS BIND_OP variable = vb_inv op = add impl = dsp
Vd_cos = (T_MID)Vd * cos_theta;
Vq_sin = (T_MID)Vq * sin_theta;
Vq_cos = (T_MID)Vq * cos_theta;
Expand Down
7 changes: 0 additions & 7 deletions motor_control/L1/include/hw/pid_control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,28 @@ void PID_Control_ap_fixed(T_IO& Res_out,
bool mode_change) {
#pragma HLS INLINE
T_ERR err;
#pragma HLS BIND_OP variable = err op = add impl = dsp
err = Sp - in_Measured;

T_ACC acc;
#pragma HLS BIND_OP variable = acc op = add impl = dsp
// acc = mode_change==true ? (T_ACC)0 : I_err_prev + err;
if (mode_change == true)
acc = 0;
else
acc = I_err_prev + err;

T_ERR diff;
#pragma HLS BIND_OP variable = diff op = add impl = dsp
diff = err - Error_prev;

T_ERR P;
#pragma HLS BIND_OP variable = P op = mul impl = dsp
P = Kp * err;

T_ERR I;
#pragma HLS BIND_OP variable = I op = mul impl = dsp
I = Ki * acc;

T_ERR D;
#pragma HLS BIND_OP variable = D op = mul impl = dsp
D = Kd * diff;

T_ACC sum;
#pragma HLS BIND_OP variable = sum op = add impl = dsp
sum = (P + I + D);
Res_out = (T_IO)sum;
Error_prev = err;
Expand Down
6 changes: 3 additions & 3 deletions motor_control/L1/include/hw/qei.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ void calcCounter(hls::stream<QEI_EdgeInfo>& strm_cntEdge,
#endif
tmp_rpm = tmp_rpm / div;

if (dir)
speed_rpm = tmp_rpm; //-tmp_rpm;
if (dir == Dirction_QEI::clockwise_p)
speed_rpm = tmp_rpm; // clockwise_p
else
speed_rpm = tmp_rpm;
speed_rpm = -tmp_rpm; // clockwise_n
ap_uint<32> tmp;
tmp.range(15, 0) = speed_rpm;
tmp.range(31, 16) = counter >> 2; // 4X mode : using any type of edges of A or B
Expand Down
14 changes: 10 additions & 4 deletions motor_control/L1/tests/IP_FOC/src/foc_demo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ void foc_demo(
// Inout for parameters
int args[FOC_ARGS_SIZE]);
// clang-format off
static const char* string_FOC_Mode[MOD_MANUAL_FLUX + 1] = {
static const char* string_FOC_Mode[MOD_TOTAL_NUM] = {
"MOD_STOPPED ",
"MOD_SPEED_WITH_TORQUE ",
"MOD_TORQUE_WITHOUT_SPEED ",
"MOD_FLUX ",
// expert modes
"MOD_MANUAL_TORQUE_FLUX_FIXED_SPEED",
"MOD_MANUAL_TORQUE_FLUX ",
"MOD_MANUAL_TORQUE ",
"MOD_MANUAL_FLUX "};
"MOD_MANUAL_FLUX ",
"MOD_MANUAL_TORQUE_FLUX_FIXED_ANGLE"};

static const char* string_FOC_PARA[FOC_ARGS_SIZE] = {
"PPR ",
Expand Down Expand Up @@ -208,6 +208,7 @@ struct FocAxiParameters {
T Ialpha_stts;
T Ibeta_stts;
T Ihomopolar_stts;
T fixed_angle_args;
long trip_cnt;
FocAxiParameters() { Init(); }
void Init() {
Expand All @@ -216,6 +217,7 @@ struct FocAxiParameters {
sample_interval_minus1_args = 0;
control_mode_args = FOC_Mode::MOD_STOPPED;
control_fixperiod_args = 0;
fixed_angle_args = 0;
flux_sp_args = 0;
flux_kp_args = 0;
flux_ki_args = 0;
Expand Down Expand Up @@ -296,6 +298,7 @@ struct FocAxiParameters {
fprintf(fp, "\tIbeta");
fprintf(fp, "\tIhomopolar");
fprintf(fp, "\trip_cnt");
fprintf(fp, "\tfixed_angle_args");
}
void printParameters(FILE* fp) {
assert(fp);
Expand Down Expand Up @@ -344,6 +347,7 @@ struct FocAxiParameters {
fprintf(fp, "\t%5.6f", Ibeta_stts.to_float());
fprintf(fp, "\t%5.6f", Ihomopolar_stts.to_float());
fprintf(fp, "\t%d", trip_cnt);
fprintf(fp, "\t%d", fixed_angle_args);
}
void sprintParameters(char* strm, int idx) {
assert(strm);
Expand Down Expand Up @@ -467,7 +471,8 @@ struct FocAxiParameters {
fout << Va_cmd_stts << ' ';
fout << Vb_cmd_stts << ' ';
fout << Vc_cmd_stts << ' ';
fout << trip_cnt << ' ' << std::endl;
fout << trip_cnt << ' ';
fout << fixed_angle_args << ' ' << std::endl;
fout.close();
}

Expand Down Expand Up @@ -519,6 +524,7 @@ struct FocAxiParameters {
istr >> Vb_cmd_stts;
istr >> Vc_cmd_stts;
istr >> trip_cnt;
istr >> fixed_angle_args;
fin.close();
}
void printPIDs(const char* head) {
Expand Down
18 changes: 12 additions & 6 deletions motor_control/L1/tests/IP_FOC/src/ip_foc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void hls_foc_periodic_ap_fixed(
volatile int& Vc_cmd_stts,
volatile int& Ialpha_stts,
volatile int& Ibeta_stts,
volatile int& Ihomopolar_stts) {
volatile int& Ihomopolar_stts,
volatile int& fixed_angle_args) {
#pragma HLS interface axis port = Ia
#pragma HLS interface axis port = Ib
#pragma HLS interface axis port = Ic
Expand Down Expand Up @@ -127,6 +128,7 @@ void hls_foc_periodic_ap_fixed(
#pragma HLS interface s_axilite port = Ialpha_stts bundle = foc_args
#pragma HLS interface s_axilite port = Ibeta_stts bundle = foc_args
#pragma HLS interface s_axilite port = Ihomopolar_stts bundle = foc_args
#pragma HLS interface s_axilite port = fixed_angle_args bundle = foc_args

#pragma HLS interface s_axilite port = return bundle = foc_args
long trip_cnt = 0x7fffffffffffffffL;
Expand All @@ -141,7 +143,7 @@ void hls_foc_periodic_ap_fixed(
//
id_stts, flux_acc_stts, flux_err_stts, flux_out_stts, iq_stts, torque_acc_stts, torque_err_stts,
torque_out_stts, speed_stts, speed_acc_stts, speed_err_stts, speed_out_stts, angle_stts,
Va_cmd_stts, Vb_cmd_stts, Vc_cmd_stts, Ialpha_stts, Ibeta_stts, Ihomopolar_stts, trip_cnt);
Va_cmd_stts, Vb_cmd_stts, Vc_cmd_stts, Ialpha_stts, Ibeta_stts, Ihomopolar_stts, fixed_angle_args, trip_cnt);
}
// clang-format on

Expand Down Expand Up @@ -195,7 +197,8 @@ void hls_foc_periodic_int( // used for testing synthesizability of hls_foc_strm_
volatile int& Vc_cmd_stts,
volatile int& Ialpha_stts,
volatile int& Ibeta_stts,
volatile int& Ihomopolar_stts) {
volatile int& Ihomopolar_stts,
volatile int& fixed_angle_args) {
#pragma HLS interface axis port = Ia
#pragma HLS interface axis port = Ib
#pragma HLS interface axis port = Ic
Expand Down Expand Up @@ -244,6 +247,7 @@ void hls_foc_periodic_int( // used for testing synthesizability of hls_foc_strm_
#pragma HLS interface s_axilite port = Ialpha_stts bundle = foc_args
#pragma HLS interface s_axilite port = Ibeta_stts bundle = foc_args
#pragma HLS interface s_axilite port = Ihomopolar_stts bundle = foc_args
#pragma HLS interface s_axilite port = fixed_angle_args bundle = foc_args

#pragma HLS interface s_axilite port = return bundle = foc_args

Expand All @@ -259,7 +263,7 @@ void hls_foc_periodic_int( // used for testing synthesizability of hls_foc_strm_
//
id_stts, flux_acc_stts, flux_err_stts, flux_out_stts, iq_stts, torque_acc_stts, torque_err_stts,
torque_out_stts, speed_stts, speed_acc_stts, speed_err_stts, speed_out_stts, angle_stts, Va_cmd_stts,
Vb_cmd_stts, Vc_cmd_stts, Ialpha_stts, Ibeta_stts, Ihomopolar_stts, trip_cnt);
Vb_cmd_stts, Vc_cmd_stts, Ialpha_stts, Ibeta_stts, Ihomopolar_stts, fixed_angle_args, trip_cnt);
}

// hls_foc_oneSample_ap_fixed is mainly used for generating testing files for cosim
Expand Down Expand Up @@ -313,7 +317,8 @@ void hls_foc_oneSample_ap_fixed(
volatile int& Vc_cmd_stts,
volatile int& Ialpha_stts,
volatile int& Ibeta_stts,
volatile int& Ihomopolar_stts) {
volatile int& Ihomopolar_stts,
volatile int& fixed_angle_args) {
#pragma HLS interface axis port = Ia
#pragma HLS interface axis port = Ib
#pragma HLS interface axis port = Ic
Expand Down Expand Up @@ -361,6 +366,7 @@ void hls_foc_oneSample_ap_fixed(
#pragma HLS interface s_axilite port = Ialpha_stts bundle = foc_args
#pragma HLS interface s_axilite port = Ibeta_stts bundle = foc_args
#pragma HLS interface s_axilite port = Ihomopolar_stts bundle = foc_args
#pragma HLS interface s_axilite port = fixed_angle_args bundle = foc_args

// #pragma HLS interface ap_none port = ppr_args
// #pragma HLS interface ap_none port = control_mode_args
Expand Down Expand Up @@ -413,5 +419,5 @@ void hls_foc_oneSample_ap_fixed(
//
id_stts, flux_acc_stts, flux_err_stts, flux_out_stts, iq_stts, torque_acc_stts, torque_err_stts,
torque_out_stts, speed_stts, speed_acc_stts, speed_err_stts, speed_out_stts, angle_stts, Va_cmd_stts,
Vb_cmd_stts, Vc_cmd_stts, Ialpha_stts, Ibeta_stts, Ihomopolar_stts, trip_cnt);
Vb_cmd_stts, Vc_cmd_stts, Ialpha_stts, Ibeta_stts, Ihomopolar_stts, fixed_angle_args, trip_cnt);
}
Loading

0 comments on commit 3c4f7b0

Please sign in to comment.