Khangai Robot Play - Naive  01
THe naive play of the Khangai Robot
bound_box.h
Go to the documentation of this file.
1 /*
2  * bound_box.h
3  *
4  * Created : 3/30/2019
5  * Author : n-is
6  * email : 073bex422.nischal@pcampus.edu.np
7  */
8 /*
9 
10  ^ 2
11  __ __ __ __
12  1 / \ 3
13  / \
14  | |
15  8 | | 4
16  | |
17  \ /
18  7 \__ __ __ __/ 5
19 
20  6
21 */
22 
23 #ifndef _BOUND_BOX_H_
24 #define _BOUND_BOX_H_
25 
26 #include "gpio.h"
27 
28 enum class Face {
29  _1 = 0,
30  _2,
31  _3,
32  _4,
33  _5,
34  _6,
35  _7,
36  _8
37 };
38 
39 
40 template <uint8_t N>
41 class Bound
42 {
43 public:
44  Bound() { clear(); }
45  Bound(Bound &&) = default;
46  Bound(const Bound &) = default;
47  Bound &operator=(Bound &&) = default;
48  Bound &operator=(const Bound &) = default;
49  ~Bound() { }
50 
62  void add_LimitSwitch(GPIO_TypeDef* gpio, uint16_t pin) {
63  gpios_[num_switches_] = gpio;
65  ++num_switches_;
66  }
67 
78  uint8_t read() { return switch_state_; }
79 
80  void update() {
81  uint8_t switch_val = 0;
82  for (uint8_t i = 0; i < num_switches_; ++i) {
83  if (HAL_GPIO_ReadPin(gpios_[i], gpio_pins_[i]) != GPIO_PIN_RESET) {
84  switch_val |= (1 << i);
85  }
86  }
87  switch_state_ = switch_val;
88  }
89 
90  uint8_t get_Num_Switches() { return num_switches_; }
91 
92  void clear() {
93  num_switches_ = 0;
94  switch_state_ = 0;
95  }
96 
97 private:
98  GPIO_TypeDef* gpios_[N];
99  uint16_t gpio_pins_[N];
100  uint8_t num_switches_;
101  uint8_t switch_state_;
102 };
103 
107 {
108 public:
109 
110 #define NUM_BOUNDS (8)
111 
112  Bound_Box(Bound_Box &&) = default;
113  Bound_Box(const Bound_Box &) = default;
114  Bound_Box &operator=(Bound_Box &&) = default;
115  Bound_Box &operator=(const Bound_Box &) = default;
117 
118  int init();
119  static Bound_Box& get_Instance();
120 
121  // updates the reading for all the available limit switches
122  void update() {
123  for (uint8_t i = 0; i < NUM_BOUNDS; ++i) {
124  if (bounds_[i].get_Num_Switches()) {
125  bounds_[i].update();
126  }
127  }
128  }
129 
130  uint8_t get_Bounds() {
131  uint8_t bounds_val = 0;
132  for (uint8_t i = 0; i < NUM_BOUNDS; ++i) {
133  if (bounds_[i].read()) {
134  bounds_val |= (1 << i);
135  }
136  }
137  return bounds_val;
138  }
139 
140  uint8_t get_Bound(uint8_t fence_no) {
141  uint8_t bound_val = 0;
142  if (fence_no && (fence_no <= NUM_BOUNDS)) {
143  bound_val = bounds_[fence_no - 1].read();
144  }
145  return bound_val;
146  }
147 
148 private:
149  Bound_Box() { }
150 
151  // Our robot is octagonal in shape and each fence will hold a maximum of
152  // 2 limit switches;
154 };
155 
156 #endif // !_BOUND_BOX_H_
Bound_Box & operator=(Bound_Box &&)=default
static Bound_Box & get_Instance()
Definition: bound_box.cpp:25
void update()
Definition: bound_box.h:122
void add_LimitSwitch(GPIO_TypeDef *gpio, uint16_t pin)
Add limit switch gpio to the bound.
Definition: bound_box.h:62
Definition: bound_box.h:41
uint8_t get_Bounds()
Definition: bound_box.h:130
Bound()
Definition: bound_box.h:44
uint8_t get_Bound(uint8_t fence_no)
Definition: bound_box.h:140
uint8_t num_switches_
Definition: bound_box.h:100
uint8_t get_Num_Switches()
Definition: bound_box.h:90
Face
Definition: bound_box.h:28
Bound< 2 > bounds_[NUM_BOUNDS]
Definition: bound_box.h:153
Bound_Box()
Definition: bound_box.h:149
void update()
Definition: bound_box.h:80
Bound & operator=(Bound &&)=default
uint8_t read()
Read which limit switches are pressed.
Definition: bound_box.h:78
#define NUM_BOUNDS
Definition: bound_box.h:110
uint8_t switch_state_
Definition: bound_box.h:101
~Bound_Box()
Definition: bound_box.h:116
uint16_t gpio_pins_[N]
Definition: bound_box.h:99
void clear()
Definition: bound_box.h:92
int init()
Definition: bound_box.cpp:32
Definition: bound_box.h:106
GPIO_TypeDef * gpios_[N]
Definition: bound_box.h:98
~Bound()
Definition: bound_box.h:49