Skip to content

Commit cc55637

Browse files
committed
Fix BalanceController usage
1 parent b7e98e5 commit cc55637

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

deploy/src/main.jl

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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, pwm_chip, pwm_left, pwm_right)
147+
hw = HardwareContext(gpio, ain1, bin1, stby, ltrans_oe, pwm_chip, pwm_left, pwm_right)
148148

149149
# Initialize I2C for IMU (bus 1, address 0x68)
150150
imu = MPU6000(1, 0x68)
@@ -159,31 +159,37 @@ function (@main)(args)::Cint
159159

160160
GPIO.set_value(ltrans_oe, 1)
161161
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()
162+
ctrl = BalanceController.BalanceController()
165163

166164
ml_update_rate_ns = 50000000
167165
imu_read_margin = 12600000
168166
println(Core.stdout, "start loop!")
169167
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)
180-
start = time_ns()
181-
if isnothing(command)
182-
car_stop!(hw)
183-
else
184-
left, right = command
185-
apply_motor_output!(hw, left, right)
168+
Base.exit_on_sigint(false)
169+
try
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)
180+
start = time_ns()
181+
if isnothing(command)
182+
car_stop!(hw)
183+
else
184+
left, right = command
185+
apply_motor_output!(hw, left, right)
186+
end
186187
end
188+
catch e
189+
rethrow()
190+
finally
191+
GPIO.set_value(ltrans_oe, 0)
192+
GPIO.set_value(stby, 0)
187193
end
188194
return 0
189195
end

0 commit comments

Comments
 (0)