encoder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, Open Source Robotics Foundation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <functional>
31 
32 #include <libphidget22/phidget22.h>
33 
34 #include "phidgets_api/encoder.h"
35 #include "phidgets_api/phidget22.h"
36 
37 namespace phidgets {
38 
40  int32_t serial_number, int hub_port, bool is_hub_port_device, int channel,
41  std::function<void(int, int, double, int)> position_change_handler)
42  : channel_(channel), position_change_handler_(position_change_handler)
43 {
44  // create the handle
45  PhidgetReturnCode ret = PhidgetEncoder_create(&encoder_handle_);
46  if (ret != EPHIDGET_OK)
47  {
48  throw Phidget22Error("Failed to create Encoder handle", ret);
49  }
50 
52  reinterpret_cast<PhidgetHandle>(encoder_handle_), serial_number,
53  hub_port, is_hub_port_device, channel);
54 
55  ret = PhidgetEncoder_setOnPositionChangeHandler(
57  if (ret != EPHIDGET_OK)
58  {
59  throw Phidget22Error(
60  "Failed to set change handler for Encoder channel " +
61  std::to_string(channel),
62  ret);
63  }
64 }
65 
67 {
68  PhidgetHandle handle = reinterpret_cast<PhidgetHandle>(encoder_handle_);
69  helpers::closeAndDelete(&handle);
70 }
71 
72 int64_t Encoder::getPosition() const
73 {
74  int64_t position;
75  PhidgetReturnCode ret =
76  PhidgetEncoder_getPosition(encoder_handle_, &position);
77  if (ret != EPHIDGET_OK)
78  {
79  throw Phidget22Error("Failed to get position for Encoder channel " +
80  std::to_string(channel_),
81  ret);
82  }
83 
84  return position;
85 }
86 
87 void Encoder::setPosition(int64_t position) const
88 {
89  PhidgetReturnCode ret =
90  PhidgetEncoder_setPosition(encoder_handle_, position);
91  if (ret != EPHIDGET_OK)
92  {
93  throw Phidget22Error("Failed to set position for Encoder channel " +
94  std::to_string(channel_),
95  ret);
96  }
97 }
98 
100 {
101  int64_t position;
102  PhidgetReturnCode ret =
103  PhidgetEncoder_getIndexPosition(encoder_handle_, &position);
104  if (ret != EPHIDGET_OK)
105  {
106  throw Phidget22Error(
107  "Failed to get index position for Encoder channel " +
108  std::to_string(channel_),
109  ret);
110  }
111 
112  return position;
113 }
114 
116 {
117  int enabled;
118  PhidgetReturnCode ret =
119  PhidgetEncoder_getEnabled(encoder_handle_, &enabled);
120  if (ret != EPHIDGET_OK)
121  {
122  throw Phidget22Error("Failed to get enabled for Encoder channel " +
123  std::to_string(channel_),
124  ret);
125  }
126 
127  return enabled == PTRUE;
128 }
129 
130 void Encoder::setEnabled(bool enabled) const
131 {
132  PhidgetReturnCode ret =
133  PhidgetEncoder_setEnabled(encoder_handle_, enabled ? PTRUE : PFALSE);
134  if (ret != EPHIDGET_OK)
135  {
136  throw Phidget22Error("Failed to set enabled for Encoder channel " +
137  std::to_string(channel_),
138  ret);
139  }
140 }
141 
142 Phidget_EncoderIOMode Encoder::getIOMode() const
143 {
144  Phidget_EncoderIOMode io_mode;
145  PhidgetReturnCode ret = PhidgetEncoder_getIOMode(encoder_handle_, &io_mode);
146  if (ret != EPHIDGET_OK)
147  {
148  throw Phidget22Error("Failed to get IO Mode for Encoder channel " +
149  std::to_string(channel_),
150  ret);
151  }
152  return io_mode;
153 }
154 
155 void Encoder::setIOMode(Phidget_EncoderIOMode io_mode) const
156 {
157  PhidgetReturnCode ret = PhidgetEncoder_setIOMode(encoder_handle_, io_mode);
158  if (ret != EPHIDGET_OK)
159  {
160  throw Phidget22Error("Failed to set IO Mode for Encoder channel " +
161  std::to_string(channel_),
162  ret);
163  }
164 }
165 
166 uint32_t Encoder::getDataInterval() const
167 {
168  uint32_t data_interval_ms;
169  PhidgetReturnCode ret =
170  PhidgetEncoder_getDataInterval(encoder_handle_, &data_interval_ms);
171  if (ret != EPHIDGET_OK)
172  {
173  throw Phidget22Error(
174  "Failed to get data interval for Encoder channel " +
175  std::to_string(channel_),
176  ret);
177  }
178  return data_interval_ms;
179 }
180 
181 void Encoder::setDataInterval(uint32_t data_interval_ms) const
182 {
183  PhidgetReturnCode ret =
184  PhidgetEncoder_setDataInterval(encoder_handle_, data_interval_ms);
185  if (ret != EPHIDGET_OK)
186  {
187  throw Phidget22Error(
188  "Failed to set data interval for Encoder channel " +
189  std::to_string(channel_),
190  ret);
191  }
192 }
193 
194 void Encoder::positionChangeHandler(int position_change, double time,
195  int index_triggered)
196 {
197  position_change_handler_(channel_, position_change, time, index_triggered);
198 }
199 
200 void Encoder::PositionChangeHandler(PhidgetEncoderHandle /* phid */, void *ctx,
201  int position_change, double time,
202  int index_triggered)
203 {
204  ((Encoder *)ctx)
205  ->positionChangeHandler(position_change, time, index_triggered);
206 }
207 
208 } // namespace phidgets
phidgets
Definition: accelerometer.h:39
encoder.h
phidgets::Encoder::getDataInterval
uint32_t getDataInterval() const
Definition: encoder.cpp:166
phidgets::Encoder::setEnabled
void setEnabled(bool enabled) const
Set the powered state of an encoder. If an encoder is not enabled, it will not be given power,...
Definition: encoder.cpp:130
phidgets::Encoder::channel_
int channel_
Definition: encoder.h:89
phidgets::Encoder::encoder_handle_
PhidgetEncoderHandle encoder_handle_
Definition: encoder.h:91
phidgets::Encoder::getPosition
int64_t getPosition() const
Reads the current position of an encoder.
Definition: encoder.cpp:72
phidgets::Encoder::setIOMode
void setIOMode(Phidget_EncoderIOMode io_mode) const
Definition: encoder.cpp:155
phidgets::Encoder::getEnabled
bool getEnabled() const
Checks if an encoder is powered on and receiving events.
Definition: encoder.cpp:115
phidgets::helpers::openWaitForAttachment
void openWaitForAttachment(PhidgetHandle handle, int32_t serial_number, int hub_port, bool is_hub_port_device, int channel)
Definition: phidget22.cpp:57
phidgets::Encoder::~Encoder
~Encoder()
Definition: encoder.cpp:66
phidgets::Encoder::PositionChangeHandler
static void PositionChangeHandler(PhidgetEncoderHandle phid, void *ctx, int position_change, double time_change, int index_triggered)
Definition: encoder.cpp:200
phidgets::helpers::closeAndDelete
void closeAndDelete(PhidgetHandle *handle) noexcept
Definition: phidget22.cpp:93
phidgets::Encoder::setPosition
void setPosition(int64_t position) const
Sets the offset of an encoder such that current position is the specified value.
Definition: encoder.cpp:87
phidget22.h
phidgets::Phidget22Error
Definition: phidget22.h:46
phidgets::Encoder::setDataInterval
void setDataInterval(uint32_t data_interval_ms) const
Definition: encoder.cpp:181
phidgets::Encoder::position_change_handler_
std::function< void(int, int, double, int)> position_change_handler_
Definition: encoder.h:90
phidgets::Encoder::Encoder
Encoder(int32_t serial_number, int hub_port, bool is_hub_port_device, int channel, std::function< void(int, int, double, int)> position_change_handler)
Definition: encoder.cpp:39
phidgets::Encoder::getIndexPosition
int64_t getIndexPosition() const
Gets the position of an encoder the last time an index pulse occured. An index pulse in this context ...
Definition: encoder.cpp:99
phidgets::Encoder
Definition: encoder.h:41
phidgets::Encoder::positionChangeHandler
void positionChangeHandler(int position_change, double time, int index_triggered)
Definition: encoder.cpp:194
phidgets::Encoder::getIOMode
Phidget_EncoderIOMode getIOMode() const
Definition: encoder.cpp:142


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Sun May 11 2025 02:20:27