wheel/components/bdc_motor/bdc_motor.c
2024-01-22 14:21:12 +08:00

79 lines
2.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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"
#include "driver/gpio.h"
static const char *TAG = "bdc_motor";
/* 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)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
gpio_set_level(low_gpio[id][0], 1);
gpio_set_level(low_gpio[id][1], 1);
return motor->enable(motor);
}
esp_err_t bdc_motor_disable(bdc_motor_handle_t motor, int id)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return motor->disable(motor);
}
esp_err_t bdc_motor_set_speed(bdc_motor_handle_t motor, uint32_t speed, int id)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return motor->set_speed(motor, speed);
}
esp_err_t bdc_motor_forward(bdc_motor_handle_t motor, int id)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
gpio_set_level(low_gpio[id][0], 1);
gpio_set_level(low_gpio[id][1], 0);
return motor->forward(motor);
}
esp_err_t bdc_motor_reverse(bdc_motor_handle_t motor, int id)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
gpio_set_level(low_gpio[id][0], 0);
gpio_set_level(low_gpio[id][1], 1);
return motor->reverse(motor);
}
esp_err_t bdc_motor_coast(bdc_motor_handle_t motor, int id)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
gpio_set_level(low_gpio[id][0], 1);
gpio_set_level(low_gpio[id][1], 1);
return motor->coast(motor);
}
esp_err_t bdc_motor_brake(bdc_motor_handle_t motor, int id)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
gpio_set_level(low_gpio[id][0], 0);
gpio_set_level(low_gpio[id][1], 0);
return motor->brake(motor);
}
esp_err_t bdc_motor_del(bdc_motor_handle_t motor, int id)
{
ESP_RETURN_ON_FALSE(motor, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return motor->del(motor);
}