cancel
Showing results for 
Search instead for 
Did you mean: 

Value within a struct suddenly changes after several runs through a for loop

ESpra.1
Senior

I've run into a problem in this function, specifically involving a struct and a for loop.

Whenever loop 1 reaches i = 27 and reaches line 29 (L293D_step), the value within struct STEPPERZ changes from 6 to some value in the ten milions.

bool readSurfaceData()
{
 
	directionX = -1 * RESET_DIRECTION;
 
 
	for(uint8_t i = 0; i < stepper_Z_steps[servoStatus] / step_length; i++)
	{
		for (uint8_t j = 0; j < (round(stepper_X_steps[servoStatus] / step_length)+1); j++)
		{
			L293D_step(STEPPERX, step_length * directionX);
 
			x = step_length * directionX;
			fillMatrixSlot_V2(tooth, i, j);
 
		}
		if (directionX < 0)
		{
			x += step_length;
		}
		else
		{
			x -= step_length;
		}
		directionX *=(-1);
		HAL_Delay(Z_delay);
 
 
		L293D_step(STEPPERZ, step_length * RESET_DIRECTION * -1);
		z+=step_length * RESET_DIRECTION * -1;
		HAL_Delay(scan_delay);
	}
	if(!sendData())
	{
		return false;
	}
 
	return true;
}

Here's L293D_step:

void L293D_step(struct Stepper_Driver *driver, int steps_to_move) {
	int steps_left = abs(steps_to_move);
	int this_step = 0;
	if (steps_to_move > 0) {
		driver->direction = 1;
	}
	if (steps_to_move < 0) {
		driver->direction = 0;
	}
	while (steps_left > 0) {
		unsigned long now = HAL_GetTick();
		if ((now - driver->last_step_time) >= (driver->step_delay)) {
			driver->last_step_time = now;
 
			if (driver->direction == 1) {
				driver->step_number++;
				if (driver->step_number == driver->number_of_steps) {
					driver->step_number = 0;
				}
			} else {
				if (driver->step_number == 0) {
					driver->step_number = driver->number_of_steps;
				}
					driver->step_number--;
 
			}
			steps_left--;
			this_step = driver ->step_number % 4;
			L293D_stepMotor(driver, this_step);
		}
	}
}

and the Stepper_Driver struct:

struct Stepper_Driver
{
	uint16_t c1;
	uint16_t c2;
	uint16_t c3;
	uint16_t c4;
 
	GPIO_TypeDef *p1;
	GPIO_TypeDef *p2;
	GPIO_TypeDef *p3;
	GPIO_TypeDef *p4;
 
	uint8_t direction;
	unsigned long step_delay;
	uint8_t number_of_steps;
	int step_number;
	unsigned long last_step_time;
};

I checked the functions in a separate project, and they worked perfectly fine, and I've been checking for inconsistencies between the two, but can't find any so far. If anyone has any idea what might be causing this glitch, any help is appreciated.

Edit: I just remembered that I had been playing around with clock configurations in MX to get a better feel for how it worked, and that it's running on a lower sysclk than the primary file. Not sure if that's the problem, but I'm going to adjust it to remove that inconsistency

1 ACCEPTED SOLUTION

Accepted Solutions

I start search in fillMatrixSlot_V2

View solution in original post

5 REPLIES 5
gbm
Lead III

Start by correcting all the data types used in your code. Forget int and unsigned long - use uintN_t or intN types. Why is step_number delared as int, not as uint8_t?

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Before I do that, would that have any impact on why the code as it is works perfectly fine in another, bulkier project, and not this one? I'm all for making those fixes, but I'd rather find what's causing this problem here before I make changes that could throw more needed fixes at me.

I start search in fillMatrixSlot_V2

ESpra.1
Senior

Well, I'm a fool. I forgot I adjusted a matrix size to something smaller than the either of the for loops, which was causing all that grief. It looks like it was hard-faulting and just... kept going?

I wish I'd seen this 34 minutes ago.