RocketPy icon indicating copy to clipboard operation
RocketPy copied to clipboard

WIP: ENH: New animate flight

Open Gui-FernandesBR opened this issue 2 years ago • 1 comments

Pull request type

Please check the type of change your PR introduces:

  • [X] Code base additions (bugfix, features)

Pull request checklist

  • ReadMe, Docs and GitHub maintenance:

    • [X] Spelling has been verified
    • [X] Code docs are working correctly
  • Code base additions (for bug fixes / features):

    • [ ] Tests for the changes have been added
    • [X] Docs have been reviewed and added / updated if needed
    • [X] Lint (black rocketpy) has passed locally and any fixes were made
    • [X] All tests (pytest --runslow) have passed locally

What is the new behavior?

I'm adding two new method to the Flight class in order to promote better visualization of rocket trajectory. This will be important to help us when trying to implement new methods regarding control methods, roll calculations.

Important to say, all credits here go to @PatrickSampaioUSP for the implementations on #https://github.com/Projeto-Jupiter/Hackathon/pull/112. I used his code as the core, but as it was not working I merged with ideas from here: https://github.com/marcomusy/vedo/tree/master/examples Therefore I added Patrick Sampaio to author's name

Does this introduce a breaking change?

  • [ ] Yes
  • [X] No

Gui-FernandesBR avatar Jun 13 '22 03:06 Gui-FernandesBR

This is currently Work in Progress, no mandatory reviewing it then.

I will work on the following to-dos before opening the pull request for review:

  • [ ] Creare unit tests (I accept suggestions here)
  • [ ] Find a way to save the animation in some file
  • [ ] Add an example somewhere (maybe at getting started)

Gui-FernandesBR avatar Jun 13 '22 03:06 Gui-FernandesBR

I will only work on this again if someone request flight animations as a feature of rocketpy. Otherwise I'll be focusing in other implementations. The branch will be staled in this repo.

Gui-FernandesBR avatar Nov 12 '22 19:11 Gui-FernandesBR

alternative:

    def animate(self, start=0, stop=None, fps=12, speed=4, elev=None, azim=None):
        """Plays an animation of the flight. Not implemented yet. Only
        kinda works outside notebook.
        """
        # Set up stopping time
        stop = self.t_final if stop is None else stop
        # Speed = 4 makes it almost real time - matplotlib is way to slow
        # Set up graph
        fig = plt.figure(figsize=(18, 15))
        axes = fig.gca(projection="3d")
        # Initialize time
        time_range = np.linspace(start, stop, fps * (stop - start))
        # Initialize first frame
        axes.set_title("Trajectory and Velocity Animation")
        axes.set_xlabel("X (m)")
        axes.set_ylabel("Y (m)")
        axes.set_zlabel("Z (m)")
        axes.view_init(elev, azim)
        R = axes.quiver(0, 0, 0, 0, 0, 0, color="r", label="Rocket")
        V = axes.quiver(0, 0, 0, 0, 0, 0, color="g", label="Velocity")
        W = axes.quiver(0, 0, 0, 0, 0, 0, color="b", label="Wind")
        S = axes.quiver(0, 0, 0, 0, 0, 0, color="black", label="Freestream")
        axes.legend()
        # Animate
        for t in time_range:
            R.remove()
            V.remove()
            W.remove()
            S.remove()
            # Calculate rocket position
            Rx, Ry, Rz = self.x(t), self.y(t), self.z(t)
            Ru = 1 * (2 * (self.e1(t) * self.e3(t) + self.e0(t) * self.e2(t)))
            Rv = 1 * (2 * (self.e2(t) * self.e3(t) - self.e0(t) * self.e1(t)))
            Rw = 1 * (1 - 2 * (self.e1(t) ** 2 + self.e2(t) ** 2))
            # Calculate rocket Mach number
            Vx = self.vx(t) / 340.40
            Vy = self.vy(t) / 340.40
            Vz = self.vz(t) / 340.40
            # Calculate wind Mach Number
            z = self.z(t)
            Wx = self.env.wind_velocity_x(z) / 20
            Wy = self.env.wind_velocity_y(z) / 20
            # Calculate freestream Mach Number
            Sx = self.stream_velocity_x(t) / 340.40
            Sy = self.stream_velocity_y(t) / 340.40
            Sz = self.stream_velocity_z(t) / 340.40
            # Plot Quivers
            R = axes.quiver(Rx, Ry, Rz, Ru, Rv, Rw, color="r")
            V = axes.quiver(Rx, Ry, Rz, -Vx, -Vy, -Vz, color="g")
            W = axes.quiver(Rx - Vx, Ry - Vy, Rz - Vz, Wx, Wy, 0, color="b")
            S = axes.quiver(Rx, Ry, Rz, Sx, Sy, Sz, color="black")
            # Adjust axis
            axes.set_xlim(Rx - 1, Rx + 1)
            axes.set_ylim(Ry - 1, Ry + 1)
            axes.set_zlim(Rz - 1, Rz + 1)
            # plt.pause(1/(fps*speed))
            try:
                plt.pause(1 / (fps * speed))
            except:
                time.sleep(1 / (fps * speed))

Gui-FernandesBR avatar Sep 16 '23 17:09 Gui-FernandesBR