Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int trigRight = 4;
- const int echoRight = 2;
- int time1,d=1;
- const int trigLeft = 12;
- const int echoLeft = 13;
- long right, left, front;
- const int trigFront = 8;
- const int echoFront = 7;
- //const int FRight =
- int dem=1;
- int controlLeftWheel = 9;
- int const in1 = 6;
- int const in2 = 11;
- int controlRightWheel = 10;
- int const in3 = 3;
- int const in4 = 5;
- float Kp=40, Ki=0 , Kd=185;
- float error=0, P=0, I=0, D=0, PID=0;
- float previous_error=0;
- int sensor[5]={0, 0, 0, 0, 0};
- int tdd=190;
- int a=1, b=0;
- void park1(void);
- void read_sensor_values(void);
- void calculate_pid(void);
- void motor_control(void);
- void setup()
- {
- Serial.begin(9600);
- pinMode(trigLeft, OUTPUT);
- pinMode(echoLeft, INPUT);
- pinMode(trigRight, OUTPUT);
- pinMode(echoRight, INPUT);
- pinMode(trigFront, OUTPUT);
- pinMode(echoFront, INPUT);
- pinMode(controlLeftWheel,OUTPUT); //PWM Pin 1
- pinMode(controlRightWheel,OUTPUT); //PWM Pin 2
- pinMode(in1,OUTPUT); //Left Motor Pin 1
- pinMode(in2,OUTPUT); //Left Motor Pin 2
- pinMode(in3,OUTPUT); //Right Motor Pin 1
- pinMode(in4,OUTPUT); //Right Motor Pin 2
- }
- void loop()
- {
- read_sensor_values();
- //if ((error == previous_error)&&(dem==1)) {park1(); dem++;}
- // read_sensor_values();
- calculate_pid();
- motor_control();
- // front = getDistance(trigFront, echoFront);
- Serial.println(d);
- }
- //------------------------------------------------------------------------------------------
- //MAZE
- void moveStop() {
- analogWrite(controlLeftWheel, 0);
- analogWrite(controlRightWheel, 0);
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, LOW);
- }
- void moveForward() {
- analogWrite(controlLeftWheel, 100);
- analogWrite(controlRightWheel, 100);
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- }
- void moveBack() {
- analogWrite(controlLeftWheel, 100);
- analogWrite(controlRightWheel, 100);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- }
- int getDistance(int trig, int echo) {
- /* Phát xung từ chân trig */
- int distance; // biến đo khoảng cách
- int duration; // biến đo thoời gian
- digitalWrite(trig,0); // tắt chân trig
- delayMicroseconds(2);
- digitalWrite(trig,1); // phát xung từ chân trig
- delayMicroseconds(5); // xung có độ dài 5 microSeconds
- digitalWrite(trig,0); // tắt chân trig
- duration = pulseIn(echo,tdd);
- distance = (int) (duration/2/29.412);
- //Serial.println(distance);
- return distance;
- delay(5);
- }
- void stepForward(int time, int td) {
- moveStop();
- analogWrite(controlLeftWheel, td);
- analogWrite(controlRightWheel, td);
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- delay(time);
- moveStop();
- delay(500);
- }
- void stepBack(int time, int td) {
- moveStop();
- analogWrite(controlLeftWheel, td);
- analogWrite(controlRightWheel, td);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- delay(time);
- moveStop();
- delay(500);
- }
- void readsensor(){
- front = getDistance(trigFront, echoFront);
- left = getDistance(trigLeft, echoLeft);
- right = getDistance( trigRight, echoRight);
- delay(5);
- // Serial.print( left);
- // Serial.print(' ');
- // Serial.print( front);
- // Serial.print(' ');
- // Serial.println( right);
- //
- }
- void turnRightRotation() {
- moveStop();
- analogWrite(controlLeftWheel, 150);
- analogWrite(controlRightWheel, 150);
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- delay(350);
- moveStop();
- //delay(500);
- }
- void turnLeftRotation() {
- moveStop();
- analogWrite(controlLeftWheel, 150);
- analogWrite(controlRightWheel, 150);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- delay(350);
- moveStop();
- //delay(500);
- }
- void turnBackRotation() {
- moveStop();
- analogWrite(controlLeftWheel, 150);
- analogWrite(controlRightWheel, 150);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- delay(500);
- moveStop();
- delay(500);
- }
- //-------------------------------------------------------------------------------------------------------------
- //PID-LINE
- void park1()
- {
- // while (error == 5){
- analogWrite(controlLeftWheel,100);
- analogWrite(controlRightWheel,100);
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- delay(1500);
- read_sensor_values();
- // }
- // if (error == -5){
- turnRightRotation();
- // stepBack(10);
- // stepForward(500);
- //break;
- // }
- }
- void read_sensor_values()
- {
- sensor[0]=digitalRead(A4);
- sensor[1]=digitalRead(A3);
- sensor[2]=digitalRead(A2);
- sensor[3]=digitalRead(A1);
- sensor[4]=digitalRead(A0);
- Serial.print(sensor[0]);
- Serial.print(sensor[1]);
- Serial.print(sensor[2]);
- Serial.print(sensor[3]);
- Serial.println(sensor[4]);
- if((sensor[0]==a)&&(sensor[1]==a)&&(sensor[2]==a)&&(sensor[3]==a)&&(sensor[4]==b))
- error=4;
- else if((sensor[0]==a)&&(sensor[1]==a)&&(sensor[2]==a)&&(sensor[3]==b)&&(sensor[4]==b))
- error=3;
- else if((sensor[0]==a)&&(sensor[1]==a)&&(sensor[2]==a)&&(sensor[3]==b)&&(sensor[4]==a))
- error=2;
- else if((sensor[0]==a)&&(sensor[1]==a)&&(sensor[2]==b)&&(sensor[3]==b)&&(sensor[4]==a))
- error=1;
- else if((sensor[0]==a)&&(sensor[1]==a)&&(sensor[2]==b)&&(sensor[3]==a)&&(sensor[4]==a))
- error=0;
- else if((sensor[0]==a)&&(sensor[1]==b)&&(sensor[2]==b)&&(sensor[3]==a)&&(sensor[4]==a))
- error=-1;
- else if((sensor[0]==a)&&(sensor[1]==b)&&(sensor[2]==a)&&(sensor[3]==a)&&(sensor[4]==a))
- error=-2;
- else if((sensor[0]==b)&&(sensor[1]==b)&&(sensor[2]==a)&&(sensor[3]==a)&&(sensor[4]==a))
- error=-3;
- else if((sensor[0]==b)&&(sensor[1]==a)&&(sensor[2]==a)&&(sensor[3]==a)&&(sensor[4]==a))
- error=-4;
- else if((sensor[0]==a)&&(sensor[1]==a)&&(sensor[2]==a)&&(sensor[3]==a)&&(sensor[4]==a))
- error=previous_error;
- else if((sensor[0]==b)&&(sensor[1]==b)&&(sensor[2]==b)&&(sensor[3]==b)&&(sensor[4]==b))
- error=previous_error;
- else if((sensor[0]==b)&&(sensor[1]==b)&&(sensor[2]==b)&&(sensor[3]==a)&&(sensor[4]==a))
- nhien();
- else if((sensor[0]==a)&&(sensor[1]==a)&&(sensor[2]==b)&&(sensor[3]==b)&&(sensor[4]==b))
- nhien();
- // else if((sensor[0]==a)&&(sensor[1]==b)&&(sensor[2]==b)&&(sensor[3]==b)&&(sensor[4]==b))
- // nhien();
- // else if((sensor[0]==b)&&(sensor[1]==b)&&(sensor[2]==b)&&(sensor[3]==b)&&(sensor[4]==a))
- // nhien();
- //Serial.println(error);
- }
- void nhien()
- {
- moveStop(); delay(50);
- if (getDistance(trigFront, echoFront) < 15)
- {
- d++;
- Serial.println("Chan");
- Serial.println(getDistance(trigFront, echoFront));
- moveStop();
- delay(5000);
- }
- else {
- stepForward(100,120);
- }
- // if (d==2){ moveStop();
- // delay(100);
- // turnRightRotation();
- // stepForward(1000,120);
- // turnLeftRotation();
- // stepForward(400,120);
- // turnLeftRotation();
- // stepForward(1000,120);
- // d++;
- // }
- // if (d==5){moveStop();
- // delay(10000000);}
- }
- void calculate_pid()
- {
- P = error;
- I = I + error;
- D = error - previous_error;
- PID = (Kp*P) + (Ki*I) + (Kd*D);
- previous_error=error;
- // Serial.print(sensor[0]);
- // Serial.print(sensor[1]);
- // Serial.print(sensor[2]);
- // Serial.print(sensor[3]);
- // Serial.println(sensor[4]);
- // Serial.println(error);
- // Serial.println( PID );
- // Serial.println( PID );
- }
- void motor_control()
- { // readsensor();
- // if (front < 10) stepBack(3000,10);
- // if (getDistance(trigFront, echoFront) < 15) stepForward(1000,40);
- // Calculating the effective motor speed:
- int leftspeed = tdd+PID;
- int rightspeed = tdd-PID;
- // The motor speed should not exceed the max PWM value
- //constrain(leftspeed,0,150);
- //constrain(rightspeed,0,150);
- if (rightspeed < 0)
- rightspeed = 0;
- if (rightspeed > tdd)
- rightspeed = tdd;
- if (leftspeed<0)
- leftspeed = 0;
- if (leftspeed > tdd)
- leftspeed = tdd;
- //Serial.print(rightspeed);
- // Serial.print(' ');
- // Serial.println(leftspeed);
- // readsensor();
- // if (front < 15) { moveStop();}
- analogWrite(controlRightWheel, rightspeed);
- analogWrite(controlLeftWheel, leftspeed);
- digitalWrite(in1,HIGH); // left
- digitalWrite(in2,LOW); // left
- digitalWrite(in3,HIGH); // right
- digitalWrite(in4,LOW); // right
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement