Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Grove_I2C_Motor_Driver.h"
- // default I2C address is 0x0f
- #define I2C_ADDRESS 0x0f
- double xStart=0.0;
- double yStart=0.0;
- bool etatCapteurGauche;
- bool etatCapteurCentreD;
- bool etatCapteurCentreG;
- bool etatCapteurDroit;
- const int capteurGauche = 2;//Capteur Gauche
- const int capteurCentreD = 4;//Capteur MilieuD
- const int capteurDroit = 5;//Capteur Droit
- const int capteurCentreG = 3;//Capteur MilieuG
- double xEnd=0.0;
- double yEnd=1.70;
- double vitesseVoiture = 0.47;
- //Création de la classe Point.
- class Point {
- //vitesse moyenne de la voiture = 0,43m/s
- double timeDelay = NULL;
- double averageSpeed = 0.43;
- double distance = NULL;
- //Variables pour le chemin de la voiture
- int previousPoint = NULL;
- int actualPoint = NULL;
- int nextPoint = NULL;
- int finalPoint = NULL;
- private :
- //On créé les attributs x et y qui sont les coordonnées d'un point, et on créé une direction.
- double x,y;
- Point* south;
- Point* west;
- Point* north;
- Point* east;
- public :
- Point(double initializeX, double initializeY){
- this->x=initializeX;
- this->y=initializeY;
- }
- double getX(){
- return this->x;
- }
- double getY(){
- return this->y;
- }
- void setPointAdjacent(Point* nextSouth,Point* nextWest, Point* nextNorth, Point* nextEast){
- if(nextSouth != nullptr){
- this->south = nextSouth;
- }
- if(nextWest != nullptr){
- this ->west = nextWest;
- }
- if (nextNorth != nullptr)
- {
- this->north = nextNorth;
- }
- if (nextEast != nullptr){
- this->east = nextEast;
- }
- }
- };
- //Initialisation de tous les points.
- Point* pA = new Point(0.0,0.0);
- Point* pB = new Point(0.0,1.70);
- Point* pC = new Point(0.0,3.08);
- Point* pD = new Point(0.0,4.24);
- Point* pE = new Point(2.08,4.24);
- Point* pF = new Point(2.08,3.08);
- Point* pG = new Point(2.79,0.92);
- Point* pH = new Point(8.9,3.16);
- Point* pI = new Point(8.9,4.20);
- Point* pJ = new Point(11.50,4.16);
- Point* pK = new Point(11.48,1.59);
- Point* pL = new Point(11.46,0.92);
- Point* pM = new Point(12.8,0.0);
- Point* pN = new Point(12.8,1.58);
- Point* pO = new Point(12.8,4.16);
- Point* pP = new Point(2.79,0.0);
- //Initialisation des points de début et de fin.
- Point* pStart = new Point(xStart, yStart);
- Point* pEnd = new Point(xEnd, yEnd);
- //Initialise notre map
- void map() {
- pA->setPointAdjacent(nullptr,nullptr,pB,pP); //nullptr est une convention qui permet d'initialiser un pointeur à null,et donc qui ne change pas la valeur initiale.
- pB->setPointAdjacent(pA,nullptr,pC,pK);
- pC->setPointAdjacent(pB,nullptr,pO,pF);
- pD->setPointAdjacent(pC,nullptr,nullptr,pE);
- pE->setPointAdjacent(pF,pD,nullptr,pI);
- pF->setPointAdjacent(nullptr,pC,pE,pH);
- pG->setPointAdjacent(pP,nullptr,nullptr,pL);
- pH->setPointAdjacent(nullptr,pF,pI,nullptr);
- pI->setPointAdjacent(pH,pE,nullptr,pJ);
- pJ->setPointAdjacent(pK,pI,nullptr,pO);
- pK->setPointAdjacent(pL,pB,pJ,pN);
- pL->setPointAdjacent(nullptr,pG,pK,nullptr);
- pM->setPointAdjacent(nullptr,pP,pN,nullptr);
- pN->setPointAdjacent(pM,pK,pO,nullptr);
- pO->setPointAdjacent(pN,pJ,nullptr,nullptr);
- pP->setPointAdjacent(nullptr,pA,pG,pM);
- pStart->setPointAdjacent(nullptr,nullptr,pB,pP);
- //pEnd->setPointAdjacent(
- };
- while(actualPoint != finalPoint){
- switch(actualPoint){
- case pA:
- if(previousPoint = pP){
- if(nextPoint = nextNorth){
- turnRight();
- delay(delayTime);
- }
- }
- else if (previousPoint = pB){
- if(nextPoint = nextEast){
- turnLeft();
- }
- }
- else{
- moveForward();
- }
- previousPoint = pA;
- break;
- case pB:
- if(previousPoint = pA){
- if(nextPoint = nextNorth){
- moveForward();
- }
- else if(nextPoint = nextEast){
- turnRight();
- }
- }
- else if(previousPoint = pK){
- if(nextPoint = pC){
- turnRight();
- }
- else if(nextPoint = pA){
- turnLeft();
- }
- }
- else if(previousPoint = pC){
- if(nextPoint = pK){
- turnLeft();
- }
- else if(nextPoint = pA){
- moveForward();
- }
- }
- else if(previousPoint = pK){
- if(nextPoint = pC){
- turnRight();
- }
- else if(nextPoint = pB){
- turnLeft();
- }
- }
- previousPoint = pB;
- break;
- case pC:
- if(previousPoint = pB){
- if(nextPoint = pF){
- turnRight();
- }
- else if(nextPoint = pD){
- moveForward();
- }
- }
- else if(previousPoint = pF){
- if(nextPoint = pD){
- turnRight();
- }
- else if(nextPoint = pB){
- turnLeft();
- }
- }
- else if(previousPoint = pD){
- if(nextPoint = pF){
- turnLeft();
- }
- else if(nextPoint = pB){
- moveForward();
- }
- }
- previousPoint = pC;
- break;
- case pD:
- if(previousPoint = pC){
- if(nextPoint = pE){
- turnRight();
- }
- }
- else(previousPoint = pE){
- if(nextPoint = pC){
- turnLeft();
- }
- }
- previousPoint = pD;
- break;
- case pE:
- if(previousPoint = pD){
- if(nextPoint = pI){
- moveForward();
- }
- else if(nextPoint = pF){
- turnRight();
- }
- }
- else if(previousPoint = pF){
- if(nextPoint = pI){
- turnRight();
- }
- else if(nextPoint = pD){
- turnLeft();
- }
- }
- else if(previousPoint = pI){
- if(nextPoint = pF){
- turnLeft();
- }
- else if(nextPoint = pD){
- moveForward();
- }
- }
- previousPoint = pE;
- break;
- case pF:
- if(previousPoint = pE){
- if(nextPoint = pC){
- turnRight();
- }
- else if(nextPoint = pH){
- turnLeft();
- }
- }
- else if(previousPoint = pH){
- if(nextPoint = pE){
- turnRight();
- }
- else if(nextPoint = pC){
- moveForward();
- }
- }
- else if(previousPoint = pC){
- if(nextPoint = pE){
- turnLeft();
- }
- else if(nextPoint = pH){
- moveForward();
- }
- }
- previousPoint = pF;
- break;
- case pH:
- if(previousPoint = pF){
- if(nextPoint = pI){
- turnLeft();
- }
- }
- else if(previousPoint = pI){
- if(nextPoint = pF){
- turnRight();
- }
- }
- previousPoint = pH;
- break;
- case pI:
- if(previousPoint = pJ){
- if(nextPoint = pH){
- turnLeft();
- }
- else if(nextPoint = pE){
- moveForward();
- }
- }
- else if(previousPoint = pH){
- if(nextPoint = pJ){
- turnRight();
- }
- else if(nextPoint = pE){
- turnLeft();
- }
- }
- else if(previousPoint = pE){
- if(nextPoint = pJ){
- moveForward();
- }
- else if(nextPoint = pH){
- turnRight();
- }
- }
- previousPoint = pI;
- break;
- case pJ:
- if(previousPoint = pI){
- if(nextPoint = pK){
- turnRight();
- }
- else if(nextPoint = pO){
- moveForward();
- }
- }
- else if(previousPoint = pK){
- if(nextPoint = pO){
- turnRight(),
- }
- else if(nextPoint = pI){
- turnLeft();
- }
- }
- else if(previousPoint = pO){
- if(nextPoint = pK){
- turnLeft();
- }
- else if(nextPoint = pI){
- moveForward();
- }
- }
- previousPoint = pJ;
- break;
- case pK:
- if(previousPoint = pJ){
- if(nextPoint = pN){
- turnLeft();
- }
- else if(nextPoint = pL){
- moveForward();
- }
- else if(nextPoint = pB){
- turnRight();
- }
- }
- else if(previousPoint = pN){
- if(nextPoint = pJ){
- turnRight();
- }
- else if(nextPoint = pL){
- turnLeft();
- }
- else if(nextPoint = pB){
- moveForward();
- }
- }
- else if(previousPoint = pL){
- if(nextPoint = pJ){
- moveForward();
- }
- else if(nextPoint = pN){
- turnRight();
- }
- else if(nextPoint = pB){
- turnLeft();
- }
- }
- else if(previousPoint = pB){
- if(nextPoint = pL){
- turnRight();
- }
- else if(nextPoint = pN){
- moveForward();
- }
- else if(nextPoint = pJ){
- turnLeft();
- }
- }
- previousPoint = pK;
- break;
- case pL:
- if(previousPoint = pK){
- if(nextPoint = pG){
- turnRight();
- }
- }
- else if(previousPoint = pG){
- if(nextPoint = pK){
- turnLeft();
- }
- }
- previousPoint = pL;
- break;
- case pM:
- if(previousPoint = pN){
- if(nextPoint = pP){
- turnRight();
- }
- }
- else if(previousPoint = pP){
- if(nextPoint = pN){
- turnLeft();
- }
- }
- previousPoint = pM;
- break;
- case pN:
- if(previousPoint = pK){
- if(nextPoint = pO){
- turnLeft();
- }
- else if(nextPoint = pM){
- turnRight();
- }
- }
- else if(previousPoint = pO){
- if(nextPoint = pK){
- turnRight();
- }
- else if(nextPoint = pM){
- moveForward();
- }
- }
- else if(previousPoint = pM){
- if(nextPoint = pK){
- turnLeft();
- }
- else if(nextPoint = pO){
- moveForward();
- }
- }
- previousPoint = pN;
- break;
- case pO:
- if(previousPoint = pJ){
- if(nextPoint = pN){
- turnRight();
- }
- }
- else if(previousPoint = pN){
- if(nextPoint = pJ){
- turnLeft();
- }
- }
- previousPoint = pO;
- break;
- case pP:
- if(previousPoint = pA){
- if(nextPoint = pG){
- turnLeft();
- }
- else if(nextPoint = pM){
- moveForward();
- }
- }
- else if(previousPoint = pG){
- if(nextPoint = pA){
- turnRight();
- }
- else if(nextPoint = pM){
- turnLeft();
- }
- }
- else if(previousPoint = pM){
- if(nextPoint = pA){
- moveForward();
- }
- else if(nextPoint = pG){
- turnRight();
- }
- }
- previousPoint = pP;
- break;
- }
- else{
- Motor.stop(MOTOR1);
- Motor.stop(MOTOR2);
- Serial.print("Vous êtes arrivés!")
- }
- }
- /*if(previousPoint = nextNorth){
- if(nextPoint = nextSouth){
- moveForward();
- }
- if(nextPoint = nextWest){
- turnRight();
- }
- if(nextPoint = nextEast){
- turnLeft();
- }
- }
- else if(previousPoint = nextSouth){
- if(nextPoint = nextNorth){
- moveForward();
- }
- if(nextPoint = nextWest){
- turnLeft();
- }
- if(nextPoint = nextEast){
- turnRight();
- }
- }
- else if(previousPoint = nextEast){
- if(nextPoint = nextNorth){
- turnRight();
- }
- if(nextPoint = nextSouth){
- turnLeft();
- }
- if(nextPoint = nextWest){
- moveForward();
- }
- }
- else if(previousPoint = nextWest){
- if(nextPoint = nextSouth){
- turnRight();
- }
- if(nextPoint = nextNorth){
- turnLeft();
- }
- if(nextPoint = nextEast){
- moveForward();
- }
- }*/
- //Les situations des capteurs
- bool isMoveForwardOnly(){
- if(!etatCapteurGauche && etatCapteurCentreG && etatCapteurCentreD && !etatCapteurDroit)
- {
- return true;
- }
- return false;
- }
- bool tooFarRight(){
- if(!etatCapteurGauche && etatCapteurCentreG && !etatCapteurCentreD && !etatCapteurDroit)
- {
- return true;
- }
- return false;
- }
- bool tooFarLeft(){
- if(!etatCapteurGauche && !etatCapteurCentreG && etatCapteurCentreD && !etatCapteurDroit)
- {
- return true;
- }
- return false;
- }
- bool offRoad(){
- if(!etatCapteurGauche && !etatCapteurCentreG && !etatCapteurCentreD && !etatCapteurDroit){
- return true;
- }
- return false;
- }
- class movement {
- public :
- char Intersec ;
- char NextIntersec;
- public :
- String Move;
- //Correction à droite
- void correctRight(){
- while(!isMoveForwardOnly())
- {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -25);
- }
- }
- //Correction à gauche
- void correctLeft(){
- while(!isMoveForwardOnly())
- {
- Motor.speed(MOTOR1, -25);
- Motor.speed(MOTOR2, 100);
- }
- }
- //S'arrêter
- void Stop(){
- Motor.stop(MOTOR1);
- Motor.stop(MOTOR2);
- }
- //Reculer
- void moveBack(){
- Motor.speed(MOTOR1, -50);
- Motor.speed(MOTOR2, -50);
- }
- //Avancer
- void moveForward(){
- while(isMoveForwardOnly()){
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, 100);
- }
- }
- //Tourner à gauche
- void turnLeft(){
- while(!isMoveForwardOnly()){
- Motor.speed(MOTOR1, -50);
- Motor.speed(MOTOR2, 50);
- }
- }
- //Tourner à droite
- void turnRight(){
- while(!isMoveForwardOnly()){
- Motor.speed(MOTOR1, 50);
- Motor.speed(MOTOR2, -50);
- }
- }
- };
- enum move {
- RIGHT,
- LEFT
- };
- class calcul{
- public :
- double calculDistance(double xFirstPoint, double yFirstPoint, double xSecondPoint, double ySecondPoint){
- double res;
- res=sqrt((xSecondPoint-xFirstPoint)*(xSecondPoint-xFirstPoint)+(ySecondPoint-yFirstPoint)*(ySecondPoint-yFirstPoint));
- return res;
- }
- double calculTemps(double distance){
- double res;
- res=(distance/vitesseVoiture);
- return res;
- }
- };
- class voiture {
- void pointAller(Point* pAvant, Point* pPresent){
- }
- };
- void setup() {
- Serial.begin(9600);
- pinMode(capteurGauche, INPUT);
- pinMode(capteurCentreD, INPUT);
- pinMode(capteurCentreG, INPUT);
- pinMode(capteurDroit, INPUT);
- Motor.begin(I2C_ADDRESS);
- map();
- };
- calcul* cal = new calcul();
- void loop() {
- etatCapteurGauche = digitalRead(capteurGauche);
- etatCapteurCentreD = digitalRead(capteurCentreD);
- etatCapteurCentreG = digitalRead(capteurCentreG);
- etatCapteurDroit = digitalRead(capteurDroit);
- timeDelay = distance / averageSpeed;
- double resDistance, resTemps;
- double xA,yA,xB,yB;
- xA=pA->getX();
- Serial.print("xA : ");
- Serial.println(pA->getX());
- Serial.print("yA : " );
- Serial.println(pA->getY());
- Serial.print("xB : ");
- Serial.println(pB->getX());
- Serial.print("yB : ");
- Serial.println(pB->getY());
- resDistance=cal->calculDistance(pA->getX(),pA->getY(),pB->getX(),pB->getY());
- Serial.print("La distance entre A et B est de : ");
- Serial.println(resDistance);
- resTemps=cal->calculTemps(resDistance);
- Serial.print("Le temps pour aller de A à B est de : ");
- Serial.println(resTemps);
- delay(50000);
- if (isMoveForwardOnly()){
- move->moveForward();
- }
- else if(tooFarRight()){
- move->correctLeft();
- }
- else if(tooFarLeft()){
- move->correctRight();
- }
- else if(offRoad()){
- move->Stop();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement