Skip to content

Commit fee7b25

Browse files
committed
ENH: build_parachutes added to nbbuilder
1 parent 7fce2c3 commit fee7b25

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

‎rocketserializer/nb_builder.py‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def build_imports(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook:
8080
# import classes
8181
text = (
8282
"from rocketpy import Environment, SolidMotor, Rocket, Flight, "
83-
+ "TrapezoidalFins, EllipticalFins, RailButtons, NoseCone, Tail\n"
83+
+ "TrapezoidalFins, EllipticalFins, RailButtons, NoseCone, Tail, "
84+
+ "Parachute\n"
8485
)
8586
text += "import datetime\n"
8687
nb["cells"].append(nbf.v4.new_code_cell(text))
@@ -242,6 +243,7 @@ def build_all_aerodynamic_surfaces(
242243
self.build_fins(nb)
243244
self.build_tails(nb)
244245
self.build_rail_buttons(nb)
246+
self.build_parachute(nb)
245247
logger.info("[NOTEBOOK BUILDER] All aerodynamic surfaces created.")
246248
return nb
247249

@@ -434,6 +436,53 @@ def build_rail_buttons(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook:
434436
logger.info("rail buttons not implemented yet")
435437
return nb
436438

439+
def build_parachute(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook:
440+
# add a markdown cell
441+
text = "### Parachutes\n"
442+
text += "As rocketpy allows for multiple parachutes, we will create a "
443+
text += "dictionary with all the parachutes and then add them to the rocket\n"
444+
nb["cells"].append(nbf.v4.new_markdown_cell(text))
445+
446+
# add a code cell
447+
text = "parachutes = {}\n"
448+
nb["cells"].append(nbf.v4.new_code_cell(text))
449+
for i in range(len(self.parameters["parachutes"])):
450+
451+
parachute_i = self.parameters["parachutes"][str(i)]
452+
cd_s = parachute_i["cd"]*parachute_i["area"]
453+
deploy_event = parachute_i["deploy_event"]
454+
455+
# evaluating trigger
456+
if deploy_event == "apogee":
457+
trigger = "apogee"
458+
elif deploy_event == "altitude":
459+
trigger = float(parachute_i["deploy_altitude"])
460+
else:
461+
logger.warning("Invalid deploy event for parachute %d", i)
462+
raise ValueError("Invalid deploy event for parachute %d", i)
463+
# adding parameters
464+
name = parachute_i["name"]
465+
text = f"parachutes[{i}] = Parachute(\n"
466+
text += f" name='{name}',\n"
467+
text += f" cd_s={cd_s:.3f},\n"
468+
# adding trigger
469+
if isinstance(trigger, str):
470+
text += f" trigger='{trigger}',\n"
471+
else:
472+
text += f" trigger={trigger:.3f},\n"
473+
474+
text += f" sampling_rate=100, \n"
475+
text += ")\n"
476+
nb["cells"].append(nbf.v4.new_code_cell(text))
477+
478+
text = "Adding parachutes to the rocket\n"
479+
nb["cells"].append(nbf.v4.new_markdown_cell(text))
480+
text = "rocket.parachutes = parachutes\n"
481+
nb["cells"].append(nbf.v4.new_code_cell(text))
482+
483+
logger.info("[NOTEBOOK BUILDER] Parachutes created.")
484+
return nb
485+
437486
def build_flight(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook:
438487
"""Generates a section defining the flight and returns the notebook."""
439488
# add a markdown cell

0 commit comments

Comments
 (0)