2025-03-14 17:01:29 +08:00
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
#include <drv_gpio.h>
|
2025-04-25 21:32:20 +08:00
|
|
|
|
#include <board.h>
|
2025-03-14 17:01:29 +08:00
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
2025-04-25 21:32:20 +08:00
|
|
|
|
// #define SERVO_PWM_DEV_NAME "pwm12"
|
|
|
|
|
#define SERVO_PWM_DEV_NAME "pwm1"
|
|
|
|
|
#define SERVO_PWM_DEV_CHANNEL 1
|
|
|
|
|
|
|
|
|
|
extern TIM_HandleTypeDef htim12;
|
2025-03-14 17:01:29 +08:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2025-04-25 21:32:20 +08:00
|
|
|
|
struct rt_device_pwm *servo_dev; // pwm设备句柄
|
|
|
|
|
rt_uint32_t servo_period = 20000; // 单位us 向左3位,变毫秒 20ms
|
2025-03-15 09:40:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 21:32:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 舵机角度控制
|
|
|
|
|
*
|
|
|
|
|
* @param num 百分比数字(-90~90).
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void servo_angle(int num)
|
|
|
|
|
{
|
|
|
|
|
// rt_pwm_set(servo_dev, SERVO_PWM_DEV_CHANNEL, servo_period, 1500+num*500/45 );
|
|
|
|
|
__HAL_TIM_SET_COMPARE(&htim12,TIM_CHANNEL_1,1500+num*500/45);
|
|
|
|
|
}
|
2025-03-14 17:01:29 +08:00
|
|
|
|
|
|
|
|
|
int mot_init(void)
|
|
|
|
|
{
|
2025-04-25 21:32:20 +08:00
|
|
|
|
// rt_pin_mode(PIN_MOTOR2, PIN_MODE_OUTPUT);
|
|
|
|
|
// rt_pin_write(PIN_MOTOR2, PIN_HIGH);
|
|
|
|
|
// 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周期和脉冲宽度默认值 */
|
|
|
|
|
// motor_speed(0);
|
|
|
|
|
// rt_pwm_enable(motor_dev, MOTOR_PWM_DEV_CHANNEL);
|
|
|
|
|
// LOG1("motor init success!");
|
|
|
|
|
}
|
|
|
|
|
int ser_init(void)
|
|
|
|
|
{
|
|
|
|
|
MX_TIM12_Init();
|
|
|
|
|
HAL_TIM_PWM_Start(&htim12,TIM_CHANNEL_1);
|
|
|
|
|
|
|
|
|
|
// servo_dev = (struct rt_device_pwm *)rt_device_find(SERVO_PWM_DEV_NAME);
|
|
|
|
|
// if (servo_dev == RT_NULL)
|
|
|
|
|
// {
|
|
|
|
|
// rt_kprintf("servo_dev run failed! can't find %s device!\n", SERVO_PWM_DEV_NAME);
|
|
|
|
|
// return RT_ERROR;
|
|
|
|
|
// }
|
|
|
|
|
servo_angle(0);
|
|
|
|
|
// rt_pwm_enable(servo_dev, SERVO_PWM_DEV_CHANNEL);
|
|
|
|
|
LOG1("servo_dev init success!");
|
2025-03-14 17:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
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);
|
2025-04-25 21:32:20 +08:00
|
|
|
|
void servo_set(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
if (argc == 2)
|
|
|
|
|
{
|
|
|
|
|
servo_angle(atoi(argv[1]));
|
|
|
|
|
}
|
|
|
|
|
LOG5("servo angle:%d",atoi(argv[1]));
|
|
|
|
|
}
|
|
|
|
|
MSH_CMD_EXPORT_ALIAS(servo_set,servo, servo_set angle -90~90);
|