【问题标题】:ESP-IDF task interruption on measurement测量时 ESP-IDF 任务中断
【发布时间】:2021-12-26 14:17:48
【问题描述】:

我的 ESP32 控制器有问题。我的系统正在使用多个任务。我用 HC-SR04 进行了测量。我想我在测量过程中有时会被打断。

我找不到描述任务间中断的文档。

这是我系统的示例代码:

// code of measurement
int ultrasonic_measure()
{

    int64_t time_start = 0;
    int64_t time_end = 0;
    int64_t time_measured = 0;
    int64_t process_start_time = 0; 

    gpio_set_direction(TRIGGER_PIN, GPIO_MODE_OUTPUT);
    gpio_set_level(TRIGGER_PIN, 0);
    delay_ms(1);
    gpio_set_level(TRIGGER_PIN, 1);
    ets_delay_us(10);
    gpio_set_level(TRIGGER_PIN, 0);

    gpio_set_direction(TRIGGER_PIN, GPIO_MODE_INPUT);

    process_start_time = esp_timer_get_time();
    while (!gpio_get_level(TRIGGER_PIN))
    {
        if (timeout_expired(process_start_time, 120000))
        {
            ESP_LOGE(TAG, "Timeout L");
            return -1;
        }
    }
    time_start = esp_timer_get_time();
    time_end = time_start;

    process_start_time = esp_timer_get_time();
    while (gpio_get_level(TRIGGER_PIN))
    {
        if (timeout_expired(process_start_time, 12000))
        {
            ESP_LOGE(TAG, "Timeout H");
            return -1;
        }
    }
    time_end = esp_timer_get_time();
    time_measured = time_end - time_start;
    int16_t distance = (time_measured / ROUNDTRIP) - CORRECTION;

    // elimination of overlimits
    distance = (distance < 70) ? 70 : distance;
    distance = (distance > 350) ? 350 : distance;
    ESP_LOGI(TAG, "Final distance after correction: %dmm", distance);
    return distance;
}
void start_background_tasks()
{
    int priority = 10;
    // THIS IS TASK WITH MEASUREMENT
    xTaskCreate(&external_sensor_tasks, "external_sensor_tasks", 2560, NULL, 2 | priority, NULL);

    xTaskCreate(&update_key_task, "update_key_task", 2560, NULL, 2 | priority, NULL);
    xTaskCreate(&print_heap_task, "print_heap_task", 2560, NULL, 2 | priority, NULL);
    xTaskCreate(&output_update_task, "output_update_task", 2560, NULL, 2 | priority, NULL);
    xTaskCreate(&send_report_task, "send_report_task", 4096, NULL, 2 | priority, NULL);

    xTaskCreate(&websocket_task, "websocket_task", 4096, NULL, 2 | priority, NULL);
    xTaskCreate(&get_host_to_connect_task, "get_host_to_connect_task", 4096, NULL, 2 | priority, NULL);
}

我没有显示我的 external_sensor_tasks() 代码,因为它有其他依赖逻辑(如保存结果等)。

我是 ESP-IDF 编程的初学者。当我在 Arduino UNO 上尝试测量时,它可以工作(我有一个核心并且没有中断)。当我的测量返回 70 毫米距离时,我遇到了这种情况。

【问题讨论】:

    标签: c task interrupt esp32 esp-idf


    【解决方案1】:

    你不能这样做(通过投票)。您需要使用 GPIO 中断。在中断处理程序中,您需要读取计时器计数器的值,然后将其发布(使用队列或直接任务通知)到您的测量任务。

    实际上,当您在 RTOS 中编程时,您需要忘记轮询。您需要使用 IPC 机制。多任务环境中的编程与朴素的 Arduino 风格编码有很大不同。

    【讨论】:

    • 你能给我发送更多关于解决方案的信息吗?例如。文档,网络上的文章。我不明白你写的所有东西。不好的是ultrasonic_measure() 或start_background_tasks()?
    • @M.Liver 您的评论表明您对 IPC 和多任务处理一无所知。从写PC软件开始,学习队列、信号量、互斥锁等。SO答案中的多任务编程课程我不会。
    猜你喜欢
    • 2023-03-03
    • 2022-08-06
    • 2018-08-27
    • 2018-12-13
    • 1970-01-01
    • 2022-10-06
    • 2021-02-16
    • 2023-01-27
    • 1970-01-01
    相关资源
    最近更新 更多