|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <math.h> |
| 9 | +#include "freertos/FreeRTOS.h" |
| 10 | +#include "freertos/task.h" |
| 11 | +#include "esp_log.h" |
| 12 | +#include "esp_system.h" |
| 13 | +#include "sdkconfig.h" |
| 14 | +#include "iot_servo.h" |
| 15 | + |
| 16 | +#define SERVO_GPIO (2) // Servo GPIO |
| 17 | + |
| 18 | +static const char *TAG = "Servo Control"; |
| 19 | + |
| 20 | +static uint16_t calibration_value_0 = 30; // Real 0 degree angle |
| 21 | +uint16_t calibration_value_180 = 195; // Real 0 degree angle |
| 22 | + |
| 23 | +// Task to test the servo |
| 24 | +static void servo_test_task(void *arg) |
| 25 | +{ |
| 26 | + ESP_LOGI(TAG, "Servo Test Task"); |
| 27 | + while (1) { |
| 28 | + // Set the angle of the servo |
| 29 | + for (int i = calibration_value_0; i <= calibration_value_180; i += 1) { |
| 30 | + iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, i); |
| 31 | + vTaskDelay(20 / portTICK_PERIOD_MS); |
| 32 | + } |
| 33 | + // Return to the initial position |
| 34 | + iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, calibration_value_0); |
| 35 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 36 | + } |
| 37 | + vTaskDelete(NULL); |
| 38 | +} |
| 39 | + |
| 40 | +static void servo_init(void) |
| 41 | +{ |
| 42 | + ESP_LOGI(TAG, "Servo Control"); |
| 43 | + |
| 44 | + // Configure the servo |
| 45 | + servo_config_t servo_cfg = { |
| 46 | + .max_angle = 180, |
| 47 | + .min_width_us = 500, |
| 48 | + .max_width_us = 2500, |
| 49 | + .freq = 50, |
| 50 | + .timer_number = LEDC_TIMER_0, |
| 51 | + .channels = { |
| 52 | + .servo_pin = { |
| 53 | + SERVO_GPIO, |
| 54 | + }, |
| 55 | + .ch = { |
| 56 | + LEDC_CHANNEL_0, |
| 57 | + }, |
| 58 | + }, |
| 59 | + .channel_number = 1, |
| 60 | + }; |
| 61 | + |
| 62 | + // Initialize the servo |
| 63 | + iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg); |
| 64 | +} |
| 65 | + |
| 66 | +void app_main(void) |
| 67 | +{ |
| 68 | + ESP_LOGI(TAG, "Servo Control"); |
| 69 | + // Initialize the servo |
| 70 | + servo_init(); |
| 71 | + // Create the servo test task |
| 72 | + xTaskCreate(servo_test_task, "servo_test_task", 2048, NULL, 5, NULL); |
| 73 | +} |
0 commit comments