ClearCore Library
EncoderInput.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
31 #ifndef __ENCODER_INPUT_H__
32 #define __ENCODER_INPUT_H__
33 
34 #include "PeripheralRoute.h"
35 
37 #define VEL_EST_SAMPLES 50
38 
39 namespace ClearCore {
40 
41 typedef void (*voidFuncPtr)(void);
42 
54 class EncoderInput {
55  friend class SysManager;
56 public:
57 
58 #ifndef HIDE_FROM_DOXYGEN
59 
63  EncoderInput();
64 
76  volatile const int16_t& HwPosition() {
77  return m_hwPosn;
78  }
79 
85  void IndexDetected(int16_t posn) {
86  m_hwIndex = posn;
87  m_processIndex = true;
88  }
89 #endif
90 
106  int32_t Position();
107 
122  int32_t Position(int32_t newPosn);
123 
136  void AddToPosition(int32_t posnAdjust);
137 
157  int32_t IndexPosition();
158 
171  void Enable(bool isEnabled);
172 
185  void SwapDirection(bool isSwapped);
186 
201  volatile const int32_t& Velocity() {
202  return m_velocity;
203  }
204 
220  volatile const bool& IndexDetected() {
221  return m_indexDetected;
222  }
223 
239  void IndexInverted(bool invert);
240 
256  bool QuadratureError();
257 
272  void ClearQuadratureError();
273 
289  volatile const int16_t& StepsLastSample() {
290  return m_stepsLast;
291  }
292 
293 private:
294  const PeripheralRoute *m_aInfo;
295  const PeripheralRoute *m_bInfo;
296  const PeripheralRoute *m_indexInfo;
297  int32_t m_curPosn;
298  int32_t m_offsetAdjustment;
299  int32_t m_velocity;
300  int16_t m_hwPosn;
301  int32_t m_posnHistory[VEL_EST_SAMPLES];
302  uint8_t m_posnHistoryIndex;
303  bool m_enabled;
304  bool m_processIndex;
305  int16_t m_hwIndex;
306  int32_t m_indexPosn;
307  bool m_indexDetected;
308  bool m_indexInverted;
309  int16_t m_stepsLast;
310 
311  void Initialize();
312 
313  void Update();
314 
315 }; // EncoderInput
316 
317 } // ClearCore namespace
318 
319 #endif /* __ENCODER_INPUT_H__ */
void IndexInverted(bool invert)
Invert the edge that the index detection triggers on.
int32_t IndexPosition()
Read the last index position of the encoder.
Defines the Peripheral Route structure, used in HardwareMapping.
void SwapDirection(bool isSwapped)
Swap the sense of positive and negative encoder directions.
ClearCore Position Sensor Decoder.
Definition: EncoderInput.h:54
void Enable(bool isEnabled)
Set whether the encoder input should be active or not.
#define VEL_EST_SAMPLES
Number of encoder samples to use for velocity calculation.
Definition: EncoderInput.h:37
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
volatile const int16_t & StepsLastSample()
Get the number of encoder steps received in the last sample time.
Definition: EncoderInput.h:289
volatile const bool & IndexDetected()
Check if there was an index pulse in the last sample time.
Definition: EncoderInput.h:220
bool QuadratureError()
Query for a quadrature error.
int32_t Position()
Read the current position of the encoder.
void(* voidFuncPtr)(void)
Definition: DigitalIn.h:43
void AddToPosition(int32_t posnAdjust)
Adjust the current position of the encoder.
void ClearQuadratureError()
Clear a quadrature error.
volatile const int32_t & Velocity()
Read the velocity of the encoder input (counts per second)
Definition: EncoderInput.h:201
ClearCore Board Supervisory System Manager.
Definition: SysManager.h:58