Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

125 rindas
3.4 KiB

  1. /*
  2. * servo.c:
  3. * Test of the softServo code.
  4. * Do not use this code - use the servoBlaster kernel module instead
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include <wiringPi.h>
  31. #include <softServo.h>
  32. // GPIO 1 (physical pin 12)
  33. static const int cPin = 1;
  34. int main()
  35. {
  36. if (wiringPiSetup() == -1)
  37. {
  38. fprintf(stdout, "oops: %s\n", strerror(errno));
  39. return 1;
  40. }
  41. softServoSetup(cPin, -1, -1, -1, -1, -1, -1, -1);
  42. // softServoWrite(1, -250);
  43. // delay(1000);
  44. // softServoWrite(1, 0);
  45. // delay(1000);
  46. // softServoWrite(1, 50);
  47. // delay(1000);
  48. // softServoWrite(1, 150);
  49. // delay(1000);
  50. // softServoWrite(1, 200);
  51. // delay(1000);
  52. // softServoWrite(1, 250);
  53. // delay(1000);
  54. // softServoWrite(1, 1250);
  55. // delay(1000);
  56. /*
  57. softServoWrite (1, 1000) ; delay(1000);
  58. softServoWrite (2, 1100) ; delay(1000);
  59. softServoWrite (3, 1200) ; delay(1000);
  60. softServoWrite (4, 1300) ; delay(1000);
  61. softServoWrite (5, 1400) ; delay(1000);
  62. softServoWrite (6, 1500) ; delay(1000);
  63. softServoWrite (7, 2200) ; delay(1000);
  64. */
  65. int i = 1;
  66. int step = 0;
  67. const int cMin = -250;
  68. const int cMax = 1250;
  69. const int cStep = 100;
  70. printf("\n");
  71. printf("Home, Max, Home...\n");
  72. softServoWrite(cPin, cMax);
  73. softServoWrite(cPin, cMin);
  74. softServoWrite(cPin, cMax);
  75. delay(1000);
  76. printf("Stepping %d steps...\n", (cMax - cMin)/cStep);
  77. for (step = cMin; step < cMax; step += cStep, ++i)
  78. {
  79. printf("%d...", i); fflush(stdout);
  80. softServoWrite(cPin, step);
  81. delay(300);
  82. }
  83. printf("\n");
  84. printf("Returning to home position.\n");
  85. softServoWrite(cPin, cMin);
  86. delay(1000);
  87. const int cStep1 = 50;
  88. const int cRange = (cMax - cMin) / cStep1;
  89. time_t t;
  90. /* Intializes random number generator */
  91. srand((unsigned) time(&t));
  92. const int cPositions = 20;
  93. int pos = cMin;
  94. int lastPos = pos;
  95. printf("Setting %d random positions...\n", cPositions);
  96. for (i = 1; i <= cPositions; ++i)
  97. {
  98. while (pos == lastPos)
  99. {
  100. pos = ((rand() % cRange) * cStep1) + cMin;
  101. }
  102. printf("%d...", i); fflush(stdout);
  103. softServoWrite(cPin, pos);
  104. lastPos = pos;
  105. delay(200);
  106. }
  107. printf("\n");
  108. printf("Returning to home position.\n");
  109. softServoWrite(cPin, cMin);
  110. delay(1000);
  111. return 0;
  112. }