fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define M_PI 3.1415
  5. #define uint32_t int
  6. #define int32_t int
  7. #define KROK 0.1//mm
  8. #define STEPS_PER_MM (1 / KROK) // 10 kroków na 1 mm
  9. //const uint32_t Lmax = 2; //mm
  10. const float Lmax = 100;//65;//42; //mm
  11. uint32_t Tmax = 500;//330; //ms
  12. uint32_t T;// = 500;//330; //ms
  13. // Tablica przechowująca pozycje (w mm) dla każdej milisekundy
  14. float positions[1000+1]; // 0-1000 ms
  15. uint32_t steps[1000+1]; // 0-1000 ms
  16. int32_t stepsToMove[1000+1]; // 0-1000 ms
  17. int32_t stepsToMoveLimit[1000+1]; // 0-1000 ms
  18. int32_t stepsToMovePamiec = 0;
  19. const uint32_t maxStepsPerMs = 4;//1000 / MIN_ODSTEP_MIEDZY_KOLEJNYMI_IMPULSAMI_US;
  20. uint32_t currentPosition = 0;
  21. uint32_t targetPosition = 0;
  22. int32_t delta = 0;
  23. //uint32_t stepsToMove = 0;
  24. // for(int k=0; k<20; k++){
  25. // Inicjalizacja tablicy przechowującej pozycje (w mm) dla każdej milisekundy
  26. int t = 0, L;
  27. int main(void)
  28. {
  29. //for (T = 200; T <= Tmax; T+=20) {
  30.  
  31. currentPosition = 0;
  32.  
  33. // for (L = 0; L <= Lmax; L++) {
  34. // currentPosition = 0;
  35. L=66;
  36. for (t = 0; t <= T; t++) {
  37.  
  38. positions[t] = (L / 2) * (sin((2.0 * t / T) * M_PI - M_PI/2) + 1);
  39. steps[t] = roundf(positions[t] * STEPS_PER_MM);
  40. targetPosition = steps[t];
  41. delta = targetPosition - currentPosition;
  42.  
  43.  
  44.  
  45. if( abs(delta) > maxStepsPerMs)
  46. {
  47. printf("Dużo: %d T=%d \n", L, T);
  48. L = Lmax;//zeby wyjsc z petli
  49. break;
  50. if( delta > 0)
  51. {
  52. stepsToMove[t] = maxStepsPerMs;
  53. }
  54. else
  55. {
  56. stepsToMove[t] = -1 * maxStepsPerMs;
  57. }
  58. }
  59. else
  60. {
  61. stepsToMove[t] = delta;
  62. }
  63.  
  64. // Generuj impulsy
  65. //for (int i = 0; i < abs(stepsToMove[t]); i++) {
  66. // // Ustaw kierunek
  67. // if( stepsToMove[t] > 0)
  68. // {
  69. // SerwoDirUp(&Serwo1);
  70. // }
  71. // else
  72. // {
  73. // SerwoDirDown(&Serwo1);
  74. // }
  75.  
  76. // SerwoImpSet(&Serwo1);
  77. // TurnOnLedSys();
  78. // TurnOffLedSys();
  79. // SerwoImpReset(&Serwo1);
  80. // TurnOnLedSys();
  81. // TurnOffLedSys();
  82.  
  83. //}
  84. // digitalWrite(PULSE_PIN, HIGH);
  85. // delayMicroseconds(50); // Minimalny czas trwania impulsu
  86. // digitalWrite(PULSE_PIN, LOW);
  87. // delayMicroseconds(50);
  88. // }
  89. stepsToMoveLimit[t] = stepsToMove[t] + stepsToMovePamiec;
  90. stepsToMovePamiec = stepsToMoveLimit[t];
  91. currentPosition = stepsToMoveLimit[t];//targetPosition;
  92.  
  93. //if (t > T) printf("OK L=%d t=%d \n", L, T);
  94. //else printf("Za dużo: L=%d t=%d \n", L, t);
  95. //if (t <= T)
  96. //{
  97. // printf("Lmax=%d t=%d \n", L-1, t-1);
  98. // break;
  99. //}
  100. }
  101. //if (t > T) continue;//printf("OK L=%d T=%d \n", L, T);
  102. //}
  103. //}
  104. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty