Revision 1b3adcdd

View differences:

devices/DiWheelDrive/global.hpp
176 176
  // Thresh FL: 5241, FR: 25528
177 177
  int threshProxyL = 5241;
178 178
  int threshProxyR = 25528;
179
  int threshWhite = 78000;
179
  int threshWhite = 51056;
180 180

  
181 181
  // PID for line following:
182 182
  float K_p = 0.0f;
......
189 189

  
190 190
// Debugging stuff --------------
191 191
  int forwardSpeed = 10;
192
  int enableRecord = 0;
192
  int enableRecord = 1;
193 193

  
194 194
  // Buffer for sensor values
195 195
  struct sensorRecord
devices/DiWheelDrive/linefollow2.cpp
2 2
#include "linefollow2.hpp" 
3 3
#include <cmath>
4 4

  
5
// Trash
6
void LineFollow::printSensorData(){
7
    chprintf((BaseSequentialStream*) &SD1, "Test!");
8
}
9 5

  
10 6

  
11 7
LineFollow::LineFollow(Global *global){
12 8
    this->global = global;
13 9
}
14

  
15
// trash
16
int LineFollow::delta(){
17
    int delta = 0;
18
    int FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
19
    int FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
20

  
21
    delta = abs(abs(global->threshProxyL-global->threshProxyR) - abs(FL-FR));
22

  
23
    if (FR > global->threshProxyR && FL > global->threshProxyL ){
24
        return delta ;
25
    }else {
26
        return delta* -1;
27
    }
28
    return  delta;
29
}
30

  
31
// old and trash
32
void LineFollow::stableFollow(int vcnl4020Proximity[4], int (&rpmFuzzyCtrl)[2], Global *global){
33
    int targetSensor = 0x38;
34
    int actualSensorL = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] ;
35
    int actualSensorR = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT] ;
36
    int targetSpeedL = global->rpmForward[constants::DiWheelDrive::LEFT_WHEEL];
37
    int targetSpeedR = global->rpmForward[constants::DiWheelDrive::RIGHT_WHEEL];
38

  
39

  
40
    int diff = actualSensorR - actualSensorL; 
41
    int error = targetSensor - (actualSensorL + actualSensorR);
42

  
43
    accSum += error;
44
    int dTerm = error - oldError;
45

  
46
    if (diff > biggestDiff){
47
        biggestDiff = diff;
48
    }
49
    int correctionSpeed = (int) (Kp * error + Ki * accSum + Kd * dTerm);   
50
    chprintf((BaseSequentialStream*) &SD1, "Correction Speed: %d\n", correctionSpeed);
51
    rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL] = targetSpeedL + correctionSpeed;
52
    rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL] = targetSpeedR - correctionSpeed;
53

  
54
    chprintf((BaseSequentialStream*) &SD1, "Diff: %d, Biggest: %d\n", correctionSpeed, biggestDiff);
55

  
10
LineFollow::LineFollow(Global *global, LineFollowStrategy strategy){
11
    this->global = global;
12
    this-> strategy = strategy;
56 13
}
57 14

  
58 15
/**
59 16
 * Calculate the error from front proxi sensors and fixed threshold values for those sensors.
60 17
 */
61 18
int LineFollow::getError(){
62
    
19
    // Get actual sensor data of both front sensors
63 20
    int FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
64 21
    int FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
65 22
    int targetL = global->threshProxyL;
66 23
    int targetR = global->threshProxyR;
67
    int error = FL -targetL + FR - targetR;
68

  
24
    int error = 0;
25
    switch (this->strategy)
26
    {
27
    case LineFollowStrategy::EDGE_RIGHT:
28
        error = -(FL -targetL + FR - targetR);
29
        break;
30
    case LineFollowStrategy::EDGE_LEFT:
31
        error = (FL -targetL + FR - targetR);
32
        break;
33
    case LineFollowStrategy::MIDDLE:
34
        // Assume that the smallest value means driving in the middle
35
        targetL = targetR = !(targetL<targetR)?targetR:targetL;
36
        error = (FL -targetL + FR - targetR);
37
        break;
38
    
39
    default:
40
        break;
41
    }
42
    // Debugging stuff ------
43
    if (global->enableRecord){
44
        global->senseRec[global->sensSamples].error = error;
45
        global->senseRec[global->sensSamples].FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
46
        global->senseRec[global->sensSamples].FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
47
        global->sensSamples++;
48
        }
49
    // ----------------------
50
    // Register white values
69 51
    if (FL+FR > global->threshWhite){
70 52
        whiteFlag = 1;
71 53
    }else{
......
74 56
    return error;
75 57
}
76 58

  
77
/**
78
 * Follow strategy for left edge. 
79
 */
80
int LineFollow::followLeftEdge(int rpmSpeed[2]){
81

  
82
    int correctionSpeed = getPidCorrectionSpeed();
83
    chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed,  global->threshWhite);
59
int LineFollow::followLine(int (&rpmSpeed)[2]){
84 60

  
85
    rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed + correctionSpeed;
86

  
87
    rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed - correctionSpeed;
88
    return whiteFlag;
89
}
90

  
91
/**
92
 * Follow strategy for right edge. 
93
 */
94
int LineFollow::followRightEdge(int rpmSpeed[2]){
61
    switch (this->strategy)
62
    {
63
    case LineFollowStrategy::FUZZY:
64
        for (int i = 0; i < 4; i++) {
65
                vcnl4020AmbientLight[i] = global->vcnl4020[i].getAmbientLight();
66
                vcnl4020Proximity[i] = global->vcnl4020[i].getProximityScaledWoOffset();
67
            }
95 68

  
96
    int correctionSpeed = getPidCorrectionSpeed();
97
    chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed,  global->threshWhite);
69
        lineFollowing(vcnl4020Proximity, rpmSpeed);
70
        break;
71
    
72
    default:
73
        int correctionSpeed = getPidCorrectionSpeed();
74
        // chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed,  global->threshWhite);
98 75

  
99
    rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed - correctionSpeed;
76
        rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed + correctionSpeed;
100 77

  
101
    rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed + correctionSpeed;
102
    return whiteFlag;
78
        rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed - correctionSpeed;
79
        return whiteFlag;
80
        break;
81
    }
103 82
}
104 83

  
84

  
105 85
/**
106 86
 * Pid controller which returns a corrections speed.
107 87
 */
