motor.cpp
Go to the documentation of this file.
1 #include "phidgets_api/motor.h"
2 
3 #include <cassert>
4 
5 // Get and return the value name of type type (I varient is indexed)
6 #define GAR(type, name) \
7  type x; \
8  int ret = CPhidgetMotorControl_get##name(motor_handle_, &x); \
9  assert(ret == EPHIDGET_OK); \
10  return x;
11 #define GAR_I(type, name, i) \
12  type x; \
13  int ret = CPhidgetMotorControl_get##name(motor_handle_, i, &x); \
14  assert(ret == EPHIDGET_OK); \
15  return x;
16 
17 // Set the value name to x (I varient is indexed)
18 #define SET(name, x) \
19  int ret = CPhidgetMotorControl_set##name(motor_handle_, x); \
20  assert(ret == EPHIDGET_OK);
21 #define SET_I(name, x, i) \
22  int ret = CPhidgetMotorControl_set##name(motor_handle_, i, x); \
23  assert(ret == EPHIDGET_OK);
24 
25 namespace phidgets {
26 
27 MotorController::MotorController() : Phidget(), motor_handle_(nullptr)
28 {
29  // create the handle
30  CPhidgetMotorControl_create(&motor_handle_);
31 
32  // pass handle to base class
33  Phidget::init((CPhidgetHandle)motor_handle_);
34 
35  // register base class callbacks
37 
38  // register motor data callbacks
39  CPhidgetMotorControl_set_OnVelocityChange_Handler(
40  motor_handle_, VelocityChangeHandler, this);
41  CPhidgetMotorControl_set_OnCurrentChange_Handler(
42  motor_handle_, CurrentChangeHandler, this);
43  CPhidgetMotorControl_set_OnInputChange_Handler(motor_handle_,
44  InputChangeHandler, this);
45  CPhidgetMotorControl_set_OnEncoderPositionChange_Handler(
46  motor_handle_, EncoderPositionChangeHandler, this);
47  CPhidgetMotorControl_set_OnEncoderPositionUpdate_Handler(
48  motor_handle_, EncoderPositionUpdateHandler, this);
49  CPhidgetMotorControl_set_OnBackEMFUpdate_Handler(
50  motor_handle_, BackEMFUpdateHandler, this);
51  CPhidgetMotorControl_set_OnSensorUpdate_Handler(motor_handle_,
52  SensorUpdateHandler, this);
53  CPhidgetMotorControl_set_OnCurrentUpdate_Handler(
54  motor_handle_, CurrentUpdateHandler, this);
55 }
56 
58 {
59 }
60 
62 {
63  GAR(int, MotorCount);
64 }
65 
67 {
68  GAR_I(double, Velocity, index);
69 }
70 
71 void MotorController::setVelocity(int index, double velocity)
72 {
73  SET_I(Velocity, velocity, index);
74 }
75 
77 {
78  GAR_I(double, Acceleration, index);
79 }
80 
81 void MotorController::setAcceleration(int index, double acceleration)
82 {
83  SET_I(Acceleration, acceleration, index);
84 }
85 
87 {
88  GAR_I(double, AccelerationMax, index);
89 }
90 
92 {
93  GAR_I(double, AccelerationMin, index);
94 }
95 
96 double MotorController::getCurrent(int index)
97 {
98  GAR_I(double, Current, index);
99 }
100 
102 {
103  GAR(int, InputCount);
104 }
105 
107 {
108  int state;
109  int ret = CPhidgetMotorControl_getInputState(motor_handle_, index, &state);
110 
111  assert(ret == EPHIDGET_OK);
112 
113  return state == PTRUE;
114 }
115 
117 {
118  GAR(int, EncoderCount);
119 }
120 
122 {
123  GAR_I(int, EncoderPosition, index);
124 }
125 
126 void MotorController::setEncoderPosition(int index, int position)
127 {
128  SET_I(EncoderPosition, position, index);
129 }
130 
132 {
133  GAR_I(int, BackEMFSensingState, index);
134 }
135 
136 void MotorController::setBackEMFSensingState(int index, int bEMFState)
137 {
138  SET_I(BackEMFSensingState, bEMFState, index);
139 }
140 
142 {
143  GAR_I(double, BackEMF, index);
144 }
145 
147 {
148  GAR(double, SupplyVoltage);
149 }
150 
152 {
153  GAR_I(double, Braking, index);
154 }
155 
156 void MotorController::setBraking(int index, double braking)
157 {
158  SET_I(Braking, braking, index);
159 }
160 
162 {
163  GAR(int, SensorCount);
164 }
165 
167 {
168  GAR_I(int, SensorValue, index);
169 }
170 
172 {
173  GAR_I(int, SensorRawValue, index);
174 }
175 
177 {
178  GAR(int, Ratiometric);
179 }
180 
181 void MotorController::setRatiometric(int ratiometric)
182 {
183  SET(Ratiometric, ratiometric);
184 }
185 
187  CPhidgetMotorControlHandle /* phid */, void *userPtr, int index,
188  double velocity)
189 {
190  ((MotorController *)userPtr)->velocityChangeHandler(index, velocity);
191  return 0;
192 }
193 
194 int MotorController::CurrentChangeHandler(CPhidgetMotorControlHandle /* phid */,
195  void *userPtr, int index,
196  double current)
197 {
198  ((MotorController *)userPtr)->currentChangeHandler(index, current);
199  return 0;
200 }
201 
202 int MotorController::InputChangeHandler(CPhidgetMotorControlHandle /* phid */,
203  void *userPtr, int index,
204  int inputState)
205 {
206  ((MotorController *)userPtr)->inputChangeHandler(index, inputState);
207  return 0;
208 }
209 
211  CPhidgetMotorControlHandle /* phid */, void *userPtr, int index, int time,
212  int positionChange)
213 {
214  ((MotorController *)userPtr)
215  ->encoderPositionChangeHandler(index, time, positionChange);
216  return 0;
217 }
218 
220  CPhidgetMotorControlHandle /* phid */, void *userPtr, int index,
221  int positionChange)
222 {
223  ((MotorController *)userPtr)
224  ->encoderPositionUpdateHandler(index, positionChange);
225  return 0;
226 }
227 
228 int MotorController::BackEMFUpdateHandler(CPhidgetMotorControlHandle /* phid */,
229  void *userPtr, int index,
230  double voltage)
231 {
232  ((MotorController *)userPtr)->backEMFUpdateHandler(index, voltage);
233  return 0;
234 }
235 
236 int MotorController::SensorUpdateHandler(CPhidgetMotorControlHandle /* phid */,
237  void *userPtr, int index,
238  int sensorValue)
239 {
240  ((MotorController *)userPtr)->sensorUpdateHandler(index, sensorValue);
241  return 0;
242 }
243 
244 int MotorController::CurrentUpdateHandler(CPhidgetMotorControlHandle /* phid */,
245  void *userPtr, int index,
246  double current)
247 {
248  ((MotorController *)userPtr)->currentUpdateHandler(index, current);
249  return 0;
250 }
251 
253  double /* velocity */)
254 {
255  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
256 }
257 
259  double /* current */)
260 {
261  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
262 }
263 
264 void MotorController::inputChangeHandler(int /* index */, int /* inputState */)
265 {
266  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
267 }
268 
270  int /* time */,
271  int /* positionChange */)
272 {
273  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
274 }
275 
277  int /* positionChange */)
278 {
279  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
280 }
281 
283  double /* voltage */)
284 {
285  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
286 }
287 
289  int /* sensorValue */)
290 {
291  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
292 }
293 
295  double /* current */)
296 {
297  // This method can be overridden in a concrete subclass (e.g., ROS wrapper)
298 }
299 
300 } // namespace phidgets
double getVelocity(int index)
Definition: motor.cpp:66
void setRatiometric(int ratiometric)
Definition: motor.cpp:181
static int InputChangeHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, int inputState)
Definition: motor.cpp:202
double getCurrent(int index)
Definition: motor.cpp:96
virtual void backEMFUpdateHandler(int index, double voltage)
Definition: motor.cpp:282
virtual void encoderPositionUpdateHandler(int index, int positionChange)
Definition: motor.cpp:276
void setEncoderPosition(int index, int position)
Definition: motor.cpp:126
void registerHandlers()
Definition: phidget.cpp:17
static int EncoderPositionChangeHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, int time, int positionChange)
Definition: motor.cpp:210
static int SensorUpdateHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, int sensorValue)
Definition: motor.cpp:236
virtual void currentUpdateHandler(int index, double current)
Definition: motor.cpp:294
static int EncoderPositionUpdateHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, int positionChange)
Definition: motor.cpp:219
virtual ~MotorController()
Definition: motor.cpp:57
static int BackEMFUpdateHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, double voltage)
Definition: motor.cpp:228
bool getInputState(int index)
Definition: motor.cpp:106
double getAcceleration(int index)
Definition: motor.cpp:76
int getEncoderPosition(int index)
Definition: motor.cpp:121
int getBackEMFSensingState(int index)
Definition: motor.cpp:131
static int CurrentChangeHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, double current)
Definition: motor.cpp:194
int getSensorRawValue(int index)
Definition: motor.cpp:171
#define SET_I(name, x, i)
Definition: motor.cpp:21
CPhidgetMotorControlHandle motor_handle_
Definition: motor.h:64
virtual void encoderPositionChangeHandler(int index, int time, int positionChange)
Definition: motor.cpp:269
virtual void sensorUpdateHandler(int index, int sensorValue)
Definition: motor.cpp:288
static int VelocityChangeHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, double velocity)
Definition: motor.cpp:186
void setVelocity(int index, double velocity)
Definition: motor.cpp:71
virtual void inputChangeHandler(int index, int inputState)
Definition: motor.cpp:264
double getBackEMF(int index)
Definition: motor.cpp:141
int getSensorValue(int index)
Definition: motor.cpp:166
#define GAR(type, name)
Definition: motor.cpp:6
void setAcceleration(int index, double acceleration)
Definition: motor.cpp:81
#define GAR_I(type, name, i)
Definition: motor.cpp:11
#define SET(name, x)
Definition: motor.cpp:18
double getBraking(int index)
Definition: motor.cpp:151
void setBraking(int index, double braking)
Definition: motor.cpp:156
void setBackEMFSensingState(int index, int bEMFState)
Definition: motor.cpp:136
double getAccelerationMax(int index)
Definition: motor.cpp:86
void init(CPhidgetHandle handle)
Definition: phidget.cpp:24
virtual void velocityChangeHandler(int index, double velocity)
Definition: motor.cpp:252
virtual void currentChangeHandler(int index, double current)
Definition: motor.cpp:258
double getAccelerationMin(int index)
Definition: motor.cpp:91
static int CurrentUpdateHandler(CPhidgetMotorControlHandle phid, void *userPtr, int index, double current)
Definition: motor.cpp:244


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Fri Apr 9 2021 02:56:02