Ok so I am trying to create a good method for feeding an ISR the data it needs to generate a move profile for multiple stepper motors. I have all the parameters in a struct and created objects of the struct. later when trying to cycle through the calculation of the individual values for those object I used a for loop to go through the objects by putting the objects in an array called "joints", I want to print the values to the serial monitor so I can verify it is working correctly however, the code compiles but when I run it the serial monitor prints "starting" and then 1600 for c0 which tells me it is getting to the void function "targetPosition" but then it gets carried away and the data on the monitor speeds up and just starts spitting out half of starting before reprinting starting. I verified my monitor was working with a simple program running a serial.print s through a for loop and all was well so its my code somehow. Would be great if someone can point out my error of which I assuming it has to do with the assigning of the struct objects. below is the code and an image of what the monitor is doing.
//------------------------------------------code----------------------------//
#define x_step 2
#define x_dir 3
#define x_enable 4
#define y_step 5
#define y_dir 6
#define y_enable 7
#define z_step 8
#define z_dir 9
#define z_enable 10
#define TIMER1_INTERRUPTS_ON TIMSK1 |= (1 << OCIE1A);
#define TIMER1_INTERRUPTS_OFF TIMSK1 &= ~(1 << OCIE1A);
struct motorParameters {
float accel;
volatile unsigned long runspeed = 80 ;
volatile long stepcount;
volatile int dir;
volatile unsigned int totalSteps;
volatile unsigned int n;
volatile long steps;
volatile long actualPosition = 0;
volatile long rampPhase = 0;
};
motorParameters joints[3];
//----------------------------pin modes and standard setups------------//
void setup() {
pinMode(x_step, OUTPUT);
pinMode(x_enable, OUTPUT);
pinMode(x_dir, OUTPUT);
pinMode(y_step, OUTPUT);
pinMode(y_enable, OUTPUT);
pinMode(y_dir, OUTPUT);
digitalWrite(x_enable, HIGH);
digitalWrite(y_enable, HIGH);
digitalWrite(z_enable, HIGH);
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1000; // compare value
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= ((1 << CS11) | (1 << CS10)); // 64 prescaler
interrupts();
Serial.begin(9600);
}
//----------------------------------------------------Main loop that calls for desired final position---------------------//
void loop() {
Serial.print("starting");
Serial.println(" ");
delay(5000);
targetPosition(400, -1600, 800);
targetPosition(-550, 380, 200);
delay(5000);
}
//-----------------------------------------------------TargetPosition function---------------------------------------------//
/* the target position fuction will take in the desired position determine the
* direction the joint needs to go and how many steps from the current position
* it needs for each motor which will then pass along the parameters to the isr
* it will also reset the count values and establish the initial conditions for
* for each move.
*/
void targetPosition (int x, int y, int z ){
int c0 = 1600;
Serial.print(c0);
delay(1000);
Serial.println(" ");
delay(1000);
Serial.println(x);
delay(1000);
Serial.println(y);
delay(1000);
Serial.println(z);
int axis[3] = {x , y, z};
for (int i = 0; i < 3; i++){ //this is the main loop that will interpret the desired location and derive
joints[i].steps = (joints[i].actualPosition - axis[i] ); // the move parameters to feed to the isr as well as reset the counts to 0
joints[i].dir = (joints[i].steps < 0 ? 1 : -1 );
joints[i].totalSteps = (abs(joints[i].steps));
joints[i].accel = c0 ;
OCR1A = joints[i].accel;
joints[i].stepcount = 0;
joints[i].n = 0;
joints[i].actualPosition = axis[i];
Serial.print(joints[i].steps);
Serial.print(" ");
Serial.print(joints[i].dir);
Serial.print(" ");
Serial.print(joints[i].totalSteps);
Serial.println(" ");
}
}