From 48792a3a776d38e1f3cd35462858b862d153a4d5 Mon Sep 17 00:00:00 2001
From: "sven_marian.goebel" <sven_marian.goebel@smail.th-koeln.de>
Date: Fri, 22 Dec 2023 12:37:25 +0100
Subject: [PATCH] Motor Pins 7 & 8 changed, Case 8.1 & 8.2 conditions changed,
 check_voltage changed from number based to time based, backward_factor &
 search_factor changed (needs revisit)

---
 BitS1-Robotcar.ino   |  1 -
 Robotcar_functions.h | 80 +++++++++++++++++++++++++-------------------
 2 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/BitS1-Robotcar.ino b/BitS1-Robotcar.ino
index 93d83ba..44ec96a 100644
--- a/BitS1-Robotcar.ino
+++ b/BitS1-Robotcar.ino
@@ -35,7 +35,6 @@ int white_surface_reflection = 150;
 void setup() 
 { //Innerhalb dieser Funktion wird alles einmal bei Start des Arduinos aufgerufen
   Robotcar_functions_object.initialise();
-  search_time = 1.5 * total_velocity;
 }
 
 void loop() 
diff --git a/Robotcar_functions.h b/Robotcar_functions.h
index 620feaf..69eb29b 100644
--- a/Robotcar_functions.h
+++ b/Robotcar_functions.h
@@ -17,6 +17,12 @@
   IFK TH Köln
 
   Last Modified:
+  22.12.2023
+    backward_factor and search_factor needs revisit
+    Fall 8.1 & 8.2 if and else conditions adapted
+    MOTOR_IN1 & 2 changed to PIN7
+    MOTOR_IN3 & 4 changed to PIN8
+    check_voltage() changed from quantity sensitive to time sensitive (2000ms)
   21.08.2023
     Switch from libraries "DeviceDriverSet_xxx0" to "thk-motor-driver-v2.0" & "MPU6050_getdata" to "thk_imu-v2.0"
   08 September 2022
@@ -39,11 +45,11 @@
 const int MOTOR_COUNT = 1;
 const int MOTOR_STBY = 3;
 const int MOTOR_PWM_A = 5;
-const int MOTOR_IN1 = 8;
-const int MOTOR_IN2 = 8;
+const int MOTOR_IN1 = 7;
+const int MOTOR_IN2 = 7;
 const int MOTOR_PWM_B = 6;
-const int MOTOR_IN3 = 7;
-const int MOTOR_IN4 = 7;
+const int MOTOR_IN3 = 8;
+const int MOTOR_IN4 = 8;
 
 thk_MotorDriverTB6612FNG motorR(MOTOR_COUNT, MOTOR_STBY, MOTOR_PWM_A, MOTOR_IN1, MOTOR_IN2, 0, 0, 0);
 thk_MotorDriverTB6612FNG motorL(MOTOR_COUNT, MOTOR_STBY, MOTOR_PWM_B, MOTOR_IN3, MOTOR_IN4, 0, 0, 0);
@@ -55,22 +61,24 @@ extern unsigned long start_time = 0;
 char commandline_input = 'E';
 char last_direction = 'D';
 int commandline_output = 3;
-int not_enough_voltage_counter = 0;
-int low_voltage_counter = 0;
 int modecounter;
 int obstacle_velocity = 0;
 int no_reflection = 950;
 int divisor = 1000;
 int rest = divisor / 2;
+int voltagetimer = 0;
 boolean mode_changed = false;
 float voltage_low = 7.2;
 float voltage_min = 7.0;
 float yaw = 0;
 float pitch = 0;
 float roll = 0;
-float backward_factor = 5.0;
+float backward_factor = 3.0; //Neuer backward_factor: Muss getestet werden
+float search_factor = 120000; //Neuer search_facotr: Muss getestet werden
+boolean last_d_F = false; //test
+int delay_factor = 10; //Neuer factor, muss getestet werden DELAY beim stehenbleiben könnte quadratisch mit der Geschwindigkeit skalieren?
+float search_time = 0;
 unsigned long time_drive = 0;
