用ch32v103r8t6开发板, 搞定了小车上的三个超声波模块之后, 供上电之后, 小车一直向左拐(即使没有达到小车判断小车左右拐的条件),会有哪方面的问题导致的的吗?下面 为部分代码
//电机初始化
void l9110s_init(left_or_right object)
{
if(object == left)
{
pwm_init(L_PWM_F, 72, 0, TIM_OCPreload_Enable); //L_PWM_F PWM4_CH3_B8
pwm_init(L_PWM_B, 72, 0, TIM_OCPreload_Enable); //L_PWM_B PWM4_CH4_B9
}
else if(object == right)
{
pwm_init(R_PWM_F, 72, 0, TIM_OCPreload_Disable); //R_PWM_F PWM3_CH3_B0
pwm_init(R_PWM_B, 72, 0, TIM_OCPreload_Disable); //R_PWM_B PWM3_CH4_B1
}
}
//-------------------------------------------------------------------------------------------------------------------
// @brief PWM初始化
// @param pwmch PWM通道号及引脚
// @param freq PWM频率
// @param duty PWM占空比
// @param TIM_OCPreload_Enable_Disable 默认设置为TIM_OCPreload_Disable,后期可删除此形参只是为了适配RGB灯做的修改
// @return void
// Sample usage: pwm_init(PWM1_CH1_A8, 50, 5000); //初始化PWM1 通道1 使用引脚A8 输出PWM频率50HZ 占空比为百分之 5000/PWM_DUTY_MAX*100
// PWM_DUTY_MAX在wh_pwm.h文件中 默认为10000
//-------------------------------------------------------------------------------------------------------------------
void pwm_init(PWMCH_enum pwmch, uint32 freq, uint32 duty, uint16_t TIM_OCPreload_Enable_Disable)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16 match_temp; //占空比值
uint16 period_temp; //周期值
uint16 freq_div = 0; //分频值
pwm_gpio_init(pwmch); //PWM引脚初始化
/* 获取系统主频 */
sys_clk = (uint32)72000000;
freq_div = (uint16)((sys_clk / freq) >> 16); //多少分频
period_temp = (uint16)(sys_clk/(freq*(freq_div + 1))); //周期
match_temp = period_temp * duty / PWM_DUTY_MAX; //占空比
/* 时基初始化 */
timbase_init((pwmch >> 8), period_temp - 1, freq_div);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //使能AFIO复用功能模块时钟
//引脚重映射
// if((pwmch >> 4) == 0x10) //PWM1的部分重映射
// GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);
// else if((pwmch >> 4) == 0x11) //PWM2的完全重映射
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);
// else if((pwmch >> 4) == 0x21) //PWM3的完全重映射
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
// else if((pwmch >> 4) == 0x22) //PWM3的部分重映射
// GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
//初始化TIM PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = match_temp;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
if((pwmch & 0x03) == 0x00) //通道选择
{
TIM_OC1Init(((TIM_TypeDef *) TIMERN[pwmch>>8]), &TIM_OCInitStructure ); //定时器通道1初始化
TIM_CtrlPWMOutputs(((TIM_TypeDef *) TIMERN[pwmch>>8]), ENABLE );
TIM_OC1PreloadConfig(((TIM_TypeDef *) TIMERN[pwmch>>8]), TIM_OCPreload_Enable_Disable); //定时器预装载配置
}
else if((pwmch & 0x03) == 0x01)
{
TIM_OC2Init(((TIM_TypeDef *) TIMERN[pwmch>>8]), &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(((TIM_TypeDef *) TIMERN[pwmch>>8]), ENABLE );
TIM_OC2PreloadConfig(((TIM_TypeDef *) TIMERN[pwmch>>8]), TIM_OCPreload_Enable_Disable);
}
else if((pwmch & 0x03) == 0x02)//left1/right1
{
TIM_OC3Init(((TIM_TypeDef *) TIMERN[pwmch>>8]), &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(((TIM_TypeDef *) TIMERN[pwmch>>8]), ENABLE );
TIM_OC3PreloadConfig(((TIM_TypeDef *) TIMERN[pwmch>>8]), TIM_OCPreload_Enable_Disable);
}
else if((pwmch & 0x03) == 0x03)//left2/rgiht2
{
TIM_OC4Init(((TIM_TypeDef *) TIMERN[pwmch>>8]), &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(((TIM_TypeDef *) TIMERN[pwmch>>8]), ENABLE );
TIM_OC4PreloadConfig(((TIM_TypeDef *) TIMERN[pwmch>>8]), TIM_OCPreload_Enable_Disable);
}
TIM_Cmd(((TIM_TypeDef *) TIMERN[pwmch>>8]), ENABLE); //定时器使能
}
/* 初始化超声波 */
ultra_init(TRIG, ECHO, 0); //左边超声波
ultra_init(A5, C2, 1); //右边超声波 trig--A5, echo--C2
ultra_init(C4, C5, 2); //左边超声波 trig-C4, echo--C5
xunji(middle_distance, right_distance, left_distance); //小车走迷宫
/*小车方向判断*/
void xunji(unsigned int middle, unsigned int right, unsigned int left)
{
unsigned int distance = 0;
if(back_flag)
{
if(right > right_int && left <= left_int)
{
new_car_turnright(2500);
change_std_dir(std_dir);
curr_dir = std_dir[1];
flag = 0;
}else if(right <= right_int && left > left_int)
{
new_car_turnleft(2500);
change_std_dir(std_dir);
curr_dir = std_dir[3];
flag = 0;
}
back_flag = 0;
}
if(middle <= middle_int)
{
if(right > right_int && left <= left_int) //死胡同
{
new_car_turnright(2500);
change_std_dir(std_dir);
curr_dir = std_dir[1];
flag = 0;
}else if(right <= right_int && left <= left_int)
{
new_car_back(2500);
curr_dir = std_dir[2];
flag = 0;
back_flag = 1;
}else if(right <=right_int && left > left_int)
{
new_car_turnleft(2500);
change_std_dir(std_dir);
curr_dir = std_dir[3];
flag = 0;
}
}else {
new_car_forward(2500);
// adjust(left, right, 2000);
curr_dir = std_dir[0];
}
Delay_Ms(500);
}
|