Revision 9c46b728 devices/DiWheelDrive/userthread.cpp

View differences:

devices/DiWheelDrive/userthread.cpp
42 42
      }
43 43
}
44 44

  
45
/**
46
 * Get the wanted strategy from the global object.
47
 */
48
LineFollowStrategy determineStrategy(){
49
  switch(global.lfStrategy){
50
    case 0:
51
      return LineFollowStrategy::EDGE_RIGHT;
52
      break;
53
    case 1:
54
      return LineFollowStrategy::EDGE_LEFT;
55
      break;
56
    case 2:
57
      return LineFollowStrategy::FUZZY;
58
      break;
59
    case 3:
60
      return LineFollowStrategy::DOCKING;
61
    default:
62
      break;
63
  }
64
  return LineFollowStrategy::NONE;
65
}
66

  
67
/**
68
 * Blocks as long as the position changes.
69
 */
70
void checkForMotion(UserThread *ut){
71
  int motion = 1;
72
  int led = 0;
73
  types::position oldPos = global.odometry.getPosition();
74
  while(motion){
75
    ut->sleep(500);
76
    types::position tmp = global.odometry.getPosition();
77
    motion = abs(oldPos.x - tmp.x)+ abs(oldPos.y - tmp.y)+abs(oldPos.z - tmp.z);
78
    oldPos = tmp;
79
      global.robot.setLightColor((led + 1) % 8, Color(Color::YELLOW));
80
      global.robot.setLightColor(led % 8, Color(Color::BLACK));
81
      led++;
82
  }
83
}
84

  
85

  
86
/**
87
 * Turns MotorControl off and checks if position remains stable.
88
 */
89
int checkDockingSuccess(UserThread *ut){
90
  // global.motorcontrol.setTargetRPM(0,0);
91
  checkForMotion(ut);
92
  int led = 0;
93
  int success = 0;
94
  global.odometry.resetPosition();
95
  types::position start = global.startPos = global.odometry.getPosition();
96
  global.motorcontrol.toggleMotorEnable();
97
  ut->sleep(1000);
98
  types::position stop = global.endPos = global.odometry.getPosition();
99
  global.motorcontrol.toggleMotorEnable();
100
  // Amiro moved, docking was not successful
101
  if ((start.x + stop.x) || (start.y + stop.y)){
102
    for(led=0; led<8; led++){
103
      global.robot.setLightColor(led, Color(Color::RED));
104
    }
105
    success = 0;
106
  }else{
107
    for(led=0; led<8; led++){
108
      global.robot.setLightColor(led, Color(Color::GREEN));
109
    }
110
    success = 1;
111
  }
112
  
113
  ut->sleep(500);
114
  for(led=0; led<8; led++){
115
          global.robot.setLightColor(led, Color(Color::BLACK));
116
  }
117
  return success;
118
}
119

  
120
void correctPosition(UserThread *ut, LineFollow &lf, int steps){
121
  int checkWhite = 0;
122
  int rpmSpeed[2] ={0};
123
  lf.setStrategy(LineFollowStrategy::EDGE_LEFT);
124
  for (int correction=0; correction<steps; correction++){
125
    checkWhite = lf.followLine(rpmSpeed);
126
    setRpmSpeed(rpmSpeed);
127
    ut->sleep(CAN::UPDATE_PERIOD);
128
  }
129
}
45 130

  
46 131

  
47 132
UserThread::UserThread() :
......
59 144
  /*
60 145
   * SETUP
61 146
   */
62
  int rpmFuzzyCtrl[2] = {0};
147
  int rpmSpeed[2] = {0};
148
  int stop[2] = {0};
149
  int proxyWheelThresh = 18000;
63 150
  for (uint8_t led = 0; led < 8; ++led) {
64 151
    global.robot.setLightColor(led, Color(Color::BLACK));
65 152
  }
......
99 186
        running = !running;
100 187
    }
101 188
    if(running){
189
      switch(utState){
190
        case states::FOLLOW_LINE:{
191
          LineFollowStrategy lStrategy = determineStrategy();
192
          if(lStrategy == LineFollowStrategy::DOCKING){
193
            utState = states::OCCUPY;
194
            break;
195
          }
196
          lf.setStrategy(lStrategy);
197
          lf.followLine(rpmSpeed);
198
          setRpmSpeed(rpmSpeed);
199
          break;
200
        }
201
        case states::OCCUPY:{
202
          // Get Wheel proxy sensors
203
          int WL = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_LEFT].getProximityScaledWoOffset();
204
          int WR = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_LEFT].getProximityScaledWoOffset();
205
          // Check for black
206
          if ((WL+WR) < proxyWheelThresh){ /*TODO: Only check for one sensor */
207
            global.motorcontrol.setTargetRPM(0,0);
102 208

  
103
    }
209
            // Stop when in docking state and take further actions
210
            if(lf.getStrategy() == LineFollowStrategy::DOCKING){
211
              if(checkDockingSuccess(this)){
212
                utState = states::CHARGING;
213
                break;
214
              }else{
215
                correctPosition(this, lf, 250);
216
                lf.setStrategy(LineFollowStrategy::DOCKING);
217
                // break;
218
              }
219
            }else{
220
              checkForMotion(this);
221
              // 180° Rotation 
222
              global.distcontrol.setTargetPosition(0, 3141592, 10000);
223
              // BaseThread::sleep(8000);
224
              checkForMotion(this);
225
              correctPosition(this, lf, 250);
226
              // break;
227
              lf.setStrategy(LineFollowStrategy::DOCKING);
228
              
229
            }
230
          }
231

  
232
          // Set correct following
233
          lf.setStrategy(LineFollowStrategy::EDGE_RIGHT);
234
          lf.followLine(rpmSpeed);
235
          setRpmSpeed(rpmSpeed);
236
          
237
          break;
238
        }
239
        case states::RELEASE:{
240
          break;
241
        }
242
        case states::CHARGING:{
243
          break;
244
        }
245
        default:{
246
          break;
247
        }
248
      }
249

  
250
    }else{
251
      // Stop
252
        setRpmSpeed(stop);
253
      }
104 254

  
105
      lf.followLine(rpmFuzzyCtrl);
106
            setRpmSpeed(rpmFuzzyCtrl);
107
    // this->sleep(US2ST(5));
108 255
    this->sleep(CAN::UPDATE_PERIOD);
109 256
  }
110 257

  

Also available in: Unified diff