108 88
int LineFollow::getPidCorrectionSpeed(){
109 89
    int error = getError();
110
    int sloap = error - global->oldError;
111
    int correctionSpeed = (int) (global->K_p*error + global->K_i*global->accumHist + global->K_d*sloap);
112
    global->oldError = error;
113
    global->accumHist += error;
90
    int sloap = error - oldError;
91
    int correctionSpeed = (int) (Kp*error + Ki*accumHist + Kd*sloap);
92
    oldError = error;
93
    // accumHist += (int) (0.01 * error);
114 94
    if (abs(error) > global->maxDist.error){
115 95
        global->maxDist.error = error;
116 96
    }
117 97
    return correctionSpeed;
118 98
}
119 99

  
120
// trash
121
// void LineFollow::calibrateZiegler(float KCrit, int rpmSpeed[2]){
122
//     int targetSpeedL = 5;
123
//     int targetSpeedR = 5;
124
//     int delta_ = error();
125
//     int correctionSpeed = (int) (KCrit * delta_);   
126
//     if (global->enableRecord){
127
//         global->senseRec[global->sensSamples].error = delta_;
128
//         global->senseRec[global->sensSamples].FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
129
//         global->senseRec[global->sensSamples].FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
130
//         global->sensSamples++;
131
//         }
132
//     if (abs(delta_) > global->maxDist.error){
133
//         global->maxDist.error = delta_;
134
//     }
135

  
136
//     rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] =   global->forwardSpeed + -1*correctionSpeed;
137
//     rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed + correctionSpeed;
138
//     chprintf((BaseSequentialStream*) &SD1, "CS:%d,LW:%d,RW:%d\n", correctionSpeed, rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL], rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL]);
139
// }
100

  
101
void LineFollow::setStrategy(LineFollowStrategy strategy){
102
    this->strategy = strategy;
103
}
104

  
105
LineFollowStrategy LineFollow::getStrategy(){
106
      return this->strategy;
107
}
108
void LineFollow::setGains(float Kp, float Ki, float Kd){
109
    this->Kp = Kp;
110
    this->Ki = Ki;
111
    this->Kd = Kd;
112
}
113

  
114

  
115

  
116

  
117

  
118
// Lagacy code, fuzzy following-----------------------------------------
119
// Line following by a fuzzy controler
120
void LineFollow::lineFollowing(int (&proximity)[4], int (&rpmFuzzyCtrl)[2]) {
121
  // FUZZYFICATION
122
  // First we need to get the fuzzy value for our 3 values {BLACK, GREY, WHITE}
123
  float leftWheelFuzzyMemberValues[3], leftFrontFuzzyMemberValues[3], rightFrontFuzzyMemberValues[3], rightWheelFuzzyMemberValues[3];
124
  fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT], leftWheelFuzzyMemberValues);
125
  fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_LEFT], leftFrontFuzzyMemberValues);
126
  fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT], rightFrontFuzzyMemberValues);
127
  fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_RIGHT], rightWheelFuzzyMemberValues);
128

  
129
  // INFERENCE RULE DEFINITION
130
  // Get the member for each sensor
131
  colorMember member[4];
132
  member[constants::DiWheelDrive::PROX_WHEEL_LEFT] = getMember(leftWheelFuzzyMemberValues);
133
  member[constants::DiWheelDrive::PROX_FRONT_LEFT] = getMember(leftFrontFuzzyMemberValues);
134
  member[constants::DiWheelDrive::PROX_FRONT_RIGHT] = getMember(rightFrontFuzzyMemberValues);
135
  member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] = getMember(rightWheelFuzzyMemberValues);
136

  
137
  // visualize sensors via LEDs
138
  global->robot.setLightColor(constants::LightRing::LED_WNW, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_LEFT]));
139
  global->robot.setLightColor(constants::LightRing::LED_NNW, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_LEFT]));
140
  global->robot.setLightColor(constants::LightRing::LED_NNE, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_RIGHT]));
141
  global->robot.setLightColor(constants::LightRing::LED_ENE, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]));
142

  
143
  // chprintf((BaseSequentialStream*) &SD1, "Left: BLACK: %f, GREY: %f, WHITE: %f\r\n", leftWheelFuzzyMemberValues[BLACK], leftWheelFuzzyMemberValues[GREY], leftWheelFuzzyMemberValues[WHITE]);
144
  // chprintf((BaseSequentialStream*) &SD1, "Right: BLACK: %f, GREY: %f, WHITE: %f\r\n", rightFuzzyMemberValues[BLACK], rightFuzzyMemberValues[GREY], rightFuzzyMemberValues[WHITE]);
145

  
146
  // DEFUZZYFICATION
147
  defuzzyfication(member, rpmFuzzyCtrl);
148
  // defuzz(member, rpmFuzzyCtrl);
149
}
150

  
151

  
152
Color LineFollow::memberToLed(colorMember member) {
153
  switch (member) {
154
    case BLACK:
155
      return Color(Color::GREEN);
156
    case GREY:
157
      return Color(Color::YELLOW);
158
    case WHITE:
159
      return Color(Color::RED);
160
    default:
161
      return Color(Color::WHITE);
162
  }
163
}
164

  
165
void LineFollow::defuzzyfication(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]) {
166
    whiteFlag = 0;
167
  // all sensors are equal
168
  if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_LEFT] &&
169
      member[constants::DiWheelDrive::PROX_FRONT_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_RIGHT] &&
170
      member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]) {
171
    // something is wrong -> stop
172
    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
173
  // both front sensor detect a line
174
  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK &&
175
      member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
176
    // straight
177
    copyRpmSpeed(global->rpmForward, rpmFuzzyCtrl);
178
  // exact one front sensor detects a line
179
  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK ||
180
             member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
181
    // soft correction
182
    if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
183
      // soft right
184
      copyRpmSpeed(global->rpmSoftRight, rpmFuzzyCtrl);
185
    } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == WHITE) {
186
      // hard right
187
      copyRpmSpeed(global->rpmHardRight, rpmFuzzyCtrl);
188
    } else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
189
      // soft left
190
      copyRpmSpeed(global->rpmSoftLeft, rpmFuzzyCtrl);
191
    } else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == WHITE) {
192
      // hard left
193
      copyRpmSpeed(global->rpmHardLeft, rpmFuzzyCtrl);
194
    }
195
  // both wheel sensors detect a line
196
  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK &&
197
             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
198
    // something is wrong -> stop
199
    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
200
  // exactly one wheel sensor detects a line
201
  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK ||
202
             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
203
    if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK) {
204
      // turn left
205
      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
206
    } else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
207
      // turn right
208
      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
209
    }
210
  // both front sensors may detect a line
211
  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY &&
212
             member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
213
    if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
214
      // turn left
215
      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
216
    } else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
217
      // turn right
218
      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
219
    }
220
  // exactly one front sensor may detect a line
221
  } else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY ||
222
             member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
223
    if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
224
      // turn left
225
      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
226
    } else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
227
      // turn right
228
      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
229
    }
230
  // both wheel sensors may detect a line
231
  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY &&
232
             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
233
    // something is wrong -> stop
234
    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
235
  // exactly one wheel sensor may detect a line
236
  } else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY ||
237
             member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
238
    if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
239
      // turn left
240
      copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
241
    } else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
242
      // turn right
243
      copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
244
    }
245
  // no sensor detects anything 
246
  } else {
247
    // line is lost -> stop
248
    whiteFlag = 1;
249
    copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
250
  }
251
    chprintf((BaseSequentialStream*) &SD1, "Fuzzy Speed: Left: %d, Right: %d\n", rpmFuzzyCtrl[0], rpmFuzzyCtrl[1]);
252
  return;
