window_obstacle_sa.BASE.sa 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import Window_SA
  2. module WindowObstacle_SA
  3. semantic adaptation reactive mealy window_obstacle_sa
  4. /*
  5. Notice that we're importing the Window_SA module... that's where the window_sa FMU is defined.
  6. However, it is not clear where is the information that describes how the window_sa is connected to the obstacle.
  7. It is not in the PowerWindowModel module, nor in the Window_SA module.
  8. As a result, I would suggest that we are able to describe these connections within this semantic adaptation.
  9. */
  10. for fmu WindowSA window_sa
  11. at "./path/to/Window_sa.fmu"
  12. with input ports displacement (rad), speed (rad/s), reaction_force (N)
  13. with output ports disp (m), tau (N.m);
  14. for fmu Obstacle obstacle
  15. at "./path/to/Obstacle.fmu"
  16. with input ports disp (m),
  17. with output ports reaction_force (m);
  18. /*
  19. This is the information that was missing before: how the semantic adaptation window_sa, which is an FMU, connects to the obstacle FMU.
  20. */
  21. window_sa.disp -> obstacle.disp,
  22. obstacle.reaction_force -> window_sa.reaction_force;
  23. /*
  24. Declares the external ports of the semantic adaptation
  25. Notice that window_sa.displacement is a dangling input port, so an extra input port will exist in the adapted FMU.
  26. */
  27. input ports speed;
  28. output ports tau;
  29. in var current_speed := 0;
  30. control var previous_speed := 0;
  31. control var future_speed := 0;
  32. param RATE := 10;
  33. in rules {
  34. true -> {
  35. /*
  36. These instructions would violate the principle that multiple calls to setValues can be made:
  37. previous_speed := future_speed;
  38. future_speed := speed;
  39. Upon execution, this block must call setValues of the original FMUs (window_sa and obstacle).
  40. The correct thing then is to execute the next block and then do the setValues of the original FMUs.
  41. */
  42. current_speed := speed;
  43. } --> {
  44. /*
  45. 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.
  46. The following variables are available in this block:
  47. 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.
  48. h - this is the communication step size that will be sent to the inner FMU.
  49. */
  50. window_sa.speed := previous_speed + (future_speed - previous_speed)*dt;
  51. }
  52. }
  53. control rules {
  54. var h := H/RATE
  55. /*
  56. 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.
  57. */
  58. previous_speed := future_speed;
  59. future_speed := current_speed; // you cannot access input ports of the adapted FMU here.
  60. for (var iter in 0 .. RATE) { // multi-rate loop
  61. var prev_height := window_sa.height
  62. var inner_time := t
  63. for (var iter in 0 .. MAXITER) {
  64. save_state(obstacle);
  65. save_state(window_sa);
  66. do_step(obstacle,inner_time,h);
  67. do_step(window_sa,inner_time,h);
  68. if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
  69. break;
  70. } else {
  71. prev_height := height;
  72. rollback(obstacle)
  73. rollback(window_sa)
  74. }
  75. /*
  76. The above block of statements is equivalent to the following:
  77. var obstacle_state := getState(obstacle)
  78. var window_sa_state := getState(window_sa)
  79. obstacle.set("height", height)
  80. obstacle.doStep(inner_time,h)
  81. reaction_force := obstacle.get("reaction_force")
  82. window_sa.set("reaction_force", reaction_force)
  83. window_sa.doStep(inner_time,h)
  84. height := window_sa.get("height")
  85. if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
  86. // 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.
  87. break;
  88. } else {
  89. prev_height := height;
  90. obstacle.setState(obstacle_state);
  91. window_sa.setState(window_sa_state);
  92. }
  93. */
  94. }
  95. inner_time := t + h;
  96. }
  97. }