@@ -144,7 +144,7 @@ function (@main)(args)::Cint
144144 println(Core. stdout , " pwm startup done" )
145145
146146 # Create hardware context
147- hw = HardwareContext(gpio, ain1, bin1, stby, ltrans_oe, pwm_chip, pwm_left, pwm_right)
147+ hw = HardwareContext(gpio, ain1, bin1, stby, pwm_chip, pwm_left, pwm_right)
148148
149149 # Initialize I2C for IMU (bus 1, address 0x68)
150150 imu = MPU6000(1 , 0x68 )
@@ -157,47 +157,33 @@ function (@main)(args)::Cint
157157 tenc_1a = ThreadedEncoder(Encoder(gpio, M1A))
158158 tenc_2a = ThreadedEncoder(Encoder(gpio, M2A))
159159
160- # Disable default SIGINT handling so we can catch Ctrl-C
161- ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 0 )
162-
163- try
164- GPIO. set_value(hw. ltrans_oe, 1 )
165- GPIO. set_value(hw. stby, 1 ) # take the motor controller out of standby
166- apply_motor_output!(hw, 512.0f0 , 512.0f0 )
167- while true end
168- ctrl = BalanceController()
169-
170- ml_update_rate_ns = 50000000
171- imu_read_margin = 12600000
172- println(Core. stdout , " start loop!" )
160+ GPIO. set_value(ltrans_oe, 1 )
161+ GPIO. set_value(stby, 1 ) # take the motor controller out of standby
162+ apply_motor_output!(hw, 512.0f0 , 512.0f0 )
163+ while true end
164+ ctrl = BalanceController()
165+
166+ ml_update_rate_ns = 50000000
167+ imu_read_margin = 12600000
168+ println(Core. stdout , " start loop!" )
169+ start = time_ns()
170+ while true
171+ wait_until(start + ml_update_rate_ns - imu_read_margin)
172+ put!(timu. request, true ) # trigger the IMU request
173+ wait_until(start + ml_update_rate_ns)
174+ imu_data = take!(timu. response)
175+ enc_1_cnts = reset!(tenc_1a)
176+ enc_2_cnts = reset!(tenc_2a)
177+ command = balance_car!(ctrl, enc_1_cnts, enc_2_cnts,
178+ imu_data. accel_x, imu_data. accel_y, imu_data. accel_z,
179+ imu_data. gyro_x, imu_data. gyro_y, imu_data. gyro_z)
173180 start = time_ns()
174- while true
175- wait_until(start + ml_update_rate_ns - imu_read_margin)
176- put!(timu. request, true ) # trigger the IMU request
177- wait_until(start + ml_update_rate_ns)
178- imu_data = take!(timu. response)
179- enc_1_cnts = reset!(tenc_1a)
180- enc_2_cnts = reset!(tenc_2a)
181- command = balance_car!(ctrl, enc_1_cnts, enc_2_cnts,
182- imu_data. accel_x, imu_data. accel_y, imu_data. accel_z,
183- imu_data. gyro_x, imu_data. gyro_y, imu_data. gyro_z)
184- start = time_ns()
185- if isnothing(command)
186- car_stop!(hw)
187- else
188- left, right = command
189- apply_motor_output!(hw, left, right)
190- end
191- end
192- catch e
193- if e isa InterruptException
194- println(Core. stdout , " \n Ctrl-C received, stopping..." )
181+ if isnothing(command)
182+ car_stop!(hw)
195183 else
196- println(Core . stdout , " Error: " , e)
197- rethrow(e )
184+ left, right = command
185+ apply_motor_output!(hw, left, right )
198186 end
199- finally
200- shutdown!(hw)
201187 end
202188 return 0
203189end
0 commit comments