【实操题】
1、(本题100分)
一、主题:
自动跟随小车。
二、器件:
三四级等级考试套件。
三、要求:
(1)现场搭建小车。
(2)小车从发车线出发,向前移动,距离前方障碍物10厘米处暂停。
(3)移动障碍物,随着障碍物的移动,小车与障碍物间的距离始终保持10厘米。
(4)通过超声波传感器进行距离检测。
(5)程序编写采用C语言,不得使用图形化软件编写。
(6)考试结束,考生将代码拷贝上交。
说明:
(1)考场编程环境为:Arduino IDE
(2)实操开始,考生需要先向主控板写入blink程序,经监考老师确认后方可进行小车组装。
(3)小车距离障碍物的距离为超声波传感器前端距离障碍物的距离。
(4)出发前,小车距离障碍物的的距离为20-30cm。
参考程序:
//因电机驱动模块有差别,请自行调试电机
int fd = 30;
int bd = 10;
void setup() {
pinMode(1, OUTPUT); //Echo
pinMode(2, INPUT); //Trig
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
float d = checkdistance();
Serial.println(d);
if ((d >= fd - bd) && (d <= fd + bd)) {
analogWrite(5, 100);
analogWrite(6, 100);
if (d > fd) {
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
forward();
}
else if (d < fd) {
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
backward();
}
else {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
pause();
}
}
else {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
pause();
}
}
float checkdistance() { //超声波测距
digitalWrite(1, LOW);
delayMicroseconds(2);
digitalWrite(1, HIGH);
delayMicroseconds(10);
digitalWrite(1, LOW);
float distance = pulseIn(2, HIGH) / 58.00;
delay(10);
return distance;
}
void forward() { //前进
analogWrite(8, 255);
analogWrite(7, 0);
analogWrite(4, 255);
analogWrite(3, 0);
}
void backward() {//后退
analogWrite(8, 0);
analogWrite(7, 255);
analogWrite(4, 0);
analogWrite(3, 255);
}
void turnleft() {//左转
analogWrite(8, 0);
analogWrite(7, 0);
analogWrite(4, 255);
analogWrite(3, 0);
}
void turnright() {//右转
analogWrite(8, 255);
analogWrite(7, 0);
analogWrite(4, 0);
analogWrite(3, 0);
}
void pause() {//停止
analogWrite(8, 0);
analogWrite(7, 0);
analogWrite(4, 0);
analogWrite(3, 0);
}