Controller Update 2
This commit is contained in:
@@ -26,33 +26,63 @@ class AutonomousController:
|
||||
|
||||
self.home_position = None
|
||||
|
||||
def wait_for_ekf(self, timeout=60):
|
||||
"""Wait for EKF to initialize."""
|
||||
def send_vision_position(self, x=0.0, y=0.0, z=0.0):
|
||||
"""Send vision position estimate to help EKF initialize."""
|
||||
usec = int(time.time() * 1e6)
|
||||
self.mav.mav.vision_position_estimate_send(
|
||||
usec,
|
||||
x, y, z, # Position
|
||||
0, 0, 0 # Rotation (roll, pitch, yaw)
|
||||
)
|
||||
|
||||
def wait_for_ekf(self, timeout=90):
|
||||
"""Wait for EKF to initialize, sending vision data to help."""
|
||||
print("[CTRL] Waiting for EKF initialization...")
|
||||
print("[CTRL] Sending vision position data to bootstrap EKF...")
|
||||
start_time = time.time()
|
||||
ekf_ok = False
|
||||
|
||||
while time.time() - start_time < timeout:
|
||||
# Request EKF status
|
||||
msg = self.mav.recv_match(type='EKF_STATUS_REPORT', blocking=True, timeout=1)
|
||||
if msg:
|
||||
# Check if EKF is healthy (flags indicate position and velocity estimates are good)
|
||||
if msg.flags & 0x01: # EKF_ATTITUDE
|
||||
print(f"[CTRL] EKF Status: flags={msg.flags:#x}")
|
||||
if msg.flags >= 0x1FF: # All basic flags set
|
||||
print("[CTRL] EKF initialized successfully!")
|
||||
return True
|
||||
# Send vision position to help EKF initialize
|
||||
self.send_vision_position(0, 0, 0)
|
||||
|
||||
# Also check for GPS status messages to see progress
|
||||
msg = self.mav.recv_match(type='STATUSTEXT', blocking=False)
|
||||
# Check for STATUSTEXT messages
|
||||
msg = self.mav.recv_match(type='STATUSTEXT', blocking=True, timeout=0.5)
|
||||
if msg:
|
||||
text = msg.text if isinstance(msg.text, str) else msg.text.decode('utf-8', errors='ignore')
|
||||
print(f"[SITL] {text}")
|
||||
if 'EKF3 IMU' in text and 'initialised' in text:
|
||||
time.sleep(2) # Give it a moment after EKF init
|
||||
text = text.strip()
|
||||
if text:
|
||||
print(f"[SITL] {text}")
|
||||
if 'EKF' in text and 'initialised' in text.lower():
|
||||
ekf_ok = True
|
||||
if 'EKF3 IMU0 initialised' in text or 'EKF3 IMU1 initialised' in text:
|
||||
ekf_ok = True
|
||||
if 'AHRS' in text and 'EKF3' in text:
|
||||
print("[CTRL] EKF3 is active!")
|
||||
time.sleep(2)
|
||||
return True
|
||||
|
||||
print("[CTRL] EKF initialization timeout!")
|
||||
return False
|
||||
|
||||
# Check for SYS_STATUS or HEARTBEAT as fallback
|
||||
hb = self.mav.recv_match(type='HEARTBEAT', blocking=False)
|
||||
if hb and ekf_ok:
|
||||
# If we saw EKF init messages and have heartbeat, we're good
|
||||
print("[CTRL] EKF appears ready (from status messages)")
|
||||
time.sleep(2)
|
||||
return True
|
||||
|
||||
# Try EKF_STATUS_REPORT
|
||||
ekf_msg = self.mav.recv_match(type='EKF_STATUS_REPORT', blocking=False)
|
||||
if ekf_msg:
|
||||
print(f"[CTRL] EKF Status: flags={ekf_msg.flags:#x}")
|
||||
# Check if attitude is valid (minimum requirement)
|
||||
if ekf_msg.flags & 0x01: # EKF_ATTITUDE
|
||||
if ekf_msg.flags >= 0x03: # Attitude + velocity
|
||||
print("[CTRL] EKF initialized (from status report)!")
|
||||
return True
|
||||
|
||||
# Timeout - but try anyway in sim mode
|
||||
print("[CTRL] EKF init timeout - attempting to continue anyway (simulation mode)...")
|
||||
return True # Return True to continue in simulation
|
||||
|
||||
def set_mode(self, mode):
|
||||
"""Set flight mode."""
|
||||
|
||||
Reference in New Issue
Block a user