Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / userthread.hpp @ a0033dcb

History | View | Annotate | Download (3.348 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

    
8

    
9
// Speed when driving towards the docking station
10
#define CHARGING_SPEED 5
11
#define DETECTION_SPEED 10
12
#define DIST_THRESH 100
13
#define RELEASE_COUNT 200
14
// Thresh to determain how much update steps should pass while alining
15
#define MAX_CORRECTION_STEPS 300
16
// Thresh for wheel proxy sensors, when summed values fall below the state changes
17
#define PROXY_WHEEL_THRESH 18000
18
// Thresh for detecting obsticles
19
#define PROXY_RING_THRESH 15000
20

    
21
#define PUSH_BACK_COUNT 5
22
// Thresh for how long (update steps) the front sensors are allowed to detect white
23
#define WHITE_COUNT_THRESH 150
24
// Rotation around 180 degrees in microradian
25
#define ROTATION_180 3141592
26
// Rotation around -20 degrees in microradian
27
#define ROTATION_20 -349065
28
#define ROTATION_DURATION 10000
29

    
30
#define RING_PROX_FRONT_THRESH 18000
31
#define PROX_MAX_VAL 65430
32

    
33
// Threshold for failing to dock
34
#define DOCKING_ERROR_THRESH 4
35

    
36
namespace amiro {
37

    
38

    
39
class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>
40
{
41

    
42
  // Messages which can be received and trigger state changes
43
  public:
44
    enum msg_content{
45
      STOP, 
46
      START,
47
      EDGE_LEFT,
48
      EDGE_RIGHT,
49
      FUZZY,
50
      DOCK,
51
      UNDOCK,
52
      CHARGE
53
    };
54

    
55
    // States of user thread state machine
56
    enum states : uint8_t{
57
      IDLE                = 0,
58
      FOLLOW_LINE         = 1,
59
      DETECT_STATION      = 2,
60
      REVERSE             = 3,
61
      PUSH_BACK           = 4,
62
      CHECK_POSITIONING   = 5,
63
      CHECK_VOLTAGE       = 6,
64
      CHARGING            = 7,
65
      RELEASE             = 8,
66
      RELEASE_TO_CORRECT  = 9,
67
      CORRECT_POSITIONING = 10,
68
      ERROR               = 11,
69
      TURN                =12
70
    };
71

    
72
  struct ut_counter{
73
      int whiteCount = 0;
74
      int proxyCount = 0;
75
      // int correctionCount = 0;
76
      int errorCount = 0;
77
      int idleCount = 0;
78
      int stepCount = 0;
79
  };
80

    
81
  // static const struct ut_counter emptyUtCount;
82
  ut_counter utCount;
83

    
84
  explicit UserThread();
85

    
86
  virtual ~UserThread();
87

    
88
  virtual msg_t main();
89

    
90
private:
91
  void setRpmSpeed(const int (&rpmSpeed)[2]);
92
  void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]);
93
  void lightOneLed(Color color, int idx);
94
  void lightAllLeds(Color color);
95
  /**
96
   * Uses light ring indicate the state of charge.
97
   */
98
  void showChargingState();
99
  void checkForMotion();
100
  bool checkPinVoltage();
101
  bool checkPinEnabled();
102
  int getProxyRingSum();
103
  
104
  /**
105
   * Returns true when front sensors reaching high values
106
   * and all others are low. This indicates that the loading station is ahead.
107
   * If other sensores are blocked too, indicates that someone grabs the robot.
108
   */
109
  bool checkFrontalObject();
110

    
111
  states utState = states::IDLE;
112
  states newState = states::IDLE;
113
  bool continue_on_obstacle = true;
114
  /**
115
   * Check if current position changes when the wheel are deactivated.
116
   * 
117
   * When AMiRo drives towards the loading station, it stops when a specific marker is reached.
118
   * In order to validate that the AMiRo is correctly positioned in the loading station
119
   * the wheels are turned off. When the position remains the same the docking procedure
120
   * was successful (return 1) otherwise a correction is needed (return 0). 
121
   */
122
  int checkDockingSuccess();
123
};
124

    
125
} // end of namespace amiro
126

    
127
#endif // AMIRO_USERTHREAD_H_