saef-api / backend /spacecraft.py
m4az19's picture
Deploy SAHAB-1 backend and pretrained AI
20b0a08 verified
Raw History Blame Contribute Delete
5.85 kB
"""Display engineering telemetry with explicit provenance and uncertainty."""
import math
def finite(value):
return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
def voltage_level(voltage):
# Approximate single-cell Li-ion resting-voltage curve, not calibrated SOC.
points = [(3.0, 0), (3.3, 5), (3.5, 10), (3.6, 15), (3.7, 35),
(3.8, 55), (3.9, 75), (4.0, 85), (4.1, 95), (4.2, 100)]
if not finite(voltage) or not 3.0 <= voltage <= 4.2:
return None
for (lo, a), (hi, b) in zip(points, points[1:]):
if voltage <= hi:
return a + (b-a)*(voltage-lo)/(hi-lo)
return 100.
def values(packet):
s = packet.get('sensors', {})
v, current = s.get('battery_voltage_v'), s.get('battery_current_a')
level = s.get('battery_level_pct')
method = s.get('battery_level_method')
if level is None:
level, method = voltage_level(v), 'voltage_estimate'
gyro = s.get('gyro_dps')
rotation = math.sqrt(sum(x*x for x in gyro)) if gyro is not None else None
accel = s.get('accel_g')
roll = pitch = None
if accel is not None:
x, y, z = accel
gravity = math.sqrt(x*x+y*y+z*z)
# Gravity tilt is only usable in an approximately stationary ground demo.
if .8 <= gravity <= 1.2 and rotation is not None and rotation <= 5:
roll = math.degrees(math.atan2(y, z))
pitch = math.degrees(math.atan2(-x, math.sqrt(y*y+z*z)))
light = s.get('ldr_fraction')
temp = s.get('temperature_c')
polarity = s.get('battery_current_polarity_verified', False)
power = v*current*1000 if v is not None and current is not None else None
reported_mode = packet.get('operating_mode')
derived_mode = None
if level is not None:
derived_mode = 'CRITICAL' if level <= 15 else 'SAFE' if level <= 35 else 'NOMINAL'
if temp is not None and (temp < -10 or temp > 60):
derived_mode = 'CRITICAL'
return {
'battery_voltage_v': v, 'battery_level_pct': level,
'battery_level_method': method if level is not None else None,
'battery_current_ma': current*1000 if current is not None else None,
'battery_current_polarity_verified': polarity,
'battery_terminal_power_mw': power,
'battery_discharge_mw': max(0., -power) if power is not None and polarity else None,
'temperature_c': temp, 'temperature_source': s.get('temperature_source', 'unknown'),
'temperature_outside_limits': temp < -10 or temp > 60 if temp is not None else None,
'humidity_pct': s.get('humidity_pct'),
'mode': reported_mode or derived_mode,
'mode_source': 'onboard logic' if reported_mode else 'ground engineering estimate' if derived_mode else None,
'rotation_dps': rotation, 'roll_deg': roll, 'pitch_deg': pitch,
'tumbling': rotation > 5 if rotation is not None else None,
'light_pct': light*100 if light is not None else None,
'illumination': ('LIT' if light >= .08 else 'DARK') if light is not None else None,
'images_taken': packet.get('images_taken'),
}
def assess(device):
packet = device.get('latest')
if not packet:
return {'current': False, 'values': None, 'history': [], 'link': {}, 'payload': None}
history = [p for p in device.get('recent_packets', [])
if p['device_id'] == packet['device_id'] and p['session_id'] == packet['session_id']]
history.sort(key=lambda p: p['sequence'])
tx_ids = {p['link']['tx_sequence'] for p in history if p['link'].get('tx_sequence') is not None and p['link']['transport']=='lora'}
expected = max(tx_ids)-min(tx_ids)+1 if len(tx_ids) >= 2 else None
link = packet['link']
ground_measured = link.get('measurement_source') == 'ground_station'
ai = packet.get('onboard_ai')
anomaly = ('ANOMALY' if ai['category'] == 'anomaly' else 'OK') if ai and ai['status'] == 'ok' else 'UNAVAILABLE'
return {
'current': device.get('status') in ('RECENT', 'CLOCK UNSYNCHRONIZED') and (device.get('received_age_seconds') or 0) <= 900,
'values': values(packet),
'history': [{'sequence': p['sequence'], 'received_at': p['received_at'],
'uptime_ms': p['uptime_ms'], **values(p),
'cloud_cover_pct': (p.get('payload_ai') or {}).get('cloud_cover_pct'),
'water_cover_pct': (p.get('payload_ai') or {}).get('water_cover_pct')}
for p in history],
'payload': packet.get('payload_ai'), 'anomaly': anomaly,
'anomaly_domain': ai.get('training_domain') if ai else None,
'link': {'rssi_dbm': link.get('rssi_dbm') if ground_measured else None,
'snr_db': link.get('snr_db') if ground_measured else None,
'ground_measured': ground_measured,
'received_packets': len(tx_ids), 'expected_packets': expected,
'missing_transmissions': expected-len(tx_ids) if expected else None,
'observed_delivery_pct': 100*len(tx_ids)/expected if expected else None,
'method': 'Unique transmission IDs within this boot and visible history; not whole-mission RF loss'},
'notes': [
'Voltage-derived battery level is an uncalibrated resting-voltage estimate and changes under load.',
'Battery discharge power is not total spacecraft consumption when solar also supplies the load.',
'Roll and pitch are gravity-based ground-demo tilt; unavailable during tumbling or free fall.',
'LDR lit/dark is an illumination proxy, not a verified orbital sunlight/eclipse determination.',
'Sequence gaps in sampling IDs include deferred transmissions; loss percentage needs transmission IDs.',
],
}