当前位置:   article > 正文

【STM32】智能避障红外小车_stm32红外避障小车程序

stm32红外避障小车程序


编译环境:
keil5 SourceInsight STM32F103X

原理图
在这里插入图片描述
在这里插入图片描述

直流电机

直流电机的原理:
    通电就会转, 反向通电就会反转
    两极的电势差 决定转速

电机驱动模块 以及 接线 
    参考图示 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

代码实现: 
  • 1

motor.c / motor.h


		#ifndef __MOTOR_H__
		#define __MOTOR_H__ 
		
		
		void motor_init(void);    //函数声明 
		
		void set_left_A( int a );
		void set_right_B( int b );
		
		
		void move_front( void );
		void move_back(void);
		void move_left(void);
		void move_right(void);
		void stop(void);
		
		
		#endif

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
#include "motor.h"			
#include "led.h"
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "SysTick.h"
		
        void motor_init(void)
        {
						
			GPIO_InitTypeDef GPIO_InitStruct;
			led_init();
            //1.使能 GPIO分组 和 AFIO 时钟
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);       
					
            //2.禁用Jtag功能, 把PB3, PB4等重新映射为普通IO口 
            GPIO_PinRemapConfig( GPIO_Remap_SWJ_JTAGDisable , ENABLE );
					
            //3.初始化GPIO --> PB0,PB1,PB6,PB7, 输出推挽 
            GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_7 ;
            GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
            GPIO_Init( GPIOB, &GPIO_InitStruct );

            //设置默认状态 --》 高电平 
            //GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_7);
        }

        //左轮
        void set_left_A( int a )
        {
          if( a == 0 )//左后退
            {
								led_ctl(6,0);
								led_ctl(7,1);
            }
            else //左前进
            {
                led_ctl(6,1);
								led_ctl(7,0);
            }
            
        }

        void set_right_B( int a )
        {
					if( a == 0 )//右后退
            {
								led_ctl(0,0);
								led_ctl(1,1);
            }
            else //右前进
            {
                led_ctl(0,1);
								led_ctl(1,0);
            }
        
        }



        //前进
        void move_front( void )
        {
            set_left_A(1);
            set_right_B(1);
            
        }

        void move_back(void)
        {
					set_left_A(0);
					set_right_B(0);
        }

        void move_left(void)
        {
				//set_left_A(0);
					set_right_B(1);
        }

        void move_right(void)
        {
					set_left_A(1);
       // set_right_B(0);
        }

        void stop(void)
        {
					led_ctl(0,0);
					led_ctl(1,0);
					led_ctl(6,0);
					led_ctl(7,0);
        }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

红外对管

红外发射管
红外接收管 

    工作原理: 
        一通电,红外发射管就会不断地往外发射红外光线 
        红外接收管不断地 接收反射回来的红外光线 


红外主控板 以及 接线 
    参考图示 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述

    灵敏度的调节 
        旋转灵敏度的调节器 至 指示灯灭,然后 反向旋转 至指示灯 刚刚正常亮的位置 
            能够让红外对管处于白色地面时,指示灯能够正常点亮
                        处于黑色轨迹时,指示灯能够熄灭

作用: 
    (1)循迹 
        红外对管要垂直于地面进行安装 

        主要是利用不同颜色的物体对于光线的反射和吸收程度不一样
            白色物体:能够反射大量的光线,吸收的比较少
            黑色物体:能够吸收大量的光线,反射的比较少
        --》 
            地面(白色):红外接收管能够接收到反射回来的光线
            轨迹(黑色):红外接收管就收不到红外光线了
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

红外循迹

原理: 
    当红外对管处于白色地面时,红外接收管能够收到反射光线,
        此时模块OUT引脚为 低电平0, 红外主控板对应的led灯就会点亮,
        连接到32芯片上的对应的GPIO引脚 为低电平0 

    当红外对管处于黑色轨迹时,红外接收管接收不到反射光线,
        此时模块OUT引脚为 高电平1, 红外主控板对应的led灯就会熄灭,
        连接到32芯片上的对应的GPIO引脚 为高电平1 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

