- /**
- * @par Copyright (C): 2010-2019, Shenzhen Yahboom Tech
- * @file main.c
- * @author liusen
- * @version V1.0
- * @date 2017.07.17
- * @brief 主函数
- * @details
- * @par History
- *
- * version: liusen_20170717
- */
- //本实验对环境的要求较高,需要用户自行调整循迹模块的灵敏度
- //灰度传感器在不同的光照条件下对不同的颜色有不同的数值
- #include "stm32f10x.h"
- #include "app_motor.h"
- #include "app_ultrasonic.h"
- #include "bsp.h"
- #include "sys.h"
- #include "delay.h"
- #include "app_buzzer.h"
- #include "bsp_linewalking.h"
- #include "bsp_colorful.h"
- #define LOW (0)
- #define HIGH (1)
- //第二位 1:左90 ; 2: 右90; 3: 180度掉头 4:直行
- //这里可以自定义路径规划, 小车放置在起始位置深圳, 根据下一个十字或者丁字路口来写 如上行注释来左转 、右转 、直行 、掉头。
- char mapLocation[38][2] = {
- {0, 2}, {1, 1}, {2, 2}, {3, 3},
- {4, 2}, {5, 4}, {6, 3},
- {7, 2}, {8, 2}, {9, 2}, {10, 3},
- {11, 4}, {12, 4}, {13, 3},
- {14, 2}, {15, 1}, {16, 1}, {17, 3},
- {18, 2}, {19, 1}, {20, 3},
- {21, 1}, {22, 1}, {23, 1}, {24, 2}, {25, 3},
- {26, 2}, {27, 2}, {28, 3},
- {29, 2}, {30, 2}, {31, 3},
- {32, 2}, {33, 1}, {34, 3},
- {35, 1}, {36, 2}, {37, 3}
- };
- int position = 0; //位置定义
- /**
- * Function Color_Dis
- * @author liusen
- * @date 2017.08.15
- * @brief 颜色设置根据实际情况采集数据显示
- * @param[in] v_ad : 采集颜色AD
- * @param[out] void
- * @retval void
- * @par History 无
- */
- void Color_Dis(int v_ad)
- {
- int Color = v_ad;
- if (Color >= 370 && Color <= 410)
- {
- bsp_Colorful_RGB_PWM(0, 0, 255);
- }
- else if (Color >= 300 && Color <= 320)
- {
- bsp_Colorful_RGB_PWM(0, 255, 0);
- }
- else if (Color >= 345 && Color <= 365)
- {
- bsp_Colorful_RGB_PWM(255, 0, 0);
- }
- else if (Color >= 330 && Color < 345)
- {
- bsp_Colorful_RGB_PWM(255, 0, 255);
- }
- else if (Color >= 270 && Color <= 290)
- {
- bsp_Colorful_RGB_PWM(0, 255, 255);
- }
- else if (Color >= 250 && Color < 270)
- {
- bsp_Colorful_RGB_PWM(255, 255, 0);
- }
- else
- {
- bsp_Colorful_RGB_PWM(0, 0, 0);
- }
- }
- int main(void)
- {
- int iGs = 0;
- int TrackSensorLeftValue1 = 1, TrackSensorLeftValue2 = 1, TrackSensorRightValue1 = 1, TrackSensorRightValue2 = 1;
- /*外设初始化*/
- bsp_init();
- /*按键启动*/
- while(bsp_GetKEY());
- Buzzer_GPIO_Init();
- while (1)
- {
- /*下面两行主要是测试当前环境光不同颜色的AD值,方便修改上面Color_Dis函数的范围值*/
- //iGs = Get_GS_Value();
- //printf("Color:%d\r\n", iGs);
-
- bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
-
- if ( ((TrackSensorLeftValue1 == LOW || TrackSensorLeftValue2 == LOW)
- && TrackSensorRightValue2 == LOW) || ( TrackSensorLeftValue1 == LOW
- && (TrackSensorRightValue1 == LOW || TrackSensorRightValue2 == LOW)))
- {
- Car_Run(3400);
- delay_ms(60);
- Car_Stop();
- delay_ms(200);
- switch (mapLocation[position][1])
- {
- case 0: Car_Stop(); break;
- case 1: //左转90,这里所说的左转90度其实是会让小车转过一定的角度,然后让下面的while(1)去判断
- //当中间的两路检测到时会停止转动,所以会有一个转动90度的效果.
- {
- /*延迟旋转转过丁字路口黑线解决探头开始就在黑线上判断*/
- Car_SpinLeft(4300, 4300);
- delay_ms(200);
- while (1)
- {
- Car_SpinLeft(4300, 4300);
- bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
- if (TrackSensorLeftValue2 == LOW || TrackSensorRightValue1 == LOW)
- {
- Car_Stop();
- break;
- }
- }
- } break;
- case 2://右转90
- {
- /*延迟旋转转过丁字路口黑线解决探头开始就在黑线上判断*/
- Car_SpinRight(4300, 4300);
- delay_ms(200);
- while (1)
- {
- Car_SpinRight(4300, 4300);
- bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
- if (TrackSensorLeftValue2 == LOW || TrackSensorRightValue1 == LOW)
- {
- Car_Stop();
- break;
- }
- }
-
- } break;
- case 3://左旋180掉头
- {
- Car_Stop();
- delay_ms(200);
- BeepOnOffMode();
-
- iGs = Get_GS_Value();
- //printf("Color:%d\r\n", iGs); //这里可以打开调试数据
- Color_Dis(iGs);
- if (position == 37) //走完地图直接停止
- {
- ModeBEEP(3);
- BeepOnOffMode();
- position = 0;
- while(1);
- }
- Car_SpinLeft(4300, 4300);
- //下面的延时相比上面的而言因为赛道的颜色区域前有一段黑线,延时长就是为了略过这条黑线的检测
- delay_ms(450);
- while (1)
- {
- Car_SpinLeft(4300, 4300);
- bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
- if (TrackSensorLeftValue2 == LOW || TrackSensorRightValue1 == LOW)
- {
- Car_Stop();
- break;
- }
- }
- } break;
- case 4: Car_Run(5000); delay_ms(50); break;//直行
- }
- position++;
- }
-
- // 0 X X X
- //最左边检测到
- else if ( TrackSensorLeftValue1 == LOW)
- {
- Car_SpinLeft(3400, 3400);
- }
- // X X X 0
- //最右边检测到
- else if ( TrackSensorRightValue2 == LOW )
- {
- Car_SpinRight(3400, 3400);
- }
- //四路循迹引脚电平状态
- // X 0 1 X
- //处理左小弯
- else if ( TrackSensorLeftValue2 == LOW && TrackSensorRightValue1 == HIGH)
- {
- Car_Left(3400);
- }
- //四路循迹引脚电平状态
- // X 1 0 X
- //处理右小弯
- else if (TrackSensorLeftValue2 == HIGH && TrackSensorRightValue1 == LOW)
- {
- Car_Right(3400);
- }
- else if (TrackSensorLeftValue2 == LOW && TrackSensorRightValue1 == LOW)
- {
- Car_Run(3400);
- }
- }
-
- }
复制代码
代码下载:
STM32游园赛道.7z
(203.43 KB, 下载次数: 36)
|