Skip to content

Commit c1c883d

Browse files
authored
Merge pull request #2077 from FoamyGuy/webb_telescope_update
Webb telescope update
2 parents 6fc30e6 + 1b71cab commit c1c883d

File tree

3 files changed

+334
-83
lines changed

3 files changed

+334
-83
lines changed

MagTag_Webb_Telescope_Status/code.py

Lines changed: 86 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
# SLEEP_TIME = 60 * 60 * 24 # seconds
3232

3333
# URL to fetch the data from
34-
JSON_GET_URL = "https://api.jwst-hub.com/track"
34+
JSON_GET_URL = (
35+
"https://jwst.nasa.gov/content/webbLaunch/flightCurrentState2.0.json?unique={}"
36+
)
3537

3638
# Whether to fetch live data or use cached
37-
TEST_RUN = False
39+
TEST_RUN = True
3840
# Cached data, helpful when developing interface
3941
# pylint: disable=line-too-long
40-
FAKE_DATA = '{"distanceEarthKm":"1103975.4","launchElapsedTime":"15:03:47:44","distanceL2Km":342356.2,"percentageCompleted":76.3291,"speedKmS":0.3778,"deploymentImgURL":"https://webb.nasa.gov/content/webbLaunch/assets/images/deployment/1000pxWide/123Crop.png","currentDeploymentStep":"WEBB IS FULLY DEPLOYED! - The largest, most complex telescope ever launched into space is fully deployed.","tempC":{"tempWarmSide1C":55,"tempWarmSide2C":11,"tempCoolSide1C":-178,"tempCoolSide2C":-200},"timestamp":"2022-01-09T16:07:44.147Z"}'
42+
FAKE_DATA = '{"currentState": {"STEPS": "MirrorAlignSteps, TempBlurb, MirrorBlurb, ArraysForPlots, Plots", "launchDateTimeString": "2021-12-25T12:20Z", "currentDeployTableIndex": 34, "tempWarmSide1C": 37.1, "tempWarmSide2C": 12.0, "tempCoolSide1C": -229.1, "tempCoolSide2C": -233.0, "---INST TEMPS IN KELVIN----": "", "tempInstNirCamK": 42.7, "tempInstNirSpecK": 39.9, "tempInstFgsNirissK": 47.3, "tempInstMiriK": 108.7, "tempInstFsmK": 37.1, "tempsShow": true, "last": ""}}'
4143

4244
# Background Color for the label texts
4345
LBL_BACKGROUND = 0x444444
@@ -75,7 +77,11 @@ def try_refresh():
7577
supervisor.reload()
7678

7779

78-
def make_label_text(text, anchor_point, anchored_position):
80+
def get_time_str():
81+
return str(time.monotonic()).replace(".", "")
82+
83+
84+
def make_name_text(text, anchor_point, anchored_position, bg_color=LBL_BACKGROUND):
7985
"""
8086
Create label object for labeling data values.
8187
It will get a background color box and appropriate padding.
@@ -90,15 +96,22 @@ def make_label_text(text, anchor_point, anchored_position):
9096
text=text,
9197
anchor_point=anchor_point,
9298
anchored_position=anchored_position,
93-
background_color=LBL_BACKGROUND,
99+
background_color=bg_color,
94100
padding_left=4,
95101
padding_right=4,
96102
padding_bottom=3,
97103
padding_top=3,
104+
line_spacing=1.0,
98105
)
99106

100107

101-
def make_value_text(anchor_point, anchored_position, custom_font=True):
108+
def make_value_text(
109+
anchor_point,
110+
anchored_position,
111+
custom_font=True,
112+
bg_color=0x000000,
113+
font_color=0xFFFFF,
114+
):
102115
"""
103116
Create label object for showing data values.
104117
@@ -112,7 +125,17 @@ def make_value_text(anchor_point, anchored_position, custom_font=True):
112125
else:
113126
_font = terminalio.FONT
114127
return bitmap_label.Label(
115-
_font, text="", anchor_point=anchor_point, anchored_position=anchored_position
128+
_font,
129+
text="",
130+
anchor_point=anchor_point,
131+
anchored_position=anchored_position,
132+
line_spacing=1.0,
133+
padding_top=3,
134+
background_color=bg_color,
135+
color=font_color,
136+
padding_right=4,
137+
padding_left=4,
138+
padding_bottom=4,
116139
)
117140

