maze solving robots Arduino

Typically: "How do I... ", "How can I... " questions
Post Reply
Ellio45
Posts: 2
Joined: 27 Nov 2023, 09:33

maze solving robots Arduino

Post by Ellio45 »

Hello everyone, I'm a beginner in Arduino coding and one of my student project is to control a e-puck robot ( composed of 6 distance sensor and a light sensor ) in the coppeliasim software. The robot start on a red square and has to find a black square in a random maze while avoiding walls. When the robot find the black square it has to find the shortest/fastest way to return on the red square .

Here is the beginning of the code given by my teacher :

Code: Select all

#define LIGHT_SENSOR_PIN A0

#define BLACK_THRESHOLD 100 // Adjust this value based on the sensor characteristics

#define PROX_SENSOR_L_PIN A1

#define PROX_SENSOR_R_PIN A2

#define PROX_SENSOR_FL_PIN A3

#define PROX_SENSOR_FR_PIN A4

#define PROX_SENSOR_RL_PIN 6

#define PROX_SENSOR_RR_PIN 12

#define PROX_SENSOR_DL_PIN A5

#define PROX_SENSOR_DR_PIN 9

#define STOP_TIME 200

#define MOTOR_RF_PIN 2

#define MOTOR_RB_PIN 4

#define MOTOR_R_SPEED 3

#define MOTOR_LF_PIN 7

#define MOTOR_LB_PIN 8

#define MOTOR_L_SPEED 5


void hardware_setup() {

new DCMotor_Hbridge(MOTOR_RF_PIN, MOTOR_RB_PIN, MOTOR_R_SPEED, "ePuck_rightJoint", 2.5, 3 * 3.14159, 1);

new DCMotor_Hbridge(MOTOR_LF_PIN, MOTOR_LB_PIN, MOTOR_L_SPEED, "ePuck_leftJoint", 2.5, 3 * 3.14159, 1);

new VisionSensor(LIGHT_SENSOR_PIN, "ePuck_lightSensor", 0.1);

new ProximitySensor(PROX_SENSOR_FL_PIN, "ePuck_proxSensor3", 0.1, 1);

new ProximitySensor(PROX_SENSOR_FR_PIN, "ePuck_proxSensor4", 0.1, 1);

new ProximitySensor(PROX_SENSOR_L_PIN, "ePuck_proxSensor1", 0.1, 1);

new ProximitySensor(PROX_SENSOR_R_PIN, "ePuck_proxSensor6", 0.1, 1);

new ProximitySensor(PROX_SENSOR_RL_PIN, "ePuck_proxSensor7", 0.1, 1);

new ProximitySensor(PROX_SENSOR_RR_PIN, "ePuck_proxSensor8", 0.1, 1);

new ProximitySensor(PROX_SENSOR_DL_PIN, "ePuck_proxSensor2", 0.1, 1);

new ProximitySensor(PROX_SENSOR_DR_PIN, "ePuck_proxSensor5", 0.1, 1);

}
I started to complete the arduino code , currently my robot can find the black square but it movement are completely random :

Code: Select all

#define LIGHT_SENSOR_PIN A0
#define BLACK_THRESHOLD 100 // Adjust this value based on your sensor characteristics
#define PROX_SENSOR_L_PIN A1
#define PROX_SENSOR_R_PIN A2
#define PROX_SENSOR_FL_PIN A3
#define PROX_SENSOR_FR_PIN A4
#define PROX_SENSOR_RL_PIN 6
#define PROX_SENSOR_RR_PIN 12
#define PROX_SENSOR_DL_PIN A5
#define PROX_SENSOR_DR_PIN 9
#define STOP_TIME 200 
#define MOTOR_RF_PIN 2
#define MOTOR_RB_PIN 4
#define MOTOR_R_SPEED 3
#define MOTOR_LF_PIN 7
#define MOTOR_LB_PIN 8
#define MOTOR_L_SPEED 5
bool lightSensorActive = true;
unsigned long blackDetectionStartTime = 0;

void hardware_setup() {
  new DCMotor_Hbridge(MOTOR_RF_PIN, MOTOR_RB_PIN, MOTOR_R_SPEED, "ePuck_rightJoint", 2.5, 3 * 3.14159, 1);
  new DCMotor_Hbridge(MOTOR_LF_PIN, MOTOR_LB_PIN, MOTOR_L_SPEED, "ePuck_leftJoint", 2.5, 3 * 3.14159, 1);

  new VisionSensor(LIGHT_SENSOR_PIN, "ePuck_lightSensor", 0.1);

  new ProximitySensor(PROX_SENSOR_FL_PIN, "ePuck_proxSensor3", 0.1, 1);
  new ProximitySensor(PROX_SENSOR_FR_PIN, "ePuck_proxSensor4", 0.1, 1);
  new ProximitySensor(PROX_SENSOR_L_PIN, "ePuck_proxSensor1", 0.1, 1);
  new ProximitySensor(PROX_SENSOR_R_PIN, "ePuck_proxSensor6", 0.1, 1);
  new ProximitySensor(PROX_SENSOR_RL_PIN, "ePuck_proxSensor7", 0.1, 1);
  new ProximitySensor(PROX_SENSOR_RR_PIN, "ePuck_proxSensor8", 0.1, 1);
  new ProximitySensor(PROX_SENSOR_DL_PIN, "ePuck_proxSensor2", 0.1, 1);
  new ProximitySensor(PROX_SENSOR_DR_PIN, "ePuck_proxSensor5", 0.1, 1);
}

