Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / linefollow.hpp @ a0033dcb

History | View | Annotate | Download (3.958 KB)

1
#ifndef AMIRO_LINEFOLLOWING_H
2
#define AMIRO_LINEFOLLOWING_H
3

    
4
#include <ch.hpp>
5
#include "global.hpp"
6
#include <amiroosconf.h>
7

    
8
#define RAND_TRESH 16000
9

    
10
namespace amiro {
11
  
12
  enum LineFollowStrategy{
13
  EDGE_LEFT,      // driving on the left edge of a black line
14
  TRANSITION_L_R, // Transition from left to right edge
15
  TRANSITION_R_L, // transition from right to left edge
16
  EDGE_RIGHT,     // driving on the right edge of a black line
17
  REVERSE,
18
  MIDDLE,         // not working, use FUZZY instead
19
  FUZZY,
20
  NONE
21
  };
22

    
23
enum colorMember : uint8_t {
24
        BLACK=0,
25
        GREY=1,
26
        WHITE=2
27
};
28

    
29
class LineFollow
30
{
31
public:
32

    
33
  int biggestDiff = 0;
34
  Global *global;
35
  LineFollow(Global *global);
36
  LineFollow(Global *global, LineFollowStrategy strategy);
37
  /**
38
   * Entry method which should be called to follow a line.
39
   * 
40
   * @param rpmSpeed speed that will be determained
41
   * @return white flag, 1 if white is detected  
42
   */
43
  int followLine(int (&rpmSpeed)[2]);
44

    
45
  /**
46
   * Setter for LineFollowStrategy which triggers transition behavior.
47
   * 
48
   * To prevent random turns while changeing the strategy a transition state is set.
49
   * In case the strategy needs to be switched immediately use promptStrategyChange().
50
   * 
51
   * @param strategy 
52
   */
53
  void setStrategy(LineFollowStrategy strategy);
54
  void promptStrategyChange(LineFollowStrategy strategy);
55
  LineFollowStrategy getStrategy();
56
  void setGains(float Kp, float Ki, float Kd);
57

    
58

    
59
  const int rpmTurnLeft[2] = {-10, 10};
60
  const int rpmTurnRight[2] = {rpmTurnLeft[1],rpmTurnLeft[0]};
61
  const int rpmHalt[2] = {0, 0};
62
  // Definition of the fuzzyfication function
63
  //  | Membership
64
  // 1|_B__   G    __W__
65
  //  |    \  /\  /
66
  //  |     \/  \/
67
  //  |_____/\__/\______ Sensor values
68
  // SEE MATLAB SCRIPT "fuzzyRule.m" for adjusting the values
69
  // All values are "raw sensor values"
70
  /* Use these values for white ground surface (e.g. paper) */
71

    
72
  const int blackStartFalling = 0x1000; // Where the black curve starts falling
73
  const int blackOff = 0x1800; // Where no more black is detected
74
  const int whiteStartRising = 0x2800; // Where the white curve starts rising
75
  const int whiteOn = 0x6000; // Where the white curve has reached the maximum value
76
  const int greyMax = (whiteOn + blackStartFalling) / 2; // Where grey has its maximum
77
  const int greyStartRising = blackStartFalling; // Where grey starts rising
78
  const int greyOff = whiteOn; // Where grey is completely off again
79

    
80
private:
81
  /**
82
   * Calculate the error from front sensors.
83
   */
84
  int getError();
85

    
86
  /**
87
   * Error calculation while changing from one EDGE_* strategy to another.
88
   * This prevents the AMiRo from random turns while switching strategies.
89
   * 
90
   * @param FL value of left front sensor
91
   * @param FR value of right front sensor
92
   * @param targetL left threshold 
93
   * @param targetR right threshold
94
   */
95
  int transitionError(int FL, int FR, int targetL, int targetR);
96

    
97
  /**
98
   * Use the error according to the strategy to calculate the correction speed.
99
   * Currently only the P part of the PID controller is used to calculate the 
100
   * correction speed.
101
   */
102
  int getPidCorrectionSpeed();
103

    
104
  // Fuzzy line following methods--------------
105
  void lineFollowing(int (&proximity)[4], int (&rpmFuzzyCtrl)[2]);
106
  Color memberToLed(colorMember member);
107
  void defuzzyfication(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]);
108
  colorMember getMember(float (&fuzzyValue)[3]);
109
  void fuzzyfication(int sensorValue, float (&fuzziedValue)[3]);
110
  void copyRpmSpeed(const int (&source)[2], int (&target)[2]);
111
  int vcnl4020AmbientLight[4] = {0};
112
  int vcnl4020Proximity[4] = {0};
113
  // -----------------------------------------
114
  LineFollowStrategy strategy = LineFollowStrategy::EDGE_RIGHT;
115
  char whiteFlag = 0;
116
  int trans = 0;
117

    
118
  // PID controller components ---------------
119
  int32_t K_p = 1400;
120
  float K_i = 0.06;
121
  float K_d = 0.1;
122
  int32_t accumHist = 0;
123
  float oldError = 0;
124
  // ----------------------------------------
125
};
126

    
127

    
128

    
129
} // end of namespace amiro
130

    
131
#endif // AMIRO_LINEFOLLOWING_H