#include #include #include #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设备句柄 rt_uint32_t period = 100000000; // 单位us 向左6位,变毫秒 10s // rt_uint32_t pulse = 1000000; #define PIN_MOTOR2 GET_PIN(B, 2) 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); } int mot_init(void) { 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!"); } void motor_set(int argc, char **argv) { // if(motor1== PIN_HIGH) // { // rt_pin_write(PIN_MOTOR1, PIN_LOW); // motor1=PIN_LOW; // } // else // { // rt_pin_write(PIN_MOTOR1, PIN_HIGH); // motor1=PIN_HIGH; // } 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); // struct rt_device_pwm *motor_dev; // pwm设备句柄 // rt_uint32_t period2 = 10000000; // 单位us 向左6位,变毫秒 10ms2 // /** // * @brief 电机速度控制(num)% // * // * @param num 百分比数字(0~100). // * // */ // void motor2_speed(int num) // { // rt_pwm_set(motor_dev, MOTOR2_PWM_DEV_CHANNEL, period2, num * period2 / 100); // } // int mo2_init(void) // { // motor_dev = (struct rt_device_pwm *)rt_device_find(MOTOR2_PWM_DEV_NAME); // if (motor_dev == RT_NULL) // { // rt_kprintf("motor2 run failed! can't find %s device!\n", MOTOR2_PWM_DEV_NAME); // return RT_ERROR; // } // /* 设置PWM周期和脉冲宽度默认值 */ // motor_speed(0); // } // void motor2_set(int argc, char **argv) // { // if (argc == 2) // { // motor2_speed(atoi(argv[1])); // } // LOG5("motor2 speed:%d",atoi(argv[1])); // } // MSH_CMD_EXPORT_ALIAS(motor2_set,motor2, motor2_set speed 0~100);