Skip to content

Commit a2aea71

Browse files
Merge pull request #23 from RocketPy-Team/enh/add_parachutes_to_nbbuilder
ENH: build_parachutes added to nbbuilder
2 parents 270e35c + a887ad2 commit a2aea71

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

rocketserializer/nb_builder.py

Lines changed: 54 additions & 2 deletions
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))
@@ -225,7 +226,8 @@ def build_rocket(self, nb: nbf.v4.new_notebook):
225226
)
226227
nb["cells"].append(nbf.v4.new_code_cell(text))
227228

228-
# add a code cell
229+
nb = self.add_parachutes_to_rocket(nb)
230+
229231
text = "### Rocket Info\n"
230232
text += "rocket.all_info()\n"
231233
nb["cells"].append(nbf.v4.new_code_cell(text))
@@ -242,6 +244,7 @@ def build_all_aerodynamic_surfaces(
242244
self.build_fins(nb)
243245
self.build_tails(nb)
244246
self.build_rail_buttons(nb)
247+
self.build_parachute(nb)
245248
logger.info("[NOTEBOOK BUILDER] All aerodynamic surfaces created.")
246249
return nb
247250

@@ -434,6 +437,55 @@ def build_rail_buttons(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook:
434437
logger.info("rail buttons not implemented yet")
435438
return nb
436439

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

0 commit comments

Comments
 (0)