소스 검색

Check if the IMU is 'good' and periodically re-initialise if not.

Michael Hope 3 년 전
부모
커밋
0a335c01ae
3개의 변경된 파일15개의 추가작업 그리고 1개의 파일을 삭제
  1. 9 1
      roverif/imu.cc
  2. 2 0
      roverif/imu.h
  3. 4 0
      roverif/main.cc

+ 9 - 1
roverif/imu.cc 파일 보기

@@ -27,7 +27,7 @@ enum Codes {
27 27
 };
28 28
 
29 29
 IMU::IMU()
30
-    : mpu_(MPU6050_ADDRESS_AD0_HIGH) {
30
+    : mpu_(MPU6050_ADDRESS_AD0_HIGH), good_(false) {
31 31
 }
32 32
 
33 33
 void IMU::init() {
@@ -50,6 +50,7 @@ uint8_t IMU::wait() {
50 50
 
51 51
 bool IMU::read(Protocol::IMU* pinto) {
52 52
     if (!mpu_.testConnection()) {
53
+        good_ = false;
53 54
         return false;
54 55
     }
55 56
 
@@ -59,9 +60,16 @@ bool IMU::read(Protocol::IMU* pinto) {
59 60
                     pinto->gyros + 0,
60 61
                     pinto->gyros + 1,
61 62
                     pinto->gyros + 2);
63
+    good_ = true;
62 64
     return true;
63 65
 }
64 66
 
67
+void IMU::check() {
68
+    if (!good_) {
69
+        init();
70
+    }
71
+}
72
+
65 73
 #define ERRORX(x) { dbg("err " #x "\r\n"); return -x; }
66 74
 #define ERROR(x) { return -x; }
67 75
 #define DBG(x)

+ 2 - 0
roverif/imu.h 파일 보기

@@ -10,6 +10,7 @@ public:
10 10
 
11 11
     void init();
12 12
     bool read(Protocol::IMU* pinto);
13
+    void check();
13 14
 
14 15
 private:
15 16
     friend class I2Cdev;
@@ -21,5 +22,6 @@ private:
21 22
     static int8_t set_address(uint8_t device, uint8_t address);
22 23
 
23 24
     MPU6050 mpu_;
25
+    bool good_;
24 26
 };
25 27
 

+ 4 - 0
roverif/main.cc 파일 보기

@@ -19,6 +19,7 @@ uint8_t RoverIf::pilot_supplied_;
19 19
 uint8_t RoverIf::demands_;
20 20
 
21 21
 Timer imu_timer;
22
+Timer check_timer;
22 23
 Timer blinker_timer;
23 24
 Timer heartbeat_timer;
24 25
 Timer pwmin_limiter;
@@ -202,6 +203,9 @@ void RoverIf::tick() {
202 203
         defer(Pending::Heartbeat);
203 204
         defer(Pending::Counters);
204 205
     }
206
+    if (check_timer.tick(500)) {
207
+        imu.check();
208
+    }
205 209
 }
206 210
 
207 211
 #define DISPATCH_REQUEST(_name) \