Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / userthread.hpp @ 309980f0

History | View | Annotate | Download (6.112 KB)

1 58fe0e0b Thomas Schöpping
#ifndef AMIRO_USERTHREAD_H_
2
#define AMIRO_USERTHREAD_H_
3
4
#include <ch.hpp>
5
#include <amiroosconf.h>
6 181f2892 galberding
#include <amiro/Color.h>
7 10bf9cc0 galberding
// #include "global.hpp"
8 8dced1c9 galberding
// #include "linefollow.hpp"
9 181f2892 galberding
10 d4c6efa9 galberding
11
12
// TODO: Clean up and sort defines!
13 181f2892 galberding
// Speed when driving towards the docking station
14
#define CHARGING_SPEED 5
15 61544eee galberding
#define DETECTION_SPEED 10
16
#define DIST_THRESH 100
17
#define RELEASE_COUNT 200
18 d4c6efa9 galberding
#define SPEED_CONVERSION_FACTOR 1000000 //Convert value to um/s
19 181f2892 galberding
// Thresh to determain how much update steps should pass while alining
20 10bf9cc0 galberding
// #define MAX_CORRECTION_STEPS 200
21
#define DOCKING_CORRECTION_TIMEOUT 200
22
#define REVERSE_DOCKING_TIMEOUT 2*DOCKING_CORRECTION_TIMEOUT
23 b24df8ad galberding
#define REVERSE_ADJUSTMENT_TIMEOUT 200
24 8dced1c9 galberding
// #define MAX_RING_PROX_VALUE_DEVIATION
25 10bf9cc0 galberding
26 181f2892 galberding
// Thresh for wheel proxy sensors, when summed values fall below the state changes
27 10bf9cc0 galberding
// #define PROXY_WHEEL_THRESH 18000
28 e2002d0e galberding
// Thresh for detecting obsticles
29 10bf9cc0 galberding
// #define PROXY_RING_THRESH 15000
30 27d4e1fa galberding
31 10bf9cc0 galberding
// #define PUSH_BACK_COUNT 5
32
#define PUSH_BACK_TIMEOUT 5
33 181f2892 galberding
// Thresh for how long (update steps) the front sensors are allowed to detect white
34 10bf9cc0 galberding
// #define WHITE_COUNT_THRESH 150
35
#define WHITE_DETETION_TIMEOUT 150
36
// #define RING_PROX_COUNT_THRESH 1000
37 8dced1c9 galberding
#define RING_PROX_DETECTION_TIMEOUT 400
38 181f2892 galberding
// Rotation around 180 degrees in microradian
39 10bf9cc0 galberding
// #define ROTATION_180 3141592
40 181f2892 galberding
#define ROTATION_180 3141592
41
// Rotation around -20 degrees in microradian
42
#define ROTATION_20 -349065
43
#define ROTATION_DURATION 10000
44
45 61544eee galberding
#define RING_PROX_FRONT_THRESH 18000
46 b24df8ad galberding
// #define PROX_MAX_VAL 65430
47
#define PROX_MAX_VAL 0xFFF0
48
49 58fe0e0b Thomas Schöpping
50 27d4e1fa galberding
// Threshold for failing to dock
51 10bf9cc0 galberding
#define DOCKING_ERROR_THRESH 3
52
#define CAN_TRANSMIT_STATE_THRESH 50
53 84b4c632 galberding
#define PROX_DEVIATION_MEAN_WINDOW 3
54 b24df8ad galberding
#define MAX_DEVIATION_CORRECTIONS 4
55 84b4c632 galberding
#define MAX_DEVIATION_FACTOR 35
56
#define DEVIATION_CORRECTION_DURATION 900
57 b24df8ad galberding
#define DEVIATION_CORRECTION_SPEED 2000000
58
#define DEVIATION_CORRECTION_VALUE (DEVIATION_CORRECTION_SPEED / 2)
59 84b4c632 galberding
#define DEVIATION_DIST_THRESH 25000
60 27d4e1fa galberding
61 3c3c3bb9 galberding
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 58fe0e0b Thomas Schöpping
namespace amiro {
70
71 181f2892 galberding
72 58fe0e0b Thomas Schöpping
class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>
73
{
74 181f2892 galberding
75
  // Messages which can be received and trigger state changes
76
  public:
77
78
    // States of user thread state machine
79 3c3c3bb9 galberding
    // 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 27d4e1fa galberding
      int whiteCount = 0;
109 10bf9cc0 galberding
      int ringProxCount = 0;
110 fbcb25cc galberding
      // int correctionCount = 0;
111 27d4e1fa galberding
      int errorCount = 0;
112 10bf9cc0 galberding
      int stateCount = 0;
113 fbcb25cc galberding
      int stepCount = 0;
114 10bf9cc0 galberding
      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 27d4e1fa galberding
  };
126 181f2892 galberding
127 10bf9cc0 galberding
128
  struct bottom_prox_calibration {
129
    bool calibrateBlack = true;
130
    uint32_t buf = 0;
131
    uint8_t meanWindow = 150;
132
  };
133 8dced1c9 galberding
134 b24df8ad galberding
  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 27d4e1fa galberding
  // static const struct ut_counter emptyUtCount;
142
  ut_counter utCount;
143 10bf9cc0 galberding
  proxy_ctrl pCtrl;
144 8dced1c9 galberding
  bottom_prox_calibration proxCalib;
145 b24df8ad galberding
  deviation_correction devCor;
146
147
148 58fe0e0b Thomas Schöpping
  explicit UserThread();
149
150
  virtual ~UserThread();
151
152
  virtual msg_t main();
153 181f2892 galberding
154
private:
155
  void setRpmSpeed(const int (&rpmSpeed)[2]);
156 c9fa414d galberding
  void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]);
157 181f2892 galberding
  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 c9fa414d galberding
  int getProxyRingSum();
167 10bf9cc0 galberding
168
  /**
169 b24df8ad galberding
  * 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 8dced1c9 galberding
  * Keep in mind that initial results are wrong.
173 b24df8ad galberding
  * */
174
  int32_t meanDeviation(uint16_t a, uint16_t b);
175
176
  /**
177 10bf9cc0 galberding
   * 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 8dced1c9 galberding
181 10bf9cc0 galberding
  /**
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 8dced1c9 galberding
189 fbcb25cc galberding
  /**
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 c9fa414d galberding
196 181f2892 galberding
  /**
197
   * Check if current position changes when the wheel are deactivated.
198 8dced1c9 galberding
   *
199 181f2892 galberding
   * 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 8dced1c9 galberding
   * was successful (return 1) otherwise a correction is needed (return 0).
203 181f2892 galberding
   */
204
  int checkDockingSuccess();
205 10bf9cc0 galberding
206 3c3c3bb9 galberding
207 10bf9cc0 galberding
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 58fe0e0b Thomas Schöpping
};
215
216
} // end of namespace amiro
217
218
#endif // AMIRO_USERTHREAD_H_