Skip to content

Commit e03d757

Browse files
committed
Added servo component to the registry
Minor fix on the idf_component.yml file Added docs link and improved readme file Added test_apps to the servo component Added servo component to the CI
1 parent aff99b9 commit e03d757

File tree

15 files changed

+483
-1
lines changed

15 files changed

+483
-1
lines changed

.github/workflows/upload_component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
components/led/lightbulb_driver;
5959
components/motor/esp_sensorless_bldc_control;
6060
components/motor/esp_simplefoc;
61+
components/motor/servo;
6162
components/openai;
6263
components/sensors/humiture/aht20;
6364
components/sensors/ntc_driver;

.gitlab/ci/build.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,18 @@ build_example_motor_foc_knob:
574574
variables:
575575
EXAMPLE_DIR: examples/motor/foc_knob_example
576576

577+
build_example_motor_servo:
578+
extends:
579+
- .build_examples_template
580+
- .rules:build:example_motor_servo
581+
parallel:
582+
matrix:
583+
- IMAGE: espressif/idf:release-v5.0
584+
- IMAGE: espressif/idf:release-v5.1
585+
- IMAGE: espressif/idf:release-v5.2
586+
variables:
587+
EXAMPLE_DIR: components/motor/servo/examples/servo_control
588+
577589
build_example_ota_simple_ota_example:
578590
extends:
579591
- .build_examples_template
@@ -1132,6 +1144,14 @@ build_components_motor_esp_simplefoc_test_apps:
11321144
variables:
11331145
EXAMPLE_DIR: components/motor/esp_simplefoc/test_apps
11341146

1147+
build_components_motor_servo_test_apps:
1148+
extends:
1149+
- .build_examples_template
1150+
- .rules:build:components_motor_servo_test_apps
1151+
- .build_idf_active_release_version
1152+
variables:
1153+
EXAMPLE_DIR: components/motor/servo/test_apps
1154+
11351155
build_components_ntc_driver_test_apps:
11361156
extends:
11371157
- .build_examples_template

components/motor/servo/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Servo Motor Component
2+
3+
This component provides an easy-to-use interface for controlling servo motors with the ESP-IDF framework. Servo motors are commonly used in robotics, automation, and various mechanical applications due to their precise control of angular position.
4+
5+
The library uses PWM (Pulse Width Modulation) to control servo motor rotation and supports customizable configurations for angle limits, pulse width, frequency, and more.
6+
7+
## Features
8+
9+
- Easy control of servo motors using PWM signals
10+
- Support for custom angle range and pulse width
11+
- Flexible configuration of PWM channels and timers
12+
- Compatible with the ESP-IDF framework
13+
14+
## Getting Started
15+
16+
### Prerequisites
17+
18+
- ESP-IDF installed and configured on your development environment
19+
- A servo motor compatible with PWM signals
20+
- Proper connections between the ESP32 and the servo motor (signal, VCC, GND)
21+
22+
## How to use
23+
24+
Initialize the sensor with the configuration:
25+
26+
```c
27+
servo_config_t servo_cfg = {
28+
.max_angle = 180,
29+
.min_width_us = 500,
30+
.max_width_us = 2500,
31+
.freq = 50,
32+
.timer_number = LEDC_TIMER_0,
33+
.channels = {
34+
.servo_pin = {
35+
SERVO_GPIO,
36+
},
37+
.ch = {
38+
LEDC_CHANNEL_0,
39+
},
40+
},
41+
.channel_number = 1,
42+
};
43+
44+
// Initialize the servo
45+
iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg);
46+
```
47+
48+
Set the angle:
49+
50+
```c
51+
uint16_t angle = 0;
52+
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, angle);
53+
```
54+
55+
## Reference
56+
57+
[Documentation](https://docs.espressif.com/projects/esp-iot-solution/en/latest/motor/servo.html)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(servo_control)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Example for the servo component
2+
3+
This example shows how to use the servo component.
4+
5+
## How to use
6+
7+
Initialize the sensor with the configuration:
8+
9+
```c
10+
servo_config_t servo_cfg = {
11+
.max_angle = 180,
12+
.min_width_us = 500,
13+
.max_width_us = 2500,
14+
.freq = 50,
15+
.timer_number = LEDC_TIMER_0,
16+
.channels = {
17+
.servo_pin = {
18+
SERVO_GPIO,
19+
},
20+
.ch = {
21+
LEDC_CHANNEL_0,
22+
},
23+
},
24+
.channel_number = 1,
25+
};
26+
27+
// Initialize the servo
28+
iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg);
29+
```
30+
31+
Set the angle:
32+
33+
```c
34+
uint16_t angle = 0;
35+
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, angle);
36+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "servo_control.c"
2+
INCLUDE_DIRS ".")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies:
2+
espressif/servo:
3+
version: "*"
4+
override_path: '../../../'
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
#include "freertos/FreeRTOS.h"
4+
#include "freertos/task.h"
5+
#include "driver/i2c_types.h"
6+
#include "esp_log.h"
7+
#include "esp_system.h"
8+
#include "sdkconfig.h"
9+
#include "iot_servo.h"
10+
11+
#define SERVO_GPIO (2) // Servo GPIO
12+
13+
static const char *TAG = "Servo Control";
14+
15+
uint16_t calibration_value_0 = 30; // Real 0 degree angle
16+
uint16_t calibration_value_180 = 195; // Real 0 degree angle
17+
18+
19+
// Task to test the servo
20+
static void servo_test_task(void *arg)
21+
{
22+
ESP_LOGI(TAG, "Servo Test Task");
23+
while (1) {
24+
// Set the angle of the servo
25+
for(int i = calibration_value_0; i <= calibration_value_180; i += 1) {
26+
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, i);
27+
vTaskDelay(20 / portTICK_PERIOD_MS);
28+
}
29+
// Return to the initial position
30+
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, calibration_value_0);
31+
vTaskDelay(1000 / portTICK_PERIOD_MS);
32+
}
33+
vTaskDelete(NULL);
34+
}
35+
36+
void servo_init(void)
37+
{
38+
ESP_LOGI(TAG, "Servo Control");
39+
40+
// Configure the servo
41+
servo_config_t servo_cfg = {
42+
.max_angle = 180,
43+
.min_width_us = 500,
44+
.max_width_us = 2500,
45+
.freq = 50,
46+
.timer_number = LEDC_TIMER_0,
47+
.channels = {
48+
.servo_pin = {
49+
SERVO_GPIO,
50+
},
51+
.ch = {
52+
LEDC_CHANNEL_0,
53+
},
54+
},
55+
.channel_number = 1,
56+
};
57+
58+
// Initialize the servo
59+
iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg);
60+
}
61+
62+
void app_main(void)
63+
{
64+
ESP_LOGI(TAG, "Servo Control");
65+
// Initialize the servo
66+
servo_init();
67+
// Create the servo test task
68+
xTaskCreate(servo_test_task, "servo_test_task", 2048, NULL, 5, NULL);
69+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 1.1.0
2+
description: Espressif's Servo Motor Component
3+
url: https://github.com/espressif/esp-iot-solution/tree/master/components/motor/servo
4+
repository: https://github.com/espressif/esp-iot-solution.git
5+
issues: https://github.com/espressif/esp-iot-solution/issues
6+
documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/motor/servo.html
7+
dependencies:
8+
idf:
9+
version: '>=4.4'

0 commit comments

Comments
 (0)