void setup() {
  Serial.begin(4800);

  pinMode(MOTOR_RF_PIN, OUTPUT);
  pinMode(MOTOR_RB_PIN, OUTPUT);
  pinMode(MOTOR_R_SPEED, OUTPUT);
  pinMode(MOTOR_LF_PIN, OUTPUT);
  pinMode(MOTOR_LB_PIN, OUTPUT);
  pinMode(MOTOR_L_SPEED, OUTPUT);

  // Set speed to max
  analogWrite(MOTOR_R_SPEED, 255);
  analogWrite(MOTOR_L_SPEED, 255);

}

void stopRobot() {
  digitalWrite(MOTOR_RF_PIN, LOW);
  digitalWrite(MOTOR_RB_PIN, LOW);
  digitalWrite(MOTOR_LF_PIN, LOW);
  digitalWrite(MOTOR_LB_PIN, LOW);
  delay(3000);
}

void avancer(){
  digitalWrite(MOTOR_RF_PIN, HIGH);
  digitalWrite(MOTOR_RB_PIN, LOW);
  digitalWrite(MOTOR_LF_PIN, HIGH);
  digitalWrite(MOTOR_LB_PIN, LOW);
  delay(20);
}

void Tdroite(){
  analogWrite(MOTOR_RF_PIN, 20);
  analogWrite(MOTOR_RB_PIN, 150);
  analogWrite(MOTOR_LF_PIN, 150);
  analogWrite(MOTOR_LB_PIN, 0);
  delay(20);
}

void Tgauche(){
  analogWrite(MOTOR_RF_PIN, 150);
  analogWrite(MOTOR_RB_PIN, 0);
  analogWrite(MOTOR_LF_PIN, 20);
  analogWrite(MOTOR_LB_PIN, 150);
  delay(20);
}

void gauche(){
  digitalWrite(MOTOR_RF_PIN, HIGH);
  digitalWrite(MOTOR_RB_PIN, LOW);
  digitalWrite(MOTOR_LF_PIN, LOW);
  digitalWrite(MOTOR_LB_PIN, HIGH);
  delay(20);
}

void droite(){
  digitalWrite(MOTOR_RF_PIN, LOW);
  digitalWrite(MOTOR_RB_PIN, HIGH);
  digitalWrite(MOTOR_LF_PIN, HIGH);
  digitalWrite(MOTOR_LB_PIN, LOW);
  delay(20);
}


void checkBlackCase() {
int lightSensorValue = analogRead(LIGHT_SENSOR_PIN);
  if (lightSensorActive == true) {

    if (lightSensorValue < BLACK_THRESHOLD) {
      // Robot is on a black case
      if (blackDetectionStartTime == 0) {
        // Start the timer
        blackDetectionStartTime = millis();
      } else {
        // Check if black has been detected for the specified time
        if (millis() - blackDetectionStartTime >= STOP_TIME) {
          stopRobot();
          lightSensorActive = false;
        }
      }
    } else {
      // Reset the timer if not on a black case
      blackDetectionStartTime = 0;
    }
  }  
}


void loop() {
int captFL = digitalRead(PROX_SENSOR_FL_PIN);
int captFR = digitalRead(PROX_SENSOR_FR_PIN);
int captL = digitalRead(PROX_SENSOR_L_PIN);
int captR = digitalRead(PROX_SENSOR_R_PIN);
int captRL = digitalRead(PROX_SENSOR_RL_PIN);
int captRR = digitalRead(PROX_SENSOR_RR_PIN);
int captDL = digitalRead(PROX_SENSOR_DL_PIN);
int captDR = digitalRead(PROX_SENSOR_DR_PIN);
int captNOIR = digitalRead(LIGHT_SENSOR_PIN);



if( captFL == 0 && captFR == 0){
  avancer();
}

if( captFL == 1 || captDL == 1 || captL == 1 && captDR == 0 || captR == 0 || captFR == 0 ){
  Tdroite();
}

if( captFL == 0 || captDL == 0 || captL == 0 && captDR == 1 || captR == 1 || captFR == 1 ){
  Tgauche();
}

if (captFR == 1){
  gauche();
}
if ( captFL == 1 ){
  droite();
}


checkBlackCase();

}
}
I hope my description was clear , i'm open to any help and advice

thanks :slight_smile:

coppelia
Site Admin
Posts: 10375
Joined: 14 Dec 2012, 00:25

Re: maze solving robots Arduino

Post by coppelia »

Hello,

I think your code is based on a specific CoppeliaSim scene, and a specific c++ code template, both of which we didn't develop nor are aware of.
Can you post a link to both so that we have a better understanding on the environment and task?

Cheers

Ellio45
Posts: 2
Joined: 27 Nov 2023, 09:33

Re: maze solving robots Arduino

Post by Ellio45 »

Hi, thanks for your reply the exercise is based on the following maze ( this maze is an example it size and the position of the black and red square can change ).
small_maze : https://drive.google.com/file/d/1OUETjd ... drive_link

The robot used is the e-puck

The piece of skecth we have to complete is the following one : https://drive.google.com/file/d/1OUETjd ... drive_link

As a reminder the goal is for the robot to find the black square as fast as possible while avoiding walls and when the black case is find it has to return as fast as possible to the red square.

Currently i only manage to make to robot "bounce" on wall , but the amount of time to find the black case is random and not futurproof.

Thanks :)

Post Reply