118141

@@ -122,118 +145,98 @@ def make_value_text(anchor_point, anchored_position, custom_font=True):
122145
# initialize custom font
123146
font = bitmap_font.load_font("fonts/LeagueSpartan-Light.bdf")
124147

125-
# value text initialization
126-
127-
# top left
128-
elapsed_time_val = make_value_text(anchor_point=(0, 0), anchored_position=(6, 6))
148+
# value text initializations
129149

130-
# top right
131-
distance_from_earth_val = make_value_text(
132-
anchor_point=(1.0, 0), anchored_position=(display.width - 6, 6)
150+
# top left. Hot side | Cold side temperature values
151+
top_left_value = make_value_text(
152+
anchor_point=(0, 0),
153+
anchored_position=(0, 6),
154+
bg_color=0xBBBBBB,
155+
font_color=0x000000,
133156
)
134157

135-
# middle right
136-
distance_to_l2_val = make_value_text(
137-
anchor_point=(1.0, 0), anchored_position=(display.width - 6, 56)
158+
# top right. Instrument temperature values
159+
top_right_value = make_value_text(
160+
anchor_point=(1.0, 0),
161+
anchored_position=(display.width - 6, 6),
138162
)
139163

140-
# bottom right
141-
percent_complete_val = make_value_text(
142-
anchor_point=(1.0, 1.0), anchored_position=(display.width - 6, display.height - 6)
143-
)
144-
145-
# middle left
146-
speed_val = make_value_text(anchor_point=(0, 0), anchored_position=(6, 56))
147-
148-
# bottom left
164+
# bottom left timestamp
149165
timestamp_val = make_value_text(
150-
anchor_point=(0, 1.0), anchored_position=(6, display.height - 6), custom_font=False
166+
anchor_point=(0, 1.0), anchored_position=(0, display.height - 6), custom_font=False
151167
)
152168

153-
# center
154-
temperature_val = make_value_text(
155-
anchor_point=(0.5, 0.0), anchored_position=(display.width // 2, 6)
156-
)
169+
main_group.append(top_left_value)
170+
main_group.append(top_right_value)
157171

158-
main_group.append(elapsed_time_val)
159-
main_group.append(distance_from_earth_val)
160-
main_group.append(distance_to_l2_val)
161-
main_group.append(percent_complete_val)
162-
main_group.append(speed_val)
163172
main_group.append(timestamp_val)
164173

165174
# label text initialization
166175

167-
# top left
168-
elapsed_time_lbl = make_label_text(
169-
text="Elapsed", anchor_point=(0.0, 0), anchored_position=(6, 26)
170-
)
171-
172-
# top right
173-
distance_from_earth_lbl = make_label_text(
174-
text="From Earth", anchor_point=(1.0, 0), anchored_position=(display.width - 6, 26)
176+
# middle left. Hot side | Cold side temps label
177+
middle_left_name = make_name_text(
178+
text="Temperature", anchor_point=(0.0, 0), anchored_position=(0, 51)
175179
)
176180

177-
# middle right
178-
distance_to_l2_lbl = make_label_text(
179-
text="To L2 Orbit", anchor_point=(1.0, 0), anchored_position=(display.width - 6, 76)
181+
# center. Instrument temp labels
182+
inst_temp_labels = "NIRCam Bench\nNIRSpec Bench\nFGS Bench\nMIRI Bench\nFSM"
183+
top_center_name = make_name_text(
184+
text=inst_temp_labels,
185+
anchor_point=(1.0, 0.0),
186+
anchored_position=(top_right_value.x - 2, 6),
180187
)
181188

182-
# center
183-
temperature_lbl = make_label_text(
184-
text="Temp", anchor_point=(0.5, 0.0), anchored_position=(display.width // 2, 54)
185-
)
186-
187-
# middle left
188-
speed_lbl = make_label_text(
189-
text="Speed", anchor_point=(0, 0), anchored_position=(6, 76)
190-
)
191-
192-
main_group.append(elapsed_time_lbl)
193-
main_group.append(distance_from_earth_lbl)
194-
main_group.append(distance_to_l2_lbl)
195-
main_group.append(temperature_lbl)
196-
main_group.append(speed_lbl)
197-
main_group.append(temperature_val)
189+
main_group.append(middle_left_name)
190+
main_group.append(top_center_name)
198191

199192
if not TEST_RUN:
200193
try:
201-
print("Fetching JSON data from %s" % JSON_GET_URL)
202-
response = requests.get(JSON_GET_URL, timeout=30)
194+
print("Fetching JSON data from {}".format(JSON_GET_URL.format(get_time_str())))
195+
response = requests.get(JSON_GET_URL.format(get_time_str()), timeout=30)
203196
except (RuntimeError, OSError) as e:
204197
print(e)
205198
print("Failed GET request. Rebooting in 3 seconds...")
206199
time.sleep(3)
207200
supervisor.reload()
208201

209202
print("-" * 40)
210-
text_data = response.text
211-
text_data = text_data.replace('"distanceEarthKm":', '"distanceEarthKm":"').replace(
212-
',"launchElapsedTime"', '","launchElapsedTime"'
213-
)
214-
215-
json_data = json.loads(text_data)
203+
print(response.headers)
204+
json_data = response.json()
205+
_time_parts = response.headers["date"].split(" ")
206+
_time_str = "{}\n{}".format(" ".join(_time_parts[:4]), " ".join(_time_parts[4:]))
216207

217208
print("JSON Response: ", json_data)
218209
print("-" * 40)
219210
response.close()
220211
else:
221212
json_data = json.loads(FAKE_DATA)
213+
_time_parts = ["Mon,", "28", "Feb", "2022", "17:17:54 GMT"]
214+
_time_str = "{}\n{}".format(" ".join(_time_parts[:4]), " ".join(_time_parts[4:]))
215+
216+
# Date/Time
217+
timestamp_val.text = _time_str
218+
219+
# instrument temps
220+
top_right_value.text = "{}K\n{}K\n{}K\n{}K\n{}K".format(
221+
json_data["currentState"]["tempInstNirCamK"],
222+
json_data["currentState"]["tempInstNirSpecK"],
223+
json_data["currentState"]["tempInstFgsNirissK"],
224+
json_data["currentState"]["tempInstMiriK"],
225+
json_data["currentState"]["tempInstFsmK"],
226+
)
222227

223-
# update the labels to display values
224-
elapsed_time_val.text = json_data["launchElapsedTime"]
225-
distance_from_earth_val.text = "{}km".format(json_data["distanceEarthKm"])
226-
distance_to_l2_val.text = "{}km".format(str(json_data["distanceL2Km"]))
227-
percent_complete_val.text = "{}%".format(str(json_data["percentageCompleted"]))
228-
speed_val.text = "{}km/s".format(str(json_data["speedKmS"]))
229-
timestamp_val.text = str(json_data["timestamp"])
230-
temperature_val.text = "{}c | {}c\n{}c | {}c".format(
231-
json_data["tempC"]["tempWarmSide1C"],
232-
json_data["tempC"]["tempCoolSide1C"],
233-
json_data["tempC"]["tempWarmSide2C"],
234-
json_data["tempC"]["tempCoolSide2C"],
228+
# hot side | cold site temps
229+
top_left_value.text = "{}C | {}C\n{}C | {}C".format(
230+
json_data["currentState"]["tempWarmSide1C"],
231+
json_data["currentState"]["tempCoolSide1C"],
232+
json_data["currentState"]["tempWarmSide2C"],
233+
json_data["currentState"]["tempCoolSide2C"],
235234
)
236235

236+
# Set the name position after the instrument temps are in the value
237+
# label, so that it's x will be in the proper position.
238+
top_center_name.anchored_position = (top_right_value.x - 2, 6)
239+
237240
# show the group
238241
display.show(main_group)
239242

MagTag_Webb_Telescope_Status/deprecated_original_version/.circuitpython.skip-screenshot

Whitespace-only changes.

0 commit comments

Comments
 (0)