WPILibC++  2020.3.2
EncoderSim.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <memory>
11 #include <utility>
12 
13 #include "CallbackStore.h"
14 #include "mockdata/EncoderData.h"
15 
16 namespace frc {
17 namespace sim {
18 class EncoderSim {
19  public:
20  explicit EncoderSim(int index) { m_index = index; }
21 
22  std::unique_ptr<CallbackStore> RegisterInitializedCallback(
23  NotifyCallback callback, bool initialNotify) {
24  auto store = std::make_unique<CallbackStore>(
25  m_index, -1, callback, &HALSIM_CancelEncoderInitializedCallback);
26  store->SetUid(HALSIM_RegisterEncoderInitializedCallback(
27  m_index, &CallbackStoreThunk, store.get(), initialNotify));
28  return store;
29  }
30 
31  bool GetInitialized() const { return HALSIM_GetEncoderInitialized(m_index); }
32 
33  void SetInitialized(bool initialized) {
34  HALSIM_SetEncoderInitialized(m_index, initialized);
35  }
36 
37  std::unique_ptr<CallbackStore> RegisterCountCallback(NotifyCallback callback,
38  bool initialNotify) {
39  auto store = std::make_unique<CallbackStore>(
40  m_index, -1, callback, &HALSIM_CancelEncoderCountCallback);
41  store->SetUid(HALSIM_RegisterEncoderCountCallback(
42  m_index, &CallbackStoreThunk, store.get(), initialNotify));
43  return store;
44  }
45 
46  int GetCount() const { return HALSIM_GetEncoderCount(m_index); }
47 
48  void SetCount(int count) { HALSIM_SetEncoderCount(m_index, count); }
49 
50  std::unique_ptr<CallbackStore> RegisterPeriodCallback(NotifyCallback callback,
51  bool initialNotify) {
52  auto store = std::make_unique<CallbackStore>(
53  m_index, -1, callback, &HALSIM_CancelEncoderPeriodCallback);
54  store->SetUid(HALSIM_RegisterEncoderPeriodCallback(
55  m_index, &CallbackStoreThunk, store.get(), initialNotify));
56  return store;
57  }
58 
59  double GetPeriod() const { return HALSIM_GetEncoderPeriod(m_index); }
60 
61  void SetPeriod(double period) { HALSIM_SetEncoderPeriod(m_index, period); }
62 
63  std::unique_ptr<CallbackStore> RegisterResetCallback(NotifyCallback callback,
64  bool initialNotify) {
65  auto store = std::make_unique<CallbackStore>(
66  m_index, -1, callback, &HALSIM_CancelEncoderResetCallback);
67  store->SetUid(HALSIM_RegisterEncoderResetCallback(
68  m_index, &CallbackStoreThunk, store.get(), initialNotify));
69  return store;
70  }
71 
72  bool GetReset() const { return HALSIM_GetEncoderReset(m_index); }
73 
74  void SetReset(bool reset) { HALSIM_SetEncoderReset(m_index, reset); }
75 
76  std::unique_ptr<CallbackStore> RegisterMaxPeriodCallback(
77  NotifyCallback callback, bool initialNotify) {
78  auto store = std::make_unique<CallbackStore>(
79  m_index, -1, callback, &HALSIM_CancelEncoderMaxPeriodCallback);
80  store->SetUid(HALSIM_RegisterEncoderMaxPeriodCallback(
81  m_index, &CallbackStoreThunk, store.get(), initialNotify));
82  return store;
83  }
84 
85  double GetMaxPeriod() const { return HALSIM_GetEncoderMaxPeriod(m_index); }
86 
87  void SetMaxPeriod(double maxPeriod) {
88  HALSIM_SetEncoderMaxPeriod(m_index, maxPeriod);
89  }
90 
91  std::unique_ptr<CallbackStore> RegisterDirectionCallback(
92  NotifyCallback callback, bool initialNotify) {
93  auto store = std::make_unique<CallbackStore>(
94  m_index, -1, callback, &HALSIM_CancelEncoderDirectionCallback);
95  store->SetUid(HALSIM_RegisterEncoderDirectionCallback(
96  m_index, &CallbackStoreThunk, store.get(), initialNotify));
97  return store;
98  }
99 
100  bool GetDirection() const { return HALSIM_GetEncoderDirection(m_index); }
101 
102  void SetDirection(bool direction) {
103  HALSIM_SetEncoderDirection(m_index, direction);
104  }
105 
106  std::unique_ptr<CallbackStore> RegisterReverseDirectionCallback(
107  NotifyCallback callback, bool initialNotify) {
108  auto store = std::make_unique<CallbackStore>(
109  m_index, -1, callback, &HALSIM_CancelEncoderReverseDirectionCallback);
110  store->SetUid(HALSIM_RegisterEncoderReverseDirectionCallback(
111  m_index, &CallbackStoreThunk, store.get(), initialNotify));
112  return store;
113  }
114 
115  bool GetReverseDirection() const {
116  return HALSIM_GetEncoderReverseDirection(m_index);
117  }
118 
119  void SetReverseDirection(bool reverseDirection) {
120  HALSIM_SetEncoderReverseDirection(m_index, reverseDirection);
121  }
122 
123  std::unique_ptr<CallbackStore> RegisterSamplesToAverageCallback(
124  NotifyCallback callback, bool initialNotify) {
125  auto store = std::make_unique<CallbackStore>(
126  m_index, -1, callback, &HALSIM_CancelEncoderSamplesToAverageCallback);
127  store->SetUid(HALSIM_RegisterEncoderSamplesToAverageCallback(
128  m_index, &CallbackStoreThunk, store.get(), initialNotify));
129  return store;
130  }
131 
132  int GetSamplesToAverage() const {
133  return HALSIM_GetEncoderSamplesToAverage(m_index);
134  }
135 
136  void SetSamplesToAverage(int samplesToAverage) {
137  HALSIM_SetEncoderSamplesToAverage(m_index, samplesToAverage);
138  }
139 
140  std::unique_ptr<CallbackStore> RegisterDistancePerPulseCallback(
141  NotifyCallback callback, bool initialNotify) {
142  auto store = std::make_unique<CallbackStore>(
143  m_index, -1, callback, &HALSIM_CancelEncoderDistancePerPulseCallback);
144  store->SetUid(HALSIM_RegisterEncoderDistancePerPulseCallback(
145  m_index, &CallbackStoreThunk, store.get(), initialNotify));
146  return store;
147  }
148 
149  double GetDistancePerPulse() const {
150  return HALSIM_GetEncoderDistancePerPulse(m_index);
151  }
152 
153  void SetDistancePerPulse(double distancePerPulse) {
154  HALSIM_SetEncoderDistancePerPulse(m_index, distancePerPulse);
155  }
156 
157  void ResetData() { HALSIM_ResetEncoderData(m_index); }
158 
159  private:
160  int m_index;
161 };
162 } // namespace sim
163 } // namespace frc
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::sim::EncoderSim
Definition: EncoderSim.h:18