Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.033 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
#include <cmath>
10

    
11

    
12

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

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

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

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

    
50

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

    
62
namespace amiro {
63

    
64

    
65
class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>
66
{
67

    
68
  // Messages which can be received and trigger state changes
69
  public:
70

    
71
    // States of user thread state machine
72
    enum states : int8_t{
73
      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
      TURN                = 12,
85
      INACTIVE            = 13,
86
      CALIBRATION         = 14,
87
      CALIBRATION_CHECK   = 15,
88
      DEVIATION_CORRECTION = 16,
89
      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
    };
97

    
98
  struct ut_counter{
99
      int whiteCount = 0;
100
      int ringProxCount = 0;
101
      // int correctionCount = 0;
102
      int errorCount = 0;
103
      int stateCount = 0;
104
      int stepCount = 0;
105
      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
  };
117

    
118

    
119
  struct bottom_prox_calibration {
120
    bool calibrateBlack = true;
121
    uint32_t buf = 0;
122
    uint8_t meanWindow = 150;
123
  };
124

    
125
  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
  // static const struct ut_counter emptyUtCount;
133
  ut_counter utCount;
134
  proxy_ctrl pCtrl;
135
  bottom_prox_calibration proxCalib;
136
  deviation_correction devCor;
137

    
138

    
139
  explicit UserThread();
140

    
141
  virtual ~UserThread();
142

    
143
  virtual msg_t main();
144

    
145
private:
146
  void setRpmSpeed(const int (&rpmSpeed)[2]);
147
  void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]);
148
  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
  int getProxyRingSum();
158

    
159
  /**
160
  * 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
  * Keep in mind that initial results are wrong.
164
  * */
165
  int32_t meanDeviation(uint16_t a, uint16_t b);
166

    
167
  /**
168
   * 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

    
172
  /**
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

    
180
  /**
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

    
187
  /**
188
   * Check if current position changes when the wheel are deactivated.
189
   *
190
   * 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
   * was successful (return 1) otherwise a correction is needed (return 0).
194
   */
195
  int checkDockingSuccess();
196

    
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
};
209

    
210
} // end of namespace amiro
211

    
212
#endif // AMIRO_USERTHREAD_H_