Skip to content

Commit 804381b

Browse files
Disable stepper driver during initialization and ensure it only activates during motion
1 parent cff3e20 commit 804381b

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

app/src/Focuser.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ namespace
2525
Focuser::Focuser(FocuserStepper &stepper, PositionStore *store, const char *firmware_version)
2626
: m_firmware_version(firmware_version), m_stepper(stepper), m_store(store)
2727
{
28-
(void)set_stepper_driver_enabled(true);
28+
/* Do not touch hardware here; start with the stepper driver disabled.
29+
* The driver is enabled only for the duration of motion.
30+
*/
2931
}
3032

3133
int Focuser::initialise()
@@ -37,7 +39,8 @@ int Focuser::initialise()
3739
return -ENODEV;
3840
}
3941

40-
int ret = set_stepper_driver_enabled(true);
42+
/* Ensure the stepper driver starts disabled; it will be enabled during motion only. */
43+
int ret = set_stepper_driver_enabled(false);
4144
if (ret != 0)
4245
{
4346
return ret;
@@ -284,20 +287,23 @@ void Focuser::move_to(uint16_t target)
284287
interval_ns = m_state.step_interval_ns;
285288
}
286289

287-
if (set_stepper_driver_enabled(true) != 0)
290+
const bool enabled_for_move = (set_stepper_driver_enabled(true) == 0);
291+
if (!enabled_for_move)
288292
{
289293
return;
290294
}
291295

292296
if (apply_step_interval(interval_ns) != 0)
293297
{
298+
(void)set_stepper_driver_enabled(false);
294299
return;
295300
}
296301

297302
int ret = m_stepper.move_to(static_cast<int32_t>(target));
298303
if (ret != 0)
299304
{
300305
LOG_ERR("Failed to start move to 0x%04x (%d)", target, ret);
306+
(void)set_stepper_driver_enabled(false);
301307
return;
302308
}
303309

tests/app/focuser/src/main.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ ZTEST(focuser_app, test_initialise_requires_ready_stepper)
132132
"should not set reference position on failure");
133133
zassert_equal(fake_stepper_set_microstep_interval_fake.call_count, 0U,
134134
"should not update step interval on failure");
135-
zassert_equal(fake_stepper_drv_enable_fake.call_count, 1U,
136-
"only constructor enable should run");
135+
zassert_equal(fake_stepper_drv_enable_fake.call_count, 0U,
136+
"driver should not be enabled during startup");
137+
zassert_equal(fake_stepper_drv_disable_fake.call_count, 0U,
138+
"driver should not be touched when hardware is not ready");
137139
}
138140

139141
ZTEST(focuser_app, test_initialise_configures_stepper_when_ready)
@@ -153,10 +155,10 @@ ZTEST(focuser_app, test_initialise_configures_stepper_when_ready)
153155
"step interval should be applied once");
154156
zassert_equal(fake_stepper_set_microstep_interval_fake.arg1_val, 500000ULL,
155157
"default speed uses 500us interval");
156-
zassert_equal(fake_stepper_drv_enable_fake.call_count, 2U,
157-
"constructor plus initialise enable calls expected");
158-
zassert_equal(fake_stepper_drv_disable_fake.call_count, 0U,
159-
"driver should remain enabled after init");
158+
zassert_equal(fake_stepper_drv_enable_fake.call_count, 0U,
159+
"driver should not be enabled during init");
160+
zassert_equal(fake_stepper_drv_disable_fake.call_count, 1U,
161+
"initialise should ensure the driver starts disabled");
160162
}
161163

162164
ZTEST(focuser_app, test_initialise_restores_position_from_store)
@@ -260,14 +262,16 @@ ZTEST(focuser_app, test_initialise_ignores_ealready_from_driver)
260262
ZephyrFocuserStepper stepper(k_stepper_controller, k_stepper_driver);
261263
Focuser focuser(stepper, nullptr, kFirmwareVersion);
262264

263-
int enable_results[] = {-EALREADY, -EALREADY};
264-
SET_RETURN_SEQ(fake_stepper_drv_enable, enable_results, 2);
265+
int disable_results[] = {-EALREADY};
266+
SET_RETURN_SEQ(fake_stepper_drv_disable, disable_results, 1);
265267

266268
const int ret = focuser.initialise();
267269

268270
zassert_equal(ret, 0, "-EALREADY responses should not fail init");
269-
zassert_equal(fake_stepper_drv_enable_fake.call_count, 2U,
270-
"two enable attempts should have occurred");
271+
zassert_equal(fake_stepper_drv_disable_fake.call_count, 1U,
272+
"initialise should attempt to disable the driver once");
273+
zassert_equal(fake_stepper_drv_enable_fake.call_count, 0U,
274+
"initialise should not enable the driver");
271275
}
272276

273277
ZTEST(focuser_app, test_eeprom_position_store_roundtrip)

0 commit comments

Comments
 (0)