@@ -16,7 +16,7 @@ use esp_hal::{
1616use esp_println:: { print, println} ;
1717
1818use embassy_executor:: Spawner ;
19- use embassy_time:: { Duration , Timer } ;
19+ use embassy_time:: { Duration , Instant , Timer } ;
2020use log:: { error, info} ;
2121use mma8x5x:: { GScale , Mma8x5x , OutputDataRate , PowerMode , ic:: Mma8451 , mode} ;
2222use pololu_tic:: { TicHandlerError , TicI2C , TicProduct , TicStepMode , base:: TicBase } ;
@@ -136,23 +136,29 @@ async fn main(spawner: Spawner) {
136136 let mut buffer = [ 0 ; 1 ] ;
137137 let mut command_string = String :: new ( ) ;
138138
139+ let mut timer = Instant :: now ( ) ;
140+
139141 loop {
140- while motor_horizontal. reset_command_timeout ( ) . is_err ( ) {
141- error ! ( "Horizontal motor communication failure, attempting reconnection" ) ;
142- motor_horizontal =
143- pololu_tic:: TicI2C :: new_with_address ( RefCellDevice :: new ( & i2c_bus) , TicProduct :: Tic36v4 , 14 ) ;
142+ if timer. elapsed ( ) > Duration :: from_millis ( 100 ) {
143+ while motor_horizontal. reset_command_timeout ( ) . is_err ( ) {
144+ error ! ( "Horizontal motor communication failure, attempting reconnection" ) ;
145+ motor_horizontal =
146+ pololu_tic:: TicI2C :: new_with_address ( RefCellDevice :: new ( & i2c_bus) , TicProduct :: Tic36v4 , 14 ) ;
147+
148+ let _ = setup_motor ( & mut motor_horizontal, MotorAxis :: Horizontal ) ;
149+ Timer :: after ( Duration :: from_secs ( 1 ) ) . await ;
150+ }
144151
145- let _ = setup_motor ( & mut motor_horizontal, MotorAxis :: Horizontal ) ;
146- Timer :: after ( Duration :: from_secs ( 1 ) ) . await ;
147- }
152+ while motor_vertical. reset_command_timeout ( ) . is_err ( ) {
153+ error ! ( "Vertical motor communication failure, attempting reconnection" ) ;
154+ motor_vertical =
155+ pololu_tic:: TicI2C :: new_with_address ( RefCellDevice :: new ( & i2c_bus) , TicProduct :: Tic36v4 , 15 ) ;
148156
149- while motor_vertical. reset_command_timeout ( ) . is_err ( ) {
150- error ! ( "Vertical motor communication failure, attempting reconnection" ) ;
151- motor_vertical =
152- pololu_tic:: TicI2C :: new_with_address ( RefCellDevice :: new ( & i2c_bus) , TicProduct :: Tic36v4 , 15 ) ;
157+ let _ = setup_motor ( & mut motor_vertical, MotorAxis :: Vertical ) ;
158+ Timer :: after ( Duration :: from_secs ( 1 ) ) . await ;
159+ }
153160
154- let _ = setup_motor ( & mut motor_vertical, MotorAxis :: Vertical ) ;
155- Timer :: after ( Duration :: from_secs ( 1 ) ) . await ;
161+ timer = Instant :: now ( ) ;
156162 }
157163
158164 let count = uart0. read_buffered_bytes ( & mut buffer) . unwrap ( ) ;
0 commit comments