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

APP_TIMER_TICKS stepper motor

Hi everyone,

I am trying to make work a stepper motor by Bluetooth with a nrf51822, Actually it is working, the motor turn, but the thing is I Don't know how to control the number or rotation it makes precisely.

I am using a stepper motor (www.pololu.com/.../2267, this stepper motor) it takes 200 steps so make a full rotation.

What I do in the code is this : 

first I receive a data from the Bluetooth app that send on to turn in one side and off to turn in the other side, I also receive a number that is use as a counter to control the time it should turn  :

static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
		char on[2] = "on";
		char off[3] = "off";
    for (uint32_t i = 0; i < length; i++)
    {
        while (app_uart_put(p_data[i]) != NRF_SUCCESS);
    }
    while (app_uart_put('\r') != NRF_SUCCESS);
    while (app_uart_put('\n') != NRF_SUCCESS);
			//uint8_t param[6] = (char*)p_data;
			ble_nus_string_send(&m_nus,  (char*)p_data, strlen((char*)p_data));
		char *ret;
		const char esc = '/'; 
		ret = strchr((char*)(p_data),esc);
		memmove(ret, ret+1, strlen(ret));
		ble_nus_string_send(&m_nus,  ret, strlen((char*)ret));
		char *ptr;
		if(strstr((char*)(p_data), on)){
			nrf_gpio_pin_set(sleepPin);
			nrf_gpio_pin_clear(LED3);
			//use to controle the number of rotation it makes
			steps_left = strtol(ret, &ptr, 10);
			Direction=true;
			uint8_t close[5] = "close";
			ble_nus_string_send(&m_nus, close, strlen((char*)close));
		}
		if(strstr((char*)(p_data), off)){
			nrf_gpio_pin_set(sleepPin);
			nrf_gpio_pin_set(LED3);
			
			//use to controle the number of rotation it makes
			steps_left = strtol(ret, &ptr, 10);
				Direction=false;
				uint8_t open[4] = "open";
				ble_nus_string_send(&m_nus, open, strlen((char*)open));
			
		}
		
}

Then I have a function that makes rotate the motor and that set the direction : 

I use APP_TIMER_TICKS as a counter, I have set correctly all the things related to this function I think, I followed the app_timer tutorial.

void stepper(){
	uint32_t err_code;
	uint32_t err_code2;
	if(Direction == true){
		nrf_gpio_pin_set(dirPin);
	}else{
		nrf_gpio_pin_clear(dirPin);
	}

	err_code = app_timer_start(timer_step, APP_TIMER_TICKS(1 , APP_TIMER_PRESCALER), NULL);

	APP_ERROR_CHECK(err_code);


}
Then I implement this in my loop : 
int main(void)
{
	
    uint32_t err_code;
    bool erase_bonds;

    // Initialize.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    uart_init();

    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
						
		
    printf("\r\nUART Start!\r\n");
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
	
		create_timers();
		nrf_gpio_pin_dir_set(stepPin, NRF_GPIO_PIN_DIR_OUTPUT);
		nrf_gpio_pin_dir_set(dirPin, NRF_GPIO_PIN_DIR_OUTPUT);
		nrf_gpio_pin_dir_set(sleepPin, NRF_GPIO_PIN_DIR_OUTPUT);
		nrf_gpio_pin_dir_set(MS1, NRF_GPIO_PIN_DIR_OUTPUT);
		nrf_gpio_pin_dir_set(MS2, NRF_GPIO_PIN_DIR_OUTPUT);

    // Enter main loop.
    for (;;)
    {
			nrf_gpio_pin_clear(MS1);
			nrf_gpio_pin_clear(MS2);
			//nrf_gpio_pin_set(stepPin);
			
			if(steps_left != 0){
				stepper();
				steps_left--;
				if(steps_left == 0){
		

					nrf_gpio_pin_clear(sleepPin);
					nrf_gpio_pin_clear(stepPin);
					nrf_gpio_pin_clear(dirPin);
					err_code = app_timer_stop_all();
					uint8_t step0[5] = "step0";
					ble_nus_string_send(&m_nus, step0, strlen((char*)step0));
					
				}
			}

				
        power_manage();
    }

	}
With all this my motor rotâtes correctly but I Don't know which number I should send(as a parameter to the first function) to do exactly one full rotation. I know that a stepper motor need 200 setps/revolution but if I set my parameter to 200 it move just a little, to have almost one full rotation I need Something like 6800, but I can't ue this number because it is not precise enough and I found it just by trying.
I have no idea how to calculate the good one.
If you have an idea that would be awesome!
Thanks!
Parents Reply Children
No Data
Related