Skip to content

Commit ebd1ce8

Browse files
committed
Use PhidgetMotorVelocityController for better motor control and odometry
1 parent dfc06f1 commit ebd1ce8

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

src/drive_control/include/motor_control.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,26 @@
77
#include "phidget22.h"
88
#include "rclcpp/qos.hpp" // Include QoS header
99
#include <vector>
10+
#include <cmath>
1011
#include <algorithm> // For std::clamp
1112

13+
// Motor and wheel specs for calulcating position and velocity factors below
14+
#define MOTOR_RPM 41
15+
#define MOTOR_GEAR_RATIO 23
16+
#define MOTOR_NUM_POLES 4
17+
#define MOTOR_NUM_PHASES 3
18+
#define WHEEL_RADIUS_METERS 0.1
19+
20+
/**
21+
* Multiplier to convert commutations to radians
22+
*/
23+
#define MOTOR_RESCALE_FACTOR (2.0 * M_PI) / (MOTOR_GEAR_RATIO * MOTOR_NUM_POLES * MOTOR_NUM_PHASES)
24+
25+
/**
26+
* Theoretical max motor velocity in radians/s
27+
*/
28+
#define MOTOR_MAX_VELOCITY_RADIANS (MOTOR_RPM / 60.0) * (2.0 * M_PI)
29+
1230
#define NUM_MOTORS 6
1331
#define DRIVE_FEEDBACK_PUBLISH_FREQUENCY_MS 100 // Publish frequency for drive_feedback_pub_
1432
#define MOTOR_FAILSAFE_INTERVAL_MS 500 // Interval for the Phidget failsafe to shut down the motors (in ms)
@@ -47,8 +65,8 @@ class MotorControlNode : public rclcpp::Node {
4765
rclcpp::TimerBase::SharedPtr failsafe_timer_;
4866

4967
// Constants and motor-related variables
50-
PhidgetBLDCMotorHandle motors[NUM_MOTORS]; // Array of motors
51-
PhidgetReturnCode ret, errorCode; // Return codes for Phidget functions
68+
PhidgetBLDCMotorHandle motors[NUM_MOTORS]; // Array of motors
69+
PhidgetMotorVelocityControllerHandle motor_velocity_controllers[NUM_MOTORS]; // Array of motor velocity controllers
5270
const char* errorString; // Error string for logging
5371
char errorDetail[100]; // Detailed error message
5472
size_t errorDetailLen = 100; // Length of the error detail buffer

src/drive_control/src/motor_control.cpp

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,31 @@
88
MotorControlNode::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

90102
void 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

127147
void 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

Comments
 (0)