Skip to content

Commit aa94cc6

Browse files
committed
feat: Added servo component to the registry
fix: Added copyright notice on the servo_control.c file fix: Added servo example to the patterns in the rules.yml fix: Typo fixed in the servo example pattern in the rules.yml fix: Typo fixed in the .rules:build:components_motor_servo_test_apps fix: Changes in the test script to reduce the test scope for multiple targets
1 parent aff99b9 commit aa94cc6

File tree

17 files changed

+508
-71
lines changed

17 files changed

+508
-71
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,16 @@ 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.2
584+
variables:
585+
EXAMPLE_DIR: components/motor/servo/examples/servo_control
586+
577587
build_example_ota_simple_ota_example:
578588
extends:
579589
- .build_examples_template
@@ -1132,6 +1142,14 @@ build_components_motor_esp_simplefoc_test_apps:
11321142
variables:
11331143
EXAMPLE_DIR: components/motor/esp_simplefoc/test_apps
11341144

1145+
build_components_motor_servo_test_apps:
1146+
extends:
1147+
- .build_examples_template
1148+
- .rules:build:components_motor_servo_test_apps
1149+
- .build_idf_active_release_version
1150+
variables:
1151+
EXAMPLE_DIR: components/motor/servo/test_apps
1152+
11351153
build_components_ntc_driver_test_apps:
11361154
extends:
11371155
- .build_examples_template

.gitlab/ci/rules.yml

Lines changed: 17 additions & 1 deletion
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/**/*"
@@ -478,6 +479,9 @@
478479
.patterns-example_motor_foc_knob: &patterns-example_motor_foc_knob
479480
- "examples/motor/foc_knob_example/**/*"
480481

482+
.patterns-example_motor_servo: &patterns-example_motor_servo
483+
- "components/motor/servo/examples/servo_control/**/*"
484+
481485
.patterns-example_ota_simple_ota_example: &patterns-example_ota_simple_ota_example
482486
- "examples/ota/simple_ota_example/**/*"
483487

@@ -1070,6 +1074,18 @@
10701074
- <<: *if-dev-push
10711075
changes: *patterns-example_motor_foc_knob
10721076

1077+
.rules:build:example_motor_servo:
1078+
rules:
1079+
- <<: *if-protected
1080+
- <<: *if-label-build
1081+
- <<: *if-trigger-job
1082+
- <<: *if-dev-push
1083+
changes: *patterns-build_system
1084+
- <<: *if-dev-push
1085+
changes: *patterns-components_motor_servo
1086+
- <<: *if-dev-push
1087+
changes: *patterns-example_motor_servo
1088+
10731089
.rules:build:example_ota_simple_ota_example:
10741090
rules:
10751091
- <<: *if-protected
@@ -1871,7 +1887,7 @@
18711887
- <<: *if-dev-push
18721888
changes: *patterns-components_motor_esp_sensorless_bldc_control
18731889

1874-
.rules:build:components_motor_servo_test:
1890+
.rules:build:components_motor_servo_test_apps:
18751891
rules:
18761892
- <<: *if-protected
18771893
- <<: *if-label-build
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)