2018-05-30 20:36:06 +08:00
|
|
|
/****************************************************************************
|
2021-03-09 05:39:04 +08:00
|
|
|
* libs/libdsp/lib_svm.c
|
2018-05-30 20:36:06 +08:00
|
|
|
*
|
2020-10-10 20:50:05 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you 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.
|
2018-05-30 20:36:06 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dsp.h>
|
2021-12-19 22:59:14 +08:00
|
|
|
#include <string.h>
|
2018-05-30 20:36:06 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: svm3_sector_get
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get current sector for space vector modulation.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-07-08 01:04:57 +08:00
|
|
|
* ijk - (in) pointer to the auxiliary ABC frame
|
2018-05-30 20:36:06 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-01 04:36:51 +08:00
|
|
|
static uint8_t svm3_sector_get(FAR abc_frame_f32_t *ijk)
|
2018-05-30 20:36:06 +08:00
|
|
|
{
|
|
|
|
uint8_t sector = 0;
|
|
|
|
float i = ijk->a;
|
|
|
|
float j = ijk->b;
|
|
|
|
float k = ijk->c;
|
|
|
|
|
|
|
|
/* Identify the correct sector based on i,j,k frame:
|
|
|
|
* 1. sector 1:
|
|
|
|
* i > 0.0
|
|
|
|
* j > 0.0
|
|
|
|
* k <= 0.0
|
|
|
|
* 2. sector 2:
|
|
|
|
* i <= 0.0
|
|
|
|
* j > 0.0
|
|
|
|
* k <= 0.0
|
|
|
|
* 3. sector 3:
|
|
|
|
* i <= 0.0
|
|
|
|
* j > 0.0
|
|
|
|
* k > 0.0
|
|
|
|
* 4. sector 4:
|
|
|
|
* i <= 0.0
|
|
|
|
* j <= 0.0
|
|
|
|
* k > 0.0
|
|
|
|
* 5. sector 5:
|
|
|
|
* i > 0.0
|
|
|
|
* j <= 0.0
|
|
|
|
* k > 0.0
|
|
|
|
* 6. sector 6:
|
|
|
|
* i > 0.0
|
|
|
|
* j <= 0.0
|
|
|
|
* k <= 0.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (k <= 0.0)
|
|
|
|
{
|
|
|
|
if (i <= 0.0)
|
|
|
|
{
|
|
|
|
sector = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (j <= 0.0)
|
|
|
|
{
|
|
|
|
sector = 6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sector = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i <= 0.0)
|
|
|
|
{
|
|
|
|
if (j <= 0.0)
|
|
|
|
{
|
|
|
|
sector = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sector = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sector = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return SVM sector */
|
|
|
|
|
|
|
|
return sector;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: svm3_duty_calc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate duty cycles for space vector modulation.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-07-08 01:04:57 +08:00
|
|
|
* s - (in/out) pointer to the SVM state data
|
|
|
|
* ijk - (in) pointer to the auxiliary ABC frame
|
2018-05-30 20:36:06 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2018-08-24 19:51:44 +08:00
|
|
|
****************************************************************************/
|
2018-05-30 20:36:06 +08:00
|
|
|
|
2021-03-01 04:36:51 +08:00
|
|
|
static void svm3_duty_calc(FAR struct svm3_state_f32_s *s,
|
|
|
|
FAR abc_frame_f32_t *ijk)
|
2018-05-30 20:36:06 +08:00
|
|
|
{
|
|
|
|
float i = ijk->a;
|
|
|
|
float j = ijk->b;
|
|
|
|
float k = ijk->c;
|
2018-07-08 01:04:57 +08:00
|
|
|
float T0 = 0.0f;
|
|
|
|
float T1 = 0.0f;
|
|
|
|
float T2 = 0.0f;
|
2018-05-30 20:36:06 +08:00
|
|
|
|
|
|
|
/* Determine T1, T2 and T0 based on the sector */
|
|
|
|
|
|
|
|
switch (s->sector)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
T1 = i;
|
|
|
|
T2 = j;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
T1 = -k;
|
|
|
|
T2 = -i;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
T1 = j;
|
|
|
|
T2 = k;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
T1 = -i;
|
|
|
|
T2 = -j;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
T1 = k;
|
|
|
|
T2 = i;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
T1 = -j;
|
|
|
|
T2 = -k;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* We should not get here */
|
|
|
|
|
2021-03-01 04:41:59 +08:00
|
|
|
LIBDSP_DEBUGASSERT(0);
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get null vector time */
|
|
|
|
|
2018-07-08 01:04:57 +08:00
|
|
|
T0 = 1.0f - T1 - T2;
|
2018-05-30 20:36:06 +08:00
|
|
|
|
|
|
|
/* Calculate duty cycle for 3 phase */
|
|
|
|
|
|
|
|
switch (s->sector)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
2020-04-04 03:18:18 +08:00
|
|
|
s->d_u = T1 + T2 + T0 * 0.5f;
|
|
|
|
s->d_v = T2 + T0 * 0.5f;
|
|
|
|
s->d_w = T0 * 0.5f;
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 2:
|
|
|
|
{
|
2020-04-04 03:18:18 +08:00
|
|
|
s->d_u = T1 + T0 * 0.5f;
|
|
|
|
s->d_v = T1 + T2 + T0 * 0.5f;
|
|
|
|
s->d_w = T0 * 0.5f;
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 3:
|
|
|
|
{
|
2020-04-04 03:18:18 +08:00
|
|
|
s->d_u = T0 * 0.5f;
|
|
|
|
s->d_v = T1 + T2 + T0 * 0.5f;
|
|
|
|
s->d_w = T2 + T0 * 0.5f;
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 4:
|
|
|
|
{
|
2020-04-04 03:18:18 +08:00
|
|
|
s->d_u = T0 * 0.5f;
|
|
|
|
s->d_v = T1 + T0 * 0.5f;
|
|
|
|
s->d_w = T1 + T2 + T0 * 0.5f;
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 5:
|
|
|
|
{
|
2020-04-04 03:18:18 +08:00
|
|
|
s->d_u = T2 + T0 * 0.5f;
|
|
|
|
s->d_v = T0 * 0.5f;
|
|
|
|
s->d_w = T1 + T2 + T0 * 0.5f;
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
case 6:
|
|
|
|
{
|
2020-04-04 03:18:18 +08:00
|
|
|
s->d_u = T1 + T2 + T0 * 0.5f;
|
|
|
|
s->d_v = T0 * 0.5f;
|
|
|
|
s->d_w = T1 + T0 * 0.5f;
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-04 03:18:18 +08:00
|
|
|
|
2018-05-30 20:36:06 +08:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* We should not get here */
|
|
|
|
|
2021-03-01 04:41:59 +08:00
|
|
|
LIBDSP_DEBUGASSERT(0);
|
2018-05-30 20:36:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: svm3
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* One step of the space vector modulation.
|
2018-07-08 01:04:57 +08:00
|
|
|
* This is most common of SVM with alternate-reverse null vector.
|
2018-05-30 20:36:06 +08:00
|
|
|
*
|
|
|
|
* Voltage vector definitions in 3-phase SVM:
|
|
|
|
*
|
|
|
|
* |---------|-----------|--------------------|-----------------|
|
2021-07-17 05:37:45 +08:00
|
|
|
* | Voltage | switching | Line to neutral | Line to line |
|
2018-05-30 20:36:06 +08:00
|
|
|
* | vector | vectors | voltage | voltage |
|
|
|
|
* | |-----------|--------------------|-----------------|
|
|
|
|
* | | a | b | c | Van | Vbn | Vcn | Vab | Vbe | Vca |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V1 | 1 | 0 | 0 | 2/3 | -1/3 | -1/3 | 1 | 0 | -1 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V2 | 1 | 1 | 0 | 1/3 | 1/3 | -2/3 | 0 | 1 | -1 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V3 | 0 | 1 | 0 | -1/3 | 2/3 | -1/3 | -1 | 1 | 0 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V4 | 0 | 1 | 1 | -2/3 | 1/3 | 1/3 | -1 | 0 | 1 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V5 | 0 | 0 | 1 | -1/3 | -1/3 | 2/3 | 0 | -1 | 1 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V6 | 1 | 0 | 1 | 1/3 | -2/3 | 1/3 | 1 | -1 | 0 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
* | V7 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
|
|
|
|
* |---------|---|---|---|------|------|------|-----|-----|-----|
|
|
|
|
*
|
|
|
|
* Voltage values given in relation to the bus voltage (Vbus)/
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* s - (out) pointer to the SVM data
|
2020-04-04 03:18:18 +08:00
|
|
|
* v_ab - (in) pointer to the modulation voltage vector in alpha-beta
|
|
|
|
* frame, normalized to magnitude (0.0 - 1.0)
|
2018-07-08 01:04:57 +08:00
|
|
|
*
|
|
|
|
* NOTE: v_ab vector magnitude must be in range <0.0, 1.0> to get correct
|
|
|
|
* SVM3 results.
|
|
|
|
*
|
2018-05-30 20:36:06 +08:00
|
|
|
* REFERENCE:
|
2020-04-04 03:18:18 +08:00
|
|
|
* https://e2e.ti.com/group/motor/m/pdf_presentations/665547/download
|
|
|
|
* pages 32-34
|
2018-05-30 20:36:06 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-01 04:36:51 +08:00
|
|
|
void svm3(FAR struct svm3_state_f32_s *s, FAR ab_frame_f32_t *v_ab)
|
2018-05-30 20:36:06 +08:00
|
|
|
{
|
2021-03-01 04:41:59 +08:00
|
|
|
LIBDSP_DEBUGASSERT(s != NULL);
|
|
|
|
LIBDSP_DEBUGASSERT(v_ab != NULL);
|
2018-07-08 01:04:57 +08:00
|
|
|
|
2021-03-01 04:36:51 +08:00
|
|
|
abc_frame_f32_t ijk;
|
2018-05-30 20:36:06 +08:00
|
|
|
|
|
|
|
/* Perform modified inverse Clarke-transformation (alpha,beta) -> (i,j,k)
|
2018-07-08 01:04:57 +08:00
|
|
|
* to obtain auxiliary frame which will be used in further calculations.
|
2018-05-30 20:36:06 +08:00
|
|
|
*/
|
|
|
|
|
2018-07-08 01:04:57 +08:00
|
|
|
ijk.a = -0.5f*v_ab->b + SQRT3_BY_TWO_F*v_ab->a;
|
2018-05-30 20:36:06 +08:00
|
|
|
ijk.b = v_ab->b;
|
2018-07-08 01:04:57 +08:00
|
|
|
ijk.c = -ijk.b - ijk.a;
|
2018-05-30 20:36:06 +08:00
|
|
|
|
|
|
|
/* Get vector sector */
|
|
|
|
|
|
|
|
s->sector = svm3_sector_get(&ijk);
|
|
|
|
|
|
|
|
/* Get duty cycle */
|
|
|
|
|
|
|
|
svm3_duty_calc(s, &ijk);
|
2018-07-08 01:04:57 +08:00
|
|
|
|
2021-03-01 05:04:48 +08:00
|
|
|
/* NOTE: we return not-saturated output. Duty-cycle saturation is
|
|
|
|
* board-specific characteristic and we have not access to this
|
|
|
|
* information here.
|
|
|
|
*/
|
2018-07-08 01:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: svm3_current_correct
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Correct ADC samples (int32) according to SVM3 state.
|
|
|
|
* NOTE: This works only with 3 shunt resistors configuration.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-01 04:36:51 +08:00
|
|
|
void svm3_current_correct(FAR struct svm3_state_f32_s *s,
|
2021-03-01 05:04:48 +08:00
|
|
|
float *c0, float *c1, float *c2)
|
2018-07-08 01:04:57 +08:00
|
|
|
{
|
|
|
|
/* Get best ADC samples according to SVM sector.
|
|
|
|
*
|
|
|
|
* In SVM phase current can be sampled only in v0 vector state, when lower
|
|
|
|
* bridge transistors are turned on.
|
|
|
|
*
|
|
|
|
* We ignore sample from phase which has the shortest V0 state and
|
|
|
|
* estimate its value with KCL for motor phases:
|
|
|
|
* i_a + i_b + i_c = 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (s->sector)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
/* Sector 1-6: ignore phase 1 */
|
|
|
|
|
|
|
|
*c0 = -(*c1 + *c2);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
/* Sector 2-3: ignore phase 2 */
|
|
|
|
|
|
|
|
*c1 = -(*c0 + *c2);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
/* Sector 4-5: ignore phase 3 */
|
|
|
|
|
|
|
|
*c2 = -(*c0 + *c1);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* We should not get here. */
|
|
|
|
|
|
|
|
*c0 = 0;
|
|
|
|
*c1 = 0;
|
|
|
|
*c2 = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: svm3_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize 3-phase SVM data.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* s - (in/out) pointer to the SVM state data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-01 05:04:48 +08:00
|
|
|
void svm3_init(FAR struct svm3_state_f32_s *s)
|
2018-07-08 01:04:57 +08:00
|
|
|
{
|
2021-03-01 04:41:59 +08:00
|
|
|
LIBDSP_DEBUGASSERT(s != NULL);
|
2018-07-08 01:04:57 +08:00
|
|
|
|
2021-03-01 04:36:51 +08:00
|
|
|
memset(s, 0, sizeof(struct svm3_state_f32_s));
|
2018-05-30 20:36:06 +08:00
|
|
|
}
|