赞
踩
原理图:
直流电机的原理:
通电就会转, 反向通电就会反转
两极的电势差 决定转速
电机驱动模块 以及 接线
参考图示
代码实现:
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
#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)循迹
红外对管要垂直于地面进行安装
主要是利用不同颜色的物体对于光线的反射和吸收程度不一样
白色物体:能够反射大量的光线,吸收的比较少
黑色物体:能够吸收大量的光线,反射的比较少
--》
地面(白色):红外接收管能够接收到反射回来的光线
轨迹(黑色):红外接收管就收不到红外光线了
原理:
当红外对管处于白色地面时,红外接收管能够收到反射光线,
此时模块OUT引脚为 低电平0, 红外主控板对应的led灯就会点亮,
连接到32芯片上的对应的GPIO引脚 为低电平0
当红外对管处于黑色轨迹时,红外接收管接收不到反射光线,
此时模块OUT引脚为 高电平1, 红外主控板对应的led灯就会熄灭,
连接到32芯片上的对应的GPIO引脚 为高电平1
//前进 -- 控速 //举例如下 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(让惯性带动车子转动) } }
避障原理:
把红外对管平行于地面进行安装
主要利用空气对于光线有一定削弱作用
当前面有障碍物时,红外接收管能够收到大量的红外光线,红外模块OUT引脚为 低电平0
当前面没有障碍物时,红外接收管接收不到红外光线,红外模块OUT引脚为 高电平1
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
#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(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。