We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
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