2022.0306避障小车学习1

要求:使用stm32f103单片机,应用RTOS实时系统,使用超声波模块,oled屏,l298n直流步进电机 驱动模块和小车底盘。  

2022.0306避障小车学习1_第1张图片2022.0306避障小车学习1_第2张图片2022.0306避障小车学习1_第3张图片

 思路在任务里用超声波实时测出距障碍物的距离,并将距离显示在oled屏上,再根据判断距离大小调用前进或者后退那些函数。

1.oled屏:显示距离,频率等数据

2022.0306避障小车学习1_第4张图片2022.0306避障小车学习1_第5张图片

2022.0306避障小车学习1_第6张图片

怎么显示。。。

 2.超声波测距

float Measure()
{
        struct timeval t1;	//超声波测距结构体,系统自带
        struct timeval t2;

        long start;	//	刚开始接受信号
        long end;	//	最后接受信号
        float distance;	

        digitalWrite(Trig,LOW);	//	让超声波一开始信号平稳
        delayMicroseconds(2);

        digitalWrite(Trig,HIGH);        //发出超声波脉冲,开始测距
        delayMicroseconds(10);
        digitalWrite(Trig,LOW);
		
		// 直到发送完信号,开始计时
        while(!(digitalRead(Echo) == 1) );      //获取当前时间,最开始接受到返回信号的时间
        gettimeofday(&t1,NULL);
		
		// 信号回来完毕,结束计时,否则阻塞等待
        while(!(digitalRead(Echo) == 0) );      //获取当前时间,最后接受到返回信号的时间
        gettimeofday(&t2,NULL);

        start = t1.tv_sec*1000000 + t1.tv_usec;

        end   = t2.tv_sec*1000000 + t2.tv_usec;

		// 空气中传播 340 m/s
        distance = (float)(end - start) / 1000000 * 34000 / 2;	//测出信号往返距离

        return distance;

}

 这个函数返回信号往返距离,然后判断1/2距离调整怎么小车怎么转。

 参考:(40条消息) 超声波测距原理、代码实现_正弦定理的博客-CSDN博客

 3.控制前进后退函数:

//占空比=50%的情况
/* USER CODE BEGIN PV */
uint8_t velocity = 50;
/* USER CODE END PV */

在main中开启pwm 

 /* USER CODE BEGIN 2 */
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_3);
	
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);
  /* USER CODE END 2 */
void Reset()
{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2,0);
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3,0);
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,0);
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1,0);

__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,0);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3,0);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,0);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3,0);//后轮
}
void forward()
{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2,0);//前右倒转       该通道的占空比为零
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3,velocity);//前右正转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,0);//前左倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1,velocity);//前左正转
	
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,0);//后左倒转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3,velocity);//后左正转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,0);//后右倒转
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3,velocity);//后右正转
}
void backward()

{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2,velocity);//前右倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3,0);//前右正转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,velocity);//前左倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1,0);//前左正转

	
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,velocity);//后左倒转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3,0);//后左正转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,velocity);//后右倒转
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3,0);//后右正转
}

void shift_left()

{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2,0);//前右倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3,0);//前左正转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,velocity);//前左倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1,velocity);//前右正转
	
	
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,0);//后左倒转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3,velocity);//后左正转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,velocity);//后右倒转
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3,0);//后右正转
}

void shift_right()
{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2,velocity);//前右倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3,velocity);//前左正转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,0);//前左倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1,0);//前右正转
	
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,velocity);//后左倒转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3,0);//后左正转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,0);//后右倒转
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3,velocity);//后右正转
}

void turn_left()
{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2,0);//前右倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3,0);//前左正转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,velocity);//前左倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1,velocity);//前右正转
	
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,velocity);//后左倒转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3,0);//后左正转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,0);//后右倒转
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3,velocity);//后右正转
}

void turn_right()
{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2,velocity);//前右倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3,velocity);//前左正转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,0);//前左倒转
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1,0);//前右正转
	
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,0);//后左倒转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3,velocity);//后左正转
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,velocity);//后右倒转
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3,0);//后右正转
}

2022.0306避障小车学习1_第7张图片 

你可能感兴趣的:(单片机,单片机,学习,stm32)