Skip to content

Commit

Permalink
Squashed 'motor_control' changes from 10685b8~10..10685b8 (#836)
Browse files Browse the repository at this point in the history
10685b8 Merge pull request Xilinx#323 from yunleiz/next
52e4ed0 [license] add SPDX-License-Identifier: X11
dad0121 [license] add SPDX-License-Identifier: X11
f793fde Merge pull request Xilinx#322 from congt/next
040bd2c [coding style]
caec7f9 [foc-sensorless]change rated current to stall current
745bc2a Merge pull request Xilinx#321 from yunleiz/next
972c283 [sensor] change rated current to stall current
3ff8839 [refine] refine comment
6325d38 Merge pull request Xilinx#320 from yunleiz/next
d2c5819 [foc] add two pics
36106e7 Merge pull request Xilinx#319 from yuanjieh/readme-license
5c8b317 Update top readme for license change and remove empty sections temporally
617bc83 Merge pull request Xilinx#318 from yunleiz/next
74bc468 [refine] clean codes
207fdd8 Merge pull request Xilinx#317 from yunleiz/next
4587527 [format] for smo
aeff12e [refine] testcase
a878fde [clang] format files
046e73b [refine] refine some unused codes
3a0fac0 [refine] remove unused files
dd143a4 Merge pull request Xilinx#316 from yunleiz/next
5ceeb86 [makefile] get back
12bb5f9 Merge pull request Xilinx#315 from yunleiz/next
e9084dd [format] format file
ed0f03d [format]
95e9cb0 [format] all files
c1ab04c [license] change to MITx11
54dbffc [license] change to MITx11
a62fbd8 Merge pull request Xilinx#313 from yunleiz/next
bc113fb [doc] update next release note

Co-authored-by: sdausr <[email protected]>
  • Loading branch information
2 people authored and GitHub Enterprise committed Apr 26, 2023
1 parent 312129d commit da443c1
Show file tree
Hide file tree
Showing 116 changed files with 2,290 additions and 6,256 deletions.
181 changes: 30 additions & 151 deletions motor_control/L1/include/hw/clarke_2p.hpp
Original file line number Diff line number Diff line change
@@ -1,145 +1,51 @@
/*
* Copyright 2022 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
SPDX-License-Identifier: X11
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Except as contained in this notice, the name of Advanced Micro Devices
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written authorization
from Advanced Micro Devices, Inc.
*/
#ifndef _CLARKE_2P_HPP_
#define _CLARKE_2P_HPP_

#include "common.hpp"
#include "utils.hpp"

//--------------------------------------------------------------------------
// Clarke Direct 2 phases
// Ialpha = Ia
// Ibeta = (Ia + 2Ib)/sqrt(3)
// Where Ia+Ib+Ic = 0
//--------------------------------------------------------------------------
#ifdef __SYNTHESIS__
template <class T_NUM>
void Clarke_Direct_2p_T_numeral(T_NUM& ialpha_out, T_NUM& ibeta_out, T_NUM ia_in, T_NUM ib_in) {
#pragma HLS INLINE off
T_NUM Ia = ia_in;
T_NUM Ib = ib_in;
T_NUM Ialpha = Ia;
T_NUM Ibeta = (Ia + 2.0 * Ib) / sqrt(3.0);
ialpha_out = Ialpha;
ibeta_out = Ibeta;
}
#endif

/**
* @brief Clarke Direct 2 phase convertion in the form of an inline HLS function
* @tparam T_IN Type of the input data. ex. short or ap_int<16> is enough for Q16.16
* @tparam T_MID Type of the data in the middle of calculation. ex. int_32t or ap_int<32> is enough for Q16.16
* @tparam T_OUT Type of the output data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MAX_OUT Limit value for the maximum output. ex. MAX_OUT is 32767 for Q16.16
* @tparam MIN_OUT Limit value for the minimum output. ex. MIN_OUT is -32767 for Q16.16
* @tparam I_SQRT3A The number \f$\frac{1}{\sqrt{3}}\f$ for Q16.16
* @tparam SQRT3A_SCALE Number significant bits of I_SQRT3A. ex. SQRT3A_SCALE is 16 for Q16.16
* @param ialpha_out Ialpha as output of Clarke Direct.
* @param ibeta_out Ibeta as output of Clarke Direct, orthogonal component of Ialpha.
* @param ia_in Ia as input of Clarke Direct.
* @param ib_in Ib as input of Clarke Direct.
*/
template <class T_IN, class T_MID, class T_OUT, int MAX_OUT, int MIN_OUT, int I_SQRT3A, int SQRT3A_SCALE>
void Clarke_Direct_2p_T(T_OUT& ialpha_out, T_OUT& ibeta_out, T_IN ia_in, T_IN ib_in) //;
{
#pragma HLS INLINE off
T_MID Ia = ia_in;
T_MID Ib = ib_in;
T_MID Ialpha = Ia;
T_MID Ibd = Ialpha + (Ib << 1); // calculate Ia+2*Ib
T_MID Ibeta = (Ibd * I_SQRT3A) >> SQRT3A_SCALE; // * 1/SQRT(3)
Ibeta = Clip_AP<T_MID>(Ibeta, (T_MID)MIN_OUT, (T_MID)MAX_OUT);
ialpha_out = Ialpha;
ibeta_out = Ibeta;
};

/**
* @brief Clarke Direct 2 phase convertion in the form of an inline HLS function
* @tparam IN_W Bit width of the input data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MID_W Bit width of the data in the middle of calculation. ex. int_32t or ap_int<32> is enough for
* Q16.16
* @tparam OUT_W Bit width of the output data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MAX_OUT Limit value for the maximum output. ex. MAX_OUT is 32767 for Q16.16
* @tparam MIN_OUT Limit value for the minimum output. ex. MIN_OUT is -32767 for Q16.16
* @tparam I_SQRT3A The number \f$\frac{1}{\sqrt{3}}\f$ for Q16.16
* @tparam SQRT3A_SCALE Number significant bits of I_SQRT3A. ex. SQRT3A_SCALE is 16 for Q16.16
* @param ialpha_out Ialpha as output of Clarke Direct.
* @param ibeta_out Ibeta as output of Clarke Direct, orthogonal component of Ialpha.
* @param ia_in Ia as input of Clarke Direct.
* @param ib_in Ib as input of Clarke Direct.
*/
template <int IN_W, int MID_W, int OUT_W, int MAX_OUT, int MIN_OUT, int I_SQRT3A, int SQRT3A_SCALE>
void Clarke_Direct_2p_T_AP(ap_int<OUT_W>& ialpha_out,
ap_int<OUT_W>& ibeta_out,
ap_int<IN_W> ia_in,
ap_int<IN_W> ib_in) {
#pragma HLS INLINE off
XF_MOTORCONTROL_HW_ASSERT(MID_W >= IN_W + SQRT3A_SCALE); // +1 ?
Clarke_Direct_2p_T<ap_int<IN_W>, ap_int<MID_W>, ap_int<OUT_W>, MAX_OUT, MIN_OUT, I_SQRT3A, SQRT3A_SCALE>(
ialpha_out, ibeta_out, ia_in, ib_in);
}

//--------------------------------------------------------------------------
// Clarke Inverse for 2 phases
// Va = Valpha
// Vb = [-Valpha + sqrt(3)*Vbeta]/2
// Vc = [-Valpha - sqrt(3)*Vbeta]/2
// Va, Vb, Vc; // Clarke Inverse -> SVPWM
// Va_out = Valpha
// Vb_out = [-Valpha + sqrt(3)*Vbeta]/2
// Vc_out = [-Valpha - sqrt(3)*Vbeta]/2
//--------------------------------------------------------------------------
#ifdef __SYNTHESIS__
template <class T_NUM>
void Clarke_Inverse_2p_T_numeral(T_NUM& va_out, T_NUM& vb_out, T_NUM& vc_out, T_NUM valpha_in, T_NUM vbeta_in) {
#pragma HLS INLINE off
T_NUM Valpha = valpha_in;
T_NUM Vbeta = vbeta_in;
va_out = Valpha;
vb_out = (-Valpha + Vbeta * sqrt(3.0)) / 2.0;
vc_out = (-Valpha - Vbeta * sqrt(3.0)) / 2.0;
}
#endif

/**
* @brief Clarke Inverse 2 phase convertion in the form of an inline HLS function
* @tparam T_IN Type of the input data. ex. short or ap_int<16> is enough for Q16.16
* @tparam T_MID Type of the data in the middle of calculation. ex. int_32t or ap_int<32> is enough for Q16.16
* @tparam T_OUT Type of the output data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MAX_OUT Limit value for the maximum output. ex. MAX_OUT is 32767 for Q16.16
* @tparam MIN_OUT Limit value for the minimum output. ex. MIN_OUT is -32767 for Q16.16
* @tparam V_SQRT3C The number \f$\sqrt{3}\f$ for Q16.16
* @tparam V_SQRT3C_SCALE Number significant bits of V_SQRT3C. ex. SQRT3C_SCALE is 16 for Q16.16
* @tparam T_IO Datatype of the input/output ap_fixed. ex. ap_fixed<32,16> is enough for Q16.16
* @param va_out Va as output of Clarke Inverse.
* @param vb_out Vb as output of Clarke Inverse.
* @param vc_out Vc as output of Clarke Inverse.
* @param valpha_in Valpha as input of Clarke Inverse.
* @param vbeta_in Vbeta as input of Clarke Inverse.
*/
template <class T_IN, class T_MID, class T_OUT, int MAX_OUT, int MIN_OUT, int V_SQRT3C, int V_SQRT3C_SCALE>
void Clarke_Inverse_2p_T(T_OUT& va_out, T_OUT& vb_out, T_OUT& vc_out, T_IN valpha_in, T_IN vbeta_in) //;
{
#pragma HLS INLINE off
T_MID Valpha = valpha_in;
T_MID Vbeta = vbeta_in;
T_MID s3vb = Vbeta * V_SQRT3C; // (sqrt(3)*(2^15))*Vbeta
va_out = Valpha;
vb_out = Clip_AP<T_MID>(((s3vb >> V_SQRT3C_SCALE) - Valpha) >> 1, (T_MID)MIN_OUT,
(T_MID)MAX_OUT); // (-Valpha + sqrt(3)*Vbeta)/2
vc_out = Clip_AP<T_MID>((0 - Valpha - (s3vb >> V_SQRT3C_SCALE)) >> 1, (T_MID)MIN_OUT,
(T_MID)MAX_OUT); // (-Valpha - sqrt(3)*Vbeta)/2
};

template <class T_IO>
void Clarke_Inverse_2p_ap_fixed(T_IO& va_out,
T_IO& vb_out,
Expand All @@ -160,31 +66,4 @@ void Clarke_Inverse_2p_ap_fixed(T_IO& va_out,
vc_out = (0 - Valpha - Vbeta * sqrt3) / 2;
};

/**
* @brief Clarke Inverse 2 phase convertion in the form of an inline HLS function
* @tparam IN_W Bit width of the input data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MID_W Bit width of the data in the middle of calculation. ex. int_32t or ap_int<32> is enough for
* Q16.16
* @tparam OUT_W Bit width of the output data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MAX_OUT Limit value for the maximum output. ex. MAX_OUT is 32767 for Q16.16
* @tparam MIN_OUT Limit value for the minimum output. ex. MIN_OUT is -32767 for Q16.16
* @tparam V_SQRT3C The number \f$\sqrt{3}\f$ for Q16.16
* @tparam V_SQRT3C_SCALE Number significant bits of V_SQRT3C. ex. SQRT3C_SCALE is 16 for Q16.16
* @param va_out Va as output of Clarke Inverse.
* @param vb_out Vb as output of Clarke Inverse.
* @param vc_out Vc as output of Clarke Inverse.
* @param valpha_in Valpha as input of Clarke Inverse.
* @param vbeta_in Vbeta as input of Clarke Inverse.
*/
template <int IN_W, int MID_W, int OUT_W, int MAX_OUT, int MIN_OUT, int V_SQRT3C, int V_SQRT3C_SCALE>
void Clarke_Inverse_2p_T_AP(ap_int<OUT_W>& va_out,
ap_int<OUT_W>& vb_out,
ap_int<OUT_W>& vc_out,
ap_int<IN_W> valpha_in,
ap_int<IN_W> vbeta_in) {
#pragma HLS INLINE off
XF_MOTORCONTROL_HW_ASSERT(MID_W >= IN_W + V_SQRT3C_SCALE + 1); // +1 ?
Clarke_Inverse_2p_T<ap_int<IN_W>, ap_int<MID_W>, ap_int<OUT_W>, MAX_OUT, MIN_OUT, V_SQRT3C, V_SQRT3C_SCALE>(
va_out, vb_out, vc_out, valpha_in, vbeta_in);
};
#endif
112 changes: 30 additions & 82 deletions motor_control/L1/include/hw/clarke_3p.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
/*
* Copyright 2022 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
SPDX-License-Identifier: X11
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Except as contained in this notice, the name of Advanced Micro Devices
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written authorization
from Advanced Micro Devices, Inc.
*/
#ifndef _CLARKE_PARK_3P_HPP_
#define _CLARKE_PARK_3P_HPP_

Expand All @@ -21,59 +33,24 @@

//--------------------------------------------------------------------------
// Clarke Direct 3p
// Ialpha = 2/3*Ia- 1/3*(Ib -Ic)
// Ibeta = 2*(Ib-Ic)/sqrt(3)
// Ihomop = 2/3*(Ia+Ib+Ic)
// Ialpha_out = 2/3*Ia- 1/3*(Ib -Ic)
// Ibeta_out = 2*(Ib-Ic)/sqrt(3)
// Ihomop_out = 2/3*(Ia+Ib+Ic)
// Where Ia+Ib+Ic != 0 ?
// iα and iβ components in an orthogonal reference frame and
// io the homopolar component of the system
//--------------------------------------------------------------------------
#ifdef __SYNTHESIS__
template <class T_NUM>
void Clarke_Direct_3p_T_numeral(
T_NUM& ialpha_out, T_NUM& ibeta_out, T_NUM& ihomop_out, T_NUM ia_in, T_NUM ib_in, T_NUM ic_in) {
#pragma HLS INLINE off
T_NUM Ialpha = (2.0 * ia_in - 1.0 * (ib_in - ic_in)) / 3.0;
T_NUM Ibeta = 2.0 * (ib_in - ic_in) / sqrt(3.0);
T_NUM Ihomop = 2.0 / 3.0 * (ia_in + ib_in + ic_in);
ialpha_out = Ialpha;
ibeta_out = Ibeta;
ihomop_out = Ihomop;
}
#endif

/**
* @brief Clarke Direct 3 phase convertion in the form of an inline HLS function
* @tparam T_IN Type of the input data. ex. short or ap_int<16> is enough for Q16.16
* @tparam T_MID Type of the data in the middle of calculation. ex. int_32t or ap_int<32> is enough for Q16.16
* @tparam T_OUT Type of the output data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MAX_OUT Limit value for the maximum output. ex. MAX_OUT is 32767 for Q16.16
* @tparam MIN_OUT Limit value for the minimum output. ex. MIN_OUT is -32767 for Q16.16
* @tparam I_SQRT3A The number \f$\frac{1}{\sqrt{3}}\f$ for Q16.16
* @tparam SQRT3A_SCALE Number significant bits of I_SQRT3A. ex. SQRT3A_SCALE is 16 for Q16.16
* @tparam T_IO Datatype of the input/output ap_fixed. ex. ap_fixed<32,16> is enough for Q16.16
* @param ialpha_out Ialpha as output of Clarke Direct.
* @param ibeta_out Ibeta as output of Clarke Direct, orthogonal component of Ialpha.
* @param ihomop_out Ihomopolar as output of Clarke Direct, homopolar component of the system.
* @param ia_in Ia as input of Clarke Direct.
* @param ib_in Ib as input of Clarke Direct.
* @param ib_in Ib as input of Clarke Direct.
*/
template <class T_IN, class T_MID, class T_OUT, int MAX_OUT, int MIN_OUT, int I_SQRT3A, int SQRT3A_SCALE>
void Clarke_Direct_3p_T(T_OUT& ialpha_out, T_OUT& ibeta_out, T_OUT& ihomop_out, T_IN ia_in, T_IN ib_in, T_IN ic_in) {
#pragma HLS INLINE off
T_MID Ia = ia_in;
T_MID Ib = ib_in;
T_MID Ic = ic_in;
T_MID Ialpha = (2 * Ia - (Ib + Ic)) / 3;
T_MID Ibd = (Ib - Ic); // calculate Ia+2*Ib
T_MID Ibeta = ((Ib - Ic) * I_SQRT3A) >> SQRT3A_SCALE; // * 1/SQRT(3)
T_MID Ihomop = 2 * (Ia + Ib + Ic) / 3;
Ibeta = Clip_AP<T_MID>(Ibeta, (T_MID)MIN_OUT, (T_MID)MAX_OUT);
ialpha_out = Ialpha;
ibeta_out = Ibeta;
ihomop_out = Ihomop;
};

template <class T_IO>
void Clarke_Direct_3p_ap_fixed(
T_IO& ialpha_out, T_IO& ibeta_out, T_IO& ihomop_out, T_IO ia_in, T_IO ib_in, T_IO ic_in) {
Expand All @@ -96,33 +73,4 @@ void Clarke_Direct_3p_ap_fixed(
ihomop_out = Ihomop;
};

/**
* @brief Clarke Direct 3 phase convertion in the form of an inline HLS function
* @tparam IN_W bit width of the input data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MID_W bit width of the data in the middle of calculation. ex. int_32t or ap_int<32> is enough for
* Q16.16
* @tparam OUT_W bit width of the output data. ex. short or ap_int<16> is enough for Q16.16
* @tparam MAX_OUT Limit value for the maximum output. ex. MAX_OUT is 32767 for Q16.16
* @tparam MIN_OUT Limit value for the minimum output. ex. MIN_OUT is -32767 for Q16.16
* @tparam I_SQRT3A The number \f$\frac{1}{\sqrt{3}}\f$ for Q16.16
* @tparam SQRT3A_SCALE Number significant bits of I_SQRT3A. ex. SQRT3A_SCALE is 16 for Q16.16
* @param ialpha_out Ialpha as output of Clarke Direct.
* @param ibeta_out Ibeta as output of Clarke Direct, orthogonal component of Ialpha.
* @param ihomop_out Ihomopolar as output of Clarke Direct, homopolar component of the system.
* @param ia_in Ia as input of Clarke Direct.
* @param ib_in Ib as input of Clarke Direct.
* @param ib_in Ib as input of Clarke Direct.
*/
template <int IN_W, int MID_W, int OUT_W, int MAX_OUT, int MIN_OUT, int I_SQRT3A, int SQRT3A_SCALE>
void Clarke_Direct_3p_T_AP(ap_int<OUT_W>& ialpha_out,
ap_int<OUT_W>& ibeta_out,
ap_int<OUT_W>& ihomop_out,
ap_int<IN_W> ia_in,
ap_int<IN_W> ib_in,
ap_int<IN_W> ic_in) {
#pragma HLS INLINE off
XF_MOTORCONTROL_HW_ASSERT(MID_W >= IN_W + SQRT3A_SCALE); // +1 ?
Clarke_Direct_3p_T<ap_int<IN_W>, ap_int<MID_W>, ap_int<OUT_W>, MAX_OUT, MIN_OUT, I_SQRT3A, SQRT3A_SCALE>(
ialpha_out, ibeta_out, ihomop_out, ia_in, ib_in, ic_in);
};
#endif
Loading

0 comments on commit da443c1

Please sign in to comment.