2025-04-25 22:42:20 +08:00

119 lines
3.0 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.

#include <rtthread.h>
#include <rtdevice.h>
#include <drv_gpio.h>
#include <board.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
// #define SERVO_PWM_DEV_NAME "pwm12"
// #define SERVO_PWM_DEV_NAME "pwm12"
// #define SERVO_PWM_DEV_CHANNEL 1
extern TIM_HandleTypeDef htim12;
struct rt_device_pwm *motor_dev; // pwm设备句柄
rt_uint32_t period = 100000000; // 单位us 向左6位变毫秒 10s
// rt_uint32_t pulse = 1000000;
struct rt_device_pwm *servo_dev; // pwm设备句柄
rt_uint32_t servo_period = 20000; // 单位us 向左3位变毫秒 20ms
#define PIN_MOTOR2 GET_PIN(B, 2)
#define PIN_MOTOR1 GET_PIN(A, 8)
int motor1=PIN_HIGH;
/**
* @brief 电机速度控制(num)%
*
* @param num 百分比数字(0~100).
*
*/
void motor_speed(int num)
{
// 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);
rt_pin_write(PIN_MOTOR1, num>0?PIN_LOW:PIN_HIGH);
}
/**
* @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);
}
int mot_init(void)
{
rt_pin_mode(PIN_MOTOR2, PIN_MODE_OUTPUT);
rt_pin_write(PIN_MOTOR2, PIN_HIGH);
rt_pin_mode(PIN_MOTOR1, PIN_MODE_OUTPUT);
rt_pin_write(PIN_MOTOR1, 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(100);
// 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!");
}
void motor_set(int argc, char **argv)
{
if (argc == 2)
{
motor_speed(atoi(argv[1]));
}
LOG5("motor speed:%d",atoi(argv[1]));
}
MSH_CMD_EXPORT_ALIAS(motor_set,motor, motor_set speed 0~100);
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);