Advertisement
Guest User

mobajl

a guest
Jan 18th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using robotymobilne_projekt.Manual;
  4. using robotymobilne_projekt.Settings;
  5.  
  6. namespace robotymobilne_projekt.Automatic
  7. {
  8.     public class LineFollower : AbstractController
  9.     {
  10.         // TODO: Tune controller
  11.         // Proportional part
  12.         private int KP = 1;
  13.  
  14.         // Derative part
  15.         private int KD = 1;
  16.  
  17.         // Integral part
  18.         //private int integral;
  19.  
  20.         // General part
  21.         private int lastError;
  22.         private int previousReading;
  23.  
  24.  
  25.         //MOJE
  26.         double error = 0;
  27.         double blackV = 0;
  28.         double whiteV = 2000;
  29.  
  30.         //double Kp = (0 - RobotSettings.Instance.MaxSpeed)/(-offset - 0);
  31.         double Kc = 0.05;
  32.         double dt = 0.02;
  33.         double Pc = 0.38;
  34.         int i = 0;
  35.  
  36.  
  37.  
  38.         double Kp = 0;
  39.         double Ki = 0;
  40.         double Kd = 0;
  41.         double integral = 0;
  42.         double derivative = 0;
  43.         double lastErr = 0;
  44.  
  45.         double turn = 0;
  46.  
  47.  
  48.         #region Setters & Getters
  49.         public ObservableCollection<int> Sensors { private get; set; }
  50.  
  51.         public int K_P
  52.         {
  53.             get
  54.             {
  55.                 return KP;
  56.             }
  57.             set
  58.             {
  59.                 KP = value;
  60.                 NotifyPropertyChanged("K_P");
  61.             }
  62.         }
  63.         public int K_D
  64.         {
  65.             get
  66.             {
  67.                 return KD;
  68.             }
  69.             set
  70.             {
  71.                 KD = value;
  72.                 NotifyPropertyChanged("K_D");
  73.             }
  74.         }
  75.  
  76.         public int PreviousError
  77.         {
  78.             get { return lastError; }
  79.             set
  80.             {
  81.                 lastError = value;
  82.                 NotifyPropertyChanged("PreviousError");
  83.             }
  84.         }
  85.  
  86.         #endregion
  87.  
  88.         public override Tuple<double, double> execute()
  89.         {
  90.             if (i < 100)
  91.             {
  92.                
  93.                 if (Sensors[2] < whiteV)
  94.                     whiteV = Sensors[2];
  95.                 if (Sensors[2] > blackV)
  96.                     blackV = Sensors[2];
  97.                 i++;
  98.                 return new Tuple<double, double>(RobotSettings.Instance.MaxSpeed, -RobotSettings.Instance.MaxSpeed);
  99.             }
  100.             else
  101.             return thirdVariant();
  102.         }
  103.  
  104.  
  105.         private Tuple<double, double> thirdVariant()
  106.         {
  107.            
  108.             if (Sensors[2] < whiteV)
  109.                 whiteV = Sensors[2];
  110.             if (Sensors[2] > blackV)
  111.                 blackV = Sensors[2];
  112.  
  113.             double motorL = 0;
  114.             double motorR = 0;
  115.  
  116.             Kp = 0.17 * Kc;
  117.             Kp = 0.3 * Kc;
  118.             Ki = 0.5 * (2 * Kp * dt) / Pc;
  119.             Kd = 4 * (Kp * Pc) / (8 * dt);
  120.            
  121.  
  122.  
  123.             double offset = (blackV + whiteV) / 2;
  124.  
  125.  
  126.             error = (double)Sensors[2] - offset;
  127.             integral = (2 / 3) * integral + error;
  128.             derivative = error - lastErr;
  129.             turn = Kp * error + Ki * integral + Kd * derivative;
  130.  
  131.             motorL = RobotSettings.Instance.MaxSpeed / 2 + turn;
  132.             motorR = RobotSettings.Instance.MaxSpeed / 2 - turn;
  133.  
  134.             if (Math.Abs(motorL) > RobotSettings.Instance.MaxSpeed)
  135.                 motorL = RobotSettings.Instance.MaxSpeed;
  136.             if (Math.Abs(motorR) > RobotSettings.Instance.MaxSpeed)
  137.                 motorR = RobotSettings.Instance.MaxSpeed;
  138.  
  139.             //if (motorL < 0)
  140.             //    motorL = motorL * -1;
  141.             //if (motorR < 0)
  142.             //    motorR = motorR * -1;
  143.  
  144.             lastErr = error;
  145.             return new Tuple<double, double>(motorL, motorR);
  146.         }
  147.  
  148.         private void callibrateSensors()
  149.         {
  150.             for (var i = 0; i < Sensors.Count; i++)
  151.             {
  152.                 Sensors[i] = (int) mapValues(Sensors[i], 0, 2000, 0, 1000);
  153.             }
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement