example91.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //////////////////////////////////////////////////////////////////
  2. // example91.cpp
  3. //
  4. // Copyright (c) 2015 Robert Ramey
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include <iostream>
  10. #include <limits>
  11. #include <boost/safe_numerics/cpp.hpp>
  12. #include <boost/safe_numerics/safe_integer.hpp>
  13. #include <boost/safe_numerics//safe_integer_range.hpp>
  14. // use same type promotion as used by the pic compiler
  15. // see the following comment in motor.c
  16. // Types: int8,int16,int32=8,16,32bit integers
  17. using pic16_promotion = boost::safe_numerics::cpp<
  18. 8, // char
  19. 8, // short
  20. 8, // int
  21. 16, // long
  22. 32 // long long
  23. >;
  24. // define safe types used desktop version of the program. In conjunction
  25. // with the promotion policy above, this will permit us to guarantee that
  26. // the resulting program will be free of arithmetic errors introduced by
  27. // C expression syntax and type promotion with no runtime penalty
  28. template <typename T> // T is char, int, etc data type
  29. using safe_t = boost::safe_numerics::safe<
  30. T,
  31. pic16_promotion,
  32. boost::safe_numerics::default_exception_policy // use for compiling and running tests
  33. >;
  34. using safe_bool_t = boost::safe_numerics::safe_unsigned_range<
  35. 0,
  36. 1,
  37. pic16_promotion,
  38. boost::safe_numerics::default_exception_policy // use for compiling and running tests
  39. >;
  40. #define DESKTOP
  41. #include "motor1.c"
  42. #include <chrono>
  43. #include <thread>
  44. void sleep(int16){
  45. std::this_thread::sleep_for(std::chrono::microseconds(ccpr));
  46. }
  47. int main(){
  48. std::cout << "start test\n";
  49. try{
  50. initialize();
  51. motor_run(100);
  52. do{
  53. isr_motor_step();
  54. }while (run_flg);
  55. // move motor to position 1000
  56. motor_run(1000);
  57. do{
  58. sleep(ccpr);
  59. isr_motor_step();
  60. }while (run_flg);
  61. // move back to position 0
  62. motor_run(0);
  63. do{
  64. sleep(ccpr);
  65. isr_motor_step();
  66. }while (run_flg);
  67. }
  68. catch(std::exception & e){
  69. std::cout << e.what() << '\n';
  70. // we expect to trap an exception
  71. return 0;
  72. }
  73. catch(...){
  74. std::cout << "test interrupted\n";
  75. return 1;
  76. }
  77. std::cout << "end test\n";
  78. return 1;
  79. }