This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

frequency ramp for PWM for BLDC

Hi there,

I want to write a code, which starts my BLDC. I want to set a voltage and a frequency as two desired values. With the PWM  the motor should start from zero to the desired values like a ramp function. The code should work like a frequency converter.

For the code I toke the lab 11 and added some lines. When I choose a small voltage (3 volts) and a small frequency (10 Hz) the motor starts spinning. But the code should work with every frequency and voltage. 

I hope there is someone with an helpful idea.

  • Could you please post the code you added in lab11? What voltage and frequency do you want to set in lab11?
  • I added a header and a source file ("hochlauf.h" and "hochlauf.c") for my code. I found out, that there is a math block (rampgen) whitch genertes a ramp (I think). So I copied this header file in the project folder. Is it possible to use this macro to ramp a frequency? I don't know what the varibles "stepanglemax" and "stepangle" mean and what values they should get.

    Because of the german varibales I used, I give you an explanation:

    ***_soll = set value

    ***_ist = actual value

    Here my code:

    -----hochlauf.h----

    #ifndef HOCHLAUF_H_
    #define HOCHLAUF_H_

    #include "sw/modules/math/src/32b/math.h"
    #include "sw/modules/iqmath/src/32b/IQmathLib.h"
    #include "user.h"
    #include "sw/modules/est/src/est_states.h"
    #include "rampgen.h"

    #define     USER_MATH_CONST1        ((float_t)((float_t)0.06)/((float_t)(MATH_TWO_PI)))
    #define     USER_TS                 ((float_t)((float_t)(1)/((float_t)USER_EST_FREQ_Hz)))
    #define     USER_MATH_CONST2        ((float_t)(50*MATH_TWO_PI*USER_TS))

    typedef struct _HOCHLAUF_Obj_
    {
        float_t phase;
        float_t freq_soll;
        float_t freq_ist;
        float_t amplitude_soll;
        float_t amplitude_ist;
        float_t inc_U;
        EST_State_e estState;
     } HOCHLAUF_Obj;

    typedef struct _HOCHLAUF_Obj_ *HOCHLAUF_Handle;

    HOCHLAUF_Handle HOCHLAUF_init(void *pMemory,const size_t numBytes);

    void HOCHLAUF_setParams(HOCHLAUF_Handle handle);

    void HOCHLAUF_reset(HOCHLAUF_Handle handle);

    void HOCHLAUF_run(HOCHLAUF_Handle handle, MATH_vec2 *pVab);

    void HOCHLAUF_setIdle(HOCHLAUF_Handle handle);

    EST_State_e HOCHLAUF_getState(HOCHLAUF_Handle handle);

    void HOCHLAUF_setRunning(HOCHLAUF_Handle handle);

    #endif /* HOCHLAUF_H_ */

    ----hochlauf.c-----

    #include "hochlauf.h"

    HOCHLAUF_Handle HOCHLAUF_init(void *pMemory, const size_t numBytes)
    {
        HOCHLAUF_Handle handle;
        if (numBytes < sizeof(HOCHLAUF_Obj))
        {
            return ((HOCHLAUF_Handle) NULL);
        }
        // assign the handle
        handle = (HOCHLAUF_Handle) pMemory;
        return (handle);
    }

    void HOCHLAUF_setParams(HOCHLAUF_Handle handle)
    {
        HOCHLAUF_Obj *obj = (HOCHLAUF_Obj*) handle;
        obj->amplitude_soll = 3;
        obj->amplitude_ist = 0;
        obj->inc_U = 0.5;
        obj->freq_soll = 5* MATH_TWO_PI;
        obj->freq_ist = 0;
       obj->phase = 0;
       obj->estState = EST_State_Idle;
      }
    void HOCHLAUF_reset(HOCHLAUF_Handle handle)
    {
        HOCHLAUF_Obj *obj = (HOCHLAUF_Obj*) handle;
        obj->phase = 0;
       }

    void HOCHLAUF_run(HOCHLAUF_Handle handle, MATH_vec2 *pVab)
    {
        HOCHLAUF_Obj *obj = (HOCHLAUF_Obj*) handle;

        if (obj->estState == EST_State_OnLine)
        {     
              RAMPGEN rg1 = RAMPGEN_DEFAULTS;
              rg1.Freq = 0;
              RG_MACRO(rg1);
              obj->freq_ist = rg1.Out;
           //Abfrage ob Frequenz_Soll erreicht ist
               if (obj->freq_ist > obj-> freq_soll)
               obj->freq_ist = obj->freq_soll;

            float_t phase_old = obj->phase;
           obj->phase = (obj->freq_ist * USER_TS + phase_old);

           if (obj->phase > MATH_TWO_PI)        //Abfrage nach 2 Pi
              obj->phase = obj->phase - MATH_TWO_PI;

           obj->amplitude_ist = obj->freq_ist * obj->inc_U; // Erhöhen der Amplitude

           if (obj->amplitude_ist > obj->amplitude_soll) // Abfrage ob Amplituden_Sollwert erreicht ist
               obj->amplitude_ist = obj->amplitude_soll;

            _iq amp = _IQ(obj->amplitude_ist/USER_IQ_FULL_SCALE_VOLTAGE_V);
            _iq phase_iq = _IQ(obj->phase/MATH_TWO_PI);
            pVab->value[0] = _IQmpy(amp, _IQsinPU(phase_iq));    // beta-Komponente
            pVab->value[1] = _IQmpy(amp, _IQcosPU(phase_iq));    // alpha-Komponente
        }
        else
        {
            obj->phase = 0;
            pVab->value[0] = 0;
            pVab->value[1] = 0;
        }
    }

    void HOCHLAUF_setIdle(HOCHLAUF_Handle handle)
    {
        HOCHLAUF_Obj *obj = (HOCHLAUF_Obj*) handle;
       obj->phase = 0;
        obj->estState = EST_State_Idle;
    }

    void HOCHLAUF_setRunning(HOCHLAUF_Handle handle)
    {
        HOCHLAUF_Obj *obj = (HOCHLAUF_Obj*) handle;
         obj->estState = EST_State_OnLine;
    }

    EST_State_e HOCHLAUF_getState(HOCHLAUF_Handle handle)
    {
        HOCHLAUF_Obj *obj = (HOCHLAUF_Obj*) handle;
        return obj->estState;
    }

    ---- end----

    I tried to start my BLDC with this code, but the motor did not spin.

    I hope you can help me.

    Best regards!