88MotorControlNode::MotorControlNode () : Node(" motor_control_node" ) {
99 RCLCPP_INFO (this ->get_logger (), " Motor Control Node Initiated" );
1010
11- // Initialize Phidget BLDC motors
11+ // Initialize Phidget Motor Velocity Controllers
1212 for (int i = 0 ; i < NUM_MOTORS; i++) {
13- PhidgetReturnCode ret = PhidgetBLDCMotor_create (&motors[i]);
14- if (ret != EPHIDGET_OK) {
15- handlePhidgetError (ret, " creating motor" , i);
16- continue ;
17- }
13+ PhidgetReturnCode ret;
14+
15+ ret = PhidgetBLDCMotor_create (&motors[i]);
16+ handlePhidgetError (ret, " creating motor" , i);
17+ ret = PhidgetMotorVelocityController_create (&motor_velocity_controllers[i]);
18+ handlePhidgetError (ret, " creating velocity controller" , i);
1819
1920 ret = Phidget_setHubPort ((PhidgetHandle)motors[i], i);
20- handlePhidgetError (ret, " set hub port" , i);
21+ handlePhidgetError (ret, " setting motor hub port" , i);
22+ ret = Phidget_setHubPort ((PhidgetHandle)motor_velocity_controllers[i], i);
23+ handlePhidgetError (ret, " setting velocity controller hub port" , i);
2124
2225 ret = Phidget_openWaitForAttachment ((PhidgetHandle)motors[i], 5000 );
23- handlePhidgetError (ret, " attachment" , i);
26+ handlePhidgetError (ret, " opening motor" , i);
27+ ret = Phidget_openWaitForAttachment ((PhidgetHandle)motor_velocity_controllers[i], 5000 );
28+ handlePhidgetError (ret, " opening velocity controller" , i);
29+
30+ ret = PhidgetBLDCMotor_setRescaleFactor (motors[i], MOTOR_RESCALE_FACTOR);
31+ handlePhidgetError (ret, " set motor rescale factor" , i);
32+ ret = PhidgetMotorVelocityController_setRescaleFactor (motor_velocity_controllers[i], MOTOR_RESCALE_FACTOR);
33+ handlePhidgetError (ret, " set rescale factor" , i);
34+
35+ PhidgetMotorVelocityController_setEngaged (motor_velocity_controllers[i], 1 );
2436 }
2537
2638 auto qos = rclcpp::QoS (rclcpp::KeepLast (64 ));
@@ -37,7 +49,7 @@ MotorControlNode::MotorControlNode() : Node("motor_control_node") {
3749
3850 // Enable failsafe for all motors
3951 for (int i = 0 ; i < NUM_MOTORS; i++) {
40- ret = PhidgetBLDCMotor_enableFailsafe (motors[i], MOTOR_FAILSAFE_INTERVAL_MS);
52+ PhidgetReturnCode ret = PhidgetBLDCMotor_enableFailsafe (motors[i], MOTOR_FAILSAFE_INTERVAL_MS);
4153 handlePhidgetError (ret, " enable failsafe" , i);
4254 }
4355
@@ -89,13 +101,13 @@ void MotorControlNode::handlePhidgetError(PhidgetReturnCode ret, const std::stri
89101
90102void MotorControlNode::runMotors (const std::vector<int >& selected_motors, float velocity) {
91103 PhidgetLog_enable (PHIDGET_LOG_INFO, " phidgetlog.log" );
92- velocity = std::clamp (velocity, -1 .0f , 1 .0f );
104+
105+ double velocity_rad_s = velocity / WHEEL_RADIUS_METERS;
106+ velocity_rad_s = std::clamp (velocity_rad_s, -MOTOR_MAX_VELOCITY_RADIANS, MOTOR_MAX_VELOCITY_RADIANS);
93107
94108 for (int motor_index : selected_motors) {
95- PhidgetReturnCode ret = PhidgetBLDCMotor_setTargetVelocity (motors[motor_index], velocity);
96- if (ret != EPHIDGET_OK) {
97- handlePhidgetError (ret, " set target velocity" , motor_index);
98- }
109+ PhidgetReturnCode ret = PhidgetMotorVelocityController_setTargetVelocity (motor_velocity_controllers[motor_index], (double )velocity_rad_s);
110+ handlePhidgetError (ret, " set target velocity" , motor_index);
99111 }
100112}
101113
@@ -107,27 +119,35 @@ void MotorControlNode::publishDriveFeedback() {
107119 message.target_velocities .resize (NUM_MOTORS);
108120 message.positions .resize (NUM_MOTORS);
109121
110- auto get_phidget_val = [&](auto phidget_func, int i, double & val, const char * label) {
111- ret = phidget_func (motors[i], &val);
122+ for (int i = 0 ; i < NUM_MOTORS; i++) {
123+ PhidgetReturnCode ret;
124+
125+ ret = PhidgetMotorVelocityController_getVelocity (motor_velocity_controllers[i], &message.velocities [i]);
112126 if (ret != EPHIDGET_OK) {
113- handlePhidgetError (ret, label , i);
127+ handlePhidgetError (ret, " get velocity " , i);
114128 message.valid_data [i] = false ;
115129 }
116- };
117130
118- for (int i = 0 ; i < NUM_MOTORS; i++) {
119- get_phidget_val (PhidgetBLDCMotor_getVelocity, i, message.velocities [i], " get velocity" );
120- get_phidget_val (PhidgetBLDCMotor_getTargetVelocity, i, message.target_velocities [i], " get target velocity" );
121- get_phidget_val (PhidgetBLDCMotor_getPosition, i, message.positions [i], " get position" );
131+ ret = PhidgetMotorVelocityController_getTargetVelocity (motor_velocity_controllers[i], &message.target_velocities [i]);
132+ if (ret != EPHIDGET_OK) {
133+ handlePhidgetError (ret, " get target velocity" , i);
134+ message.valid_data [i] = false ;
135+ }
136+
137+ ret = PhidgetBLDCMotor_getPosition (motors[i], &message.positions [i]);
138+ if (ret != EPHIDGET_OK) {
139+ handlePhidgetError (ret, " get position" , i);
140+ message.valid_data [i] = false ;
141+ }
122142 }
123143
124144 drive_feedback_pub_->publish (message);
125145}
126146
127147void MotorControlNode::resetFailsafe () {
128148 for (int i = 0 ; i < NUM_MOTORS; i++) {
129- ret = PhidgetBLDCMotor_resetFailsafe (motors[i]);
130- if (ret != EPHIDGET_OK) handlePhidgetError (ret, " failsafe" , i);
149+ PhidgetReturnCode ret = PhidgetBLDCMotor_resetFailsafe (motors[i]);
150+ handlePhidgetError (ret, " failsafe" , i);
131151 }
132152}
133153
0 commit comments