Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / userthread.hpp @ 8dced1c9

History | View | Annotate | Download (6.033 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 10bf9cc0 galberding
#include <cmath>
10 181f2892 galberding
11 d4c6efa9 galberding
12
13
// TODO: Clean up and sort defines!
14 181f2892 galberding
// Speed when driving towards the docking station
15
#define CHARGING_SPEED 5
16 61544eee galberding
#define DETECTION_SPEED 10
17
#define DIST_THRESH 100
18
#define RELEASE_COUNT 200
19 d4c6efa9 galberding
#define SPEED_CONVERSION_FACTOR 1000000 //Convert value to um/s
20 181f2892 galberding
// Thresh to determain how much update steps should pass while alining
21 10bf9cc0 galberding
// #define MAX_CORRECTION_STEPS 200
22
#define DOCKING_CORRECTION_TIMEOUT 200
23
#define REVERSE_DOCKING_TIMEOUT 2*DOCKING_CORRECTION_TIMEOUT
24 b24df8ad galberding
#define REVERSE_ADJUSTMENT_TIMEOUT 200
25 8dced1c9 galberding
// #define MAX_RING_PROX_VALUE_DEVIATION
26 10bf9cc0 galberding
27 181f2892 galberding
// Thresh for wheel proxy sensors, when summed values fall below the state changes
28 10bf9cc0 galberding
// #define PROXY_WHEEL_THRESH 18000
29 e2002d0e galberding
// Thresh for detecting obsticles
30 10bf9cc0 galberding
// #define PROXY_RING_THRESH 15000
31 27d4e1fa galberding
32 10bf9cc0 galberding
// #define PUSH_BACK_COUNT 5
33
#define PUSH_BACK_TIMEOUT 5
34 181f2892 galberding
// Thresh for how long (update steps) the front sensors are allowed to detect white
35 10bf9cc0 galberding
// #define WHITE_COUNT_THRESH 150
36
#define WHITE_DETETION_TIMEOUT 150
37
// #define RING_PROX_COUNT_THRESH 1000
38 8dced1c9 galberding
#define RING_PROX_DETECTION_TIMEOUT 400
39 181f2892 galberding
// Rotation around 180 degrees in microradian
40 10bf9cc0 galberding
// #define ROTATION_180 3141592
41 181f2892 galberding
#define ROTATION_180 3141592
42
// Rotation around -20 degrees in microradian
43
#define ROTATION_20 -349065
44
#define ROTATION_DURATION 10000
45
46 61544eee galberding
#define RING_PROX_FRONT_THRESH 18000
47 b24df8ad galberding
// #define PROX_MAX_VAL 65430
48
#define PROX_MAX_VAL 0xFFF0
49
50 58fe0e0b Thomas Schöpping
51 27d4e1fa galberding
// Threshold for failing to dock
52 10bf9cc0 galberding
#define DOCKING_ERROR_THRESH 3
53
#define CAN_TRANSMIT_STATE_THRESH 50
54 84b4c632 galberding
#define PROX_DEVIATION_MEAN_WINDOW 3
55 b24df8ad galberding
#define MAX_DEVIATION_CORRECTIONS 4
56 84b4c632 galberding
#define MAX_DEVIATION_FACTOR 35
57
#define DEVIATION_CORRECTION_DURATION 900
58 b24df8ad galberding
#define DEVIATION_CORRECTION_SPEED 2000000
59
#define DEVIATION_CORRECTION_VALUE (DEVIATION_CORRECTION_SPEED / 2)
60 84b4c632 galberding
#define DEVIATION_DIST_THRESH 25000
61 27d4e1fa galberding
62 58fe0e0b Thomas Schöpping
namespace amiro {
63
64 181f2892 galberding
65 58fe0e0b Thomas Schöpping
class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>
66
{
67 181f2892 galberding
68
  // Messages which can be received and trigger state changes
69
  public:
70
71
    // States of user thread state machine
72 10bf9cc0 galberding
    enum states : int8_t{
73 27d4e1fa galberding
      IDLE                = 0,
74
      FOLLOW_LINE         = 1,
75
      DETECT_STATION      = 2,
76
      REVERSE             = 3,
77
      PUSH_BACK           = 4,
78
      CHECK_POSITIONING   = 5,
79
      CHECK_VOLTAGE       = 6,
80
      CHARGING            = 7,
81
      RELEASE             = 8,
82
      RELEASE_TO_CORRECT  = 9,
83
      CORRECT_POSITIONING = 10,
84 10bf9cc0 galberding
      TURN                = 12,
85
      INACTIVE            = 13,
86
      CALIBRATION         = 14,
87
      CALIBRATION_CHECK   = 15,
88 b24df8ad galberding
      DEVIATION_CORRECTION = 16,
89 10bf9cc0 galberding
      DOCKING_ERROR       = -1,
90
      REVERSE_TIMEOUT_ERROR   = -2,
91
      CALIBRATION_ERROR   = -3,
92
      WHITE_DETECTION_ERROR   = -4,
93
      PROXY_DETECTION_ERROR   = -5,
94
      NO_CHARGING_POWER_ERROR   = -6,
95
      UNKNOWN_STATE_ERROR   = -7
96 181f2892 galberding
    };
97
98 27d4e1fa galberding
  struct ut_counter{
99
      int whiteCount = 0;
100 10bf9cc0 galberding
      int ringProxCount = 0;
101 fbcb25cc galberding
      // int correctionCount = 0;
102 27d4e1fa galberding
      int errorCount = 0;
103 10bf9cc0 galberding
      int stateCount = 0;
104 fbcb25cc galberding
      int stepCount = 0;
105 10bf9cc0 galberding
      uint32_t stateTime = 0;
106
  };
107
108
  struct proxy_ctrl {
109
    int threshLow = 100;
110
    int threshMid = 500;
111
    int threshHigh = 1500;
112
    // int threshHigh = 1500;
113
    int penalty = 100000;
114
    int pFactor = 310000;
115
    int staticCont = 0;
116 27d4e1fa galberding
  };
117 181f2892 galberding
118 10bf9cc0 galberding
119
  struct bottom_prox_calibration {
120
    bool calibrateBlack = true;
121
    uint32_t buf = 0;
122
    uint8_t meanWindow = 150;
123
  };
124 8dced1c9 galberding
125 b24df8ad galberding
  struct deviation_correction {
126
    bool RCase = true;
127
    int8_t pCount = 0;
128
    int32_t proxbuf[PROX_DEVIATION_MEAN_WINDOW] = { 0 };
129
    int32_t currentDeviation = 0;
130
  };
131
132 27d4e1fa galberding
  // static const struct ut_counter emptyUtCount;
133
  ut_counter utCount;
134 10bf9cc0 galberding
  proxy_ctrl pCtrl;
135 8dced1c9 galberding
  bottom_prox_calibration proxCalib;
136 b24df8ad galberding
  deviation_correction devCor;
137
138
139 58fe0e0b Thomas Schöpping
  explicit UserThread();
140
141
  virtual ~UserThread();
142
143
  virtual msg_t main();
144 181f2892 galberding
145
private:
146
  void setRpmSpeed(const int (&rpmSpeed)[2]);
147 c9fa414d galberding
  void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]);
148 181f2892 galberding
  void lightOneLed(Color color, int idx);
149
  void lightAllLeds(Color color);
150
  /**
151
   * Uses light ring indicate the state of charge.
152
   */
153
  void showChargingState();
154
  void checkForMotion();
155
  bool checkPinVoltage();
156
  bool checkPinEnabled();
157 c9fa414d galberding
  int getProxyRingSum();
158 10bf9cc0 galberding
159
  /**
160 b24df8ad galberding
  * Returns percentage of mean deviation between two given values.
161
  * It is intended to calculate the mean deviation between two proxy sensor
162
  * values. PROX_DEVIATION_MEAN_WINDOW determains the size of the mean window.
163 8dced1c9 galberding
  * Keep in mind that initial results are wrong.
164 b24df8ad galberding
  * */
165
  int32_t meanDeviation(uint16_t a, uint16_t b);
166
167
  /**
168 10bf9cc0 galberding
   * Check sectors around and stop if a thresh in one sector is detected.
169
   */
170
  void preventCollision(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]);
