33 #include <libphidget22/phidget22.h>
40 Motor::Motor(int32_t serial_number,
int hub_port,
bool is_hub_port_device,
42 std::function<
void(
int,
double)> duty_cycle_change_handler,
43 std::function<
void(
int,
double)> back_emf_change_handler)
45 duty_cycle_change_handler_(duty_cycle_change_handler),
46 back_emf_change_handler_(back_emf_change_handler)
48 PhidgetReturnCode ret = PhidgetDCMotor_create(&
motor_handle_);
49 if (ret != EPHIDGET_OK)
51 throw Phidget22Error(
"Failed to create Motor handle for channel " +
52 std::to_string(channel),
57 reinterpret_cast<PhidgetHandle
>(
motor_handle_), serial_number, hub_port,
58 is_hub_port_device, channel);
60 ret = PhidgetDCMotor_setOnVelocityUpdateHandler(
62 if (ret != EPHIDGET_OK)
65 "Failed to set duty cycle update handler for Motor channel " +
66 std::to_string(channel),
72 if (ret != EPHIDGET_OK)
75 "Failed to set back EMF update handler for Motor channel " +
76 std::to_string(channel),
83 PhidgetHandle handle =
reinterpret_cast<PhidgetHandle
>(
motor_handle_);
90 PhidgetReturnCode ret =
92 if (ret != EPHIDGET_OK)
94 throw Phidget22Error(
"Failed to get duty cycle for Motor channel " +
103 PhidgetReturnCode ret =
105 if (ret != EPHIDGET_OK)
107 throw Phidget22Error(
"Failed to set duty cycle for Motor channel " +
116 PhidgetReturnCode ret =
118 if (ret != EPHIDGET_OK)
120 throw Phidget22Error(
"Failed to get acceleration for Motor channel " +
129 PhidgetReturnCode ret =
131 if (ret != EPHIDGET_OK)
133 throw Phidget22Error(
"Failed to set acceleration for Motor channel " +
142 PhidgetReturnCode ret = PhidgetDCMotor_getBackEMF(
motor_handle_, &backemf);
143 if (ret != EPHIDGET_OK)
145 throw Phidget22Error(
"Failed to get back EMF for Motor channel " +
154 PhidgetReturnCode ret =
155 PhidgetDCMotor_setDataInterval(
motor_handle_, data_interval_ms);
156 if (ret != EPHIDGET_OK)
158 throw Phidget22Error(
"Failed to set data interval for Motor channel " +
167 PhidgetReturnCode ret =
169 if (ret != EPHIDGET_OK)
172 "Failed to get braking strength for Motor channel " +
181 PhidgetReturnCode ret =
182 PhidgetDCMotor_setTargetBrakingStrength(
motor_handle_, braking);
183 if (ret != EPHIDGET_OK)
186 "Failed to set braking strength for Motor channel " +
203 void *ctx,
double duty_cycle)
205 ((
Motor *)ctx)->dutyCycleChangeHandler(duty_cycle);
209 void *ctx,
double back_emf)
211 ((
Motor *)ctx)->backEMFChangeHandler(back_emf);