Roboshark / Mbed 2 deprecated Roboshark_V62

Dependencies:   mbed

Fork of Roboshark_V6 by Roboshark

Files at this revision

API Documentation at this revision

Comitter:
ahlervin
Date:
Mon May 07 14:11:06 2018 +0000
Parent:
7:862d80e0ea2d
Commit message:
funkt regler

Changed in this revision

Fahren.cpp Show annotated file Show diff for this revision Revisions of this file
Fahren.h Show annotated file Show diff for this revision Revisions of this file
Regler.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Fahren.cpp	Fri May 04 16:26:59 2018 +0000
+++ b/Fahren.cpp	Mon May 07 14:11:06 2018 +0000
@@ -12,14 +12,15 @@
 
 using namespace std;
 
-const float Fahren :: PERIOD = 0.8f;
+const float Fahren :: PERIOD = 0.2f;
 
 //Konstruktor
-Fahren::Fahren(Controller& controller, EncoderCounter& counterLeft, EncoderCounter& counterRight, Regler& regler, int reglerEin):
-    controller(controller), counterLeft(counterLeft), counterRight(counterRight), regler(regler), reglerEin(reglerEin){
+Fahren::Fahren(Controller& controller, EncoderCounter& counterLeft, EncoderCounter& counterRight, Regler& regler, int reglerEin, IRSensor& iRSensor):
+    controller(controller), counterLeft(counterLeft), counterRight(counterRight), regler(regler), reglerEin(reglerEin), iRSensor (iRSensor){
     
-    SpeedR = 50;
-    SpeedL = 50;
+    SpeedR = 80;
+    SpeedL = 80;
+    disF = 0;
     ticker.attach(callback(this, &Fahren::getSpeed), PERIOD);
 }
 
@@ -31,12 +32,13 @@
 
 //Korrigierte Geschwindigkeit aus der Klasse Regler aufrufen
 void Fahren::getSpeed(){
-    if(reglerEin == 1){
-    SpeedR = regler.getSpeedR();
-    SpeedL = regler.getSpeedL();
-    }else {SpeedR = 50;
-            SpeedL = 50;
-    }
+    //if((reglerEinR == 1) && (reglerEinL == 1)){
+    //SpeedR = regler.getSpeedR();
+    //SpeedL = regler.getSpeedL();
+    SpeedR = 80;
+    SpeedL = 80;
+    
+    //disF = iRSensor.readF();
     
     //printf("SpeedR in F = %f\n",SpeedR);
     //printf("SpeedL in F = %f\n",SpeedL);
@@ -61,6 +63,7 @@
     //Zustand, dass der Roboter nicht gestoppt hat
     stopRight = false;
     stopLeft = false;
+
     
     //Drehrichtung der Motoren
     controller.setDesiredSpeedRight(SpeedR);
@@ -100,8 +103,8 @@
     initialClicksLeft = counterLeft.read();
     
     //Anzahl Clicks die der Encoder zählen soll
-    wegRechts = 1773;
-    wegLinks = 1773;
+    wegRechts = 1776;
+    wegLinks = 1776;
         
     //Geschwindigkeit
     //speedRight = 50;//SpeedR
@@ -112,28 +115,40 @@
     //Zustand, dass der Roboter nicht gestoppt hat
     stopRight = false;
     stopLeft = false;
+    reglerEinR = 1;
+    reglerEinL = 1;
     
     //Drehrichtung der Motoren
-    controller.setDesiredSpeedRight(SpeedR);
-    controller.setDesiredSpeedLeft(-(SpeedL));
-        printf("SpeedR in F2= %f\n",SpeedR);
-        printf("SpeedL in F2= %f\n",SpeedL);
+   
+        //printf("SpeedR in F2= %f\n",SpeedR);
+        //printf("SpeedL in F2= %f\n",SpeedL);
     
     while(1){
+    float diff = (iRSensor.readR() - iRSensor.readL())*0.9f;
+    if(diff > 7.0f) diff = 0;
+    if(iRSensor.readR()>60.0f) diff=0;
+    if(iRSensor.readL()>60.0f) diff=0;
+    //printf("diff =%f \n",diff);
+    controller.setDesiredSpeedRight(SpeedR+diff);
+    controller.setDesiredSpeedLeft(-(SpeedL-diff));
+    
+    wait(0.05);
        
        // printf("Encoderrechts %d \n\r", counterRight.read());                   //Debugging Zweck (Putty benutzen!)
        // printf("Encoderlinks %d \n\r", counterLeft.read());                     //Debugging Zweck (Putty benutzen!)
         
         //If Schleife, wenn Clicks erreicht sind, muss der Motor stoppen
-        if(counterRight.read() - initialClicksRight >= wegRechts || counterRight.read() - initialClicksRight <= -wegRechts){    
+        if(counterRight.read() - initialClicksRight >= wegRechts || counterRight.read() - initialClicksRight <= -wegRechts || iRSensor.readF() < 50.0f){    
             controller.setDesiredSpeedRight(0);
             stopRight = true;
+            reglerEinR = 0;
             }
             
         //If Schleife, wenn Clicks erreicht sind, muss der Motor stoppen    
-        if(counterLeft.read() - initialClicksLeft >= wegLinks || counterLeft.read() - initialClicksLeft <= -wegLinks ){
+        if(counterLeft.read() - initialClicksLeft >= wegLinks || counterLeft.read() - initialClicksLeft <= -wegLinks || iRSensor.readF() < 50.0f){
             controller.setDesiredSpeedLeft(0);
             stopLeft = true;
+            reglerEinL = 0;
             }
      
         if(stopRight == true && stopLeft == true){
@@ -252,8 +267,8 @@
     initialClicksLeft = counterLeft.read();
     
     //Anzahl Clicks die der Encoder zählen soll
-    wegRechts = 878;
-    wegLinks = 878;
+    wegRechts = 868;
+    wegLinks = 868;
     
     //Geschwindigkeit
     //speedRight = 50;
--- a/Fahren.h	Fri May 04 16:26:59 2018 +0000
+++ b/Fahren.h	Mon May 07 14:11:06 2018 +0000
@@ -18,7 +18,7 @@
 class Fahren{
     
     public:    
-    Fahren(Controller& controller, EncoderCounter& counterLeft, EncoderCounter& counterRight, Regler& regler, int reglerEin); //Konstruktor
+    Fahren(Controller& controller, EncoderCounter& counterLeft, EncoderCounter& counterRight, Regler& regler, int reglerEin, IRSensor& iRSensor); //Konstruktor
     
     virtual ~Fahren();
     
@@ -36,6 +36,7 @@
     EncoderCounter& counterLeft;
     EncoderCounter& counterRight;
     Regler& regler;
+    IRSensor& iRSensor;
     Ticker ticker;
     void getSpeed();
 
@@ -51,6 +52,9 @@
     short stopLeft;
     float SpeedR;
     float SpeedL;
+    float disF;
+    int reglerEinL;
+    int reglerEinR;
     static const float PERIOD;
     int reglerEin;
    
--- a/Regler.cpp	Fri May 04 16:26:59 2018 +0000
+++ b/Regler.cpp	Mon May 07 14:11:06 2018 +0000
@@ -13,9 +13,9 @@
 
 using namespace std;
 
-const float Regler :: PERIOD = 0.2f;
+const float Regler :: PERIOD = 0.001f;
 const int Regler :: FIXSPEED = 50;
-float faktor = 0.1;
+float faktor = 0.12;
 
 
 
@@ -32,7 +32,7 @@
     }
     
     void Regler::setSpeed (){
-        measR2 = iRSensor.readR();            // Converts and read the analog input value (value from 0.0 to 1.0)
+        measR2 = iRSensor.readR();            // Converts and read the analog input value
         measL2 = iRSensor.readL();             
         
         if((measR2 > 100) && (measL2 < 100)) {                //keine Wnad rechts
@@ -89,7 +89,7 @@
         }
         if ((measR2 < measL2)&& (measL2 - measR2 > 3)) {              //IR Sensor werte werden verglichen und die Korrektur wird berechnet
             div1 = measR2 - measL2;                             // An beiden seinen Wände
-            kor1 = 0.1f*div1;
+            kor1 = 0.12f*div1;
                 div2 = 0;
                 div3 = 0;
                 div4 = 0;
@@ -99,7 +99,7 @@
             SpeedL = FIXSPEED + kor1;
         } else if ((measR2 > measL2) && (measR2 - measL2 >3)) {
             div2 = measL2 - measR2;
-            kor2 = 0.1f*div2;
+            kor2 = 0.12f*div2;
                 div1 = 0;
                 div3 = 0;
                 div4 = 0;
--- a/main.cpp	Fri May 04 16:26:59 2018 +0000
+++ b/main.cpp	Mon May 07 14:11:06 2018 +0000
@@ -70,7 +70,7 @@
 
 Regler regler(IrRight, IrLeft, iRSensor);
 
-Fahren fahren(controller, counterLeft, counterRight, regler, reglerEin);                           //Reihenfolge!!!!!!!!!!!!!!!!!!! Ultra wichtig
+Fahren fahren(controller, counterLeft, counterRight, regler, reglerEin, iRSensor);                           //Reihenfolge!!!!!!!!!!!!!!!!!!! Ultra wichtig
 
 int main()
 {
@@ -137,7 +137,7 @@
         } else {caseDrive = 0;                                                  // Folge; Stop
         }
         
-        printf("caseDrive = %d\n",caseDrive);
+      //  printf("caseDrive = %d\n",caseDrive);
         //printf("ende = %d\n",ende);
 
         wait (0.1);