Skip to content

Commit 8fd8bdc

Browse files
committed
Merge branch 'servo-motor-as-component-registry' into 'master'
feat: Added servo component to the registry See merge request ae_group/esp-iot-solution!1160
2 parents b3a7634 + 8408baa commit 8fd8bdc

File tree

18 files changed

+573
-149
lines changed

18 files changed

+573
-149
lines changed

.github/workflows/upload_component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
components/motor/drv10987;
6262
components/motor/esp_sensorless_bldc_control;
6363
components/motor/esp_simplefoc;
64+
components/motor/servo;
6465
components/openai;
6566
components/sensors/gesture/apds9960;
6667
components/sensors/humiture/aht20;

.gitlab/ci/build.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,17 @@ build_example_motor_foc_knob:
563563
variables:
564564
EXAMPLE_DIR: examples/motor/foc_knob_example
565565

566+
build_example_motor_servo:
567+
extends:
568+
- .build_examples_template
569+
- .rules:build:example_motor_servo
570+
parallel:
571+
matrix:
572+
- IMAGE: espressif/idf:release-v4.4
573+
- IMAGE: espressif/idf:release-v5.2
574+
variables:
575+
EXAMPLE_DIR: components/motor/servo/examples/servo_control
576+
566577
build_example_ota_simple_ota_example:
567578
extends:
568579
- .build_examples_template
@@ -1170,6 +1181,14 @@ build_components_motor_esp_simplefoc_test_apps:
11701181
variables:
11711182
EXAMPLE_DIR: components/motor/esp_simplefoc/test_apps
11721183

1184+
build_components_motor_servo_test_apps:
1185+
extends:
1186+
- .build_examples_template
1187+
- .rules:build:components_motor_servo_test_apps
1188+
- .build_idf_active_release_version
1189+
variables:
1190+
EXAMPLE_DIR: components/motor/servo/test_apps
1191+
11731192
build_components_ntc_driver_test_apps:
11741193
extends:
11751194
- .build_examples_template

.gitlab/ci/rules.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191

192192
.patterns-components_motor_servo: &patterns-components_motor_servo
193193
- "components/motor/servo/**/*"
194+
- "components/tools/cmake_utilities/package_manager.cmake"
194195

195196
.patterns-components_openai: &patterns-components_openai
196197
- "components/openai/**/*"
@@ -485,6 +486,9 @@
485486
.patterns-example_motor_foc_knob: &patterns-example_motor_foc_knob
486487
- "examples/motor/foc_knob_example/**/*"
487488

489+
.patterns-example_motor_servo: &patterns-example_motor_servo
490+
- "components/motor/servo/examples/servo_control/**/*"
491+
488492
.patterns-example_ota_simple_ota_example: &patterns-example_ota_simple_ota_example
489493
- "examples/ota/simple_ota_example/**/*"
490494

@@ -1072,6 +1076,18 @@
10721076
- <<: *if-dev-push
10731077
changes: *patterns-example_motor_foc_knob
10741078

1079+
.rules:build:example_motor_servo:
1080+
rules:
1081+
- <<: *if-protected
1082+
- <<: *if-label-build
1083+
- <<: *if-trigger-job
1084+
- <<: *if-dev-push
1085+
changes: *patterns-build_system
1086+
- <<: *if-dev-push
1087+
changes: *patterns-components_motor_servo
1088+
- <<: *if-dev-push
1089+
changes: *patterns-example_motor_servo
1090+
10751091
.rules:build:example_ota_simple_ota_example:
10761092
rules:
10771093
- <<: *if-protected
@@ -1936,7 +1952,7 @@
19361952
- <<: *if-dev-push
19371953
changes: *patterns-components_motor_esp_sensorless_bldc_control
19381954

1939-
.rules:build:components_motor_servo_test:
1955+
.rules:build:components_motor_servo_test_apps:
19401956
rules:
19411957
- <<: *if-protected
19421958
- <<: *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)