This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

driving a stepper motor

Hi guys,

I wrote a function that generates a set of constants to be used in assigning a delay value for each pulse for driving a stepper motor. My code just stop at the first iteration of the code, please have a look below and tell me what you think the cause is. I am thinking it is because of the complexity of the math operation, am I right? Thank you all in advance.

#include"constants.h"
#include<math.h>

const unsigned long C0_constant;

unsigned long calc();

void stepper_loop()
{ unsigned long constants_array[1772]; unsigned long Cn_vals; unsigned int j;

unsigned long *Px; // decalre the pointer first Px= constants_array; //make the pointer points to the address of first element in the array

for(j=0; j<1772; j++) {

Cn_vals=calc();

constants_array[j]=Cn_vals; Px++; // increment the pointer to point to the next element in the array }
}

unsigned long calc()
{ static unsigned long K_val; static unsigned int step; unsigned long Delta_T; unsigned long value_sqrd; unsigned long C0_constant;

if(step==0) { value_sqrd=2*Alpha/angular_accel;

C0_constant=sqrt(value_sqrd/period); // program stop at this line. K_val=C0_constant; }

step=step+1;

K_val=K_val-(2*K_val/(4*step+1)); Delta_T=K_val; return(Delta_T);
}

#include<math.h>

#ifndef constants_H
#define constants_H

#define PI 3.14159
//#define PI 3
#define Alpha 0.000625*PI
//#define Alpha 5*PI
#define period 0.00000004
//#define period 4
#define angular_accel 8.6955

#endif

  • Are you sure about the variable types you are using?

    value_sqrd=2*Alpha/angular_accel;
    

    results to 0.000451611847161 which is truncated to 0 since the result is stored in an unsigned long.

    The line following this is useless , it calculates the square root of 0

    C0_constant=sqrt(value_sqrd/period);
    

    Alex

  • You should wrap your code in the 'pre' tag.
    But before you do that, please make sure that your code uses consistent indentation, placement of braces and so on. This code makes my eyes sore.

  • Hello Andre,

    better formatting will help to spot problems (and posting it in this format, of cause).

    void stepper_loop()
    {
      unsigned long constants_array[1772];
      unsigned long Cn_vals;
      unsigned int j;
    
      unsigned long *Px; // decalre the pointer first
      Px= constants_array; //make the pointer points to the address of first element in the array
    
      for(j=0; j<1772; j++)
      {
    
        Cn_vals=calc();
    
        constants_array[j]=Cn_vals;
        Px++; // increment the pointer to point to the next element in the array
      }
    }
    

    On the first glance I see that:
    1) "long constants_array" is a local variable and placed on the stack, which typically is very limited in the embedded world. Make it static, for example.
    2)It is only written as well as Px, so optimizer may remove some of the code, which makes debugging difficult.

    Hope that helps
    Martin

  • Thank you all for your reply. How to avoid truncation of my numeric results, what data type I should be using. I'm new to write code in embedded C, hope you guys won't mind my question; does anybody have a sample code that deals with math operations( multiplication and division).
    Thank you so much!

  • It's nothing specifically to do with embedded 'C' - it is the standard language operation in any context.

    For some 'C' learning & reference materials, see: blog.antronics.co.uk/.../

  • Can some one says what exactly is causing my loop not run? What do I need to change?

  • Thank you all for your reply; my issue is fixed; all I had to do is change my data type to float, and a bit of adjustment of my code.