window_obstacle_sa_flat.BASE.sa 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. module WindowObstacleLoop_SA
  2. semantic adaptation reactive mealy WindowObstableLoopSA adaptedFMU
  3. at "./path/to/WindowObstableLoopSA.fmu"
  4. for inner fmu WindowSA window_sa
  5. at "./path/to/WindowSA.fmu"
  6. with input ports displacement (rad), speed (rad/s), reaction_force (N)
  7. with output ports disp (m), tau (N.m)
  8. for inner fmu Obstacle obstacle
  9. at "./path/to/Obstacle.fmu"
  10. with input ports disp (m)
  11. with output ports reaction_force (m)
  12. /*
  13. This is the information that was missing before: how the semantic adaptation window_sa, which is an FMU, connects to the obstacle FMU.
  14. */
  15. with window_sa.disp -> obstacle.disp
  16. with obstacle.reaction_force -> window_sa.reaction_force
  17. /*
  18. Declares the external ports of the semantic adaptation
  19. Notice that window_sa.displacement is a dangling input port, so an extra input port will exist in the adapted FMU.
  20. */
  21. input ports speed
  22. output ports tau
  23. param RATE := 10,
  24. MAXITER := 10,
  25. REL_TOL := 1e-05,
  26. ABS_TOL := 1e-05;
  27. control var previous_speed := 0, future_speed := 0;
  28. control rules {
  29. var inner_dt := H/RATE;
  30. /*
  31. The two instructions below need to be made, because previously they were made in the sa_in block, but multiple calls to the sa_in block would make them fail.
  32. */
  33. previous_speed := future_speed;
  34. future_speed := current_speed; // you cannot access input ports of the adapted FMU here.
  35. for (var iter in 0 .. RATE) { // multi-rate loop
  36. var prev_disp := window_sa.disp;
  37. var inner_time := t;
  38. for (var iter in 0 .. MAXITER) {
  39. save_state(obstacle);
  40. save_state(window_sa);
  41. do_step(obstacle,inner_time,inner_dt);
  42. do_step(window_sa,inner_time,inner_dt);
  43. if (is_close(prev_disp, disp, REL_TOL, ABS_TOL)) {
  44. break;
  45. } else {
  46. prev_disp := disp;
  47. rollback(obstacle);
  48. rollback(window_sa);
  49. }
  50. /*
  51. The above block of statements is equivalent to the following:
  52. var obstacle_state := getState(obstacle)
  53. var window_sa_state := getState(window_sa)
  54. obstacle.set("height", height)
  55. obstacle.doStep(inner_time,h)
  56. reaction_force := obstacle.get("reaction_force")
  57. window_sa.set("reaction_force", reaction_force)
  58. window_sa.doStep(inner_time,h)
  59. height := window_sa.get("height")
  60. if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
  61. // The commits don't do anything, as far as I see. But they serve an analysis which ensures that states are correctly obtained and restored.
  62. break;
  63. } else {
  64. prev_height := height;
  65. obstacle.setState(obstacle_state);
  66. window_sa.setState(window_sa_state);
  67. }
  68. */
  69. }
  70. inner_time := t + inner_dt;
  71. }
  72. return H;
  73. }
  74. in var current_speed := 0;
  75. in rules {
  76. true -> {
  77. /*
  78. These instructions would violate the principle that multiple calls to setValues can be made:
  79. previous_speed := future_speed;
  80. future_speed := speed;
  81. Upon execution, this block must call setValues of the original FMUs (window_sa and obstacle).
  82. The correct thing then is to execute the next block and then do the setValues of the original FMUs.
  83. */
  84. current_speed := speed;
  85. } --> {
  86. /*
  87. This block will be called whenever any of the input ports that are unconnected in the original FMUs is read, in the control rules block.
  88. The following variables are available in this block:
  89. dt - In the control rules block, (t + dt) is the time that will be passed to the doStep function of the inner FMU. In the control rules block, this can be computed by doing dt := inner_time - t.
  90. h - this is the communication step size that will be sent to the inner FMU.
  91. */
  92. window_sa.speed := previous_speed + (future_speed - previous_speed)*dt;
  93. };
  94. }