APM:Libraries
WheelEncoder_Quadrature.h
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 #pragma once
16 
17 #include "AP_WheelEncoder.h"
18 #include "WheelEncoder_Backend.h"
19 #include <Filter/Filter.h>
20 #include <AP_Math/AP_Math.h>
21 
23 {
24 public:
25  // constructor
27 
28  // update state
29  void update(void);
30 
31 private:
32 
33  // gpio interrupt handlers
34  static int irq_handler0_pina(int irq, void *context); // instance 0's pin_a handler
35  static int irq_handler0_pinb(int irq, void *context); // instance 0's pin_b handler
36  static int irq_handler1_pina(int irq, void *context); // instance 1's pin_a handler
37  static int irq_handler1_pinb(int irq, void *context); // instance 1's pin_b handler
38  static void irq_handler(uint8_t instance, bool pin_a); // combined irq handler
39 
40  // get gpio id from pin number
41  static uint32_t get_gpio(uint8_t pin_number);
42 
43  // convert pin a and b status to phase
44  static uint8_t pin_ab_to_phase(bool pin_a, bool pin_b);
45 
46  // update phase, distance_count and error count using pin a and b's latest state
47  static void update_phase_and_error_count(bool pin_a_now, bool pin_b_now, uint8_t &phase, int32_t &distance_count, uint32_t &total_count, uint32_t &error_count);
48 
49  struct IrqState {
50  uint32_t last_gpio_a; // gpio used for pin a
51  uint32_t last_gpio_b; // gpio used for pin b
52  uint8_t phase; // current phase of encoder (from 0 to 3)
53  int32_t distance_count; // distance measured by cumulative steps forward or backwards
54  uint32_t total_count; // total number of successful readings from sensor (used for sensor quality calcs)
55  uint32_t error_count; // total number of errors reading from sensor (used for sensor quality calcs)
56  uint32_t last_reading_ms; // system time of last update from encoder
57  };
59 
60  // private members
61  uint8_t last_pin_a;
62  uint8_t last_pin_b;
63 };
static uint32_t get_gpio(uint8_t pin_number)
#define WHEELENCODER_MAX_INSTANCES
AP_WheelEncoder_Quadrature(AP_WheelEncoder &frontend, uint8_t instance, AP_WheelEncoder::WheelEncoder_State &state)
static int irq_handler0_pina(int irq, void *context)
static int irq_handler1_pinb(int irq, void *context)
static int state
Definition: Util.cpp:20
static struct IrqState irq_state[WHEELENCODER_MAX_INSTANCES]
static int irq_handler0_pinb(int irq, void *context)
static void irq_handler(uint8_t instance, bool pin_a)
static uint8_t pin_ab_to_phase(bool pin_a, bool pin_b)
static int irq_handler1_pina(int irq, void *context)
static void update_phase_and_error_count(bool pin_a_now, bool pin_b_now, uint8_t &phase, int32_t &distance_count, uint32_t &total_count, uint32_t &error_count)