Advertisement
Guest User

Barndoor

a guest
Mar 25th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////
  2. // This Arduino example demonstrates bidirectional operation of a
  3. // 28BYJ-48, which is readily available on eBay, using a ULN2003
  4. // interface board to drive the stepper. The 28BYJ-48 motor is a 4-
  5. // phase, 8-beat motor, geared down by a factor of 64. One bipolar
  6. // winding is on motor pins 1 & 3 and the other on motor pins 2 & 4.
  7. // Refer to the manufacturer's documentation of Changzhou Fulling
  8. // Motor Co., Ltd., among others. The step angle is 5.625/64 and the
  9. // operating Frequency is 100pps. Current draw is 92mA.
  10. //////////////////////////////////////////////////////////////////////////////
  11. // Motor step angle = 5.625 degrees ==> 64 pulses per internal rev
  12. // Gear reduction = 1/64 ==> 4096 pulses per external rev
  13. //////////////////////////////////////////////////////////////////////////////
  14.  
  15. // Motor pins: blue=1, pink=2, yellow=3, orange=4
  16. int motorPin[4] = {8, 9, 10, 11};
  17. // One counter-clockwise internal revolution going down the rows
  18. int ccw[8][4] =
  19. {
  20. {HIGH, LOW, LOW , LOW},
  21. {HIGH, HIGH, LOW , LOW},
  22. {LOW , HIGH, LOW , LOW},
  23. {LOW , HIGH, HIGH, LOW},
  24. {LOW , LOW, HIGH, LOW},
  25. {LOW , LOW, HIGH, HIGH},
  26. {LOW , LOW, LOW, HIGH},
  27. {HIGH, LOW, LOW, HIGH}
  28. };
  29.  
  30. // Global variables used by the stepper algorithm
  31. long ip, np, npMin;
  32. double phi0, t, t0, dtpp;
  33. //////////////////////////////////////////////////////////////////////////////
  34. void setup() {
  35.  
  36. // Basic constants
  37. double pi = 4*atan(1.0); // Pi = 3.1415927
  38. double inch2cm = 2.54; // One inch is 2.54 cm
  39. double secSidDay = 86164.1; // Seconds in a siderial year
  40. double eRot = 2*pi/secSidDay; // Earth's rotation in radians per sec
  41.  
  42. // Motor characteristics
  43. int ppir = 64; // Pulses per internal rev (360/5.625)
  44. int ppr = 64*ppir; // Pulses per revolution (gear reduction factor = 64)
  45.  
  46. // Barndoor geometry - initialized in setup
  47. double tpi; // Threads per inch of the screw
  48. double beam; // Hinge to T-nut/cap nut in cm
  49. double y0; // Initial vertical T-nut position in cm
  50. double y1; // Final vertical T-nut position in cm
  51.  
  52. tpi = 20;
  53. beam = 19.6;
  54. y0 = 3.0;
  55. y1 = 1.0;
  56.  
  57. double cmpr = inch2cm/tpi; // cm per revolution (1 thread of the screw)
  58. double phipr = cmpr/beam; // Angle (phi) difference per revolution (in radians)
  59. double dtpr = phipr/eRot; // The time it takes the sky to rotate that much
  60. dtpp = dtpr/ppr; // Pulse time in sec, nominal value = 6471.8 mu
  61.  
  62. phi0 = y0/beam; // Initial angle between the beams (approximate)
  63. np = (y0/inch2cm)*tpi*ppr; // Number of pulses needed from y0 down to 0
  64. npMin = (y1/inch2cm)*tpi*ppr; // Turn off at the last y1 cm
  65. ip = 0; // Pulse counter
  66.  
  67. // Declare the motor pins as outputs
  68. for (int j = 0; j < 4; j++)
  69. pinMode(motorPin[j], OUTPUT);
  70. analogReference(DEFAULT);
  71.  
  72. t = micros(); // Time in microseconds. Note, the math above is in seconds.
  73. t0 = t;
  74. ip = 0;
  75.  
  76. } //void setup end
  77.  
  78. //////////////////////////////////////////////////////////////////////////////
  79. void loop(){
  80. // We don't need this outer loop, let's write our own for better control
  81. // so this only gets called once
  82. while (ip + npMin <= np) {
  83. int ic = ip % 8; // Internal cycle index
  84. double dt, dt1;
  85. // Determine dt
  86. dt = dtpp;
  87.  
  88. // Compensate for the fact that we are using a straight screw
  89. double fac = cos((phi0*(np - ip))/(2*np));
  90. dt = dt/fac;
  91.  
  92. // Step the motor
  93. for (int j = 0; j < 4; j++) {
  94. digitalWrite(motorPin[3-j], ccw[ic][j]);
  95. }
  96.  
  97. // Update the timeline
  98. t = t + 1000000*dt;
  99.  
  100. // Delay to regulate the speed.
  101. // delayMicroseconds() is accurate up to a few milliseonds so delay
  102. // in 1 ms chunks. Note also that micros() cycles in 70 minutes.
  103. while (t > micros() + 1000)
  104. delayMicroseconds(1000);
  105. dt1 = t - micros();
  106. if (dt1 > 0)
  107. delayMicroseconds(dt1);
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement