Coil winder - need people's input on the subject of.

Discussion in 'The Projects Forum' started by Steve Tofflemire, Feb 22, 2016.

  1. Steve Tofflemire

    Thread Starter New Member

    Aug 13, 2015
    3
    0
    Almost Complete - Coil Winder
    Just a tad more tinkering and some programming is left.
    Very close currently to making the first coil.
    Right now, just working on obtaining the value for number of encoder ticks per revolution of the spool motor (brushed and controlled with a MOSFET; in 1 direction that is).
    The code may be a bit off in terms of stepping at the point in time when necessary to increase the horizontal displacement of the guide (stepper motor; bi-directional).
    The limits all work and we've defined the average step distance on the linear drive (stepper - as just mentioned).
    You will have to ask me a question if you're lost in what the code does and it may help me familiarize what I wrote. It's somewhat of a large code but don't get discouraged because I'm quite excellent with my format of code although I do lack the use of comments.
    Please give me your input. If anyone is interested I'll post a picture. Thanks! Here's the code:

    Code (Text):
    1. /* Digital pins 0 to 13 (14 total)
    2. * Analog pins A0 to A7 can be used as digital too (8 total)
    3. * Grand Total: 22 digital input/output pins
    4. * PWM: 3, 5, 6, 9, 10, and 11. (980Hz on 5 and 6, otherwise 490Hz)
    5. *
    6. * Connect a tactile button switch (or something similar) from Arduino pin X to ground.
    7. * HIGH RES PWM LIBRARY
    8. *
    9. * rotational_spool.encoder.total_ticks = 32;
    10. * linear.distance_per_step = 55.88mm/1711 = 0.03265926358854mm;
    11. */
    12.  
    13. //include
    14. #include "Encoder_Polling_V2.h"
    15. #include "Button.h"
    16. #include <AccelStepper.h>
    17.  
    18. //constants
    19. const int PIN_LIM0 = A2;  // mot1 limit switch 0
    20. const int PIN_LIM1 = A3;  // mot1 limit switch 1
    21. const int PIN_LIM2 = A4;  // mot0 limit switch
    22.  
    23. const int PIN_ENC0_A = 2; // spool encoder
    24. const int PIN_ENC0_B = 4;
    25. const int PIN_ENC1_A = 3; // guide encoder
    26. const int PIN_ENC1_B = 5;
    27.  
    28. const int PIN_MOT0_EN = 6; // spool motor   (brushed)
    29.  
    30. //const int MOT1_SPR = 48;
    31. const int PIN_MOT1_A = 8;  // linear motor  (stepper)
    32. const int PIN_MOT1_B = 9;
    33. const int PIN_MOT1_C = 10;
    34. const int PIN_MOT1_D = 11;
    35.  
    36. //const int PIN_LED = 13;
    37.  
    38. const unsigned long TS1 = 50L;
    39. const int HIGHN = 5000000;
    40.  
    41. const float DPS = 0.0326593f;
    42. const int RUNSPD = 112;
    43.  
    44. //variables
    45. Button LIM0_btn(PIN_LIM0, true, true, 20);
    46. Button LIM1_btn(PIN_LIM1, true, true, 20);
    47. Button LIM2_btn(PIN_LIM2, true, true, 20);
    48.  
    49. bool LIM0_met = false;
    50. bool LIM1_met = false;
    51. bool LIMx_met = false;
    52. long LIM0_pos, LIM1_pos;
    53.  
    54. bool b_complete = false;
    55.  
    56. long pos0 = 0, pos1 = 0, opos0 = 0, opos1 = 0;
    57. unsigned long TC_ms, TS, T, TCO1, TCO2, dTCO;
    58. float vel0, vel1;
    59. float X0, X1;
    60. float wire_t = 0.1; // in mm
    61.  
    62.  
    63. AccelStepper MOT1(AccelStepper::FULL4WIRE, PIN_MOT1_A, PIN_MOT1_B, PIN_MOT1_C, PIN_MOT1_D);
    64.  
    65.  
    66. //methods
    67. void setup() {
    68.   Serial.begin(115200);
    69.   Serial.println("START");
    70.   Serial.println("1");
    71.  
    72.  
    73.   pinMode(PIN_ENC0_A, INPUT);
    74.   pinMode(PIN_ENC0_B, INPUT);
    75.   pinMode(PIN_ENC1_A, INPUT);
    76.   pinMode(PIN_ENC1_B, INPUT);
    77.   digitalWrite(PIN_ENC0_A, HIGH);
    78.   digitalWrite(PIN_ENC0_B, HIGH);
    79.   digitalWrite(PIN_ENC1_A, HIGH);
    80.   digitalWrite(PIN_ENC1_B, HIGH);
    81.  
    82.   encoder_begin();
    83.   attach_encoder(0, PIN_ENC0_A, PIN_ENC0_B);
    84.   attach_encoder(1, PIN_ENC1_A, PIN_ENC1_B);
    85.  
    86.   pinMode(PIN_MOT0_EN, OUTPUT);
    87.   analogWrite(PIN_MOT0_EN, RUNSPD);
    88.  
    89.   MOT1.setMaxSpeed(9);
    90.   MOT1.setAcceleration(9);
    91.  
    92.   Serial.print("Enter the wire thickness: ");
    93.   while (Serial.available() == 0) {}
    94.   wire_t = Serial.parseFloat();
    95.   Serial.print(wire_t);
    96.   Serial.println(" mm of wire thickness.");
    97.   Serial.println("");
    98.  
    99.   MOT1.moveTo(HIGHN);
    100. }
    101.  
    102. void loop() {
    103.   int dir0, dir1;
    104.  
    105.   T = 0;
    106.   TS = 0;
    107.   TC_ms = millis();
    108.   TCO1 = micros();
    109.  
    110.   while (1) {
    111.     //interrupts();
    112.    
    113.     MOT1.run();
    114.    
    115.     dir0 = encoder_data(0);
    116.     dir1 = encoder_data(1);
    117.     if (dir0 != 0) {
    118.       pos0 += (long)dir0;
    119.     }
    120.     if (dir1 != 0) {
    121.       pos1 += (long)dir1;
    122.     }
    123.    
    124.     if (T%5000==0) {
    125.       TCO2 = micros();
    126.       dTCO = TCO2 - TCO1;
    127.       vel0 = dTCO == 0 ? 0 : 1000000.0f*(pos0-opos0)/dTCO;
    128.       vel1 = dTCO == 0 ? 0 : 1000000.0f*(pos1-opos1)/dTCO;
    129.  
    130.       /*
    131.       Serial.println("");
    132.       Serial.print(pos0);
    133.       Serial.print("<-ENC0_pos:ENC1_pos->");
    134.       Serial.print(pos1);
    135.       Serial.println(";");
    136.       Serial.print(vel0, 1);
    137.       Serial.print("<-ENC0_vel:ENC1_vel->");
    138.       Serial.print(vel1, 1);
    139.       Serial.println(";");
    140.      
    141.       Serial.print(MOT1.currentPosition());
    142.       Serial.println("");
    143.       */
    144.      
    145.       opos0 = pos0;
    146.       opos1 = pos1;
    147.       TCO1 = micros();
    148.     }
    149.  
    150.    
    151.     LIM0_btn.read();
    152.     LIM1_btn.read();
    153.     LIM2_btn.read();
    154.     if (LIM2_btn.wasPressed()) {
    155.       Serial.print("LIM2 ");
    156.       Serial.print(pos0);
    157.       Serial.println(" : pos0 - enc0 - spool");
    158.     }
    159.     if (LIMx_met) {
    160.       if (!b_complete) makeCoil();
    161.     } else {
    162.       if (LIM0_btn.wasPressed()) {
    163.         Serial.println("LIM0");
    164.         LIM0();
    165.       }
    166.       if (LIM1_btn.wasPressed()) {
    167.         Serial.println("LIM1");
    168.         LIM1();
    169.       }
    170.     }
    171.  
    172.     if (millis() >= TC_ms + TS1) {
    173.       TS++;
    174.       TC_ms = millis();
    175.     }
    176.    
    177.     T++;
    178.   }
    179. }
    180.  
    181.  
    182. //sub-methods
    183. void makeCoil() {
    184.   if (MOT1.currentPosition() > (LIM0_pos > LIM1_pos ? LIM0_pos : LIM1_pos)) {
    185.     analogWrite(PIN_MOT0_EN, 0);
    186.     MOT1.disableOutputs();
    187.     Serial.println("STOP");
    188.     b_complete = true;
    189.     return;
    190.   }
    191.   X0 = float(wire_t*pos0/32.0f);
    192.   X1 = float(MOT1.currentPosition()*DPS);
    193.   if (X0 < X1) {
    194.     MOT1.moveTo(MOT1.currentPosition()+1);
    195.   }
    196.  
    197.   /*
    198.   if (T%5000 == 0) {
    199.     Serial.print(pos0);
    200.     Serial.print(" : pos0, ");
    201.     Serial.print(MOT1.currentPosition());
    202.     Serial.print(" : MOT1.c_pos, ");
    203.     Serial.print(X0);
    204.     Serial.print(" : X0, ");
    205.     Serial.print(X1);
    206.     Serial.println(" : X1");
    207.   }
    208.   */
    209. }
    210.  
    211. void LimSpan() {
    212.   unsigned long N = abs(LIM1_pos-LIM0_pos);
    213.   Serial.print(N);
    214.   Serial.println("");
    215.   LIMx_met = true;
    216.   MOT1.stop();
    217.  
    218.   X0 = 0.0f;
    219.   X1 = 0.0f;
    220.   pos0 = 0L;
    221.   analogWrite(PIN_MOT0_EN, RUNSPD);
    222. }
    223.  
    224. void LIM0() {
    225.   if (!LIM0_met) {
    226.     LIM0_met = true;
    227.     LIM0_pos = MOT1.currentPosition();
    228.     Serial.print(LIM0_pos);
    229.     Serial.println(" LIM0 position");
    230.     if (LIM1_met) {
    231.       LimSpan();
    232.     } else {
    233.       MOT1.moveTo(-HIGHN);
    234.     }
    235.   }
    236. }
    237.  
    238. void LIM1() {
    239.   if (!LIM1_met) {
    240.     LIM1_met = true;
    241.     LIM1_pos = MOT1.currentPosition();
    242.     Serial.print(LIM1_pos);
    243.     Serial.println(" LIM1 position");
    244.     if (LIM0_met) {
    245.       LimSpan();
    246.     } else {
    247.       MOT1.moveTo(HIGHN);
    248.     }
    249.   }
    250. }
    251.  
     
Loading...