2025-03-14 17:01:29 +08:00
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
#include <drv_gpio.h>
|
|
|
|
|
|
|
|
|
|
#define LOG_TAG "app.robot"
|
|
|
|
|
#define LOG_LVL LOG_LVL_DBG
|
|
|
|
|
// #define LOG_LVL LOG_LVL_INFO
|
|
|
|
|
#define USE_LOG1
|
|
|
|
|
#define USE_LOG2
|
|
|
|
|
#define USE_LOG3
|
|
|
|
|
#define USE_LOG4
|
|
|
|
|
#define USE_LOG5
|
|
|
|
|
#define USE_LOG6
|
|
|
|
|
#define USE_LOG7
|
|
|
|
|
#define USE_LOG12
|
|
|
|
|
#define USE_LOG_D
|
|
|
|
|
#include "logn.h"
|
|
|
|
|
|
|
|
|
|
#define MOTOR_PWM_DEV_NAME "pwm1"
|
|
|
|
|
#define MOTOR_PWM_DEV_CHANNEL 1
|
|
|
|
|
|
|
|
|
|
#define MOTOR2_PWM_DEV_NAME "pwm1"
|
|
|
|
|
#define MOTOR2_PWM_DEV_CHANNEL 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct rt_device_pwm *motor_dev; // pwm设备句柄
|
2025-03-15 09:40:38 +08:00
|
|
|
|
rt_uint32_t period = 100000000; // 单位us 向左6位,变毫秒 10s
|
|
|
|
|
// rt_uint32_t pulse = 1000000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define PIN_MOTOR2 GET_PIN(B, 2)
|
|
|
|
|
int motor1=PIN_HIGH;
|
2025-03-14 17:01:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 电机速度控制(num)%
|
|
|
|
|
*
|
|
|
|
|
* @param num 百分比数字(0~100).
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void motor_speed(int num)
|
|
|
|
|
{
|
2025-03-15 09:40:38 +08:00
|
|
|
|
rt_pwm_set(motor_dev, MOTOR_PWM_DEV_CHANNEL, period, (100-num)/ 100 * period );
|
|
|
|
|
rt_pin_write(PIN_MOTOR2, num>0?PIN_LOW:PIN_HIGH);
|
2025-03-14 17:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int mot_init(void)
|
|
|
|
|
{
|
2025-03-15 09:40:38 +08:00
|
|
|
|
rt_pin_mode(PIN_MOTOR2, PIN_MODE_OUTPUT);
|
|
|
|
|
rt_pin_write(PIN_MOTOR2, PIN_HIGH);
|
2025-03-15 08:43:21 +08:00
|
|
|
|
motor_dev = (struct rt_device_pwm *)rt_device_find(MOTOR_PWM_DEV_NAME);
|
|
|
|
|
if (motor_dev == RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_kprintf("motor run failed! can't find %s device!\n", MOTOR_PWM_DEV_NAME);
|
|
|
|
|
return RT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
/* 设置PWM周期和脉冲宽度默认值 */
|
2025-03-15 09:40:38 +08:00
|
|
|
|
motor_speed(0);
|
|
|
|
|
rt_pwm_enable(motor_dev, MOTOR_PWM_DEV_CHANNEL);
|
2025-03-14 17:01:29 +08:00
|
|
|
|
LOG1("motor init success!");
|
|
|
|
|
}
|
|
|
|
|
void motor_set(int argc, char **argv)
|
|
|
|
|
{
|
2025-03-15 08:43:21 +08:00
|
|
|
|
if (argc == 2)
|
|
|
|
|
{
|
|
|
|
|
motor_speed(atoi(argv[1]));
|
|
|
|
|
}
|
|
|
|
|
LOG5("motor speed:%d",atoi(argv[1]));
|
2025-03-14 17:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
MSH_CMD_EXPORT_ALIAS(motor_set,motor, motor_set speed 0~100);
|