project:cncduino
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
project:cncduino [2012/01/12 06:06] – unipolar/bipolar harvie | project:cncduino [2016/11/25 07:16] – ↷ Links adapted because of a move operation ruza | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== *duino CNC ====== | ||
+ | {{template> | ||
+ | name=duino stepper driver| | ||
+ | image=EDITME| | ||
+ | sw=-| | ||
+ | hw=-| | ||
+ | founder=[[user: | ||
+ | interested=[[user: | ||
+ | status=active | ||
+ | }} | ||
+ | |||
+ | In lots of brmlab projects ([[project: | ||
+ | |||
+ | This will include power transistors to drive motors and maybe some optocoders or at least zero-position sensors. | ||
+ | |||
+ | ===== Parts ===== | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | |||
+ | ===== Other thoughts ===== | ||
+ | ==== G-Code ==== | ||
+ | * Firmware should use standart G-Code protocol! [[wp> | ||
+ | * Maybe we can just use some already writter universal state-of-the-art firmware for CNC like GRBL: | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | ==== Unipolar+Bipolar driver hybrid ==== | ||
+ | Board could support both unipolar and bipolar steppers. just insert L293D instead of ULN and change jumper settings or something. We can also provide two IC sockets (one for ULN and second for L293D), then you will insert only one of those ICs to select if you want to drive unipolar or bipolar steppers | ||
+ | |||
+ | Or maybe we can just use only L293D (or some high-power alternative), | ||
+ | ==== Driving steper using ULN with only two pins connected to UC ==== | ||
+ | According to http:// | ||
+ | {{http:// | ||
+ | I guess you can use something similar with L293D IC (if you don't need active braking). | ||
+ | |||
+ | ===== Code ===== | ||
+ | <file c cncdu.ino> | ||
+ | #include < | ||
+ | |||
+ | char InCmd[32]; | ||
+ | byte InCmdIndex = 0; | ||
+ | byte InByte = 0; | ||
+ | unsigned long lastTime = 0; | ||
+ | int LaserPIN = 14; | ||
+ | int Ypojezd = 19; | ||
+ | int Xpojezd = 18; | ||
+ | int FDir = 17; | ||
+ | int FStep = 16; | ||
+ | |||
+ | unsigned posX = 0; | ||
+ | unsigned posY = 0; | ||
+ | |||
+ | #define STEPS 200 | ||
+ | Stepper stepperX(STEPS, | ||
+ | Stepper stepperY(STEPS, | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | stepperX.setSpeed(10); | ||
+ | stepperY.setSpeed(10); | ||
+ | |||
+ | pinMode(LaserPIN, | ||
+ | digitalWrite(LaserPIN, | ||
+ | pinMode(Ypojezd, | ||
+ | digitalWrite(Ypojezd, | ||
+ | pinMode(Xpojezd, | ||
+ | digitalWrite(Xpojezd, | ||
+ | |||
+ | pinMode(FDir, | ||
+ | digitalWrite(FDir, | ||
+ | pinMode(FStep, | ||
+ | digitalWrite(FStep, | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | while(Serial.available()) { | ||
+ | lastTime = millis(); | ||
+ | InByte = Serial.read(); | ||
+ | if (InCmdIndex >= sizeof(InCmd)){ | ||
+ | InCmdIndex = 0; | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | if (InByte == 10 || InByte == 13 ) { | ||
+ | // | ||
+ | ExeCmd(InCmd); | ||
+ | | ||
+ | for (InCmdIndex++; | ||
+ | | ||
+ | } | ||
+ | |||
+ | InCmdIndex = 0; | ||
+ | |||
+ | } | ||
+ | else{ | ||
+ | InCmd[InCmdIndex++] = InByte; | ||
+ | }// | ||
+ | } | ||
+ | |||
+ | |||
+ | if ( (lastTime + 15000) < millis() ) { | ||
+ | digitalWrite(LaserPIN, | ||
+ | digitalWrite(2, | ||
+ | digitalWrite(3, | ||
+ | digitalWrite(4, | ||
+ | digitalWrite(5, | ||
+ | digitalWrite(6, | ||
+ | digitalWrite(7, | ||
+ | digitalWrite(8, | ||
+ | digitalWrite(9, | ||
+ | digitalWrite(10, | ||
+ | digitalWrite(11, | ||
+ | digitalWrite(12, | ||
+ | digitalWrite(13, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void ExeCmd(char *InCmd){ | ||
+ | byte MotorIndex; | ||
+ | byte MotorSpeed; | ||
+ | int MotorSteps; | ||
+ | byte SeqID; | ||
+ | byte LaserPower; | ||
+ | byte SCommand; | ||
+ | |||
+ | SCommand = 0; | ||
+ | LaserPower = 0; | ||
+ | SeqID = 0; | ||
+ | MotorSteps = 0; | ||
+ | MotorSpeed = 0; | ||
+ | MotorIndex = 0; | ||
+ | int toX = 0; | ||
+ | int toY = 0; | ||
+ | |||
+ | // for (int i = 0; | ||
+ | // | ||
+ | // } | ||
+ | // | ||
+ | // | ||
+ | switch (InCmd[0]){ | ||
+ | |||
+ | case ' | ||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | MotorIndex = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | MotorSpeed = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > -10001 && atoi(InCmd) < 10001 ){ | ||
+ | MotorSteps = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | SeqID = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | switch (MotorIndex){ | ||
+ | |||
+ | case 1: | ||
+ | stepperX.setSpeed(MotorSpeed); | ||
+ | stepperX.step(MotorSteps); | ||
+ | posX += MotorSteps; | ||
+ | break; | ||
+ | |||
+ | case 2: | ||
+ | stepperY.setSpeed(MotorSpeed); | ||
+ | stepperY.step(MotorSteps * 3); | ||
+ | posY += MotorSteps; | ||
+ | break; | ||
+ | |||
+ | |||
+ | } | ||
+ | Serial.println(SeqID, | ||
+ | break; | ||
+ | |||
+ | case ' | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) >= 0 && atoi(InCmd) < 255){ | ||
+ | LaserPower = atoi(InCmd); | ||
+ | // | ||
+ | if (LaserPower == 254) { | ||
+ | digitalWrite(LaserPIN, | ||
+ | } | ||
+ | else{ | ||
+ | digitalWrite(LaserPIN, | ||
+ | } | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | SeqID = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | Serial.println(SeqID, | ||
+ | break; | ||
+ | |||
+ | case ' | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | SCommand=(atoi(InCmd)); | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | SeqID = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | switch (SCommand){ | ||
+ | case 1: | ||
+ | XYReset(); | ||
+ | Serial.println(SeqID, | ||
+ | break; | ||
+ | default: | ||
+ | Serial.print(" | ||
+ | Serial.println(SCommand, | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | break; | ||
+ | |||
+ | case ' | ||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | MotorSpeed = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) >= 0 && atoi(InCmd) < 10001 ){ | ||
+ | toX = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) >= 0 && atoi(InCmd) < 10001 ){ | ||
+ | toY = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | while (*InCmd != ' ') InCmd++; | ||
+ | while (isspace(*InCmd)) InCmd++; | ||
+ | if (atoi(InCmd) > 0 && atoi(InCmd) < 255){ | ||
+ | SeqID = atoi(InCmd); | ||
+ | // | ||
+ | } | ||
+ | else{ | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | stepperX.setSpeed(MotorSpeed); | ||
+ | stepperY.setSpeed(MotorSpeed); | ||
+ | line(posX, posY, toX, toY); | ||
+ | Serial.println(SeqID, | ||
+ | break; | ||
+ | |||
+ | |||
+ | case ' | ||
+ | PrintHelp(); | ||
+ | break; | ||
+ | |||
+ | Serial.println(SeqID, | ||
+ | break; | ||
+ | default: | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void linej(int x1, int y1, int x2, int y2) | ||
+ | { | ||
+ | int dx, dy, inx, iny, e, stepX, stepY; | ||
+ | |||
+ | while(posX != x2 && posY != y2) { | ||
+ | dx = x2 - x1; | ||
+ | dy = y2 - y1; | ||
+ | |||
+ | inx = dx > 0 ? 1 : -1; | ||
+ | iny = dy > 0 ? 1 : -1; | ||
+ | |||
+ | // | ||
+ | posX += inx; | ||
+ | // if(dx*(posY-y1)-(posX-x1)*dy*(dy*dx)> | ||
+ | if((float)posY/ | ||
+ | // | ||
+ | posX += inx; | ||
+ | } | ||
+ | else { | ||
+ | // | ||
+ | posY += iny; | ||
+ | } | ||
+ | Serial.print(posX, | ||
+ | Serial.print(" | ||
+ | Serial.println(posY, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void line(int x1, int y1, int x2, int y2) | ||
+ | { | ||
+ | int dx, dy, inx, iny, e, stepX, stepY; | ||
+ | |||
+ | dx = x2 - x1; | ||
+ | dy = y2 - y1; | ||
+ | inx = dx > 0 ? 1 : -1; | ||
+ | iny = dy > 0 ? 1 : -1; | ||
+ | stepX = 0; | ||
+ | stepY = 0; | ||
+ | | ||
+ | dx = abs(dx); | ||
+ | dy = abs(dy); | ||
+ | |||
+ | if(dx >= dy) { | ||
+ | dy <<= 1; | ||
+ | e = dy - dx; | ||
+ | dx <<= 1; | ||
+ | while (x1 != x2) { | ||
+ | // | ||
+ | if(e >= 0) { | ||
+ | y1 += iny; | ||
+ | stepperY.step(iny*3); | ||
+ | e-= dx; | ||
+ | } | ||
+ | e += dy; | ||
+ | x1 += inx; | ||
+ | stepperX.step(inx); | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | dx <<= 1; | ||
+ | e = dx - dy; | ||
+ | dy <<= 1; | ||
+ | while (y1 != y2) { | ||
+ | // | ||
+ | if(e >= 0) { | ||
+ | x1 += inx; | ||
+ | stepperX.step(inx); | ||
+ | e -= dy; | ||
+ | } | ||
+ | e += dx; | ||
+ | y1 += iny; | ||
+ | stepperY.step(iny*3); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | |||
+ | posX = x1; | ||
+ | posY = y1; | ||
+ | } | ||
+ | |||
+ | void XYReset(){ | ||
+ | posX = 0; | ||
+ | posY = 0; | ||
+ | stepperY.setSpeed(75); | ||
+ | while (digitalRead(Ypojezd)==true){ | ||
+ | stepperY.step(-1); | ||
+ | } | ||
+ | stepperY.setSpeed(150); | ||
+ | stepperY.step(1600); | ||
+ | |||
+ | stepperX.setSpeed(75); | ||
+ | while (digitalRead(Xpojezd)==true){ | ||
+ | stepperX.step(-1); | ||
+ | } | ||
+ | stepperX.setSpeed(150); | ||
+ | stepperX.step(600); | ||
+ | |||
+ | } | ||
+ | |||
+ | void PrintHelp(){ | ||
+ | /* Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(); | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(); | ||
+ | Serial.println(" | ||
+ | |||
+ | } | ||
+ | </ |
project/cncduino.txt · Last modified: 2016/11/25 08:14 by ruza