Skip to content

Commit

Permalink
Added servo component to the registry
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pedrominatel committed Nov 26, 2024
1 parent aff99b9 commit e03d757
Show file tree
Hide file tree
Showing 15 changed files with 483 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/upload_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
components/led/lightbulb_driver;
components/motor/esp_sensorless_bldc_control;
components/motor/esp_simplefoc;
components/motor/servo;
components/openai;
components/sensors/humiture/aht20;
components/sensors/ntc_driver;
Expand Down
20 changes: 20 additions & 0 deletions .gitlab/ci/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,18 @@ build_example_motor_foc_knob:
variables:
EXAMPLE_DIR: examples/motor/foc_knob_example

build_example_motor_servo:
extends:
- .build_examples_template
- .rules:build:example_motor_servo
parallel:
matrix:
- IMAGE: espressif/idf:release-v5.0
- IMAGE: espressif/idf:release-v5.1
- IMAGE: espressif/idf:release-v5.2
variables:
EXAMPLE_DIR: components/motor/servo/examples/servo_control

build_example_ota_simple_ota_example:
extends:
- .build_examples_template
Expand Down Expand Up @@ -1132,6 +1144,14 @@ build_components_motor_esp_simplefoc_test_apps:
variables:
EXAMPLE_DIR: components/motor/esp_simplefoc/test_apps

build_components_motor_servo_test_apps:
extends:
- .build_examples_template
- .rules:build:components_motor_servo_test_apps
- .build_idf_active_release_version
variables:
EXAMPLE_DIR: components/motor/servo/test_apps

build_components_ntc_driver_test_apps:
extends:
- .build_examples_template
Expand Down
57 changes: 57 additions & 0 deletions components/motor/servo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Servo Motor Component

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.

The library uses PWM (Pulse Width Modulation) to control servo motor rotation and supports customizable configurations for angle limits, pulse width, frequency, and more.

## Features

- Easy control of servo motors using PWM signals
- Support for custom angle range and pulse width
- Flexible configuration of PWM channels and timers
- Compatible with the ESP-IDF framework

## Getting Started

### Prerequisites

- ESP-IDF installed and configured on your development environment
- A servo motor compatible with PWM signals
- Proper connections between the ESP32 and the servo motor (signal, VCC, GND)

## How to use

Initialize the sensor with the configuration:

```c
servo_config_t servo_cfg = {
.max_angle = 180,
.min_width_us = 500,
.max_width_us = 2500,
.freq = 50,
.timer_number = LEDC_TIMER_0,
.channels = {
.servo_pin = {
SERVO_GPIO,
},
.ch = {
LEDC_CHANNEL_0,
},
},
.channel_number = 1,
};

// Initialize the servo
iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg);
```
Set the angle:
```c
uint16_t angle = 0;
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, angle);
```

## Reference

[Documentation](https://docs.espressif.com/projects/esp-iot-solution/en/latest/motor/servo.html)
6 changes: 6 additions & 0 deletions components/motor/servo/examples/servo_control/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(servo_control)
36 changes: 36 additions & 0 deletions components/motor/servo/examples/servo_control/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Example for the servo component

This example shows how to use the servo component.

## How to use

Initialize the sensor with the configuration:

```c
servo_config_t servo_cfg = {
.max_angle = 180,
.min_width_us = 500,
.max_width_us = 2500,
.freq = 50,
.timer_number = LEDC_TIMER_0,
.channels = {
.servo_pin = {
SERVO_GPIO,
},
.ch = {
LEDC_CHANNEL_0,
},
},
.channel_number = 1,
};

// Initialize the servo
iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg);
```
Set the angle:
```c
uint16_t angle = 0;
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, angle);
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
idf_component_register(SRCS "servo_control.c"
INCLUDE_DIRS ".")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies:
espressif/servo:
version: "*"
override_path: '../../../'
69 changes: 69 additions & 0 deletions components/motor/servo/examples/servo_control/main/servo_control.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c_types.h"
#include "esp_log.h"
#include "esp_system.h"
#include "sdkconfig.h"
#include "iot_servo.h"

#define SERVO_GPIO (2) // Servo GPIO

static const char *TAG = "Servo Control";

uint16_t calibration_value_0 = 30; // Real 0 degree angle
uint16_t calibration_value_180 = 195; // Real 0 degree angle


// Task to test the servo
static void servo_test_task(void *arg)
{
ESP_LOGI(TAG, "Servo Test Task");
while (1) {
// Set the angle of the servo
for(int i = calibration_value_0; i <= calibration_value_180; i += 1) {
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, i);
vTaskDelay(20 / portTICK_PERIOD_MS);
}
// Return to the initial position
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, calibration_value_0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}

void servo_init(void)
{
ESP_LOGI(TAG, "Servo Control");

// Configure the servo
servo_config_t servo_cfg = {
.max_angle = 180,
.min_width_us = 500,
.max_width_us = 2500,
.freq = 50,
.timer_number = LEDC_TIMER_0,
.channels = {
.servo_pin = {
SERVO_GPIO,
},
.ch = {
LEDC_CHANNEL_0,
},
},
.channel_number = 1,
};

// Initialize the servo
iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg);
}

void app_main(void)
{
ESP_LOGI(TAG, "Servo Control");
// Initialize the servo
servo_init();
// Create the servo test task
xTaskCreate(servo_test_task, "servo_test_task", 2048, NULL, 5, NULL);
}
9 changes: 9 additions & 0 deletions components/motor/servo/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 1.1.0
description: Espressif's Servo Motor Component
url: https://github.com/espressif/esp-iot-solution/tree/master/components/motor/servo
repository: https://github.com/espressif/esp-iot-solution.git
issues: https://github.com/espressif/esp-iot-solution/issues
documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/motor/servo.html
dependencies:
idf:
version: '>=4.4'
Loading

0 comments on commit e03d757

Please sign in to comment.