symphony.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <cassert>
  2. #include <chrono>
  3. #include "simple/motion/movement.hpp"
  4. #include "simple/motion/symphony.hpp"
  5. using namespace std::literals;
  6. using duration = std::chrono::duration<float>;
  7. using simple::support::way;
  8. using simple::motion::symphony;
  9. template <typename T> class show_type;
  10. void SharpTurn()
  11. {
  12. // TODO: use rational, can't rely on float
  13. using movement = simple::motion::movement<duration,float,float>;
  14. std::array<float,2> block{0,0};
  15. auto sharp_turn = symphony(std::vector<movement>{
  16. movement{1250ms, 0,1},
  17. movement{1250ms, 0,1}
  18. });
  19. auto range = sharp_turn.move(block, 500ms).updated;
  20. assert( block[0] == way(0.f,1.f, 0.5f / 1.25f) );
  21. assert( block[1] == 0.f );
  22. assert( range.end() - range.begin() == 1 );
  23. range = sharp_turn.move(block, 500ms).updated;
  24. assert( block[0] == way(0.f,1.f, 1.f / 1.25f) );
  25. assert( block[1] == 0.f );
  26. assert( range.end() - range.begin() == 1 );
  27. range = sharp_turn.move(block, 500ms).updated;
  28. assert( block[0] == way(0.f,1.f, 1.f) );
  29. assert( block[1] == way(0.f,1.f, 0.25f / 1.25f) );
  30. assert( range.end() - range.begin() == 2 );
  31. range = sharp_turn.move(block, 500ms).updated;
  32. assert( block[0] == way(0.f,1.f, 1.f) );
  33. assert( block[1] == way(0.f,1.f, 0.75f / 1.25f) );
  34. assert( range.end() - range.begin() == 1 );
  35. }
  36. void Advance()
  37. {
  38. // TODO: use rational, can't rely on float
  39. using movement = simple::motion::movement<std::chrono::milliseconds,float,float>;
  40. auto sequence = symphony(std::vector{
  41. movement{10ms, 0,1},
  42. movement{10ms, 0,1},
  43. movement{10ms, 0,1},
  44. movement{10ms, 0,1}
  45. });
  46. const auto begin = sequence.range().begin();
  47. auto result = sequence.advance(10ms);
  48. assert( not result.done );
  49. assert( result.updated.lower() == begin+0 );
  50. assert( result.updated.upper() == begin+1 );
  51. assert( result.remaining == 0ms );
  52. result = sequence.advance(10ms);
  53. assert( not result.done );
  54. assert( result.updated.lower() == begin+1 );
  55. assert( result.updated.upper() == begin+2 );
  56. assert( result.remaining == 0ms );
  57. result = sequence.advance(10ms);
  58. assert( not result.done );
  59. assert( result.updated.lower() == begin+2 );
  60. assert( result.updated.upper() == begin+3 );
  61. assert( result.remaining == 0ms );
  62. result = sequence.advance(10ms);
  63. assert( result.done );
  64. assert( result.updated.lower() == begin+3 );
  65. assert( result.updated.upper() == begin+4 );
  66. assert( result.remaining == 0ms );
  67. sequence.reset();
  68. result = sequence.advance(15ms);
  69. assert( not result.done );
  70. assert( result.updated.lower() == begin+0 );
  71. assert( result.updated.upper() == begin+2 );
  72. assert( result.remaining == 0ms );
  73. result = sequence.advance(15ms);
  74. assert( not result.done );
  75. assert( result.updated.lower() == begin+1 );
  76. assert( result.updated.upper() == begin+3 );
  77. assert( result.remaining == 0ms );
  78. result = sequence.advance(15ms);
  79. assert( result.done );
  80. assert( result.updated.lower() == begin+3 );
  81. assert( result.updated.upper() == begin+4 );
  82. assert( result.remaining == 5ms );
  83. }
  84. int main()
  85. {
  86. SharpTurn();
  87. Advance();
  88. return 0;
  89. }