Skip to content

Commit 3be1564

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 Added changelog file and added servo to rules.yml Apply suggestions from code review Co-authored-by: leeebo <[email protected]> fix: Added copyright notice on the servo_control.c file
1 parent aff99b9 commit 3be1564

File tree

17 files changed

+508
-1
lines changed

17 files changed

+508
-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

.gitlab/ci/rules.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190

191191
.patterns-components_motor_servo: &patterns-components_motor_servo
192192
- "components/motor/servo/**/*"
193+
- "components/tools/cmake_utilities/package_manager.cmake"
193194

194195
.patterns-components_openai: &patterns-components_openai
195196
- "components/openai/**/*"
@@ -1070,6 +1071,18 @@
10701071
- <<: *if-dev-push
10711072
changes: *patterns-example_motor_foc_knob
10721073

1074+
.rules:build:example_motor_servo:
1075+
rules:
1076+
- <<: *if-protected
1077+
- <<: *if-label-build
1078+
- <<: *if-trigger-job
1079+
- <<: *if-dev-push
1080+
changes: *patterns-build_system
1081+
- <<: *if-dev-push
1082+
changes: *patterns-components_motor_servo
1083+
- <<: *if-dev-push
1084+
changes: *patterns-example_motor_servo
1085+
10731086
.rules:build:example_ota_simple_ota_example:
10741087
rules:
10751088
- <<: *if-protected
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ChangeLog
2+
3+
## v0.1.0 - 2024-11-27
4+
5+
### Enhancements:
6+
7+
* Initial version

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

0 commit comments

Comments
 (0)