Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <AFMotor.h>
- //#include <NewPing.h>
- #include "SR04.h"
- #include <IRremote.h>
- //SD module settings
- File myFile;
- const int8_t DISABLE_CHIP_SELECT = 10;
- int pinCS = 53; // Pin komunikacji z karta SD
- //Sensors settings
- int LEFT_SENSOR_TRIG = A2;
- int LEFT_SENSOR_ECHO = A3;
- int FRONT_SENSOR_TRIG = A0;
- int FRONT_SENSOR_ECHO = A1;
- SR04 sr04_left = SR04(LEFT_SENSOR_ECHO, LEFT_SENSOR_TRIG);
- SR04 sr04_front = SR04(FRONT_SENSOR_ECHO, FRONT_SENSOR_TRIG);
- //Remote controll settings
- int RECV_PIN = 40;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- //DC motors settings
- AF_DCMotor motor1(1, MOTOR12_1KHZ);
- AF_DCMotor motor2(3, MOTOR34_1KHZ);
- int NO_OBSTACLE = 0;
- int OBSTACLE = 1;
- //PID
- const float Kp_wall = 20;
- const float Ki_wall = 0.003;
- const float Kd_wall = 9;
- const float Kp_front = 30;
- const float Ki_front = 0;
- const float Kd_front = 15;
- const int offset = 12; // target distance
- const int max_front_distance = 16; // max front distanc
- int Tp = 128; // target power
- float error_wall = 0;
- float lastError_wall = 0;
- int correction_wall = 0;
- float p_wall = 0;
- float i_wall = 0;
- float d_wall = 0;
- int Wall = 0;
- int Front = 0;
- float error_front = 0;
- float lastError_front = 0;
- float correction_front = 0;
- float p_front = 0;
- float i_front = 0;
- float d_front = 0;
- int leftMotorSpeed = 0;
- int rightMotorSpeed = 0;
- int front_status = 0;
- //int rangeFrontObstacle = 0;
- //int rangeWallObstacle = 0;
- //int rangeFront = 0;
- //int rangeWall = 0;
- //int speedSet = 0;
- //0 - true; 1 - false
- int rightTurnBegin = 0;
- int leftTurnBegin = 0;
- int straightLineBegin = 0;
- int speedSet = 0;
- void setup() {
- pinMode(RECV_PIN, INPUT);
- irrecv.enableIRIn();
- Serial.begin(9600);
- pinMode(pinCS, OUTPUT);
- delay(5000);
- if (SD.begin())
- {
- Serial.println("SD card is ready to use.");
- } else
- {
- Serial.println("SD card initialization failed");
- return;
- }
- }
- void loop() {
- beginning:
- myFile = SD.open("DaneMapy.txt", FILE_WRITE);
- if (irrecv.decode(&results))
- {
- switch (results.value)
- {
- case 0xFF22DD: //vol-
- Serial.print("falling back");
- fall_back();
- break;
- case 0xFF02FD: //vol+
- drive_forward();
- break;
- case 0xFFC23D: //play/pause
- motor_stop();
- break;
- }
- irrecv.resume();
- }
- //checks if there is obstacle in front
- front_status = GET_FRONT_STATUS();
- if (front_status == OBSTACLE) {
- if (leftTurnBegin == 0) {
- zero_wall();
- }
- followWall_front();
- //changing turn flag until next obstacle
- rightTurnBegin = 1;
- straightLineBegin = 0;
- goto beginning;
- }
- followWall();
- }
- void followWall() {
- //When robot drives straight (until there is no obstacle) changing flag to true
- rightTurnBegin = 0;
- Wall = sr04_left.Distance();
- error_wall = Wall - offset;
- i_wall = (i_wall + error_wall);
- d_wall = (error_wall - lastError_wall);
- correction_wall = Kp_wall * error_wall + Ki_wall * i_wall + Kd_wall * d_wall;
- //if error is less then 10, it means that the robot follow the wall correctly
- if (error_wall < 10) {
- if (straightLineBegin == 0) {
- zero_wall();
- }
- if (correction_wall > 127 && correction_wall > 0) {
- correction_wall = 127;
- }
- if (correction_wall < -127 && correction_wall < 0) {
- correction_wall = -127;
- }
- rightMotorSpeed = Tp + correction_wall;
- leftMotorSpeed = Tp - correction_wall;
- leftTurnBegin = 0;
- straightLineBegin = 1;
- } else {
- //if error if greater then 10, robot has to turn right
- if (leftTurnBegin == 0) {
- zero_wall();
- zero_front();
- }
- //PID left turn
- int speed = 2.5 * error_wall + 8 * d_wall;
- if (speed > 127 && speed > 0) {
- speed = 127;
- }
- if (speed < -127 && speed < 0) {
- speed = -127;
- }
- rightMotorSpeed = Tp + (speed);
- leftMotorSpeed = Tp - (speed);
- leftTurnBegin = 1;
- straightLineBegin = 0;
- }
- motor1.setSpeed(rightMotorSpeed);
- motor1.run(FORWARD);
- motor2.setSpeed(leftMotorSpeed);
- motor2.run(FORWARD);
- lastError_wall = error_wall;
- zero_front();
- }
- void zero_wall(void) {
- i_wall = 0;
- d_wall = 0;
- lastError_wall = 0;
- }
- void zero_front(void) {
- i_front = 0;
- d_front = 0;
- lastError_front = 0;
- }
- void followWall_front()
- {
- Front = sr04_front.Distance();
- error_front = (Front - max_front_distance);
- i_front = (i_front + error_front);
- d_front = (error_front - lastError_front);
- correction_front = Kp_front * error_front + Ki_front * i_front + Kd_front * d_front;
- if (correction_front > 127 && correction_front > 0) {
- correction_front = 127;
- }
- if (correction_front < -127 && correction_front < 0) {
- correction_front = -127;
- }
- rightMotorSpeed = Tp + correction_front;
- leftMotorSpeed = Tp - correction_front;
- motor1.setSpeed(rightMotorSpeed);
- motor1.run(FORWARD);
- motor2.setSpeed(leftMotorSpeed);
- motor2.run(FORWARD);
- lastError_front = error_front;
- }
- void drive_forward() {
- motor1.setSpeed(128);
- motor2.setSpeed(128);
- motor1.run(FORWARD);
- motor2.run(FORWARD);
- delay(400);
- }
- //zatrzymuje robota na 5 sekund
- void motor_stop() {
- motor1.run(RELEASE);
- motor2.run(RELEASE);
- delay(5000);
- }
- //wycofanie gdy robot wjedzie w przeszkode
- void fall_back() {
- motor1.setSpeed(128);
- motor2.setSpeed(128);
- motor1.run(BACKWARD);
- motor2.run(BACKWARD);
- delay(500);
- motor1.run(BACKWARD);
- motor2.run(FORWARD);
- delay(400);
- Serial.print("falling back");
- Serial.println();
- }
- int GET_LEFT_STATUS(void) {
- if (sr04_left.Distance() < offset)
- return OBSTACLE;
- else
- return NO_OBSTACLE;
- }
- int GET_FRONT_STATUS(void) {
- if (sr04_front.Distance() < max_front_distance)
- return OBSTACLE;
- else
- return NO_OBSTACLE;
- }
Advertisement
Add Comment
Please, Sign In to add comment