window_obstacle_sa.BASE.sa 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import PowerWindowModel
  2. import Window_SA
  3. module WindowObstacle_SA
  4. semantic adaptation window_obstacle_sa
  5. /*
  6. Notice that we're importing the Window_SA module... that's where the window_sa FMU is defined.
  7. */
  8. for fmu window_sa, obstacle
  9. input ports displacement -> displacement, // I should not need to say this.
  10. speed;
  11. output ports tau -> tau;
  12. in var previous_speed := 0;
  13. in var future_speed := 0;
  14. in var current_speed := 0;
  15. param RATE := 10;
  16. in rules {
  17. true -> {
  18. /*
  19. These instructions would violate the principle that multiple calls to setValues can be made:
  20. previous_speed := future_speed;
  21. future_speed := speed;
  22. Upon execution, this block must call setValues of the original FMUs (window_sa and obstacle).
  23. The correct thing then is to execute the next block and then do the setValues of the original FMUs.
  24. */
  25. current_speed := adapted.speed
  26. } --> {
  27. /*
  28. 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.
  29. The following variables are available in this block:
  30. 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.
  31. h - this is the communication step size that will be sent to the inner FMU.
  32. t - This is the communication time also available at the control rules block.
  33. As previously described, the displacement input port is taken care of automatically by the declaration "displacement -> displacement" in the input ports.
  34. So the declaration would be equivalent to having this intruction in this block.
  35. displacement := displacement;
  36. */
  37. window_sa.speed := previous_speed + (future_speed - previous_speed)*dt;
  38. }
  39. // Feedback: everything should be prefixed, and the prefixes should be short.
  40. }
  41. control rules {
  42. var h := H/RATE
  43. /*
  44. 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.
  45. */
  46. previous_speed := future_speed;
  47. future_speed := current_speed; // you cannot access input ports of the adapted FMU here.
  48. for (var iter in 0 .. RATE) { // multi-rate loop
  49. var prev_height := window_sa.height
  50. var inner_time := t
  51. for (var iter in 0 .. MAXITER) {
  52. transaction(obstacle) // Inspired by transactions in databases.
  53. transaction(window_sa)
  54. obstacle.doStep(inner_time,h)
  55. window_sa.doStep(inner_time,h)
  56. if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
  57. commit(obstacle);
  58. commit(window_sa);
  59. break;
  60. } else {
  61. prev_height := height;
  62. rollback(obstacle)
  63. rollback(window_sa)
  64. }
  65. /*
  66. The above block of statements is equivalent to the following:
  67. var obstacle_state := getState(obstacle)
  68. var window_sa_state := getState(window_sa)
  69. obstacle.set("height", height)
  70. obstacle.doStep(inner_time,h)
  71. reaction_force := obstacle.get("reaction_force")
  72. window_sa.set("reaction_force", reaction_force)
  73. window_sa.doStep(inner_time,h)
  74. height := window_sa.get("height")
  75. if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
  76. // 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.
  77. break;
  78. } else {
  79. prev_height := height;
  80. obstacle.setState(obstacle_state);
  81. window_sa.setState(window_sa_state);
  82. }
  83. */
  84. }
  85. var inner_time := t + h;
  86. }
  87. }
  88. out rules {
  89. true -> { } --> { };
  90. }