cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRTOS - Task keep running and remain of infinite loop

Angle
Associate II

Hi,

Why the task keep running and remain of infinite loop, and do not enter another function ?

8 REPLIES 8
KnarfB
Principal III

your code snippets?

I don't know why it keep remain at first loop only.

Could you please help to see whats wrong with this code.

/* USER CODE BEGIN Header_StartDefaultTask */

/**

 * @brief Function implementing the defaultTask thread.

 * @param argument: Not used

 * @retval None

 */

/* USER CODE END Header_StartDefaultTask */

void StartDefaultTask(void *argument)

{

data = 0x00;

HAL_I2C_Mem_Write(&hi2c4, MPU6050_ADDR, PWR_MGMT_1_REG, 1, &data, 1,HAL_MAX_DELAY);

data = 0x08;

HAL_I2C_Mem_Write(&hi2c4, MPU6050_ADDR, GYRO_CONFIG_REG, 1, &data, 1,HAL_MAX_DELAY);

data = 0x10;

HAL_I2C_Mem_Write(&hi2c4, MPU6050_ADDR, ACC_CONFIG_REG, 1, &data, 1,HAL_MAX_DELAY);

for (i = 0; i < 2000; i++) {

prevtime2 = time2;

time2 = HAL_GetTick();

elapsedtime2 = (time2 - prevtime2) * 1000;

cuffer[0] = 0x43;

HAL_I2C_Master_Transmit(&hi2c4, MPU6050_ADDR, cuffer, 1, HAL_MAX_DELAY);

HAL_I2C_Master_Receive(&hi2c4, MPU6050_ADDR, cuffer, 6, HAL_MAX_DELAY);

gyro_raw[0] = (cuffer[0] << 8 | cuffer[1]);  //x-axis raw data

gyro_raw[1] = (cuffer[2] << 8 | cuffer[3]);  //y-axis raw data

gyro_raw[2] = (cuffer[4] << 8 | cuffer[5]);  //z-axis raw data

gyro_cal[0] += gyro_raw[0];

gyro_cal[1] += gyro_raw[1];

gyro_cal[2] += gyro_raw[2];

osDelay(3);

// HAL_Delay(3);

}

gyro_cal[0] /= 2000;

gyro_cal[1] /= 2000;

gyro_cal[2] /= 2000;

 /* USER CODE BEGIN 5 */

 /* Infinite loop */

 for(;;)

 {

  prevtime1 = time1;

time1 = HAL_GetTick();

elapsedtime1 = (time1 - prevtime1) * 1000;

//accelerometer register address

tuffer[0] = 0x3B;

HAL_I2C_Master_Transmit(&hi2c4, MPU6050_ADDR, tuffer, 1, HAL_MAX_DELAY);

HAL_I2C_Master_Receive(&hi2c4, MPU6050_ADDR, tuffer, 6, HAL_MAX_DELAY);

acc_raw[0] = (tuffer[0] << 8 | tuffer[1]);  //x-axis raw data

acc_raw[1] = (tuffer[2] << 8 | tuffer[3]);  //y-axis raw data

acc_raw[2] = (tuffer[4] << 8 | tuffer[5]);  //z-axis raw data

//temperature register address

buffer[0] = 0x41;

HAL_I2C_Master_Transmit(&hi2c4, MPU6050_ADDR, buffer, 1, HAL_MAX_DELAY);

HAL_I2C_Master_Receive(&hi2c4, MPU6050_ADDR, buffer, 2, HAL_MAX_DELAY);

raw_temp = (buffer[0] << 8 | buffer[1]);

temp = (raw_temp / 340.0) + 36.53;

//gyro register address

cuffer[0] = 0x43;

HAL_I2C_Master_Transmit(&hi2c4, MPU6050_ADDR, cuffer, 1, HAL_MAX_DELAY);

HAL_I2C_Master_Receive(&hi2c4, MPU6050_ADDR, cuffer, 6, HAL_MAX_DELAY);

gyro_raw[0] = (cuffer[0] << 8 | cuffer[1]); //x-axis

gyro_raw[1] = (cuffer[2] << 8 | cuffer[3]); //y-axis

gyro_raw[2] = (cuffer[4] << 8 | cuffer[5]); //z-axis

gyro_raw[0] -= gyro_cal[0];

gyro_raw[1] -= gyro_cal[1];

gyro_raw[2] -= gyro_cal[2] + 0.79;

angle_pitch_gyro += gyro_raw[0] * 0.0000611;

angle_roll_gyro += gyro_raw[1] * 0.0000611;

angle_pitch_gyro += angle_roll_gyro * sin(gyro_raw[2] * 0.000001066);

angle_roll_gyro -= angle_pitch_gyro * sin(gyro_raw[2] * 0.000001066);

//EULERS Angle(accelerometer)

acc_total_vector = sqrt((acc_raw[0] * acc_raw[0]) + (acc_raw[1] * acc_raw[1])+ (acc_raw[2] * acc_raw[2]));

angle_pitch_acc = asin((float) acc_raw[1] / acc_total_vector) * 57.296; //57.296

angle_roll_acc = asin((float) acc_raw[0] / acc_total_vector) * -57.296; //-57.296

angle_pitch_acc -= 0.00;

angle_roll_acc -= 0.00;

//complementary filter (LPF & HPF)

if (set_gyro)

{

angle_pitch = angle_pitch_gyro * 0.9996 + angle_pitch_acc * 0.0004;

angle_roll = angle_roll_gyro * 0.9996 + angle_roll_acc * 0.0004;

angle_yaw = angle_yaw + gyro_raw[2] / elapsedtime1;

} else

{

angle_pitch = angle_pitch_acc;

set_gyro = true;

}

while ((HAL_GetTick() - prevtime) * 1000 < 4000);

prevtime = HAL_GetTick();

printf(" pitch = %.2f  |  roll = %.2f  |  yaw = %.2f\r\n", angle_pitch, angle_roll, angle_yaw);

printf("\r\n");

// printf("counting = %d \r\n", counter++);

//  osDelay(100);

 }

 /* USER CODE END 5 */

}

its got stuck at this loop and do no enter into another function

0693W00000Y8VJBQA3.png

Similar code works fine. Loop variable i is not defined as a local variable? Is it shared with other tasks? The HAL functions in the loop have potentially inifinite delay, use shorter timeouts, like 1000, and check the return codes of HAL functions.

Don't use global variables, define small variables locally (like i, data) on the thread stack, and larger ones as static.

hth

KnarfB

i only have task running only . i define as a global variable but not share with others. this code is testing fine without freertos, but it got stuck in loop when include the freertos function. Sorry fo the shorter timeout, where do i need to change it ?

Find the docs for the HAL API.

Debug step though the code to analyze whats happening.

hth

KnarfB

gbm
Lead III

And your definition of i is so precious you can't share it.

My wild guess: either the i variable is used by some other code (I don't understand why you didn't declare it in the for statement), or it's declared as 8-bit, like uint8_t.

In the latter case the compiler issued some warnings and you ignored them, which is defnitely not a good practice.

Angle
Associate II

okay i have change i into local variable but it still remain execute at this loop. Could you help to see whats wrong with these code? What i missing or wrong ? i attached the code