@@ -97,7 +97,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
97
97
public:
98
98
// / @brief Configuration for the haptic motor
99
99
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
101
101
float kp_factor{2 }; // /< Factor to multiply the detent strength by to get kp (default 2). Used
102
102
// /< for both detents and end stops. \note Depending on the motor, this may
103
103
// /< need to be adjusted to get the desired behavior.
@@ -132,7 +132,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
132
132
" \t kd_factor_max: {}" ,
133
133
kp_factor_, kd_factor_min_, kd_factor_max_);
134
134
// 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);
136
136
// create the motor task
137
137
motor_task_ = Task::make_unique (
138
138
{.callback = std::bind (&BldcHaptics::motor_task, this , std::placeholders::_1,
@@ -161,7 +161,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
161
161
// enable the motor
162
162
{
163
163
std::unique_lock<std::mutex> lk (motor_mutex_);
164
- motor_. get (). enable ();
164
+ motor_-> enable ();
165
165
}
166
166
if (is_running ()) {
167
167
return ;
@@ -174,7 +174,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
174
174
// disable the motor
175
175
{
176
176
std::unique_lock<std::mutex> lk (motor_mutex_);
177
- motor_. get (). disable ();
177
+ motor_-> disable ();
178
178
}
179
179
if (!is_running ()) {
180
180
return ;
@@ -199,7 +199,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
199
199
void update_detent_config (const detail::DetentConfig &config) {
200
200
std::unique_lock<std::mutex> lk (detent_mutex_);
201
201
// update the detent center
202
- current_detent_center_ = motor_. get (). get_shaft_angle ();
202
+ current_detent_center_ = motor_-> get_shaft_angle ();
203
203
204
204
// update the detent config
205
205
detent_config_ = config;
@@ -261,19 +261,19 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
261
261
// TODO: Use the PID controller to control the haptics
262
262
// Play a hardcoded haptic "click"
263
263
float strength = config.strength ; // 5 or 1.5 were used in SmartKnob
264
- motor_. get (). move (strength);
264
+ motor_-> move (strength);
265
265
using namespace std ::chrono_literals;
266
266
for (uint8_t i = 0 ; i < 3 ; i++) {
267
- motor_. get (). loop_foc ();
267
+ motor_-> loop_foc ();
268
268
std::this_thread::sleep_for (1ms);
269
269
}
270
- motor_. get (). move (-strength);
270
+ motor_-> move (-strength);
271
271
for (uint8_t i = 0 ; i < 3 ; i++) {
272
- motor_. get (). loop_foc ();
272
+ motor_-> loop_foc ();
273
273
std::this_thread::sleep_for (1ms);
274
274
}
275
- motor_. get (). move (0 );
276
- motor_. get (). loop_foc ();
275
+ motor_-> move (0 );
276
+ motor_-> loop_foc ();
277
277
}
278
278
279
279
protected:
@@ -313,7 +313,7 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
313
313
314
314
// check our position vs the nearest detent, and update our position if we're
315
315
// close enough to snap to another detent
316
- float motor_angle = motor_. get (). get_shaft_angle ();
316
+ float motor_angle = motor_-> get_shaft_angle ();
317
317
float angle_to_detent_center = motor_angle - current_detent_center_;
318
318
319
319
// 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 {
383
383
384
384
// Apply motor torque based on our angle to the nearest detent (detent
385
385
// 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 ) {
387
387
// Don't apply torque if velocity is too high (helps avoid positive
388
388
// feedback loop/runaway)
389
389
logger_.info_rate_limited (" velocity too high, not applying torque" );
390
- motor_. get (). move (0 );
390
+ motor_-> move (0 );
391
391
} else {
392
392
// apply torque based on our angle to the nearest detent
393
393
float input = -angle_to_detent_center + dead_zone_adjustment;
@@ -409,9 +409,9 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
409
409
logger_.debug_rate_limited (" angle: {:0.3f}, input: {:0.3f}, torque: {:0.3f}" , motor_angle,
410
410
input, torque);
411
411
// 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 ();
415
415
} // end motor_mutex_
416
416
417
417
// now sleep
@@ -435,8 +435,8 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
435
435
std::mutex detent_mutex_; // /< Mutex for accessing the detents
436
436
detail::DetentConfig detent_config_; // /< Configuration for the detents
437
437
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
440
440
441
441
std::unique_ptr<Task> motor_task_; // /< Task which runs the haptic motor
442
442
};
0 commit comments