171 8dced1c9 galberding
172 10bf9cc0 galberding
  /**
173
   * Same as prevent collision but also lowers the speed when object is detected.
174
   */
175
  void proxSectorSpeedCorrection(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]);
176
  void getProxySectorVals(uint16_t (&proxVals)[8], uint16_t (&sProx)[8]);
177
  void getMaxFrontSectorVal(uint16_t (&sProx)[8], int32_t &sPMax);
178
  void chargeAsLED();
179 8dced1c9 galberding
180 fbcb25cc galberding
  /**
181
   * Returns true when front sensors reaching high values
182
   * and all others are low. This indicates that the loading station is ahead.
183
   * If other sensores are blocked too, indicates that someone grabs the robot.
184
   */
185
  bool checkFrontalObject();
186 c9fa414d galberding
187 181f2892 galberding
  /**
188
   * Check if current position changes when the wheel are deactivated.
189 8dced1c9 galberding
   *
190 181f2892 galberding
   * When AMiRo drives towards the loading station, it stops when a specific marker is reached.
191
   * In order to validate that the AMiRo is correctly positioned in the loading station
192
   * the wheels are turned off. When the position remains the same the docking procedure
193 8dced1c9 galberding
   * was successful (return 1) otherwise a correction is needed (return 0).
194 181f2892 galberding
   */
195
  int checkDockingSuccess();
196 10bf9cc0 galberding
197
  // State Variables
198
  states prevState = states::IDLE;
199
  states currentState = states::IDLE;
200
  states newState = states::IDLE;
201
202
  bool continue_on_obstacle = true;
203
  uint16_t rProx[8]; // buffer for ring proxy values
204
  int rpmSpeed[2] = {0};
205
  int stop[2] = {0};
206
  int turn[2] = {5,-5};
207
208 58fe0e0b Thomas Schöpping
};
209
210
} // end of namespace amiro
211
212
#endif // AMIRO_USERTHREAD_H_