BaCLNS


NameBaCLNS JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/sof-danny/BaCLNS
SummaryEfficient Backstepping Control for Linear and Nonlinear Dynamic Systems
upload_time2024-08-12 19:45:31
maintainerNone
docs_urlNone
authorSamuel O. Folorunsho
requires_python>=3.6
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # BaCLNS: Efficient Backstepping Control for Linear and Nonlinear Dynamic Systems

BaCLNS (Backstepping Control for Linear and Nonlinear Systems) is a Python package designed to provide efficient and robust control solutions for dynamic systems (Control Affine Systems) using backstepping techniques. This package offers a flexible and user-friendly interface for designing, simulating, and analyzing control systems, with options for plotting and saving results.

Introduction to Backstepping Control

Backstepping is a systematic and recursive control design technique primarily used for stabilizing nonlinear systems. Unlike traditional control methods that might struggle with complex nonlinearities, backstepping breaks down the control problem into smaller, more manageable sub-problems. These sub-problems are then solved sequentially, "stepping back" from the output to the input, hence the name "backstepping."

Key Concepts:

Virtual Control: Intermediate control laws are designed for each state, leading to the final control input.
Lyapunov Function: A mathematical function that helps ensure system stability. Backstepping uses this function to guide the control design process.
Recursive Design: The control input is designed by recursively stabilizing each state in the system.

