Skip to content

Commit

Permalink
modified: CHANGELOG.md
Browse files Browse the repository at this point in the history
	modified:   Project.toml
	modified:   src/AutomationLabsSystems.jl
	modified:   src/main/main_systems.jl
	modified:   test/runtests.jl
	new file:   test/system_discretization_test.jl
  • Loading branch information
Pierre BLAUD committed Mar 2, 2023
1 parent 1a48df6 commit 8704d99
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# AutomationLabsSystems Changelog

## v0.1.2

* Add continuous linear system discretization function.
* Improve tests.

## v0.1.1

* Add system linearization.
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "AutomationLabsSystems"
uuid = "6d3dfdf0-e107-48c6-a6ff-eced1a5a9334"
authors = ["Pierre BLAUD <pierre.blaud@ikmail.com> and contributors"]
version = "0.1.1"
version = "0.1.2"

[deps]
AutomationLabsIdentification = "48ff5a6f-d08b-4053-9585-6a9e3e078386"
ControlSystems = "a6e380b2-a6ca-5380-bf3e-84a91bcd477e"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LazySets = "b4f0291d-fe17-52bc-9479-3d1a343d9043"
Expand All @@ -22,6 +23,7 @@ MLJ = "0.19"
MLJMultivariateStatsInterface = "0.5"
MathematicalSystems = "0.13"
NNlib = "0.8"
ControlSystems = "1.6"
julia = "1"

[extras]
Expand Down
3 changes: 2 additions & 1 deletion src/AutomationLabsSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import LazySets
import Flux
import ForwardDiff
import NNlib
import ControlSystems

export proceed_system
export proceed_system_linearization

#export proceed_system_discretization
export proceed_system_discretization
export proceed_system_model_evaluation

# Load files
Expand Down
34 changes: 29 additions & 5 deletions src/main/main_systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
A function for system creation.
The following variables are mendatories:
* `model_method`: a String for continous or discrete system.
* `model_origin`: a String for origin of the model.
It is possible to define optional variables kws.
Expand Down Expand Up @@ -129,7 +130,7 @@ function proceed_system(model_method, model_origin; kws_...)
end

"""
system_linearization
proceed_system_linearization
Function that linearises a system from MathematicalSystems at state and input references.
The function uses ForwardDiff package and the jacobian function.
Expand Down Expand Up @@ -179,10 +180,13 @@ function proceed_system_linearization(system::MathematicalSystems.ConstrainedLin
end


# to continuous system to discrete system

"""
proceed_system_model_evaluation
Function that return the types of the model inside the systems from AutomationLabsIdentification.
# Extract model from a system from identification and user function
** Required fields **
* `system`: the mathematital system that as in it the julia non-linear function `f` from AutomationLabsIdentification.
"""
function proceed_system_model_evaluation(system::MathematicalSystems.ConstrainedBlackBoxControlDiscreteSystem)

if typeof(system.f) == Flux.Chain{Tuple{Flux.Dense{typeof(identity), Matrix{Float32}, Bool}, Flux.Chain{NTuple{4, Flux.Dense{typeof(NNlib.relu), Matrix{Float32}, Vector{Float32}}}}, Flux.Dense{typeof(identity), Matrix{Float32}, Bool}}}
Expand All @@ -205,6 +209,26 @@ function proceed_system_model_evaluation(system::MathematicalSystems.Constrained

end


return model_type
end

"""
proceed_system_discretization
Function that linearises a system from MathematicalSystems at state and input references.
The function uses ForwardDiff package and the jacobian function.
** Required fields **
* `system`: the continuous mathematital system that as in it the julia linear or non-linear function `f`.
* `sample_time`: the sample time for discretization.
"""
function proceed_system_discretization(system::MathematicalSystems.ConstrainedLinearControlContinuousSystem, sample_time)

D = 0
C = ones(size(system.A, 1), size(system.A, 2))
sys_c = ControlSystems.ss(system.A, system.B, C, D)
sys_d = ControlSystems.c2d(sys_c, sample_time)
A_sys = sys_d.A
B_sys = sys_d.B

return MathematicalSystems.@system x⁺ = A_sys*x + B_sys*u xsystem.X usystem.U
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ print("Testing AutomationLabsSystems sub functions...")
took_seconds = @elapsed include("./system_linearization_test.jl");
println("done (took ", took_seconds, " seconds)")


print("Testing AutomationLabsSystems sub functions...")
took_seconds = @elapsed include("./system_discretization_test.jl");
println("done (took ", took_seconds, " seconds)")
50 changes: 50 additions & 0 deletions test/system_discretization_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2022: Pierre Blaud and contributors
########################################################
# This Source Code Form is subject to the terms of the #
# Mozilla Public License, v. 2.0. If a copy of the MPL #
# was not distributed with this file, #
# You can obtain one at https://mozilla.org/MPL/2.0/. #
########################################################
module SystemsLinearizationTest

using Test
using AutomationLabsSystems
using AutomationLabsIdentification
using MathematicalSystems
using LazySets
using MLJ
using Flux

@testset "Discretize a linear continuous system" begin

model_origin = "user"
A = [1 1; 0 0.9]
B = [1; 0.5]
nbr_state = 2
nbr_input = 1

x_cons = [-5.0 5.0;
-5.0 5.0]

u_cons = [-1.0 1.0]

sys_c = proceed_system("continuous", model_origin;
A = A,
B = B,
input_constraint = u_cons,
state_constraint = x_cons)

sample_time = 5.0
sys_d = proceed_system_discretization(sys_c, sample_time)

@test typeof(sys_c) == ConstrainedLinearControlContinuousSystem{Float64, Matrix{Float64}, Matrix{Float64}, Hyperrectangle{Float64, Vector{Float64}, Vector{Float64}}, Hyperrectangle{Float64, Vector{Float64}, Vector{Float64}}}
@test typeof(sys_d) == ConstrainedLinearControlDiscreteSystem{Float64, Matrix{Float64}, Matrix{Float64}, Hyperrectangle{Float64, Vector{Float64}, Vector{Float64}}, Hyperrectangle{Float64, Vector{Float64}, Vector{Float64}}}

@test sys_c.A != sys_d.A
@test sys_c.B != sys_d.B
@test sys_c.X == sys_d.X
@test sys_c.U == sys_d.U

end

end

0 comments on commit 8704d99

Please sign in to comment.