wheel/components/bdc_motor/bdc_motor.c

79 lines
2.4 KiB
C
Raw Permalink Normal View History

2024-01-20 17:56:00 +08:00
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <string.h>
#include <sys/cdefs.h>
#include "esp_log.h"
#include "esp_check.h"
#include "bdc_motor.h"
#include "bdc_motor_interface.h"
2024-01-22 14:21:12 +08:00
#include "driver/gpio.h"
2024-01-20 17:56:00 +08:00
static const char *TAG = "bdc_motor";
2024-01-22 14:21:12 +08:00
/* H桥low的两个gpio控制引脚 */
/* 注原来的这些函数只处理了pwm的信号没有处理H桥low的两个mos管状态这里采用全局变量的方式暂时的修正 */
/* 左轮low0 low1 右轮low0 low1 */
uint32_t low_gpio[2][2];
esp_err_t bdc_motor_enable(bdc_motor_handle_t motor, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
2024-01-22 14:21:12 +08:00
gpio_set_level(low_gpio[id][0], 1);
gpio_set_level(low_gpio[id][1], 1);
2024-01-20 17:56:00 +08:00
return motor->enable(motor);
}
2024-01-22 14:21:12 +08:00
esp_err_t bdc_motor_disable(bdc_motor_handle_t motor, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return motor->disable(motor);
}
2024-01-22 14:21:12 +08:00
esp_err_t bdc_motor_set_speed(bdc_motor_handle_t motor, uint32_t speed, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return motor->set_speed(motor, speed);
}
2024-01-22 14:21:12 +08:00
esp_err_t bdc_motor_forward(bdc_motor_handle_t motor, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
2024-01-22 14:21:12 +08:00
gpio_set_level(low_gpio[id][0], 1);
gpio_set_level(low_gpio[id][1], 0);
2024-01-20 17:56:00 +08:00
return motor->forward(motor);
}
2024-01-22 14:21:12 +08:00
esp_err_t bdc_motor_reverse(bdc_motor_handle_t motor, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
2024-01-22 14:21:12 +08:00
gpio_set_level(low_gpio[id][0], 0);
gpio_set_level(low_gpio[id][1], 1);
2024-01-20 17:56:00 +08:00
return motor->reverse(motor);
}
2024-01-22 14:21:12 +08:00
esp_err_t bdc_motor_coast(bdc_motor_handle_t motor, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
2024-01-22 14:21:12 +08:00
gpio_set_level(low_gpio[id][0], 1);
gpio_set_level(low_gpio[id][1], 1);
2024-01-20 17:56:00 +08:00
return motor->coast(motor);
}
2024-01-22 14:21:12 +08:00
esp_err_t bdc_motor_brake(bdc_motor_handle_t motor, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
2024-01-22 14:21:12 +08:00
gpio_set_level(low_gpio[id][0], 0);
gpio_set_level(low_gpio[id][1], 0);
2024-01-20 17:56:00 +08:00
return motor->brake(motor);
}
2024-01-22 14:21:12 +08:00
esp_err_t bdc_motor_del(bdc_motor_handle_t motor, int id)
2024-01-20 17:56:00 +08:00
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return motor->del(motor);
}