@@ -95,21 +95,33 @@ static const DRV8434S_DriverCfg_t drv8434_cfg = {
9595// if less than 1000 µsteps between two notches: notch with small gap detected
9696// we can then go to the 0/passthrough by applying a 120º+center degree movement
9797#define POLARIZER_CLOSE_NOTCH_DETECTION_MICROSTEPS 1000
98-
98+ #define POLARIZER_WHEEL_HOMING_SPIN_ATTEMPTS 3
9999static K_SEM_DEFINE (home_sem , 0 , 1 ) ;
100100
101101// Enable encoder interrupt
102102static ret_code_t
103- enable_encoder_interrupt (void )
103+ enable_encoder (void )
104104{
105+ const int ret = gpio_pin_configure_dt (& polarizer_encoder_enable_spec ,
106+ GPIO_OUTPUT_ACTIVE );
107+ if (ret ) {
108+ return ret ;
109+ }
110+
105111 return gpio_pin_interrupt_configure_dt (& polarizer_encoder_spec ,
106112 GPIO_INT_EDGE_RISING );
107113}
108114
109115// Disable the interrupt
110116static ret_code_t
111- disable_encoder_interrupt (void )
117+ disable_encoder (void )
112118{
119+ const int ret = gpio_pin_configure_dt (& polarizer_encoder_enable_spec ,
120+ GPIO_OUTPUT_INACTIVE );
121+ if (ret ) {
122+ return ret ;
123+ }
124+
113125 return gpio_pin_interrupt_configure_dt (& polarizer_encoder_spec ,
114126 GPIO_INT_DISABLE );
115127}
@@ -170,8 +182,9 @@ polarizer_move(const uint32_t frequency)
170182static int
171183polarizer_stop ()
172184{
185+ const int ret = pwm_set_dt (polarizer_step_pwm_spec , 0 , 0 );
173186 disable_step_interrupt ();
174- return pwm_set_dt ( polarizer_step_pwm_spec , 0 , 0 ) ;
187+ return ret ;
175188}
176189
177190static int
@@ -293,6 +306,15 @@ polarizer_wheel_step_relative(uint32_t frequency, int32_t step_count)
293306 return ret_val ;
294307}
295308
309+ static void
310+ homing_failed ()
311+ {
312+ g_polarizer_wheel_instance .status =
313+ orb_mcu_HardwareDiagnostic_Status_STATUS_INITIALIZATION_ERROR ;
314+ polarizer_stop ();
315+ disable_encoder ();
316+ }
317+
296318static void
297319polarizer_wheel_auto_homing_thread (void * p1 , void * p2 , void * p3 )
298320{
@@ -303,7 +325,7 @@ polarizer_wheel_auto_homing_thread(void *p1, void *p2, void *p3)
303325 clear_step_interrupt ();
304326
305327 // enable encoder interrupt to detect notches
306- enable_encoder_interrupt ();
328+ enable_encoder ();
307329
308330 /*
309331 * Below is a representation of the notches on the wheel (encoder):
@@ -316,22 +338,40 @@ polarizer_wheel_auto_homing_thread(void *p1, void *p2, void *p3)
316338 g_polarizer_wheel_instance .homing .notch_count = 0 ;
317339 set_direction (POLARIZER_WHEEL_DIRECTION_FORWARD );
318340 while (!notch_0_detected ) {
319- // on each loop, turn the wheel more than 120º to detect the next
320- // encoder notch
321- polarizer_wheel_step_relative (
322- POLARIZER_WHEEL_SPIN_PWM_FREQUENCY_DEFAULT ,
323- POLARIZER_WHEEL_MICROSTEPS_120_DEGREES * 2 );
324-
325- // wait for notch detection
326- const int ret = k_sem_take (& home_sem , K_SECONDS (10 ));
327- if (ret != 0 ) {
328- // no wheel?
329- g_polarizer_wheel_instance .status =
330- orb_mcu_HardwareDiagnostic_Status_STATUS_INITIALIZATION_ERROR ;
331- polarizer_stop ();
332- disable_encoder_interrupt ();
333- LOG_ERR ("polarizer wheel not detected" );
334- return ;
341+ size_t spin_attempt = 0 ;
342+ while (spin_attempt < POLARIZER_WHEEL_HOMING_SPIN_ATTEMPTS ) {
343+ // clear the step count before each spin attempt
344+ atomic_clear (& g_polarizer_wheel_instance .step_count .current );
345+
346+ // spin the wheel 120º, to be done up to
347+ // POLARIZER_WHEEL_HOMING_SPIN_ATTEMPTS times
348+ int ret = polarizer_wheel_step_relative (
349+ POLARIZER_WHEEL_SPIN_PWM_FREQUENCY_DEFAULT ,
350+ POLARIZER_WHEEL_MICROSTEPS_120_DEGREES );
351+ if (ret != RET_SUCCESS ) {
352+ LOG_ERR ("Unable to spin polarizer wheel: %d, attempt %u" , ret ,
353+ spin_attempt );
354+ homing_failed ();
355+ return ;
356+ }
357+ ret = k_sem_take (& home_sem , K_SECONDS (4 ));
358+ if (ret == 0 ) {
359+ break ;
360+ }
361+ spin_attempt ++ ;
362+ }
363+
364+ if (spin_attempt != 0 ) {
365+ LOG_WRN ("Spin attempt %u, current step counter: %u" , spin_attempt ,
366+ (uint32_t )atomic_get (
367+ & g_polarizer_wheel_instance .step_count .current ));
368+ if (spin_attempt == POLARIZER_WHEEL_HOMING_SPIN_ATTEMPTS ) {
369+ // encoder not detected, no wheel?
370+ homing_failed ();
371+ LOG_ERR (
372+ "Encoder not detected, is there a wheel? is it moving?" );
373+ return ;
374+ }
335375 }
336376
337377 LOG_INF ("homing: steps: %ld, notch count: %d" ,
@@ -362,9 +402,9 @@ polarizer_wheel_auto_homing_thread(void *p1, void *p2, void *p3)
362402 POLARIZER_WHEEL_MICROSTEPS_120_DEGREES );
363403
364404 // wait for completion and disconnect interrupt
365- k_sleep (K_SECONDS (2 ));
405+ k_sleep (K_SECONDS (4 ));
366406 disable_step_interrupt ();
367- disable_encoder_interrupt ();
407+ disable_encoder ();
368408
369409 LOG_INF ("Polarizer wheel homed" );
370410 g_polarizer_wheel_instance .status =
@@ -510,7 +550,7 @@ polarizer_wheel_init(const orb_mcu_Hardware *hw_version)
510550
511551 // Enable the polarizer motor encoder
512552 ret_val = gpio_pin_configure_dt (& polarizer_encoder_enable_spec ,
513- GPIO_OUTPUT_ACTIVE );
553+ GPIO_OUTPUT_INACTIVE );
514554 if (ret_val != 0 ) {
515555 ASSERT_SOFT (ret_val );
516556 return RET_ERROR_INTERNAL ;
@@ -540,7 +580,7 @@ polarizer_wheel_init(const orb_mcu_Hardware *hw_version)
540580 return RET_ERROR_INTERNAL ;
541581 }
542582
543- ret_val = disable_encoder_interrupt ();
583+ ret_val = disable_encoder ();
544584 if (ret_val != 0 ) {
545585 ASSERT_SOFT (ret_val );
546586 return RET_ERROR_INTERNAL ;
0 commit comments