-
Notifications
You must be signed in to change notification settings - Fork 733
/
iot_servo.h
96 lines (85 loc) · 2.62 KB
/
iot_servo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _IOT_SERVO_H_
#define _IOT_SERVO_H_
#include "esp_err.h"
#include "driver/ledc.h"
#include "driver/gpio.h"
/**
* @brief Configuration of servo motor channel
*
*/
typedef struct {
gpio_num_t servo_pin[LEDC_CHANNEL_MAX]; /**< Pin number of pwm output */
ledc_channel_t ch[LEDC_CHANNEL_MAX]; /**< The ledc channel which used */
} servo_channel_t;
/**
* @brief Configuration of servo motor
*
*/
typedef struct {
uint16_t max_angle; /**< Servo max angle */
uint16_t min_width_us; /**< Pulse width corresponding to minimum angle, which is usually 500us */
uint16_t max_width_us; /**< Pulse width corresponding to maximum angle, which is usually 2500us */
uint32_t freq; /**< PWM frequency */
ledc_timer_t timer_number; /**< Timer number of ledc */
servo_channel_t channels; /**< Channels to use */
uint8_t channel_number; /**< Total channel number */
} servo_config_t;
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Initialize ledc to control the servo
*
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param config Pointer of servo configure struct
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Configure ledc failed
*/
esp_err_t iot_servo_init(ledc_mode_t speed_mode, const servo_config_t *config);
/**
* @brief Deinitialize ledc for servo
*
* @param speed_mode Select the LEDC channel group with specified speed mode.
*
* @return
* - ESP_OK Success
*/
esp_err_t iot_servo_deinit(ledc_mode_t speed_mode);
/**
* @brief Set the servo motor to a certain angle
*
* @note This API is not thread-safe
*
* @param speed_mode Select the LEDC channel group with specified speed mode.
* @param channel LEDC channel, select from ledc_channel_t
* @param angle The angle to go
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t iot_servo_write_angle(ledc_mode_t speed_mode, uint8_t channel, float angle);
/**
* @brief Read current angle of one channel
*
* @param speed_mode Select the LEDC channel group with specified speed mode.
* @param channel LEDC channel, select from ledc_channel_t
* @param angle Current angle of the channel
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t iot_servo_read_angle(ledc_mode_t speed_mode, uint8_t channel, float *angle);
#ifdef __cplusplus
}
#endif
#endif /* _IOT_SERVO_H_ */