int PWM_left = 6; int left_dir = 7; int PWM_right = 5; int right_dir = 2; int base_motor_speed = 10; int count = 1; int threshold = 940; //地面传感器感知黑色的阈值,大于则是黑色,小于则不是黑色

//沿墙 int red_led = 11; volatile int proximity[8]; unsigned char analogPin[20] = {A8, A9, A10, A11, A0, A1, A2, A3, A4, A5, A6, A7}; int distance = 0; //表示距离,单位毫米 int iCurLeftMotorForwardSpeed = 0, iCurRightMotorForwardSpeed = 0, iCurLeftMotorBackwardSpeed, iCurRightMotorBackwardSpeed = 0; //当前左轮和右轮的速度

void setup() { //初始化电动机相关引脚为输出模式 DDRJ |= 0x0F; DDRK &= ~0x0F; //将PK0-PK3设置为输入模式 initMotor(); //初始化沿墙相关引脚为输出模式 pinMode(red_led, OUTPUT); Serial.begin(57600); }

void loop() { bool leftIsBlack = readGroundSensorsIsBlack(1); bool rightIsBlack = readGroundSensorsIsBlack(2); //若两边同时感应到则转弯,其余直行 if(leftIsBlack && rightIsBlack) { //感应到后先暂停,给予机器人反应机会 MotorStop(); Serial.println("MotorStop"); delay(5000); //停止5秒 //根据计数器确定转弯方向 if(count % 2 == 0) { rightTurn(12); Serial.println("rightTurn"); } else { leftTurn(12); Serial.println("leftTurn"); } count++; //计数器加1 } else { forWard(12); Serial.println("forWard"); }

//沿墙运动 if (readProximityDistance(0) < 15 && readProximityDistance(1) < 15 && readProximityDistance(2) < 15 && readProximityDistance(3) < 15) { //四个传感器同时感应到黑色时停止 MotorStop(); Serial.println("MotorStop"); } else { //根据传感器读数调整左右电机速度 int distance1 = readProximityDistance(7); int distance2 = readProximityDistance(5); Serial.print("distance1:"); Serial.println(distance1); Serial.print("distance2:");
Serial.println(distance2); if((distance1 >= 15 && distance1 < 20) && (distance2 >= 15 && distance2 < 20)) { leftMotorForward(base_motor_speed + 2); rightMotorForward(base_motor_speed); } else if(distance1 >= 20 && distance2 < 20) { leftMotorForward(base_motor_speed); rightMotorForward(base_motor_speed + 1); } else if(distance1 >= 20 && distance2 >= 20) { leftMotorForward(base_motor_speed); rightMotorForward(base_motor_speed + 3); } else if(distance1 < 15 && distance2 > 15) { leftMotorForward(base_motor_speed + 3); rightMotorForward(base_motor_speed); } else if (distance1 < 15 && distance2 < 15) { leftMotorForward(base_motor_speed + 5); rightMotorForward(base_motor_speed); } } delay(10); }

//打开ground LED void groundLEDon(unsigned char lineIndex) { PORTJ &= ~(1 << lineIndex); }

//关闭ground LED void groundLEDoff(unsigned char lineIndex) { PORTJ |= 1 << lineIndex; }

//读取第lineIndex个传感器的值 int readGroundSensor(unsigned char lineIndex) { return analogRead(analogPin[lineIndex]); }

//读出第地面传感器的值 bool readGroundSensorsIsBlack(unsigned char lineIndex) { groundLEDon(lineIndex); delay(1); int val = readGroundSensor(lineIndex); groundLEDoff(lineIndex); Serial.print("Ground Sensor "); Serial.print(lineIndex); Serial.print(" : "); Serial.println(val); if(val > threshold) //检测到黑色 { return true; } else { return false; } }

//初始化电动机相关引脚为输出模式 void initMotor() { pinMode(PWM_right, OUTPUT); pinMode(PWM_left, OUTPUT); pinMode(right_dir, OUTPUT); pinMode(left_dir, OUTPUT); }

//左转 void leftTurn(float duty) { leftMotorForward(0); rightMotorForward(duty); }

//右转 void rightTurn(float duty) { leftMotorForward(duty); rightMotorForward(0); }

//前进 void forWard(float duty) { leftMotorForward(duty); rightMotorForward(duty); }

void MotorStop() { iCurLeftMotorForwardSpeed = 0; iCurRightMotorForwardSpeed = 0; iCurLeftMotorBackwardSpeed = 0; iCurRightMotorBackwardSpeed = 0; digitalWrite(right_dir, HIGH); digitalWrite(PWM_right, HIGH); digitalWrite(left_dir, HIGH); digitalWrite(PWM_left, HIGH); }

//左轮前进,速度duty void leftMotorForward(float duty) { int dutyInt = duty; if(iCurLeftMotorForwardSpeed != dutyInt) { int dutyVal = map(dutyInt, 0, 100, 0, 255); analogWrite(PWM_left, dutyVal); analogWrite(left_dir, 0); iCurLeftMotorForwardSpeed = dutyInt; } }

//右轮前进,速度duty void rightMotorForward(float duty) { int dutyInt = duty; if(iCurRightMotorForwardSpeed != dutyInt) { int dutyVal = map(dutyInt, 0, 100, 0, 255); analogWrite(PWM_right, dutyVal); analogWrite(right_dir, 0); iCurRightMotorForwardSpeed = dutyInt; } }

//左轮后退,速度duty void leftMotorBackward(float duty) { int dutyInt = duty; if(iCurLeftMotorBackwardSpeed != dutyInt) { int dutyVal = map(dutyInt, 0, 100, 0, 200);
analogWrite(PWM_left, 0); analogWrite(left_dir, dutyVal); iCurLeftMotorBackwardSpeed = dutyInt; } }

//右轮后退,速度duty void rightMotorBackward(float duty) { int dutyInt = duty; if(iCurRightMotorBackwardSpeed != dutyInt) { int dutyVal = map(dutyInt, 0, 100, 0, 200); analogWrite(PWM_right, 0); analogWrite(right_dir, dutyVal); iCurRightMotorBackwardSpeed = dutyInt; } }

//打开沿墙LED voi

这是关于沿墙的独立代码:int red_led = 11;volatile int proximity8 ;unsigned char analogPin8 = A0A1A2A3A4A5A6A7;int PWM_left = 6;int left_dir = 7;int PWM_right = 5;int right_dir = 2;volatile int base_speed = 12;voi

原文地址: https://www.cveoy.top/t/topic/fHRn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录