This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Stepper motor control and BLE

Hi all,

I would like to control a stepper motor over BLE. The motor takes STEP(rising edge) inputs for each step. The motor move is based on pre-calculated look up tables for achieving accurate motor profile.

For example, in my use case is that I have to run 8 pulse with 0.00133s time period then the next 10 pulses with 0.00126s time period and so on untill the motor can attain the max rpm at 0.000667s timeperiod.

Basically I should be able to change the base frequency after n pulses untill the motor attain the maximum speed and then keep on that constant timeperiod.

I implemented the same using the "waveform mode" in the PWM driver to achieve this and seems to be working.

NRF52832
NRFDK - s132 - sdk 17

The sample code is given below. This will continue to execute the number of pulses as per the given time period in the two arrays.

static int sequence_count = 0;
// demo PWM time period in uS.
static int time_period_array[] ={600,700,800,900,1000,1100};
// demo PWM pulse repeat count.
static int pulse_count_array[] ={1,2,3,4,5,6};

static nrf_pwm_values_wave_form_t pwm_seq_values;

static nrf_pwm_sequence_t const wave_seq =
{
.values.p_wave_form = &pwm_seq_values,
.length = NRF_PWM_VALUES_LENGTH(pwm_seq_values),
.repeats = 0,
.end_delay = 0
};

/*
*@brief Function will change the pwm time period
*T pwm = T pwm_clk * countertop value.
*
*T pwm_clk = 1/500 ms = 2 uS . Hence countertop = value/2
*
*/
static void change_pwm_freq (uint16_t value)
{
uint16_t counter_top_value = value/2;
pwm_seq_values.channel_0 = counter_top_value/2; // 50 percent duty cycle
pwm_seq_values.counter_top = counter_top_value;
}

1. Is this an efficient method to achieve the purpose or are there any other more efficient method to do this by reducing processor interference so that we can achieve realtime control of motor without any apparent delays?


2. We are planning to integrate PWM and the central + peripheral example of sdk 17.0.2. I also wanted to know if there will be any delay in excecuting the pwm event handler when there will be a ble event which needs to be processed, and the things that we need to consider while designing BLE handlers so as to minimze the delay.

NOTE: while the motor is moving, both central and peripheral connection events need to be handled.

3. Any further comments on the scenario is also appreciated.

regards,
Vinu

  • Hello Vinu,

    For example, in my use case is that I have to run 8 pulse with 0.00133s time period then the next 10 pulses with 0.00126s time period and so on untill the motor can attain the max rpm at 0.000667s timeperiod.

    This sounds like Pulse Density Modulation, is this what your motor is controlled by?

    Basically I should be able to change the base frequency after n pulses untill the motor attain the maximum speed and then keep on that constant timeperiod.

    Please be advised that you will have to reinitialize the PWM driver to change the base frequency, if you are using the driver. It is not clear to me from the provided code snippet if you intend to use the PWM HAL, driver or library. I suppose since you already have this working, that you are working with the HAL - is this correct?

    1. Is this an efficient method to achieve the purpose or are there any other more efficient method to do this by reducing processor interference so that we can achieve realtime control of motor without any apparent delays?

    2. We are planning to integrate PWM and the central + peripheral example of sdk 17.0.2. I also wanted to know if there will be any delay in excecuting the pwm event handler when there will be a ble event which needs to be processed, and the things that we need to consider while designing BLE handlers so as to minimze the delay.

    If you make use of the easyDMA feature for the decoder, and the PPI peripheral to start waveform generations, you will avoid having to have the CPU interact on each waveform generation.
    Which delays are you currently facing?
    Please also be advised that once you add BLE to this project, the SoftDevice will wrest control of the CPU to meet its timing requirements. This means that you will have to account for the CPU being busy every so often (this is not an issue when using easyDMA + PPI peripheral to trigger tasks).
    Is there a specific delay or timing requirement inbetween the frequency changes in your motor control signal?

    For future reference, please use the "Insert -> Code" option when sharing code here on DevZone - it increases the readability tenfolds! :)

    Best regards,
    Karl

  • Hi Karl,

    Thanks for the reply.

    Yes it is similar to pulse density modulation. We are using PWM HAL for testing. 

    If you make use of the easyDMA feature for the decoder, and the PPI peripheral to start waveform generations, you will avoid having to have the CPU interact on each waveform generation.

    Ok, how can we include PPI into the current PWM example demo ?

    Is there a specific delay or timing requirement inbetween the frequency changes in your motor control signal?

    No, the timings between step inputs are not strict, however it shouldn't drift too much. 

    Thanks,

    Vinu

  • Hello Vinu,

    VinuGopalakrishnan said:
    Ok, how can we include PPI into the current PWM example demo ?

    The SAADC peripheral example demonstrates how to setup PPI and use to to connect an EVENT directly to a TASK. 
    You could set it up for the PWM in the same way, just replace the triggering event and the connected task.

    Best regards,
    Karl

Related