commit 1b3adcdd278abb4e6daba0478081040b9fbbcb86
Author: galberding <galberding@techfak.uni-bielefeld.de>
Date:   Thu Oct 10 13:35:37 2019 +0200

    Implement differnet line following strategies

diff --git a/devices/DiWheelDrive/linefollow2.cpp b/devices/DiWheelDrive/linefollow2.cpp
index 4fbd65f..44b4682 100644
--- a/devices/DiWheelDrive/linefollow2.cpp
+++ b/devices/DiWheelDrive/linefollow2.cpp
@@ -2,70 +2,52 @@
 #include "linefollow2.hpp" 
 #include <cmath>
 
-// Trash
-void LineFollow::printSensorData(){
-    chprintf((BaseSequentialStream*) &SD1, "Test!");
-}
 
 
 LineFollow::LineFollow(Global *global){
     this->global = global;
 }
-
-// trash
-int LineFollow::delta(){
-    int delta = 0;
-    int FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
-    int FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
-
-    delta = abs(abs(global->threshProxyL-global->threshProxyR) - abs(FL-FR));
-
-    if (FR > global->threshProxyR && FL > global->threshProxyL ){
-        return delta ;
-    }else {
-        return delta* -1;
-    }
-    return  delta;
-}
-
-// old and trash
-void LineFollow::stableFollow(int vcnl4020Proximity[4], int (&rpmFuzzyCtrl)[2], Global *global){
-    int targetSensor = 0x38;
-    int actualSensorL = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] ;
-    int actualSensorR = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT] ;
-    int targetSpeedL = global->rpmForward[constants::DiWheelDrive::LEFT_WHEEL];
-    int targetSpeedR = global->rpmForward[constants::DiWheelDrive::RIGHT_WHEEL];
-
-
-    int diff = actualSensorR - actualSensorL; 
-    int error = targetSensor - (actualSensorL + actualSensorR);
-
-    accSum += error;
-    int dTerm = error - oldError;
-
-    if (diff > biggestDiff){
-        biggestDiff = diff;
-    }
-    int correctionSpeed = (int) (Kp * error + Ki * accSum + Kd * dTerm);   
-    chprintf((BaseSequentialStream*) &SD1, "Correction Speed: %d\n", correctionSpeed);
-    rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL] = targetSpeedL + correctionSpeed;
-    rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL] = targetSpeedR - correctionSpeed;
-
-    chprintf((BaseSequentialStream*) &SD1, "Diff: %d, Biggest: %d\n", correctionSpeed, biggestDiff);
-
+LineFollow::LineFollow(Global *global, LineFollowStrategy strategy){
+    this->global = global;
+    this-> strategy = strategy;
 }
 
 /**
  * Calculate the error from front proxi sensors and fixed threshold values for those sensors.
  */
 int LineFollow::getError(){
-    
+    // Get actual sensor data of both front sensors
     int FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
     int FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
     int targetL = global->threshProxyL;
     int targetR = global->threshProxyR;
-    int error = FL -targetL + FR - targetR;
-
+    int error = 0;
+    switch (this->strategy)
+    {
+    case LineFollowStrategy::EDGE_RIGHT:
+        error = -(FL -targetL + FR - targetR);
+        break;
+    case LineFollowStrategy::EDGE_LEFT:
+        error = (FL -targetL + FR - targetR);
+        break;
+    case LineFollowStrategy::MIDDLE:
+        // Assume that the smallest value means driving in the middle
+        targetL = targetR = !(targetL<targetR)?targetR:targetL;
+        error = (FL -targetL + FR - targetR);
+        break;
+    
+    default:
+        break;
+    }
+    // Debugging stuff ------
+    if (global->enableRecord){
+        global->senseRec[global->sensSamples].error = error;
+        global->senseRec[global->sensSamples].FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
+        global->senseRec[global->sensSamples].FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
+        global->sensSamples++;
+        }
+    // ----------------------
+    // Register white values
     if (FL+FR > global->threshWhite){
         whiteFlag = 1;
     }else{
@@ -74,66 +56,275 @@ int LineFollow::getError(){
     return error;
 }
 
-/**
- * Follow strategy for left edge. 
- */
-int LineFollow::followLeftEdge(int rpmSpeed[2]){
-
-    int correctionSpeed = getPidCorrectionSpeed();
-    chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed,  global->threshWhite);
+int LineFollow::followLine(int (&rpmSpeed)[2]){
 
-    rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed + correctionSpeed;
-
-    rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed - correctionSpeed;
-    return whiteFlag;
-}
-
-/**
- * Follow strategy for right edge. 
- */
-int LineFollow::followRightEdge(int rpmSpeed[2]){
+    switch (this->strategy)
+    {
+    case LineFollowStrategy::FUZZY:
+        for (int i = 0; i < 4; i++) {
+                vcnl4020AmbientLight[i] = global->vcnl4020[i].getAmbientLight();
+                vcnl4020Proximity[i] = global->vcnl4020[i].getProximityScaledWoOffset();
+            }
 
-    int correctionSpeed = getPidCorrectionSpeed();
-    chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed,  global->threshWhite);
+        lineFollowing(vcnl4020Proximity, rpmSpeed);
+        break;
+    
+    default:
+        int correctionSpeed = getPidCorrectionSpeed();
+        // chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed,  global->threshWhite);
 
-    rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed - correctionSpeed;
+        rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed + correctionSpeed;
 
-    rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed + correctionSpeed;
-    return whiteFlag;
+        rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed - correctionSpeed;
+        return whiteFlag;
+        break;
+    }
 }
 
+
 /**
  * Pid controller which returns a corrections speed.
  */
 int LineFollow::getPidCorrectionSpeed(){
     int error = getError();
-    int sloap = error - global->oldError;
-    int correctionSpeed = (int) (global->K_p*error + global->K_i*global->accumHist + global->K_d*sloap);
-    global->oldError = error;
-    global->accumHist += error;
+    int sloap = error - oldError;
+    int correctionSpeed = (int) (Kp*error + Ki*accumHist + Kd*sloap);
+    oldError = error;
+    // accumHist += (int) (0.01 * error);
     if (abs(error) > global->maxDist.error){
         global->maxDist.error = error;
     }
     return correctionSpeed;
 }
 
-// trash
-// void LineFollow::calibrateZiegler(float KCrit, int rpmSpeed[2]){
-//     int targetSpeedL = 5;
-//     int targetSpeedR = 5;
-//     int delta_ = error();
-//     int correctionSpeed = (int) (KCrit * delta_);   
-//     if (global->enableRecord){
-//         global->senseRec[global->sensSamples].error = delta_;
-//         global->senseRec[global->sensSamples].FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
-//         global->senseRec[global->sensSamples].FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
-//         global->sensSamples++;
-//         }
-//     if (abs(delta_) > global->maxDist.error){
-//         global->maxDist.error = delta_;
-//     }
-
-//     rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed + -1*correctionSpeed;
-//     rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed + correctionSpeed;
-//     chprintf((BaseSequentialStream*) &SD1, "CS:%d,LW:%d,RW:%d\n", correctionSpeed, rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL], rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL]);
-// }
\ No newline at end of file
+
+void LineFollow::setStrategy(LineFollowStrategy strategy){
+    this->strategy = strategy;
+}
+
+LineFollowStrategy LineFollow::getStrategy(){
+      return this->strategy;
+}
+void LineFollow::setGains(float Kp, float Ki, float Kd){
+    this->Kp = Kp;
+    this->Ki = Ki;
+    this->Kd = Kd;
+}
+
+
+
+
+
+// Lagacy code, fuzzy following-----------------------------------------
+// Line following by a fuzzy controler
+void LineFollow::lineFollowing(int (&proximity)[4], int (&rpmFuzzyCtrl)[2]) {
+  // FUZZYFICATION
+  // First we need to get the fuzzy value for our 3 values {BLACK, GREY, WHITE}
+  float leftWheelFuzzyMemberValues[3], leftFrontFuzzyMemberValues[3], rightFrontFuzzyMemberValues[3], rightWheelFuzzyMemberValues[3];
+  fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT], leftWheelFuzzyMemberValues);
+  fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_LEFT], leftFrontFuzzyMemberValues);
+  fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT], rightFrontFuzzyMemberValues);
+  fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_RIGHT], rightWheelFuzzyMemberValues);
+
+  // INFERENCE RULE DEFINITION
+  // Get the member for each sensor
+  colorMember member[4];
+  member[constants::DiWheelDrive::PROX_WHEEL_LEFT] = getMember(leftWheelFuzzyMemberValues);
+  member[constants::DiWheelDrive::PROX_FRONT_LEFT] = getMember(leftFrontFuzzyMemberValues);
+  member[constants::DiWheelDrive::PROX_FRONT_RIGHT] = getMember(rightFrontFuzzyMemberValues);
+  member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] = getMember(rightWheelFuzzyMemberValues);
+
+  // visualize sensors via LEDs
+  global->robot.setLightColor(constants::LightRing::LED_WNW, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_LEFT]));
+  global->robot.setLightColor(constants::LightRing::LED_NNW, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_LEFT]));
+  global->robot.setLightColor(constants::LightRing::LED_NNE, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_RIGHT]));
+  global->robot.setLightColor(constants::LightRing::LED_ENE, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]));
+
+  // chprintf((BaseSequentialStream*) &SD1, "Left: BLACK: %f, GREY: %f, WHITE: %f\r\n", leftWheelFuzzyMemberValues[BLACK], leftWheelFuzzyMemberValues[GREY], leftWheelFuzzyMemberValues[WHITE]);
+  // chprintf((BaseSequentialStream*) &SD1, "Right: BLACK: %f, GREY: %f, WHITE: %f\r\n", rightFuzzyMemberValues[BLACK], rightFuzzyMemberValues[GREY], rightFuzzyMemberValues[WHITE]);
+
+  // DEFUZZYFICATION
+  defuzzyfication(member, rpmFuzzyCtrl);
+  // defuzz(member, rpmFuzzyCtrl);
+}
+
+
+Color LineFollow::memberToLed(colorMember member) {
+  switch (member) {
+    case BLACK:
+      return Color(Color::GREEN);
+    case GREY:
+      return Color(Color::YELLOW);
+    case WHITE:
+      return Color(Color::RED);
+    default:
+      return Color(Color::WHITE);
+  }
+}
+
+void LineFollow::defuzzyfication(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]) {
+    whiteFlag = 0;
+  // all sensors are equal
+  if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_LEFT] &&
+      member[constants::DiWheelDrive::PROX_FRONT_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_RIGHT] &&
+      member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]) {
+    // something is wrong -> stop
+    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
+  // both front sensor detect a line
+  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK &&
+      member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
+    // straight
+    copyRpmSpeed(global->rpmForward, rpmFuzzyCtrl);
+  // exact one front sensor detects a line
+  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK ||
+             member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
+    // soft correction
+    if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
+      // soft right
+      copyRpmSpeed(global->rpmSoftRight, rpmFuzzyCtrl);
+    } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == WHITE) {
+      // hard right
+      copyRpmSpeed(global->rpmHardRight, rpmFuzzyCtrl);
+    } else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
+      // soft left
+      copyRpmSpeed(global->rpmSoftLeft, rpmFuzzyCtrl);
+    } else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == WHITE) {
+      // hard left
+      copyRpmSpeed(global->rpmHardLeft, rpmFuzzyCtrl);
+    }
+  // both wheel sensors detect a line
+  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK &&
+             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
+    // something is wrong -> stop
+    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
+  // exactly one wheel sensor detects a line
+  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK ||
+             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
+    if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK) {
+      // turn left
+      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
+    } else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
+      // turn right
+      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
+    }
+  // both front sensors may detect a line
+  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY &&
+             member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
+    if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
+      // turn left
+      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
+    } else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
+      // turn right
+      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
+    }
+  // exactly one front sensor may detect a line
+  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY ||
+             member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
+    if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
+      // turn left
+      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
+    } else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
+      // turn right
+      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
+    }
+  // both wheel sensors may detect a line
+  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY &&
+             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
+    // something is wrong -> stop
+    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
+  // exactly one wheel sensor may detect a line
+  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY ||
+             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
+    if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
+      // turn left
+      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
+    } else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
+      // turn right
+      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
+    }
+  // no sensor detects anything 
+  } else {
+    // line is lost -> stop
+    whiteFlag = 1;
+    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
+  }
+    chprintf((BaseSequentialStream*) &SD1, "Fuzzy Speed: Left: %d, Right: %d\n", rpmFuzzyCtrl[0], rpmFuzzyCtrl[1]);
+  return;
+}
+
+colorMember LineFollow::getMember(float (&fuzzyValue)[3]) {
+  colorMember member;
+
+  if (fuzzyValue[BLACK] > fuzzyValue[GREY])
+    if (fuzzyValue[BLACK] > fuzzyValue[WHITE])
+      member = BLACK;
+    else
+      member = WHITE;
+  else
+    if (fuzzyValue[GREY] > fuzzyValue[WHITE])
+      member = GREY;
+    else
+      member = WHITE;
+
+  return member;
+}
+
+// Fuzzyfication of the sensor values
+void LineFollow::fuzzyfication(int sensorValue, float (&fuzziedValue)[3]) {
+  if (sensorValue < blackStartFalling ) {
+    // Only black value
+    fuzziedValue[BLACK] = 1.0f;
+    fuzziedValue[GREY] = 0.0f;
+    fuzziedValue[WHITE] = 0.0f;
+  } else if (sensorValue > whiteOn ) {
+    // Only white value
+    fuzziedValue[BLACK] = 0.0f;
+    fuzziedValue[GREY] = 0.0f;
+    fuzziedValue[WHITE] = 1.0f;
+  } else if ( sensorValue < greyMax) {
+    // Some greyisch value between black and grey
+
+    // Black is going down
+    if ( sensorValue > blackOff) {
+      fuzziedValue[BLACK] = 0.0f;
+    } else {
+      fuzziedValue[BLACK] = static_cast<float>(sensorValue-blackOff) / (blackStartFalling-blackOff);
+    }
+
+    // Grey is going up
+    if ( sensorValue < greyStartRising) {
+      fuzziedValue[GREY] = 0.0f;
+    } else {
+      fuzziedValue[GREY] = static_cast<float>(sensorValue-greyStartRising) / (greyMax-greyStartRising);
+    }
+
+    // White is absent
+    fuzziedValue[WHITE] = 0.0f;
+
+  } else if ( sensorValue >= greyMax) {
+    // Some greyisch value between grey white
+
+    // Black is absent
+    fuzziedValue[BLACK] = 0.0f;
+
+    // Grey is going down
+    if ( sensorValue < greyOff) {
+      fuzziedValue[GREY] = static_cast<float>(sensorValue-greyOff) / (greyMax-greyOff);
+    } else {
+      fuzziedValue[GREY] = 0.0f;
+    }
+
+    // White is going up
+    if ( sensorValue < whiteStartRising) {
+      fuzziedValue[WHITE] = 0.0f;
+    } else {
+      fuzziedValue[WHITE] = static_cast<float>(sensorValue-whiteStartRising) / (whiteOn-whiteStartRising);
+    }
+  }
+}
+
+void LineFollow::copyRpmSpeed(const int (&source)[2], int (&target)[2]) {
+  target[constants::DiWheelDrive::LEFT_WHEEL] = source[constants::DiWheelDrive::LEFT_WHEEL];
+  target[constants::DiWheelDrive::RIGHT_WHEEL] = source[constants::DiWheelDrive::RIGHT_WHEEL];
+  // chprintf((BaseSequentialStream*) &SD1, "Speed left: %d, Speed right: %d\r\n", target[0], target[1]);
+}
\ No newline at end of file
