2021-03-26 12:07 PM
Hello,
I have a problem with my H7 and FreeRTOS.
I have 3 thread:
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.priority = (osPriority_t) osPriorityAboveNormal,
.stack_size = 2048 * 4
};
/* Definitions for USB_ReceiveData */
osThreadId_t USB_ReceiveDataHandle;
const osThreadAttr_t USB_ReceiveData_attributes = {
.name = "USB_ReceiveData",
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 832 * 4
};
/* Definitions for ADC_Task */
osThreadId_t ADC_TaskHandle;
const osThreadAttr_t ADC_Task_attributes = {
.name = "ADC_Task",
.priority = (osPriority_t) osPriorityBelowNormal,
.stack_size = 768 * 4
};
and a Semaphore:
/* Definitions for Semaphore_resource */
osMutexId_t Semaphore_resourceHandle;
const osMutexAttr_t Semaphore_resource_attributes = {
.name = "Semaphore_resource"
};
The first task is for USB & TouchGFX:
void StartDefaultTask(void *argument)
{
/* init code for USB_DEVICE */
MX_USB_DEVICE_Init();
/* USER CODE BEGIN StartDefaultTask */
MX_TouchGFX_Process();
/* Infinite loop */
for(;;)
{
osSemaphoreAcquire(Semaphore_resourceHandle, 100);
osSemaphoreRelease(Semaphore_resourceHandle);
osDelay(1);
}
/* USER CODE END StartDefaultTask */
}
The second one receives commands from pc and moves a servomotor:
void USB_Receive_Data(void *argument)
{
/* USER CODE BEGIN USB_Receive_Data */
/* Infinite loop */
for(;;)
{
if ((VCP_chkInputData()==true))
{
command actual_command;
actual_command= VCP_retrieveInputData();
command_to_motor(actual_command);
}
osDelay(5);
}
/* USER CODE END USB_Receive_Data */
}
Sending command to servo can take 20ms.
And finally, a task that read from ADC and sends data to PC (data are used by a PID and to show info)
void Start_ADC_Task(void *argument)
{
/* USER CODE BEGIN Start_ADC_Task */
/* Infinite loop */
for(;;)
{
/****************************************
READ DATA
****************************************/
osSemaphoreAcquire(Semaphore_resourceHandle, 100);
Read_CH23(); read channel 1-2
Read_CH45(); read channel 3-4
uint8_t valiex;
valiex= CDC_Transmit_FS((uint8_t*)united_data.buffer_csv, sizeof(united_data.buffer_csv));
if (valiex==USBD_OK)
{
MOTOR_MOV_NOT_ALLOWED=false;
error_counter=0;
}
else{
error_counter++;
if (error_counter>15)
{
error_counter=0;
error_USB();
}
}
#endif
}
osSemaphoreRelease(Semaphore_resourceHandle);
osDelay(1);
}
}
If I read @30Hz, I have no problem, but if I increase the sampling rate to @150 the tasks break down...
Sometimes it doesn't accept commands from USB, other times stop sending data from ADC.
I tried to increase/decrease osDelay () and task priority but without results.
Any idea?
Thanks and best regards
2021-03-29 09:07 AM
I think the problem is in the osDelay() values (do I need to increase them?) but I'm not sure how to define these delays: I set TouchGFX to osDelay(100) without problems but between ADC function (about 5-6 ms to complete the function) and USB to motor (from 0 -no instruction- to 50ms) all is more complicated.
"USB_Receive_Data" receives data from USB and sends it to the motor via MODBUS, and every message needs about 10ms of delay. These delays are via vTaskDelay(), so i.e. Start_ADC_Task() can use this time to give me a value.
Sometimes, the system slows down in data exchange responsiveness. I tried to use Percepio Tracealyzer but I have an SWD interface and it seems not to permit live analysis. Can someone suggest me a method for tuning my FREERTOS?
Thanks!