-float search_time = 200;
 float line_factor = 1.2;
 CRGB leds[NUM_LEDS];
 
@@ -144,6 +152,7 @@ class Robotcar_functions
       {
         turn_factor = turn_factor * -1.0;
       } 
+      search_time = search_factor*1/((float)total_velocity);
     }
 
 
@@ -399,34 +408,25 @@ class Robotcar_functions
       }
     }
 
-    int check_voltage()
+    int check_voltage() //gibt 0 wieder wenn Spannug zu niedrig, 1 wenn Spannung niedrig, 2 wenn Spannung gut
     {
       float voltage = analogRead(Voltage_pin) * 0.0375;
       voltage = voltage + (voltage * 0.08);
-      if (voltage < voltage_min)
+      if (voltage > voltage_min) //Wenn die gemessene Spannung größer ist, als die mindest Spannung...
       {
-        not_enough_voltage_counter++;
-
-        if (not_enough_voltage_counter >= 500)
+        voltagetimer = millis(); //Speichere die Systemzeit auf die Variable voltagetimer
+        return 2;
+      } else if((millis()-voltagetimer)>=2000) //Überprüfe, ob 2 Sekunden lang die Spannung zu niedrig ist
+      {
+        if(voltage > voltage_low){
+          return 1;
+        } else 
         {
-          current_mode = standby_mode;
-          leds[0] = CRGB::Red;
-          FastLED.show();
+          return 0;
         }
-      }
-      if (voltage < voltage_low)
-      {
-        low_voltage_counter++;
-      }
-      if (not_enough_voltage_counter >= 500)
-      {
-        return 0;
-      } else if (not_enough_voltage_counter < 499 && low_voltage_counter >= 500) {
-        return 1;
       } else {
         return 2;
       }
-
     }
 
 
@@ -589,35 +589,45 @@ class Robotcar_functions
             time_drive = millis();
           }
           else if ((optic_Robotcar_functions_object[0] < white_surface_reflection) && (optic_Robotcar_functions_object[1] < white_surface_reflection) && (optic_Robotcar_functions_object[2] < white_surface_reflection)) // Fall 8
-          {
+          {            
             if ((last_direction == 'L')) // Fall 8.1 (Fahrzeug sucht die Linie mit zeitlicher Begrenzung)
             {
-              if (millis() - time_drive >= search_time)
+              if (millis() - time_drive < search_time)
+              {
+                drive(Left, turn_velocity);
+              }
+              else if(millis() - time_drive < 2*search_time)
               {
                 drive(Right, turn_velocity);
               }
-              else
+              else 
               {
-                drive(Left, turn_velocity);
+                drive(Break, 0);
+                current_mode = standby_mode;
               }
             }
 
             else if ((last_direction == 'R')) // Fall 8.2 (Fahrzeug sucht die Linie mit zeitlicher Begrenzung)
             {
-              if (millis() - time_drive >= search_time)
+              if (millis() - time_drive < search_time)
+              {
+                drive(Right, turn_velocity);
+              }
+              else if(millis() - time_drive < 2*search_time)
               {
                 drive(Left, turn_velocity);
               }
-              else
+              else 
               {
-                drive(Right, turn_velocity);
+                drive(Break, 0);
+                current_mode = standby_mode;
               }
             }
 
             else if (last_direction == 'F') // Fall 8.3 (Fahrzeug fährt Rückwärts bis wieder eine Linie erkannt wird)
             {
               drive(Break, 0);
-              delay(2000);
+              delay(total_velocity*delay_factor);
               while ((get_brightness('L') < white_surface_reflection) && (get_brightness('M') < white_surface_reflection) && (get_brightness('R') < white_surface_reflection))
               {
                 drive(Backward, (total_velocity / backward_factor));
-- 
GitLab