Skip to content

Commit 59a6ad4

Browse files
authored
feat(bldc_haptics): Update to use shared_ptr instead of reference_wrapper (#442)
1 parent 1d1550f commit 59a6ad4

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

components/bldc_haptics/example/main/bldc_haptics_example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extern "C" void app_main(void) {
5858

5959
// now make the bldc motor
6060
using BldcMotor = espp::BldcMotor<espp::BldcDriver, Encoder>;
61-
auto motor = BldcMotor(BldcMotor::Config{
61+
auto motor = std::make_shared<BldcMotor>(BldcMotor::Config{
6262
// measured by setting it into ANGLE_OPENLOOP and then counting how many
6363
// spots you feel when rotating it.
6464
.num_pole_pairs = 7,

components/bldc_haptics/include/bldc_haptics.hpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
9797
public:
9898
/// @brief Configuration for the haptic motor
9999
struct Config {
100-
std::reference_wrapper<M> motor; ///< Pointer to the motor to use for haptics
100+
std::shared_ptr<M> motor; ///< Pointer to the motor to use for haptics
101101
float kp_factor{2}; ///< Factor to multiply the detent strength by to get kp (default 2). Used
102102
///< for both detents and end stops. \note Depending on the motor, this may
103103
///< need to be adjusted to get the desired behavior.
@@ -132,7 +132,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
132132
"\tkd_factor_max: {}",
133133
kp_factor_, kd_factor_min_, kd_factor_max_);
134134
// set the motion control type to torque
135-
motor_.get().set_motion_control_type(detail::MotionControlType::TORQUE);
135+
motor_->set_motion_control_type(detail::MotionControlType::TORQUE);
136136
// create the motor task
137137
motor_task_ = Task::make_unique(
138138
{.callback = std::bind(&BldcHaptics::motor_task, this, std::placeholders::_1,
@@ -161,7 +161,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
161161
// enable the motor
162162
{
163163
std::unique_lock<std::mutex> lk(motor_mutex_);
164-
motor_.get().enable();
164+
motor_->enable();
165165
}
166166
if (is_running()) {
167167
return;
@@ -174,7 +174,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
174174
// disable the motor
175175
{
176176
std::unique_lock<std::mutex> lk(motor_mutex_);
177-
motor_.get().disable();
177+
motor_->disable();
178178
}
179179
if (!is_running()) {
180180
return;
@@ -199,7 +199,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
199199
void update_detent_config(const detail::DetentConfig &config) {
200200
std::unique_lock<std::mutex> lk(detent_mutex_);
201201
// update the detent center
202-
current_detent_center_ = motor_.get().get_shaft_angle();
202+
current_detent_center_ = motor_->get_shaft_angle();
203203

204204
// update the detent config
205205
detent_config_ = config;
@@ -261,19 +261,19 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
261261
// TODO: Use the PID controller to control the haptics
262262
// Play a hardcoded haptic "click"
263263
float strength = config.strength; // 5 or 1.5 were used in SmartKnob
264-
motor_.get().move(strength);
264+
motor_->move(strength);
265265
using namespace std::chrono_literals;
266266
for (uint8_t i = 0; i < 3; i++) {
267-
motor_.get().loop_foc();
267+
motor_->loop_foc();
268268
std::this_thread::sleep_for(1ms);
269269
}
270-
motor_.get().move(-strength);
270+
motor_->move(-strength);
271271
for (uint8_t i = 0; i < 3; i++) {
272-
motor_.get().loop_foc();
272+
motor_->loop_foc();
273273
std::this_thread::sleep_for(1ms);
274274
}
275-
motor_.get().move(0);
276-
motor_.get().loop_foc();
275+
motor_->move(0);
276+
motor_->loop_foc();
277277
}
278278

279279
protected:
@@ -313,7 +313,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
313313

314314
// check our position vs the nearest detent, and update our position if we're
315315
// close enough to snap to another detent
316-
float motor_angle = motor_.get().get_shaft_angle();
316+
float motor_angle = motor_->get_shaft_angle();
317317
float angle_to_detent_center = motor_angle - current_detent_center_;
318318

319319
// Handle the snap point - if we're close enough to the snap point, snap
@@ -383,11 +383,11 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
383383

384384
// Apply motor torque based on our angle to the nearest detent (detent
385385
// strength, etc is handled by the pid parameters)
386-
if (std::abs(motor_.get().get_shaft_velocity()) > 60) {
386+
if (std::abs(motor_->get_shaft_velocity()) > 60) {
387387
// Don't apply torque if velocity is too high (helps avoid positive
388388
// feedback loop/runaway)
389389
logger_.info_rate_limited("velocity too high, not applying torque");
390-
motor_.get().move(0);
390+
motor_->move(0);
391391
} else {
392392
// apply torque based on our angle to the nearest detent
393393
float input = -angle_to_detent_center + dead_zone_adjustment;
@@ -409,9 +409,9 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
409409
logger_.debug_rate_limited("angle: {:0.3f}, input: {:0.3f}, torque: {:0.3f}", motor_angle,
410410
input, torque);
411411
// apply the torque to the motor
412-
motor_.get().move(torque);
413-
} // end if std::abs(motor_.get().get_shaft_velocity()) > 60
414-
motor_.get().loop_foc();
412+
motor_->move(torque);
413+
} // end if std::abs(motor_->get_shaft_velocity()) > 60
414+
motor_->loop_foc();
415415
} // end motor_mutex_
416416

417417
// now sleep
@@ -435,8 +435,8 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
435435
std::mutex detent_mutex_; ///< Mutex for accessing the detents
436436
detail::DetentConfig detent_config_; ///< Configuration for the detents
437437

438-
std::mutex motor_mutex_; ///< Mutex for accessing the motor
439-
std::reference_wrapper<M> motor_; ///< Pointer to the motor to use for haptics
438+
std::mutex motor_mutex_; ///< Mutex for accessing the motor
439+
std::shared_ptr<M> motor_; ///< Pointer to the motor to use for haptics
440440

441441
std::unique_ptr<Task> motor_task_; ///< Task which runs the haptic motor
442442
};

0 commit comments

Comments
 (0)