The Lunar Hopper project is a lunar exploration planning project aimed at solving the intricate problem of lunar hopper missions. It focuses on optimizing the spacecraft's trajectory across the whole flight. Leveraging state-of-the-art optimal problem-solving algorithms, specifically the MPOPT Python library, this project seeks to determine the most efficient path for maximizing the distance traveled during the mission. It empowers users to customize essential parameters such as spacecraft mass, fuel quantity, thrust, and specific impulse.
This example provides an optimal trajectory solver for Lunar Hopper. The result computes by solving non-linear optimal control problems(OCP) in the standard Bolza form using pseudo-spectral collocation methods and adjusted using an additional real dynamic function. The OCP solver used in this example is MPOPT (based on IPOPT) library modified by Lulav Space team. You can find more information about MPOPT optimal control solving library on the MPOPT GitHub or website.
The main goal of the example is to find the optimal way to "hop" on the Moon as far as possible with given vessel parameters. The dynamic function for MPOPT is:
For this example terminal cost function, path constraints and terminal constraints functions used as well:
- Terminal contraints (we want to start from the given initial conditions and finish with the given final conditions):
- Path constraints (we need to limit controls values within the maximum possible thrust):
- Cost function:
The project consists of two main files:
-
lunar_hopper.py
- the main executable file of the project. This file defines a ROS 2 node named "lunar_hopper" for simulating the dynamics of a lunar hopper spacecraft. The node publishes the spacecraft's state, control commands, and time information.Key functionalities and components of the code include:
-
It inherits from the Node class provided by the RCLPy (ROS 2 Python client library) for creating a ROS node.
-
The node publishes data on the spacecraft's state, control inputs, and time via ROS topics.
-
Various parameters related to the lunar hopper and its mission are declared and initialized, such as fuel mass, dry mass, thrust, specific impulse, and more.
-
Dynamics functions, terminal constraints, and cost functions are defined to describe the behavior of the lunar hopper during different phases of its mission (takeoff, flight, landing).
-
The lopt.solve function is called to solve the optimal control problem for the lunar hopper's trajectory, taking into account the defined parameters and functions.
-
A timer callback function is implemented to periodically publish the simulation results on ROS topics.
-
The main function initializes the ROS 2 node, runs the simulation, and handles the node's lifecycle.
Overall, this code serves as a ROS 2 node for simulating and controlling a lunar hopper's trajectory using an optimal control approach, making it a valuable tool for lunar mission planning and analysis.
-
-
lopt_hopper.py
- OCP solver wrapper file. This Python code is designed to solve optimal control problems using the MPOPT Python library. It provides a single-phase optimal control simulation, focusing on spacecraft dynamics. Here is an overview of the code's functionality:-
The code begins by importing necessary libraries, including MPOPT, Matplotlib, NumPy, and CasADi. It also defines functions for interpolating results and solving optimal control problems.
-
The solve function is the main entry point for solving an optimal control problem. It takes various parameters, including dynamics functions, cost functions, initial and target conditions, bounds, and more.
-
Inside the solve function, an optimal control problem (OCP) is initialized using the MPOPT library. Parameters such as the initial guess for simulation duration (ocp_tf0), initial and target state conditions (ocp_x00 and ocp_xf0), and bounds on states and controls are set.
-
The OCP's dynamics, terminal constraints, and terminal costs are defined based on the provided functions (dyn_func, term_constr, and term_cost).
-
The code proceeds to validate the OCP, ensuring that all parameters are correctly defined.
-
The simulation is then performed using the MPOPT library with adaptive settings. The solver iteratively refines the solution, and the results are processed.
-
Finally, the code performs interpolation on the simulation results to provide a smoother representation of states and controls over time.
In summary, this code serves as a tool for solving optimal control problems related to spacecraft dynamics. It uses the MPOPT library and provides a flexible interface for specifying problem parameters and functions while delivering interpolated results for further analysis or visualization.
-
All project installation and usage information also available in the project GitHub page.
- Docker engine. This project runs inside Docker container, and requires Docker Engine/Docker Desktop. Follow the instructions on Docker official website.
- To use Docker inside VS Code several extensions are required. Install Dev Containers and Docker extensions from Extensions tab on your left control panel.
- Clone the repository:
git [email protected]:citros-garden/lunar_hopper.git
- Open project root folder in VS Code.
- Navigate to the lower-left corner of VS Code window and click on green mark.
- Select "Reopen in container" option in the list on the top of the VS Code window. Wait a minute while Docker container is starting.
- Open
/src/lunar_hopper/config/params.xml
file to set parameters for simulation or just keep it default. Don't forget to save your changes! - Build ROS 2 environment:
colcon build
- Source the environment:
source install/local_setup.bash
FoxGlove Studio is a robotics visualization and debugging tool, which can connect to ROS topic and get the data publishing through it. We will use it to visualizate the results of our simulations.
First of all, you need to download it from the official website and install following the instructions.
Next step is connecting to your ROS node. To perform it, open FoxGlove Studio and select Open connection option, then select Rosbridge option. Check the WebSocket URL field on the right of the window, it should contain ws:https://localhost:9090
. Now we are almost ready to go!
Last step is configuring the layout of FoxGlove. There are two ways to do it: using prepared layout file or manualy.
Manual plot layout configuration: we have 5 states in the output messages, so we need 5 plots. Add 5 plots using third tab on the left FoxGlove control panel. Then click on the fourth tab and set 'Message Path' for each plot: the path should be /lunar_hopper/state.data[n]
, where n - number of the state. Use /lunar_hopper/control.data[m]
as a Message Path for control vector, where m - number of the control (0 or 1).
OR
You can use prepared layout: Go to the Layout
tab on the top panel, then click on import_layout button and select the file from foxglove_layouts folder.
The best way to process simulation results is CITROS notebook π :)
- Go back to the VS Code.
- Prepare your FoxGlove studio (previous step, if you haven't done it yet).
- Launch ROS 2 package:
ros2 launch lunar_hopper lunar_hopper.launch.py
- Watch the FoxGlove plot built from results!
OR
You can use Visual Code Tasks: simply press Alt+T
and select Launch
task to build, source and launch the project automaticly.
Although you can get simulation results using FoxGlove, the best way to work with such simulations and process the results is CITROS! With its power, it is possible to create complex data processing scenarios, including the construction of more complex graphs, mathematical analysis and other high-level processing methods.
First of all, to use all the powerfull CITROS features usage requires CITROS installation: follow the instructions on the CITROS CLI GitHub page.
After all the prerequisites done, we can start configuring our project. The starting point is the Lunar_Starship devcontainer loaded and running, CITROS CLI is installed and ready.
- Initialize CITROS:
citros init
Now you can see .citros
folder in the explorer.
-
Configuring the setup. We need to set up the maximum perfomance available: timeout, CPU, GPU and Memory. To perform it, we need to define it in the
.citros/simulations/simulation_lunar_hopper.json
. The recommended setup is minimum 600 seconds timeout, 4 CPU, 4 GPU and 4096 MB of Memory. Don't forget to save the file! -
Configuring the params setup. You can find default setup in
.citros/parameter_setups/default_param_setup.json
. Variables with '_0' are initial conditions, and variables with '_f' are final conditions respectively.
Don't forget to save the file!
Now we can sync our project settings with CITROS server:
citros commit
citros push
Since all the preparations done, we can launch it locally (your project should be built and sourced before that, check the instructions above):
citros run -n 'Lunar_hopper' -m 'local test run'
Select the launch file (should be the only one here) by pressing Enter
button and wait for the output in the terminal. To plot the local run results you can use FoxGlove.
- We need to build and push Docker container image to the CITROS server:
citros docker-build-push
- Finally, we can run it in the cloud! Simply add
-r
to the terminal command:
citros run -n 'Lunar_hopper' -m 'cloud test run' -r
Select the launch file (should be the only one here) by pressing Enter
button. Now the simulation is running in the CITROS server, and it will upload results to the CITROS database automaticly.