Worked examples can be found in [this paper]( https://doi.org/10.1016/B978-0-12-817582-8.00008-8)


## Installation

To install the package, simply run:

```bash
pip install BaCLNS
```

## Usage

1. Importing the Package
To use the package, import the necessary functions:

```bash
import sympy as sp
import numpy as np
from baclns import generic_backstepping_controller, simulate_system, plot_responses, save_responses
```

2. Define the System 
First, define your system's state equations and parameters. For example, if you have a 3D system:

```bash
# Define system parameters
num_states = 2

x1, x2, x3 = sp.symbols('x1 x2 x3')
u = sp.Symbol('u')
a, b, c = sp.symbols('a b c')

# Define the state equations for a 3D system
state_equations = [
    a * x1 + x2,
    b * x2 + x3,
    c * x3 + u
]

# Define the gains
gains_vals = [10.0, 10.0, 15.0]
```
3. Creating the Control Law
Use the 'generic_backstepping_controller' function to create the control law for your system:

```bash
final_control_law, states, gains = generic_backstepping_controller(num_states, state_equations, 'u', gains_vals)
```

4. Simulating the System
Simulate the system using the 'simulate_system' function. You can configure it to print the control law, plot the results, and save the responses:

```bash 
# Simulation parameters
time = np.linspace(0, 10, 1000)  # 10 seconds of simulation with 1000 time steps
initial_conditions = [0.1, 0.0, 0.1]  # Initial conditions for x1, x2, x3
params_subs = {a: 1.0, b: 0.5, c: 0.2, 'k1': gains_vals[0], 'k2': gains_vals[1], 'k3': gains_vals[2]}

# Simulate the system
state_values, control_inputs, errors = simulate_system(
    final_control_law, states, gains_vals, initial_conditions, time, state_equations, params_subs, 
    plot=True, save_path='results.json', print_law=True
)
```

5. Plotting the Results
If you haven't plotted the results directly in the simulation, you can use the plot_responses function to plot the states, control inputs, and errors on separate plots:

```bash
plot_responses(time, state_values, control_inputs, errors)
```

6. Saving the Results
The results can be saved to a JSON file for later analysis. The simulate_system function allows you to save the results directly during the simulation by specifying the save_path:

```bash
simulate_system(final_control_law, states, gains_vals, initial_conditions, time, state_equations, params_subs, save_path='results.json')
```

Alternatively, you can save the results after the simulation using the 'save_responses' function:

```bash
save_responses('results.json', time, state_values, control_inputs)
```

## Example Workflow: 2D Linear system

```bash
import sympy as sp
import numpy as np
from baclns import generic_backstepping_controller, simulate_system, plot_responses, save_responses

# 1. Define system parameters and state equations

num_states = 2
x1, x2 = sp.symbols('x1 x2')
u = sp.Symbol('u')
a, b = sp.symbols('a b')

# Define the state equations for a 2D system
state_equations = [
    a * x1 + x2,
    b * x2 + u
]

# Define the gains
gains_vals = [10.0, 15.0]

# 2. Create the control law using generic_backstepping_controller

final_control_law, states, gains = generic_backstepping_controller(num_states, state_equations, 'u', gains_vals)

# 3. Define simulation parameters

time = np.linspace(0, 10, 500)  # 10 seconds of simulation with 500 time steps
initial_conditions = [1.0, 0.0]  # Initial conditions for x1, x2
params_subs = {a: 1.0, b: 0.5, 'k1': gains_vals[0], 'k2': gains_vals[1]}

# 4. Simulate the system

state_values, control_inputs, errors = simulate_system(
    final_control_law, states, gains_vals, initial_conditions, time, state_equations, params_subs, 
    plot=True, print_law=True
)

# 5. Plot the results

plot_responses(time, state_values, control_inputs, errors)

# 6. Save the results to a JSON file

save_responses(time, state_values, control_inputs, 'test_results.json', errors)
```


## Example Results

### State Response
![State Response](https://github.com/sof-danny/BaCLNS/blob/main/tests/states_response.png)

### Errors Over Time
![Errors](https://github.com/sof-danny/BaCLNS/blob/main/tests/errors.png)

### Control Input
![Control Input](https://github.com/sof-danny/BaCLNS/blob/main/tests/control_input.png)

...

## License
This project is licensed under the MIT License - see the [LICENSE](https://github.com/sof-danny/BaCLNS/blob/main/LICENSE) file for details.



## Contact 
If you have questions or issues using the package or understanding Backstepping techniques, please reach out to [Samuel Folorunsho](https://github.com/sof-danny)

## Citation
If you find this useful for your class or research, please cite:

[BaCLNS: Efficient Control for Dynamic Systems](https://github.com/sof-danny/BaCLNS)


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sof-danny/BaCLNS",
    "name": "BaCLNS",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": null,
    "author": "Samuel O. Folorunsho",
    "author_email": "folorunshosamuel001@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/83/bd/7a9b8bcffcab8c3f2174a6ce1119ec22a297fdd738197a25d5a6998e399d/baclns-0.1.1.tar.gz",
    "platform": null,
    "description": "# BaCLNS: Efficient Backstepping Control for Linear and Nonlinear Dynamic Systems\n\nBaCLNS (Backstepping Control for Linear and Nonlinear Systems) is a Python package designed to provide efficient and robust control solutions for dynamic systems (Control Affine Systems) using backstepping techniques. This package offers a flexible and user-friendly interface for designing, simulating, and analyzing control systems, with options for plotting and saving results.\n\nIntroduction to Backstepping Control\n\nBackstepping is a systematic and recursive control design technique primarily used for stabilizing nonlinear systems. Unlike traditional control methods that might struggle with complex nonlinearities, backstepping breaks down the control problem into smaller, more manageable sub-problems. These sub-problems are then solved sequentially, \"stepping back\" from the output to the input, hence the name \"backstepping.\"\n\nKey Concepts:\n\nVirtual Control: Intermediate control laws are designed for each state, leading to the final control input.\nLyapunov Function: A mathematical function that helps ensure system stability. Backstepping uses this function to guide the control design process.\nRecursive Design: The control input is designed by recursively stabilizing each state in the system.\n\nWorked examples can be found in [this paper]( https://doi.org/10.1016/B978-0-12-817582-8.00008-8)\n\n\n## Installation\n\nTo install the package, simply run:\n\n```bash\npip install BaCLNS\n```\n\n## Usage\n\n1. Importing the Package\nTo use the package, import the necessary functions:\n\n```bash\nimport sympy as sp\nimport numpy as np\nfrom baclns import generic_backstepping_controller, simulate_system, plot_responses, save_responses\n```\n\n2. Define the System \nFirst, define your system's state equations and parameters. For example, if you have a 3D system:\n\n```bash\n# Define system parameters\nnum_states = 2\n\nx1, x2, x3 = sp.symbols('x1 x2 x3')\nu = sp.Symbol('u')\na, b, c = sp.symbols('a b c')\n\n# Define the state equations for a 3D system\nstate_equations = [\n    a * x1 + x2,\n    b * x2 + x3,\n    c * x3 + u\n]\n\n# Define the gains\ngains_vals = [10.0, 10.0, 15.0]\n```\n3. Creating the Control Law\nUse the 'generic_backstepping_controller' function to create the control law for your system:\n\n```bash\nfinal_control_law, states, gains = generic_backstepping_controller(num_states, state_equations, 'u', gains_vals)\n```\n\n4. Simulating the System\nSimulate the system using the 'simulate_system' function. You can configure it to print the control law, plot the results, and save the responses:\n\n```bash \n# Simulation parameters\ntime = np.linspace(0, 10, 1000)  # 10 seconds of simulation with 1000 time steps\ninitial_conditions = [0.1, 0.0, 0.1]  # Initial conditions for x1, x2, x3\nparams_subs = {a: 1.0, b: 0.5, c: 0.2, 'k1': gains_vals[0], 'k2': gains_vals[1], 'k3': gains_vals[2]}\n\n# Simulate the system\nstate_values, control_inputs, errors = simulate_system(\n    final_control_law, states, gains_vals, initial_conditions, time, state_equations, params_subs, \n    plot=True, save_path='results.json', print_law=True\n)\n```\n\n5. Plotting the Results\nIf you haven't plotted the results directly in the simulation, you can use the plot_responses function to plot the states, control inputs, and errors on separate plots:\n\n```bash\nplot_responses(time, state_values, control_inputs, errors)\n```\n\n6. Saving the Results\nThe results can be saved to a JSON file for later analysis. The simulate_system function allows you to save the results directly during the simulation by specifying the save_path:\n\n```bash\nsimulate_system(final_control_law, states, gains_vals, initial_conditions, time, state_equations, params_subs, save_path='results.json')\n```\n\nAlternatively, you can save the results after the simulation using the 'save_responses' function:\n\n```bash\nsave_responses('results.json', time, state_values, control_inputs)\n```\n\n## Example Workflow: 2D Linear system\n\n```bash\nimport sympy as sp\nimport numpy as np\nfrom baclns import generic_backstepping_controller, simulate_system, plot_responses, save_responses\n\n# 1. Define system parameters and state equations\n\nnum_states = 2\nx1, x2 = sp.symbols('x1 x2')\nu = sp.Symbol('u')\na, b = sp.symbols('a b')\n\n# Define the state equations for a 2D system\nstate_equations = [\n    a * x1 + x2,\n    b * x2 + u\n]\n\n# Define the gains\ngains_vals = [10.0, 15.0]\n\n# 2. Create the control law using generic_backstepping_controller\n\nfinal_control_law, states, gains = generic_backstepping_controller(num_states, state_equations, 'u', gains_vals)\n\n# 3. Define simulation parameters\n\ntime = np.linspace(0, 10, 500)  # 10 seconds of simulation with 500 time steps\ninitial_conditions = [1.0, 0.0]  # Initial conditions for x1, x2\nparams_subs = {a: 1.0, b: 0.5, 'k1': gains_vals[0], 'k2': gains_vals[1]}\n\n# 4. Simulate the system\n\nstate_values, control_inputs, errors = simulate_system(\n    final_control_law, states, gains_vals, initial_conditions, time, state_equations, params_subs, \n    plot=True, print_law=True\n)\n\n# 5. Plot the results\n\nplot_responses(time, state_values, control_inputs, errors)\n\n# 6. Save the results to a JSON file\n\nsave_responses(time, state_values, control_inputs, 'test_results.json', errors)\n```\n\n\n## Example Results\n\n### State Response\n![State Response](https://github.com/sof-danny/BaCLNS/blob/main/tests/states_response.png)\n\n### Errors Over Time\n![Errors](https://github.com/sof-danny/BaCLNS/blob/main/tests/errors.png)\n\n### Control Input\n![Control Input](https://github.com/sof-danny/BaCLNS/blob/main/tests/control_input.png)\n\n...\n\n## License\nThis project is licensed under the MIT License - see the [LICENSE](https://github.com/sof-danny/BaCLNS/blob/main/LICENSE) file for details.\n\n\n\n## Contact \nIf you have questions or issues using the package or understanding Backstepping techniques, please reach out to [Samuel Folorunsho](https://github.com/sof-danny)\n\n## Citation\nIf you find this useful for your class or research, please cite:\n\n[BaCLNS: Efficient Control for Dynamic Systems](https://github.com/sof-danny/BaCLNS)\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Efficient Backstepping Control for Linear and Nonlinear Dynamic Systems",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/sof-danny/BaCLNS"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3498d7fab88ed636d5c47471c3d96b781b08fdd517367dcc84cffb7849808cbf",
                "md5": "070ad8a8153a68eb96d9ffabd69a4f21",
                "sha256": "f1ec468071dc94c91516a4c35a3b3b50768e0de13b302c19fccebba0f7677c6e"
            },
            "downloads": -1,
            "filename": "BaCLNS-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "070ad8a8153a68eb96d9ffabd69a4f21",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 7094,
            "upload_time": "2024-08-12T19:45:30",
            "upload_time_iso_8601": "2024-08-12T19:45:30.509873Z",
            "url": "https://files.pythonhosted.org/packages/34/98/d7fab88ed636d5c47471c3d96b781b08fdd517367dcc84cffb7849808cbf/BaCLNS-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "83bd7a9b8bcffcab8c3f2174a6ce1119ec22a297fdd738197a25d5a6998e399d",
                "md5": "7fae5086a20667b4def134c5327e060f",
                "sha256": "c359453e6cf03f58f5eaef69eb36d0640ca5dd7bd80592f9360fca042d8b9131"
            },
            "downloads": -1,
            "filename": "baclns-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7fae5086a20667b4def134c5327e060f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 6346,
            "upload_time": "2024-08-12T19:45:31",
            "upload_time_iso_8601": "2024-08-12T19:45:31.564326Z",
            "url": "https://files.pythonhosted.org/packages/83/bd/7a9b8bcffcab8c3f2174a6ce1119ec22a297fdd738197a25d5a6998e399d/baclns-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-12 19:45:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sof-danny",
    "github_project": "BaCLNS",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "baclns"
}
        
Elapsed time: 0.67175s