利用脉冲宽度调制技术(PWM)控制电机转速

    				//前进 -- 控速
    				//举例如下
    void move_front_PWM()
    {
        unsigned char i = 2;

        set_left_a( 1 );
        set_right_a( 1 );

        while( i-- )
        {
            set_left_b( 0 );
            set_right_b( 0 );
            delay_ms( 80 );     //前进80ms

            set_left_b( 1 );
            set_right_b( 1 );
            delay_ms( 20 );     //停止20ms(让惯性带动车子转动)
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

避障

避障原理:
    把红外对管平行于地面进行安装

    主要利用空气对于光线有一定削弱作用 
        当前面有障碍物时,红外接收管能够收到大量的红外光线,红外模块OUT引脚为 低电平0 
        当前面没有障碍物时,红外接收管接收不到红外光线,红外模块OUT引脚为 高电平1 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

red.c / red.h

			#ifndef __RED_H__
			#define __RED_H__
			
			
			void gpio_redINit(void);
			void ourdelay(int time);
			
			void red_init(void);
			void red_ctl_led(void);
			
			void red_control(void);
			void red_ctl_car(void);
			void red_ctl_car_PWM(void);
			void red_avoid_car(void);
			void red_avoid_car1(void);
			
			
			
			#endif
			


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
		#include "stm32f10x.h"
		#include "SysTick.h"
		#include "system.h"
		#include "wheel.h"
		#include "led.h"
		#include "beep.h"
		#include "red.h"
		
		
		void ourdelay(int time)
		{
		int i;
		
		for( i=1;i<=time;i++)
		{
		delay_ms(1000);
		}
		}
		void gpio_redINit(void)
		{
			GPIO_InitTypeDef GPIO_InitStruct;
			GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
			GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
		    GPIO_InitStruct.GPIO_Mode =  GPIO_Mode_IPU ;
			GPIO_Init( GPIOB, &GPIO_InitStruct );
		
		}
		
		void red_init(void)
		{
			// 1.使能GPIO分组和AFIO时钟
			RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
			// 2.禁用Jtag功能,把PB3, PB4等重新映射为普通IO口 
			GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
			// 3.初始化GPIO -->带上拉的输入
			gpio_redINit();
		
		}
		
		// 
		void red_ctl_led(void)
		{
			
			while(1)
			{
			uint8_t red1 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) ;
			uint8_t red2 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) ;
		
				
			if(red1 ==0) lightledon(6); // sw1开关按下
			else  lightledoff(0);
			if(red2 ==0) lightledon(7);
			else  lightledoff(1);
				
			}
		}
		
		void red_ctl_car(void)
		{
			SysTick_Init(72);
			red_init();
			wheel_Init();
			beep_control_on();
			while(1)
			{
				uint8_t red1 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) ;
				uint8_t red2 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) ;
		
				// 左轮白地0,右轮白地0 -->正常前进
				if(red1==0 && red2==0)
				{
						// 前进
						move_front();
				}
				// 左轮检测到黑1,右轮白地0 -->需要左转
				if(red1 == 1&& red2 ==0)
				{	
						// 左转
					move_left();
				}
				// 左轮检测到白0,右轮检测到黑1 -->需要右转
				if(red1==0&&red2==1)
				{
						// 右转
					move_right();
				}
				// 左轮检测到黑1,右轮检测到黑1 -->需要停止
				if(red1 == 1&& red2 == 1)
				{
					// 停止
					stop();
				}
			}
		}
		
		void red_ctl_car_PWM(void)
		{
			red_init();
			wheel_Init();
			while(1)
			{
				uint8_t red1 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) ;
				uint8_t red2 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) ;
		
				// 左轮白地0,右轮白地0 -->正常前进
				if(red1==0 && red2==0)
				{
						// 前进
						move_front_PWM(80);
				}
				// 左轮检测到黑1,右轮白地0 -->需要左转
				if(red1 == 1&& red2 ==0)
				{	
						// 左转
					move_left_PWM(80);
				}
				// 左轮检测到白0,右轮检测到黑1 -->需要右转
				if(red1==0&&red2==1)
				{
						// 右转
					move_right_PWM(80);
				}
				// 左轮检测到黑1,右轮检测到黑1 -->需要停止
				if(red1 == 1&& red2 == 1)
				{
					// 停止
					stop();
				}
			}
		}
		
		// 避障
		void red_avoid_car(void)
		{
			red_init();
			wheel_Init();
			while(1)
			{
				uint8_t red1 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) ;
				uint8_t red2 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) ;
		
				// 左轮1,右轮1 -->无障碍物正常前进
				if(red1==1 && red2==1)
				{
						// 前进
						move_front();
				}
				// 左轮检测到障碍物0,右轮正常1 -->需要右转
				if(red1 == 0&& red2 ==1)
				{	
					//  也可以后退一点点 再右转 90°需要延时
					stop();
					delay_ms(1000);
					move_back();
					delay_ms(200);
					stop();
					delay_ms(500);
					move_right();
					delay_ms(750);
					//move_front_PWM(80);
				}
				// 左轮正常1,右轮检测到障碍物0 -->需要左转
				if(red1==1&&red2==0)
				{
					stop();
					delay_ms(1000);
					// 先后退
					move_back();
					// 延时
					delay_ms(200);
					stop();
					delay_ms(500);
					move_left();
					delay_ms(750);
				//	move_front_PWM(80);
				}
				// 前面都有障碍物 0 0 -->需要停止
				if(red1 == 0&& red2 == 0)
				{
					// 停止
					stop();
					delay_ms(1000);
					// 先后退
					move_back();
					// 延时
					delay_ms(600);
					
					move_left();
					delay_ms(1000);
					delay_ms(500);
					//move_front_PWM(80);
				}
			}
		}
		
		// 避障
		void red_avoid_car1(void)
		{
			red_init();
			wheel_Init();
			while(1)
			{
				uint8_t red1 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) ;
				uint8_t red2 =GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) ;
		
				// 左轮1,右轮1 -->无障碍物正常前进
				if(red1==1 && red2==1)
				{
						// 前进
						move_front_PWM(80);
				}
				// 左轮检测到障碍物0,右轮正常1 -->需要右转
				if(red1 == 0&& red2 ==1)
				{	
					//  也可以后退一点点 再右转 90°需要延时
		
					move_right_PWM(80);
				
				}
				// 左轮正常1,右轮检测到障碍物0 -->需要左转
				if(red1==1&&red2==0)
				{
				
					move_left_PWM(80);
					
				}
				// 前面都有障碍物 0 0 -->需要停止
				if(red1 == 0&& red2 == 0)
				{
					// 先后退
					move_back();
				}
			}
		}
		
		
		
		void red_control(void)
		{
			red_init();
			red_ctl_led();
		}





  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/715669
推荐阅读
相关标签
  

闽ICP备14008679号