253
}
254

  
255
colorMember LineFollow::getMember(float (&fuzzyValue)[3]) {
256
  colorMember member;
257

  
258
  if (fuzzyValue[BLACK] > fuzzyValue[GREY])
259
    if (fuzzyValue[BLACK] > fuzzyValue[WHITE])
260
      member = BLACK;
261
    else
262
      member = WHITE;
263
  else
264
    if (fuzzyValue[GREY] > fuzzyValue[WHITE])
265
      member = GREY;
266
    else
267
      member = WHITE;
268

  
269
  return member;
270
}
271

  
272
// Fuzzyfication of the sensor values
273
void LineFollow::fuzzyfication(int sensorValue, float (&fuzziedValue)[3]) {
274
  if (sensorValue < blackStartFalling ) {
275
    // Only black value
276
    fuzziedValue[BLACK] = 1.0f;
277
    fuzziedValue[GREY] = 0.0f;
278
    fuzziedValue[WHITE] = 0.0f;
279
  } else if (sensorValue > whiteOn ) {
280
    // Only white value
281
    fuzziedValue[BLACK] = 0.0f;
282
    fuzziedValue[GREY] = 0.0f;
283
    fuzziedValue[WHITE] = 1.0f;
284
  } else if ( sensorValue < greyMax) {
285
    // Some greyisch value between black and grey
286

  
287
    // Black is going down
288
    if ( sensorValue > blackOff) {
289
      fuzziedValue[BLACK] = 0.0f;
290
    } else {
291
      fuzziedValue[BLACK] = static_cast<float>(sensorValue-blackOff) / (blackStartFalling-blackOff);
292
    }
293

  
294
    // Grey is going up
295
    if ( sensorValue < greyStartRising) {
296
      fuzziedValue[GREY] = 0.0f;
297
    } else {
298
      fuzziedValue[GREY] = static_cast<float>(sensorValue-greyStartRising) / (greyMax-greyStartRising);
299
    }
300

  
301
    // White is absent
302
    fuzziedValue[WHITE] = 0.0f;
303

  
304
  } else if ( sensorValue >= greyMax) {
305
    // Some greyisch value between grey white
306

  
307
    // Black is absent
308
    fuzziedValue[BLACK] = 0.0f;
309

  
310
    // Grey is going down
311
    if ( sensorValue < greyOff) {
312
      fuzziedValue[GREY] = static_cast<float>(sensorValue-greyOff) / (greyMax-greyOff);
313
    } else {
314
      fuzziedValue[GREY] = 0.0f;
315
    }
316

  
317
    // White is going up
318
    if ( sensorValue < whiteStartRising) {
319
      fuzziedValue[WHITE] = 0.0f;
320
    } else {
321
      fuzziedValue[WHITE] = static_cast<float>(sensorValue-whiteStartRising) / (whiteOn-whiteStartRising);
322
    }
323
  }
324
}
325

  
326
void LineFollow::copyRpmSpeed(const int (&source)[2], int (&target)[2]) {
327
  target[constants::DiWheelDrive::LEFT_WHEEL] = source[constants::DiWheelDrive::LEFT_WHEEL];
328
  target[constants::DiWheelDrive::RIGHT_WHEEL] = source[constants::DiWheelDrive::RIGHT_WHEEL];
329
  // chprintf((BaseSequentialStream*) &SD1, "Speed left: %d, Speed right: %d\r\n", target[0], target[1]);
330
}
devices/DiWheelDrive/linefollow2.hpp
6 6
#include <amiroosconf.h>
7 7

  
8 8
namespace amiro {
9
  enum class LineFollowStrategy{
10
  EDGE_LEFT,
11
  EDGE_RIGHT,
12
  MIDDLE,
13
  FUZZY
14
};
15

  
16
enum colorMember : uint8_t {
17
	BLACK=0,
18
	GREY=1,
19
	WHITE=2
20
};
9 21

  
10 22
class LineFollow
11 23
{
12 24
public:
13
  void printSensorData();
14
  void stableFollow(int vcnl4020Proximity[4], int (&rpmFuzzyCtrl)[2], Global *global);
15
  float SetPoint = 0x4000; // (0x1800+0x2800) >> 8
16
  float Kp = 0.001;
17
  float Ki = 0.00001;
18
  float Kd = 0.5;
19
  int accSum = 0;
20
  float oldError = 0;
25

  
26
  
21 27
  int biggestDiff = 0;
22 28
  Global *global;
23 29
  LineFollow(Global *global);
30
  LineFollow(Global *global, LineFollowStrategy strategy);
24 31
  // void calibrateZiegler(float KCrit, int rpmSpeed[2]);
25
  int followLeftEdge(int rpmSpeed[2]);
26
  int followRightEdge(int rpmSpeed[2]);
32
  int followLine(int (&rpmSpeed)[2]);
33
  // int followLeftEdge(int rpmSpeed[2]);
34
  // int followRightEdge(int rpmSpeed[2]);
35
  // int followMiddle(int rpmSpeed[2]);
36
  void setStrategy(LineFollowStrategy strategy);
37
  LineFollowStrategy getStrategy();
38
  void setGains(float Kp, float Ki, float Kd);
39

  
40

  
41
  const int rpmTurnLeft[2] = {-10, 10};
42
  const int rpmTurnRight[2] = {rpmTurnLeft[1],rpmTurnLeft[0]};
43
  const int rpmHalt[2] = {0, 0};
44
  // Definition of the fuzzyfication function
45
  //  | Membership
46
  // 1|_B__   G    __W__
47
  //  |    \  /\  /
48
  //  |     \/  \/
49
  //  |_____/\__/\______ Sensor values
50
  // SEE MATLAB SCRIPT "fuzzyRule.m" for adjusting the values
51
  // All values are "raw sensor values"
52
  /* Use these values for white ground surface (e.g. paper) */
53

  
54
  const int blackStartFalling = 0x1000; // Where the black curve starts falling
55
  const int blackOff = 0x1800; // Where no more black is detected
56
  const int whiteStartRising = 0x2800; // Where the white curve starts rising
57
  const int whiteOn = 0x6000; // Where the white curve has reached the maximum value
58
  const int greyMax = (whiteOn + blackStartFalling) / 2; // Where grey has its maximum
59
  const int greyStartRising = blackStartFalling; // Where grey starts rising
60
  const int greyOff = whiteOn; // Where grey is completely off again
27 61

  
28 62
  private:
29 63
    int delta();
30 64
    int getError();
31 65
    int getPidCorrectionSpeed();
66
    void lineFollowing(int (&proximity)[4], int (&rpmFuzzyCtrl)[2]);
67
    // void defuzz(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]);
68
    Color memberToLed(colorMember member);
69
    void defuzzyfication(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]);
70
    colorMember getMember(float (&fuzzyValue)[3]);
71
    void fuzzyfication(int sensorValue, float (&fuzziedValue)[3]);
72
    void copyRpmSpeed(const int (&source)[2], int (&target)[2]);
32 73

  
33 74
    char whiteFlag = 0;
34
    
35

  
75
    LineFollowStrategy strategy = LineFollowStrategy::EDGE_RIGHT;
76
    float Kp = 0.003;
77
    float Ki = 0;
78
    float Kd = 0;
79
    int accumHist = 0;
80
    float oldError = 0;
81
    int vcnl4020AmbientLight[4] = {0};
82
    int vcnl4020Proximity[4] = {0};
36 83
};
37 84

  
85

  
86

  
38 87
} // end of namespace amiro
39 88

  
40 89
#endif // AMIRO_LINEFOLLOWING_H
devices/DiWheelDrive/main.cpp
702 702
 * 
703 703
 * */
704 704
void calibrateLineSensores(BaseSequentialStream *chp, int argc, char *argv[]) {
705
    int vcnl4020AmbientLight[4];
705
    // int vcnl4020AmbientLight[4];
706 706
    int vcnl4020Proximity[4];
707 707
    int rounds = 1;
708 708
    int proxyL = 0;
......
725 725

  
726 726
  for (int j = 0; j < rounds; j++) {
727 727
    for (int i = 0; i < 4; i++) {
728
        vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight();
728
        // vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight();
729 729
        vcnl4020Proximity[i] = global.vcnl4020[i].getProximityScaledWoOffset();
730 730
    }
731 731
    global.robot.setLightColor(j % 8, Color(Color::BLACK));
......
769 769

  
770 770

  
771 771
void proxySensorData(BaseSequentialStream *chp, int argc, char *argv[]) {
772
  uint16_t vcnl4020AmbientLight[4];
772
  // uint16_t vcnl4020AmbientLight[4];
773 773
  uint16_t vcnl4020Proximity[4];
774 774
  uint16_t rounds = 1;
775
  uint16_t proxyL = global.threshProxyL;
776
  uint16_t proxyR = global.threshProxyR;
777
  uint16_t maxDelta = 0;
775
  // uint16_t proxyL = global.threshProxyL;
776
  // uint16_t proxyR = global.threshProxyR;
777
  // uint16_t maxDelta = 0;
778 778
  
779
  int sensorL = 0;
780
  int sensorR = 0;
779
  // int sensorL = 0;
780
  // int sensorR = 0;
781 781
  if (argc == 1){
782 782
    chprintf(chp, "Test %i rounds \n", atoi(argv[0]));
783 783
    rounds = atoi(argv[0]);
......
787 787

  
788 788
  for (int j = 0; j < rounds; j++) {
789 789
    for (int i = 0; i < 4; i++) {
790
        vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight();
790
        // vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight();
791 791
        vcnl4020Proximity[i] = global.vcnl4020[i].getProximityScaledWoOffset();
792 792
    }
793 793
    
......
827 827

  
828 828

  
829 829
void zieglerMeth2(BaseSequentialStream *chp, int argc, char *argv[]) {
830
  int vcnl4020AmbientLight[4];
831
  int vcnl4020Proximity[4];
830
  // int vcnl4020AmbientLight[4];
831
  // int vcnl4020Proximity[4];
832 832
  int rpmSpeed[2] = {0};
833 833
  int steps = 0;
834
  int proxyL = global.threshProxyL;
835
  int proxyR = global.threshProxyR;
834
  // int proxyL = global.threshProxyL;
835
  // int proxyR = global.threshProxyR;
836 836
  int maxDelta = 0;
837 837
  float KCrit = 0.0f;
838 838
  global.sensSamples = 0;
......
865 865
  // global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000);
866 866
  int checkWhite = 0;
867 867
  int it_switch = steps / 2;
868
  // lf.setStrategie(LineFollowStrategie::MIDDLE);
868 869
  for(int s=0; s < steps; s++){
870
    
871
    checkWhite = lf.followLine(rpmSpeed);
869 872
    // chprintf(chp,"S:%d,",s);
870
    if(global.threshWhite)
871
    if(s < it_switch){
872

  
873
      checkWhite = lf.followRightEdge(rpmSpeed);
874
    }else{
875
      checkWhite = lf.followLeftEdge(rpmSpeed);
876
    }
873
    // if(global.threshWhite)
874
    // if(s < it_switch){
875
    //   lf.setStrategie(LineFollowStrategie::EDGE_LEFT);
876
    //   checkWhite = lf.followLine(rpmSpeed);
877
    // }else{
878
    //   lf.setStrategie(LineFollowStrategie::EDGE_RIGHT);
879
    //   checkWhite = lf.followLine(rpmSpeed);
880
    // }
877 881
    if(checkWhite){
882
      global.motorcontrol.setTargetRPM(0,0);
878 883
      for(led=0; led<8; led++){
879 884
        global.robot.setLightColor(led, Color(Color::RED));
880 885
      }
881
      global.motorcontrol.setTargetRPM(0,0);
882 886
    }else{
883 887
      global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000);
884 888
    }
......
891 895
}
892 896

  
893 897

  
894

  
895
void recordMove(BaseSequentialStream *chp, int argc, char *argv[]){
896
  // int vcnl4020AmbientLight[4];
897
  int vcnl4020Proximity[4];
898
  int rpmSpeed[2] = {0};
899
  int steps = 0;
898
void followLine(BaseSequentialStream *chp, int argc, char *argv[]){
899
  int steps = 1000;
900 900
  int speed = 0;
901
  int strategy = 0;
902
  int led = 0;
903
  int checkWhite = 0;
904
  int rpmSpeed[2] = {0};
905
  LineFollow lf(&global);
901 906
  if (argc == 1){
902 907
      chprintf(chp, "%i steps \n", atoi(argv[0]));
903 908
      steps = atoi(argv[0]);
904
      speed = 30;
905
  }else if (argc == 2){
906
    steps = atoi(argv[0]);
907
    speed = atoi(argv[1]);
908
  }else{
909
    chprintf(chp, "No steps given!\n");
910
    return;
911
  }
912
  global.sensSamples = steps;
913
  chprintf((BaseSequentialStream*)&global.sercanmux1, "Recodring starts in five seconds...\n");
914
  BaseThread::sleep(MS2ST(5000));
915
  // int sensSamples = 0;
916
  // sensorRecord senseRec[1000];
917

  
918
  for (int j = 0; j < steps; j++) {
919
    for (int i = 0; i < 4; i++) {
920
        // vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight();
921
        vcnl4020Proximity[i] = global.vcnl4020[i].getProximityScaledWoOffset();
909
    }else if (argc == 2){
910
      steps = atoi(argv[0]);
911
      speed = atoi(argv[1]);
912
    }else if (argc == 3){
913
      steps = atoi(argv[0]);
914
      speed = atoi(argv[1]);
915
      strategy = atoi(argv[2]);
916
    }else{
917
      chprintf(chp, "Use: followLine <steps> <speed> <strategy>\n");
918
      return;
919
    }
920
    global.forwardSpeed = speed;
921
    switch (strategy)
922
    {
923
    case 0:
924
      lf.setStrategy(amiro::LineFollowStrategy::EDGE_RIGHT);
925
      break;
926
    case 1:
927
      lf.setStrategy(amiro::LineFollowStrategy::EDGE_LEFT);
928
      break;
929
    case 2:
930
      lf.setStrategy(amiro::LineFollowStrategy::FUZZY);
931
      break;
932
    default:
933
      break;
922 934
    }
923 935

  
924
    int FL = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
925
    int FR = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
926
    
927 936

  
928
    global.senseRec[j].FL = FL;
929
    global.senseRec[j].FR = FR;
930
    // chprintf(chp,"FL: 0x%x, FR: 0x%x, Delta: %d, ProxyL: %x, ProxyR: %x, MaxDelta: %d\n", 
931
    //               vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT],
932
    //               vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT],
933
    //               vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] - vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT]);
934
    global.motorcontrol.setTargetRPM(speed * 1000000, -speed * 1000000);
935
    BaseThread::sleep(CAN::UPDATE_PERIOD);
937
    for(int s=0; s < steps; s++){
938
    
939
      checkWhite = lf.followLine(rpmSpeed);
940
      if(checkWhite){
941
        global.motorcontrol.setTargetRPM(0,0);
942
        for(led=0; led<8; led++){
943
          global.robot.setLightColor(led, Color(Color::RED));
944
        }
945
      }else{
946
        global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000);
947
      }
948
      
949
      BaseThread::sleep(CAN::UPDATE_PERIOD);
936 950
  }
951

  
937 952
  global.motorcontrol.setTargetRPM(0,0);
938
  for(int k=0; k<8;k++){
939
    global.robot.setLightColor(k, Color(Color::WHITE));
940
  }
941
  BaseThread::sleep(MS2ST(1000));
942
  for(int k=0; k<8;k++){
943
    global.robot.setLightColor(k, Color(Color::BLACK));
944
  }
945 953
}
946 954

  
955

  
947 956
void printMove(BaseSequentialStream *chp, int argc, char *argv[]){
948 957

  
949 958
  for (int j=0; j<global.sensSamples;j++){
......
985 994
  {"dev_ziegler2", zieglerMeth2},
986 995
  // TODO: Stop user process from execution to finish/force calibration before anything starts
987 996
  {"calibrate_line", calibrateLineSensores}, 
988
  {"record_move_l", recordMove},
997
  // {"record_move", recordMove},
989 998
  {"print_record", printMove},
990 999
  {"setRecord", setRecord},
1000
  {"followLine", followLine},
991 1001
  {NULL, NULL}
992 1002
};
993 1003

  
devices/DiWheelDrive/userthread.cpp
42 42
// BLACK is the line itselfe
43 43
// GREY is the boarder between the line and the surface
44 44
// WHITE is the common surface
45
enum colorMember : uint8_t {
46
	BLACK=0,
47
	GREY=1,
48
	WHITE=2
49
};
45
// enum colorMember : uint8_t {
46
// 	BLACK=0,
47
// 	GREY=1,
48
// 	WHITE=2
49
// };
50 50

  
51 51
// a buffer for the z-value of the accelerometer
52 52
int16_t accel_z;
......
57 57
int policyCounter = 0; // Do not change this, it points to the beginning of the policy
58 58

  
59 59
// Different speed settings (all values in "rounds per minute")
60
const int rpmTurnLeft[2] = {-10, 10};
61
const int rpmTurnRight[2] = {rpmTurnLeft[1],rpmTurnLeft[0]};
62
const int rpmHalt[2] = {0, 0};
60
// const int rpmTurnLeft[2] = {-10, 10};
61
// const int rpmTurnRight[2] = {rpmTurnLeft[1],rpmTurnLeft[0]};
62
// const int rpmHalt[2] = {0, 0};
63 63

  
64 64
// Definition of the fuzzyfication function
65 65
//  | Membership
......
71 71
// All values are "raw sensor values"
72 72
/* Use these values for white ground surface (e.g. paper) */
73 73

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

  
82 82
/* Use these values for gray ground surfaces */
83 83
/*
......
111 111
	// chprintf((BaseSequentialStream*) &SD1, "Speed left: %d, Speed right: %d\r\n", target[0], target[1]);
112 112
}
113 113

  
114
// Fuzzyfication of the sensor values
115
void fuzzyfication(int sensorValue, float (&fuzziedValue)[3]) {
116
	if (sensorValue < blackStartFalling ) {
117
		// Only black value
118
		fuzziedValue[BLACK] = 1.0f;
119
		fuzziedValue[GREY] = 0.0f;
120
		fuzziedValue[WHITE] = 0.0f;
121
	} else if (sensorValue > whiteOn ) {
122
		// Only white value
123
		fuzziedValue[BLACK] = 0.0f;
124
		fuzziedValue[GREY] = 0.0f;
125
		fuzziedValue[WHITE] = 1.0f;
126
	} else if ( sensorValue < greyMax) {
127
		// Some greyisch value between black and grey
128

  
129
		// Black is going down
130
		if ( sensorValue > blackOff) {
131
			fuzziedValue[BLACK] = 0.0f;
132
		} else {
133
			fuzziedValue[BLACK] = static_cast<float>(sensorValue-blackOff) / (blackStartFalling-blackOff);
134
		}
135

  
136
		// Grey is going up
137
		if ( sensorValue < greyStartRising) {
138
			fuzziedValue[GREY] = 0.0f;
139
		} else {
140
			fuzziedValue[GREY] = static_cast<float>(sensorValue-greyStartRising) / (greyMax-greyStartRising);
141
		}
142

  
143
		// White is absent
144
		fuzziedValue[WHITE] = 0.0f;
145

  
146
	} else if ( sensorValue >= greyMax) {
147
		// Some greyisch value between grey white
148

  
149
		// Black is absent
150
		fuzziedValue[BLACK] = 0.0f;
151

  
152
		// Grey is going down
153
		if ( sensorValue < greyOff) {
154
			fuzziedValue[GREY] = static_cast<float>(sensorValue-greyOff) / (greyMax-greyOff);
155
		} else {
156
			fuzziedValue[GREY] = 0.0f;
157
		}
158

  
159
		// White is going up
160
		if ( sensorValue < whiteStartRising) {
161
			fuzziedValue[WHITE] = 0.0f;
162
		} else {
163
			fuzziedValue[WHITE] = static_cast<float>(sensorValue-whiteStartRising) / (whiteOn-whiteStartRising);
164
		}
165
	}
166
}
167

  
168
// Return the color, which has the highest fuzzy value
169
colorMember getMember(float (&fuzzyValue)[3]) {
170
	colorMember member;
171

  
172
	if (fuzzyValue[BLACK] > fuzzyValue[GREY])
173
		if (fuzzyValue[BLACK] > fuzzyValue[WHITE])
174
			member = BLACK;
175
		else
176
			member = WHITE;
177
	else
178
		if (fuzzyValue[GREY] > fuzzyValue[WHITE])
179
			member = GREY;
180
		else
181
			member = WHITE;
182

  
183
	return member;
184
}
185

  
186
// Get a crisp output for the steering commands
187
void defuzzyfication(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]) {
188

  
189
	// all sensors are equal
190
	if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_LEFT] &&
191
	    member[constants::DiWheelDrive::PROX_FRONT_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_RIGHT] &&
192
	    member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]) {
193
		// something is wrong -> stop
194
		copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
195
	// both front sensor detect a line
196
	} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK &&
197
	    member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
198
		// straight
199
		copyRpmSpeed(global.rpmForward, rpmFuzzyCtrl);
200
	// exact one front sensor detects a line
201
	} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK ||
202
	           member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
203
		// soft correction
204
		if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
205
			// soft right
206
			copyRpmSpeed(global.rpmSoftRight, rpmFuzzyCtrl);
207
		} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == WHITE) {
208
			// hard right
209
			copyRpmSpeed(global.rpmHardRight, rpmFuzzyCtrl);
210
		} else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
211
			// soft left
212
			copyRpmSpeed(global.rpmSoftLeft, rpmFuzzyCtrl);
213
		} else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == WHITE) {
214
			// hard left
215
			copyRpmSpeed(global.rpmHardLeft, rpmFuzzyCtrl);
216
		}
217
	// both wheel sensors detect a line
218
	} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK &&
219
	           member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
220
		// something is wrong -> stop
221
		copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
222
	// exactly one wheel sensor detects a line
223
	} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK ||
224
	           member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
225
		if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK) {
226
			// turn left
227
			copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
228
		} else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
229
			// turn right
230
			copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
231
		}
232
	// both front sensors may detect a line
233
	} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY &&
234
	           member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
235
		if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
236
			// turn left
237
			copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
238
		} else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
239
			// turn right
240
			copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
241
		}
242
	// exactly one front sensor may detect a line
243
	} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY ||
244
	           member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
245
		if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
246
			// turn left
247
			copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
248
		} else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
249
			// turn right
250
			copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
251
		}
252
	// both wheel sensors may detect a line
253
	} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY &&
254
	           member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
255
		// something is wrong -> stop
256
		copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
257
	// exactly one wheel sensor may detect a line
258
	} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY ||
259
	           member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
260
		if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
261
			// turn left
262
			copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
263
		} else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
264
			// turn right
265
			copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
266
		}
267
	// no sensor detects anything 
268
	} else {
269
		// line is lost -> stop
270
		copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
271
	}
272

  
273
	return;
274
}
275

  
276
Color memberToLed(colorMember member) {
277
	switch (member) {
278
		case BLACK:
279
			return Color(Color::GREEN);
280
		case GREY:
281
			return Color(Color::YELLOW);
282
		case WHITE:
283
			return Color(Color::RED);
284
		default:
285
			return Color(Color::WHITE);
286
	}
287
}
288

  
289
//void lineFollowing_new(xyz) {}
290

  
291
void defuzz(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]){
292
	// all sensors are equal
293
	// if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_LEFT] &&
294
	//     member[constants::DiWheelDrive::PROX_FRONT_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_RIGHT] &&
295
	//     member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]) {
296
	// 	// something is wrong -> stop
297
	// 	copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
298
	// // both front sensor detect a line
299
	if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK &&
300
	    (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY)) {
301
		// straight
302
		copyRpmSpeed(global.rpmForward, rpmFuzzyCtrl);
303
	// Deviation to right
304
	} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK
305
		&& member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == WHITE){
306
			copyRpmSpeed(global.rpmSoftLeft, rpmFuzzyCtrl);
307
	// Deviation to left
308
	}else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK
309
		&& member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK){
310
			copyRpmSpeed(global.rpmSoftRight, rpmFuzzyCtrl);
311
	// Hard deviatio to right
312
	}else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY
313
		&& member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == WHITE){
314
			copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl);
315
	// Hard deviation to left
316
	}else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY
317
		&& member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK){
318
			copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl);
319
	// stop if white
320
	}else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == WHITE
321
		&& member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == WHITE ){
322
			copyRpmSpeed(rpmHalt, rpmFuzzyCtrl);
323
	}
324
}
325

  
326
// Line following by a fuzzy controler
327
void lineFollowing(int (&proximity)[4], int (&rpmFuzzyCtrl)[2]) {
328
	// FUZZYFICATION
329
	// First we need to get the fuzzy value for our 3 values {BLACK, GREY, WHITE}
330
	float leftWheelFuzzyMemberValues[3], leftFrontFuzzyMemberValues[3], rightFrontFuzzyMemberValues[3], rightWheelFuzzyMemberValues[3];
331
	fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT], leftWheelFuzzyMemberValues);
332
	fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_LEFT], leftFrontFuzzyMemberValues);
333
	fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT], rightFrontFuzzyMemberValues);
334
	fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_RIGHT], rightWheelFuzzyMemberValues);
335

  
336
	// INFERENCE RULE DEFINITION
337
	// Get the member for each sensor
338
	colorMember member[4];
339
	member[constants::DiWheelDrive::PROX_WHEEL_LEFT] = getMember(leftWheelFuzzyMemberValues);
340
	member[constants::DiWheelDrive::PROX_FRONT_LEFT] = getMember(leftFrontFuzzyMemberValues);
341
	member[constants::DiWheelDrive::PROX_FRONT_RIGHT] = getMember(rightFrontFuzzyMemberValues);
342
	member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] = getMember(rightWheelFuzzyMemberValues);
343

  
344
	// visualize sensors via LEDs
345
	global.robot.setLightColor(constants::LightRing::LED_WNW, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_LEFT]));
346
	global.robot.setLightColor(constants::LightRing::LED_NNW, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_LEFT]));
347
	global.robot.setLightColor(constants::LightRing::LED_NNE, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_RIGHT]));
348
	global.robot.setLightColor(constants::LightRing::LED_ENE, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]));
349

  
350
	// chprintf((BaseSequentialStream*) &SD1, "Left: BLACK: %f, GREY: %f, WHITE: %f\r\n", leftWheelFuzzyMemberValues[BLACK], leftWheelFuzzyMemberValues[GREY], leftWheelFuzzyMemberValues[WHITE]);
351
	// chprintf((BaseSequentialStream*) &SD1, "Right: BLACK: %f, GREY: %f, WHITE: %f\r\n", rightFuzzyMemberValues[BLACK], rightFuzzyMemberValues[GREY], rightFuzzyMemberValues[WHITE]);
352

  
353
	// DEFUZZYFICATION
354
	// defuzzyfication(member, rpmFuzzyCtrl);
355
	defuzz(member, rpmFuzzyCtrl);
356
}
357

  
358

  
359

  
360 114

  
361 115
// Set the speed by the array
362 116
void setRpmSpeed(const int (&rpmSpeed)[2]) {
......
452 206
                vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight();
453 207
                vcnl4020Proximity[i] = global.vcnl4020[i].getProximityScaledWoOffset();
454 208
            }
455
			lf.stableFollow(vcnl4020Proximity, rpmFuzzyCtrl, &global);
209
			// lf.stableFollow(vcnl4020Proximity, rpmFuzzyCtrl, &global);
456 210
            // chprintf((BaseSequentialStream*) &SD1, "0x%04X 0x%04X 0x%04X 0x%04X\n",
457 211
            //         vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT],
458 212
            //         vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT],

Also available in: Unified diff