Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / userthread.hpp @ 3c3c3bb9

History | View | Annotate | Download (6.112 KB)

1
#ifndef AMIRO_USERTHREAD_H_
2
#define AMIRO_USERTHREAD_H_
3

    
4
#include <ch.hpp>
5
#include <amiroosconf.h>
6
#include <amiro/Color.h>
7
// #include "global.hpp"
8
// #include "linefollow.hpp"
9

    
10

    
11

    
12
// TODO: Clean up and sort defines!
13
// Speed when driving towards the docking station
14
#define CHARGING_SPEED 5
15
#define DETECTION_SPEED 10
16
#define DIST_THRESH 100
17
#define RELEASE_COUNT 200
18
#define SPEED_CONVERSION_FACTOR 1000000 //Convert value to um/s
19
// Thresh to determain how much update steps should pass while alining
20
// #define MAX_CORRECTION_STEPS 200
21
#define DOCKING_CORRECTION_TIMEOUT 200
22
#define REVERSE_DOCKING_TIMEOUT 2*DOCKING_CORRECTION_TIMEOUT
23
#define REVERSE_ADJUSTMENT_TIMEOUT 200
24
// #define MAX_RING_PROX_VALUE_DEVIATION
25

    
26
// Thresh for wheel proxy sensors, when summed values fall below the state changes
27
// #define PROXY_WHEEL_THRESH 18000
28
// Thresh for detecting obsticles
29
// #define PROXY_RING_THRESH 15000
30

    
31
// #define PUSH_BACK_COUNT 5
32
#define PUSH_BACK_TIMEOUT 5
33
// Thresh for how long (update steps) the front sensors are allowed to detect white
34
// #define WHITE_COUNT_THRESH 150
35
#define WHITE_DETETION_TIMEOUT 150
36
// #define RING_PROX_COUNT_THRESH 1000
37
#define RING_PROX_DETECTION_TIMEOUT 400
38
// Rotation around 180 degrees in microradian
39
// #define ROTATION_180 3141592
40
#define ROTATION_180 3141592
41
// Rotation around -20 degrees in microradian
42
#define ROTATION_20 -349065
43
#define ROTATION_DURATION 10000
44

    
45
#define RING_PROX_FRONT_THRESH 18000
46
// #define PROX_MAX_VAL 65430
47
#define PROX_MAX_VAL 0xFFF0
48

    
49

    
50
// Threshold for failing to dock
51
#define DOCKING_ERROR_THRESH 3
52
#define CAN_TRANSMIT_STATE_THRESH 50
53
#define PROX_DEVIATION_MEAN_WINDOW 3
54
#define MAX_DEVIATION_CORRECTIONS 4
55
#define MAX_DEVIATION_FACTOR 35
56
#define DEVIATION_CORRECTION_DURATION 900
57
#define DEVIATION_CORRECTION_SPEED 2000000
58
#define DEVIATION_CORRECTION_VALUE (DEVIATION_CORRECTION_SPEED / 2)
59
#define DEVIATION_DIST_THRESH 25000
60

    
61

    
62

    
63
// Map Tracking Parameters
64
// enable amiro map to continuously build internal map representation
65
#define AMIRO_MAP_AUTO_TRACKING true
66

    
67

    
68

    
69
namespace amiro {
70

    
71

    
72
class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>
73
{
74

    
75
  // Messages which can be received and trigger state changes
76
  public:
77

    
78
    // States of user thread state machine
79
    // enum ut_states : int8_t {
80
    //   UT_IDLE = 0,
81
    //   UT_FOLLOW_LINE = 1,
82
    //   UT_DETECT_STATION = 2,
83
    //   UT_REVERSE = 3,
84
    //   UT_PUSH_BACK = 4,
85
    //   UT_CHECK_POSITIONING = 5,
86
    //   UT_CHECK_VOLTAGE = 6,
87
    //   UT_CHARGING = 7,
88
    //   UT_RELEASE = 8,
89
    //   UT_RELEASE_TO_CORRECT = 9,
90
    //   UT_CORRECT_POSITIONING = 10,
91
    //   UT_TURN = 12,
92
    //   UT_INACTIVE = 13,
93
    //   UT_CALIBRATION = 14,
94
    //   UT_CALIBRATION_CHECK = 15,
95
    //   UT_DEVIATION_CORRECTION = 16,
96
    //   UT_TEST_MAP_STATE = 17,
97
    //   UT_TEST_MAP_AUTO_STATE = 18,
98
    //   UT_DOCKING_ERROR = -1,
99
    //   UT_REVERSE_TIMEOUT_ERROR = -2,
100
    //   UT_CALIBRATION_ERROR = -3,
101
    //   UT_WHITE_DETECTION_ERROR = -4,
102
    //   UT_PROXY_DETECTION_ERROR = -5,
103
    //   UT_NO_CHARGING_POWER_ERROR = -6,
104
    //   UT_UNKNOWN_STATE_ERROR = -7
105
    // };
106

    
107
    struct ut_counter {
108
      int whiteCount = 0;
109
      int ringProxCount = 0;
110
      // int correctionCount = 0;
111
      int errorCount = 0;
112
      int stateCount = 0;
113
      int stepCount = 0;
114
      uint32_t stateTime = 0;
115
  };
116

    
117
  struct proxy_ctrl {
118
    int threshLow = 100;
119
    int threshMid = 500;
120
    int threshHigh = 1500;
121
    // int threshHigh = 1500;
122
    int penalty = 100000;
123
    int pFactor = 310000;
124
    int staticCont = 0;
125
  };
126

    
127

    
128
  struct bottom_prox_calibration {
129
    bool calibrateBlack = true;
130
    uint32_t buf = 0;
131
    uint8_t meanWindow = 150;
132
  };
133

    
134
  struct deviation_correction {
135
    bool RCase = true;
136
    int8_t pCount = 0;
137
    int32_t proxbuf[PROX_DEVIATION_MEAN_WINDOW] = { 0 };
138
    int32_t currentDeviation = 0;
139
  };
140

    
141
  // static const struct ut_counter emptyUtCount;
142
  ut_counter utCount;
143
  proxy_ctrl pCtrl;
144
  bottom_prox_calibration proxCalib;
145
  deviation_correction devCor;
146

    
147

    
148
  explicit UserThread();
149

    
150
  virtual ~UserThread();
151

    
152
  virtual msg_t main();
153

    
154
private:
155
  void setRpmSpeed(const int (&rpmSpeed)[2]);
156
  void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]);
