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:
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:
/* Digital pins 0 to 13 (14 total)
* Analog pins A0 to A7 can be used as digital too (8 total)
* Grand Total: 22 digital input/output pins
* PWM: 3, 5, 6, 9, 10, and 11. (980Hz on 5 and 6, otherwise 490Hz)
*
* Connect a tactile button switch (or something similar) from Arduino pin X to ground.
* HIGH RES PWM LIBRARY
*
* rotational_spool.encoder.total_ticks = 32;
* linear.distance_per_step = 55.88mm/1711 = 0.03265926358854mm;
*/
//include
#include "Encoder_Polling_V2.h"
#include "Button.h"
#include <AccelStepper.h>
//constants
const int PIN_LIM0 = A2; // mot1 limit switch 0
const int PIN_LIM1 = A3; // mot1 limit switch 1
const int PIN_LIM2 = A4; // mot0 limit switch
const int PIN_ENC0_A = 2; // spool encoder
const int PIN_ENC0_B = 4;
const int PIN_ENC1_A = 3; // guide encoder
const int PIN_ENC1_B = 5;
const int PIN_MOT0_EN = 6; // spool motor (brushed)
//const int MOT1_SPR = 48;
const int PIN_MOT1_A = 8; // linear motor (stepper)
const int PIN_MOT1_B = 9;
const int PIN_MOT1_C = 10;
const int PIN_MOT1_D = 11;
//const int PIN_LED = 13;
const unsigned long TS1 = 50L;
const int HIGHN = 5000000;
const float DPS = 0.0326593f;
const int RUNSPD = 112;
//variables
Button LIM0_btn(PIN_LIM0, true, true, 20);
Button LIM1_btn(PIN_LIM1, true, true, 20);
Button LIM2_btn(PIN_LIM2, true, true, 20);
bool LIM0_met = false;
bool LIM1_met = false;
bool LIMx_met = false;
long LIM0_pos, LIM1_pos;
bool b_complete = false;
long pos0 = 0, pos1 = 0, opos0 = 0, opos1 = 0;
unsigned long TC_ms, TS, T, TCO1, TCO2, dTCO;
float vel0, vel1;
float X0, X1;
float wire_t = 0.1; // in mm
AccelStepper MOT1(AccelStepper::FULL4WIRE, PIN_MOT1_A, PIN_MOT1_B, PIN_MOT1_C, PIN_MOT1_D);
//methods
void setup() {
Serial.begin(115200);
Serial.println("START");
Serial.println("1");
pinMode(PIN_ENC0_A, INPUT);
pinMode(PIN_ENC0_B, INPUT);
pinMode(PIN_ENC1_A, INPUT);
pinMode(PIN_ENC1_B, INPUT);
digitalWrite(PIN_ENC0_A, HIGH);
digitalWrite(PIN_ENC0_B, HIGH);
digitalWrite(PIN_ENC1_A, HIGH);
digitalWrite(PIN_ENC1_B, HIGH);
encoder_begin();
attach_encoder(0, PIN_ENC0_A, PIN_ENC0_B);
attach_encoder(1, PIN_ENC1_A, PIN_ENC1_B);
pinMode(PIN_MOT0_EN, OUTPUT);
analogWrite(PIN_MOT0_EN, RUNSPD);
MOT1.setMaxSpeed(9);
MOT1.setAcceleration(9);
Serial.print("Enter the wire thickness: ");
while (Serial.available() == 0) {}
wire_t = Serial.parseFloat();
Serial.print(wire_t);
Serial.println(" mm of wire thickness.");
Serial.println("");
MOT1.moveTo(HIGHN);
}
void loop() {
int dir0, dir1;
T = 0;
TS = 0;
TC_ms = millis();
TCO1 = micros();
while (1) {
//interrupts();
MOT1.run();
dir0 = encoder_data(0);
dir1 = encoder_data(1);
if (dir0 != 0) {
pos0 += (long)dir0;
}
if (dir1 != 0) {
pos1 += (long)dir1;
}
if (T%5000==0) {
TCO2 = micros();
dTCO = TCO2 - TCO1;
vel0 = dTCO == 0 ? 0 : 1000000.0f*(pos0-opos0)/dTCO;
vel1 = dTCO == 0 ? 0 : 1000000.0f*(pos1-opos1)/dTCO;
/*
Serial.println("");
Serial.print(pos0);
Serial.print("<-ENC0_pos:ENC1_pos->");
Serial.print(pos1);
Serial.println(";");
Serial.print(vel0, 1);
Serial.print("<-ENC0_vel:ENC1_vel->");
Serial.print(vel1, 1);
Serial.println(";");
Serial.print(MOT1.currentPosition());
Serial.println("");
*/
opos0 = pos0;
opos1 = pos1;
TCO1 = micros();
}
LIM0_btn.read();
LIM1_btn.read();
LIM2_btn.read();
if (LIM2_btn.wasPressed()) {
Serial.print("LIM2 ");
Serial.print(pos0);
Serial.println(" : pos0 - enc0 - spool");
}
if (LIMx_met) {
if (!b_complete) makeCoil();
} else {
if (LIM0_btn.wasPressed()) {
Serial.println("LIM0");
LIM0();
}
if (LIM1_btn.wasPressed()) {
Serial.println("LIM1");
LIM1();
}
}
if (millis() >= TC_ms + TS1) {
TS++;
TC_ms = millis();
}
T++;
}
}
//sub-methods
void makeCoil() {
if (MOT1.currentPosition() > (LIM0_pos > LIM1_pos ? LIM0_pos : LIM1_pos)) {
analogWrite(PIN_MOT0_EN, 0);
MOT1.disableOutputs();
Serial.println("STOP");
b_complete = true;
return;
}
X0 = float(wire_t*pos0/32.0f);
X1 = float(MOT1.currentPosition()*DPS);
if (X0 < X1) {
MOT1.moveTo(MOT1.currentPosition()+1);
}
/*
if (T%5000 == 0) {
Serial.print(pos0);
Serial.print(" : pos0, ");
Serial.print(MOT1.currentPosition());
Serial.print(" : MOT1.c_pos, ");
Serial.print(X0);
Serial.print(" : X0, ");
Serial.print(X1);
Serial.println(" : X1");
}
*/
}
void LimSpan() {
unsigned long N = abs(LIM1_pos-LIM0_pos);
Serial.print(N);
Serial.println("");
LIMx_met = true;
MOT1.stop();
X0 = 0.0f;
X1 = 0.0f;
pos0 = 0L;
analogWrite(PIN_MOT0_EN, RUNSPD);
}
void LIM0() {
if (!LIM0_met) {
LIM0_met = true;
LIM0_pos = MOT1.currentPosition();
Serial.print(LIM0_pos);
Serial.println(" LIM0 position");
if (LIM1_met) {
LimSpan();
} else {
MOT1.moveTo(-HIGHN);
}
}
}
void LIM1() {
if (!LIM1_met) {
LIM1_met = true;
LIM1_pos = MOT1.currentPosition();
Serial.print(LIM1_pos);
Serial.println(" LIM1 position");
if (LIM0_met) {
LimSpan();
} else {
MOT1.moveTo(HIGHN);
}
}
}