You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

128 lines
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.
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://github.com/WiringPi/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. /*
  43. softServoWrite(1, -250);
  44. delay(1000);
  45. softServoWrite(1, 0);
  46. delay(1000);
  47. softServoWrite(1, 50);
  48. delay(1000);
  49. softServoWrite(1, 150);
  50. delay(1000);
  51. softServoWrite(1, 200);
  52. delay(1000);
  53. softServoWrite(1, 250);
  54. delay(1000);
  55. softServoWrite(1, 1250);
  56. delay(1000);
  57. */
  58. /*
  59. softServoWrite (1, 1000) ; delay(1000);
  60. softServoWrite (2, 1100) ; delay(1000);
  61. softServoWrite (3, 1200) ; delay(1000);
  62. softServoWrite (4, 1300) ; delay(1000);
  63. softServoWrite (5, 1400) ; delay(1000);
  64. softServoWrite (6, 1500) ; delay(1000);
  65. softServoWrite (7, 2200) ; delay(1000);
  66. */
  67. int i = 1;
  68. int step = 0;
  69. const int cMin = -250;
  70. const int cMax = 1250;
  71. const int cStep = 100;
  72. printf("\n");
  73. printf("Home, Max, Home...\n");
  74. softServoWrite(cPin, cMax);
  75. softServoWrite(cPin, cMin);
  76. softServoWrite(cPin, cMax);
  77. delay(1000);
  78. printf("Stepping %d steps...\n", (cMax - cMin)/cStep);
  79. for (step = cMin; step < cMax; step += cStep, ++i)
  80. {
  81. printf("%d...", i); fflush(stdout);
  82. softServoWrite(cPin, step);
  83. delay(300);
  84. }
  85. printf("\n");
  86. printf("Returning to home position.\n");
  87. softServoWrite(cPin, cMin);
  88. delay(1000);
  89. const int cStep1 = 50;
  90. const int cRange = (cMax - cMin) / cStep1;
  91. time_t t;
  92. /* Intializes random number generator */
  93. srand((unsigned) time(&t));
  94. const int cPositions = 20;
  95. int pos = cMin;
  96. int lastPos = pos;
  97. printf("Setting %d random positions...\n", cPositions);
  98. for (i = 1; i <= cPositions; ++i)
  99. {
  100. while (pos == lastPos)
  101. {
  102. pos = ((rand() % cRange) * cStep1) + cMin;
  103. }
  104. printf("%d...", i); fflush(stdout);
  105. softServoWrite(cPin, pos);
  106. lastPos = pos;
  107. delay(200);
  108. }
  109. printf("\n");
  110. printf("Returning to home position.\n");
  111. softServoWrite(cPin, cMin);
  112. delay(1000);
  113. return 0;
  114. }