157
  void lightOneLed(Color color, int idx);
158
  void lightAllLeds(Color color);
159
  /**
160
   * Uses light ring indicate the state of charge.
161
   */
162
  void showChargingState();
163
  void checkForMotion();
164
  bool checkPinVoltage();
165
  bool checkPinEnabled();
166
  int getProxyRingSum();
167

    
168
  /**
169
  * Returns percentage of mean deviation between two given values.
170
  * It is intended to calculate the mean deviation between two proxy sensor
171
  * values. PROX_DEVIATION_MEAN_WINDOW determains the size of the mean window.
172
  * Keep in mind that initial results are wrong.
173
  * */
174
  int32_t meanDeviation(uint16_t a, uint16_t b);
175

    
176
  /**
177
   * Check sectors around and stop if a thresh in one sector is detected.
178
   */
179
  void preventCollision(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]);
180

    
181
  /**
182
   * Same as prevent collision but also lowers the speed when object is detected.
183
   */
184
  void proxSectorSpeedCorrection(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]);
185
  void getProxySectorVals(uint16_t (&proxVals)[8], uint16_t (&sProx)[8]);
186
  void getMaxFrontSectorVal(uint16_t (&sProx)[8], int32_t &sPMax);
187
  void chargeAsLED();
188

    
189
  /**
190
   * Returns true when front sensors reaching high values
191
   * and all others are low. This indicates that the loading station is ahead.
192
   * If other sensores are blocked too, indicates that someone grabs the robot.
193
   */
194
  bool checkFrontalObject();
195

    
196
  /**
197
   * Check if current position changes when the wheel are deactivated.
198
   *
199
   * When AMiRo drives towards the loading station, it stops when a specific marker is reached.
200
   * In order to validate that the AMiRo is correctly positioned in the loading station
201
   * the wheels are turned off. When the position remains the same the docking procedure
202
   * was successful (return 1) otherwise a correction is needed (return 0).
203
   */
204
  int checkDockingSuccess();
205

    
206

    
207

    
208
  bool continue_on_obstacle = true;
209
  uint16_t rProx[8]; // buffer for ring proxy values
210
  int rpmSpeed[2] = {0};
211
  int stop[2] = {0};
212
  int turn[2] = {5,-5};
213

    
214
};
215

    
216
} // end of namespace amiro
217

    
218
#endif // AMIRO_USERTHREAD_H_