Ád
Ád
Ád
h>
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters).
Maximum sensor distance is rated at 400-500cm.
int dir;
#define STOP 0
#define FORWARD 1
#define BACKWARD 2
#define LEFT 3
#define RIGHT 4
float P = 0.7 ;
float D = 0.5 ;
float I = 0.4 ;
float oldErrorP ;
float totalError ;
int offset = 5 ;
int wall_threshold = 13 ;
//int left_threshold = 10 ;
//int right_threshold = 10 ;
int front_threshold = 7 ;
boolean frontwall ;
boolean leftwall ;
boolean rightwall ;
boolean first_turn ;
boolean rightWallFollow ;
boolean leftWallFollow ;
int en1 = 2 ;
int en2 = 3 ;
int en3 = 4 ;
int en4 = 5 ;
int enA = 10 ;
int enB = 11 ;
int baseSpeed = 70 ;
int RMS ;
int LMS ;
int LED = 13 ;
int led1 = 8 ;
int led2 = 9 ;
unsigned int pingSpeed = 30; // How frequently are we going to send out a ping (in
milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer; // Holds the next ping time.
//int TestNUM = 1 ;
void setup() {
first_turn = false ;
rightWallFollow = false ;
leftWallFollow = false ;
void loop() {
//========================================START====================================
====//
ReadSensors();
walls();
if ( first_turn == false ) {
pid_start();
}
else if (leftWallFollow == true ) {
PID(true) ;
}
else if (rightWallFollow == true ) {
PID(false) ;
}
// turnright();
PID(false) ;
if ( first_turn == false ) {
first_turn = true ;
rightWallFollow = true ;
digitalWrite(led2 , LOW );
digitalWrite(led1 ,HIGH );
}
}
if (leftwall == false && rightwall == true && frontwall == true ) {
// turnleft();
PID(true) ;
if ( first_turn == false ) {
first_turn = true ;
leftWallFollow = true ;
digitalWrite(LED , HIGH);
}
}
if ( leftSensor == 0 || leftSensor > 100 && rightSensor == 0 || rightSensor >
100 && frontSensor == 0 || frontSensor > 100 ) {
setDirection(STOP);
}
Serial.print("error=");
Serial.println(totalError);
if ( dir == FORWARD ) {
digitalWrite(en1, LOW); // Left wheel forward
digitalWrite(en2, HIGH);
digitalWrite(en3, LOW); // Right wheel forward
digitalWrite(en4, HIGH);
}
else if ( dir == LEFT ) {
digitalWrite(en1, HIGH); // Left wheel forward
digitalWrite(en2, LOW );
digitalWrite(en3, LOW ); // Right wheel forward
digitalWrite(en4, HIGH);
}
else if ( dir == RIGHT ) {
digitalWrite(en1, LOW); // Left wheel forward
digitalWrite(en2, HIGH);
digitalWrite(en3, HIGH); // Right wheel forward
digitalWrite(en4, LOW);
}
else if ( dir == STOP ) {
digitalWrite(en1, HIGH); // Left wheel forward
digitalWrite(en2, HIGH );
digitalWrite(en3, HIGH ); // Right wheel forward
digitalWrite(en4, HIGH);
}
else if ( dir == BACKWARD ) {
digitalWrite(en1, HIGH ); // Left wheel forward
digitalWrite(en2, LOW );
digitalWrite(en3, HIGH ); // Right wheel forward
digitalWrite(en4, LOW );
}
}
//---------------------------------------------------------------------------//
void ReadSensors() {
//leftSensor = sonarLeft.convert_cm(leftSensor);
//rightSensor = sonarRight.convert_cm(rightSensor);
//frontSensor = sonarFront.convert_cm(frontSensor);
//---------------------------------------------------------------------------//
void pid_start() {
oldErrorP = errorP ;
if (RMS < 0) {
setDirection(RIGHT);
}
else if (LMS < 0) {
LMS = map(LMS , 0 , -255, 0, 255);
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(LEFT);
}
else {
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(FORWARD);
}
if (left == true ) {
oldErrorP = errorP ;
if (RMS < 0) {
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(RIGHT);
}
else if (LMS < 0) {
LMS = map(LMS , 0 , -255, 0, 255);
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(LEFT);
}
else {
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(FORWARD);
}
}
else {
oldErrorP = errorP ;
if (RMS < 0) {
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(RIGHT);
}
else if (LMS < 0) {
LMS = map(LMS , 0 , -255, 0, 255);
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(LEFT);
}
else {
analogWrite(enA , RMS);
analogWrite(enB , LMS);
setDirection(FORWARD);
}
void walls() {
//---------------------------------------------------------------------------//
void turnright() {
LMS = baseSpeed ;
//---------------------------------------------------------------------------//
void turnleft() {
RMS = baseSpeed ;
LMS = RMS * leftSensor / ( leftSensor + 11 ) ;
//---------------------------------------------------------------------------//