Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
3Axis CNC
#include <Stepper.h>
const int stepsPerRevolution = 100; // change this to fit the number of steps per revolution
// for your motor
Stepper stepperX(stepsPerRevolution, 6,7,8,9);
Stepper stepperY(stepsPerRevolution, 2,3,4,5);
Stepper stepperZ(stepsPerRevolution, 10,11,12,13);
//------------------------------------------------------------------------------
// CONSTANTS
//------------------------------------------------------------------------------
//#define VERBOSE (1) // add to get a lot more serial output.
#define VERSION (2) // firmware version
#define BAUD (57600) // How fast is the Arduino talking?
#define MAX_BUF (640) // What is the longest message Arduino can store?
#define STEPS_PER_TURN (400) // depends on your stepper motor. most are 200.
#define MIN_STEP_DELAY (50)
#define MAX_FEEDRATE (1000000/MIN_STEP_DELAY)
#define MIN_FEEDRATE (0.01)
#define NUM_AXIES (4)
//------------------------------------------------------------------------------
// STRUCTS
//------------------------------------------------------------------------------
// for line()
typedef struct {
long delta;
long absdelta;
int dir;
long over;
} Axis;
//------------------------------------------------------------------------------
// GLOBALS
//------------------------------------------------------------------------------
// Initialize Adafruit stepper controller
// Connect stepper motors with 400 steps per revolution (1.8 degree)
// Create the motor shield object with the default I2C address
Axis a[4]; // for line()
Axis atemp; // for line()
char buffer[MAX_BUF]; // where we store the message until we get a ';'
int sofar; // how much is in the buffer
float px, py, pz; // location
// speeds
float fr=0; // human version
long step_delay; // machine version
// settings
char mode_abs=1; // absolute mode?
//------------------------------------------------------------------------------
// METHODS
//------------------------------------------------------------------------------
/**
* delay for the appropriate number of microseconds
* @input ms how many milliseconds to wait
*/
void pause(long ms) {
delay(ms/1000);
delayMicroseconds(ms%1000); // delayMicroseconds doesn't work for values > ~16k.
}
/**
* Set the feedrate (speed motors will move)
* @input nfr the new speed in steps/second
*/
void feedrate(float nfr) {
if(fr==nfr) return; // same as last time? quit now.
if(nfr>MAX_FEEDRATE || nfr<MIN_FEEDRATE) { // don't allow crazy feed rates
Serial.print(F("New feedrate must be greater than "));
Serial.print(MIN_FEEDRATE);
Serial.print(F("steps/s and less than "));
Serial.print(MAX_FEEDRATE);
Serial.println(F("steps/s."));
return;
}
step_delay = 1000000.0/nfr;
fr=nfr;
}
/**
* Set the logical position
* @input npx new position x
* @input npy new position y
* @input npz new position z
*/
void position(float npx,float npy,float npz) {
// here is a good place to add sanity tests
px=npx;
py=npy;
pz=npz;
}
/**
* Supports movement with both styles of Motor Shield
* @input newx the destination x position
* @input newy the destination y position
**/
void onestep(int motor,int direction) {
#ifdef VERBOSE
char *letter="XYZ";
Serial.print(letter[motor]);
#endif
//onestep(m[motor], direction>0?FORWARD:BACKWARD,SINGLE);
switch(motor)
{
case 0:
Serial.print('X');
stepperX.step(direction > 0 ? 1 : -1);
// stopX();
break;
case 1:
Serial.print('Y');
stepperY.step(direction > 0 ? 1 : -1);
// stopY();
break;
case 2:
Serial.print('Z');
stepperZ.step(direction > 0 ? 1 : -1);
break;
}
}
void release() {
int i;
for(i=0;i<2;++i) {
// m[i]->release();
}
}
/**
* Uses bresenham's line algorithm to move both motors
* @input newx the destination x position
* @input newy the destination y position
**/
void line(float newx,float newy,float newz) {
a[0].delta = newx-px;
a[1].delta = newy-py;
a[2].delta = newz-pz;
long i,j,maxsteps=0;
for(i=0; i < NUM_AXIES; ++i) {
a[i].absdelta = abs(a[i].delta);
a[i].dir = a[i].delta > 0 ? 1:-1;
if( maxsteps < a[i].absdelta ) maxsteps = a[i].absdelta;
a[i].over=0;
}
#ifdef VERBOSE
Serial.println(F("Start >"));
#endif
for( i=0; i<maxsteps; ++i ) {
for(j=0; j < NUM_AXIES; ++j) {
a[j].over += a[j].absdelta;
if(a[j].over >= maxsteps) {
a[j].over -= maxsteps;
onestep(j,a[j].dir);
}
}
pause(step_delay);
}
#ifdef VERBOSE
Serial.println(F("< Done."));
#endif
position(newx,newy,newz);
stopX();
stopY();
stopZ();
}
/**
* Look for character /code/ in the buffer and read the float that immediately follows it.
* @return the value found. If nothing is found, /val/ is returned.
* @input code the character to look for.
* @input val the return value if /code/ is not found.
**/
float parsenumber(char code,float val) {
char *ptr=buffer;
while(ptr && *ptr && ptr<buffer+sofar) {
if(*ptr==code) {
return atof(ptr+1);
}
ptr=strchr(ptr,' ')+1;
}
return val;
}
/**
* write a string followed by a float to the serial line. Convenient for debugging.
* @input code the string.
* @input val the float.
*/
void output(char *code,float val) {
Serial.print(code);
Serial.println(val);
}
/**
* print the current position, feedrate, and absolute mode.
*/
void where() {
output("X",px);
output("Y",py);
output("Z",pz);
output("F",fr);
Serial.println(mode_abs?"ABS":"REL");
}
/**
* display helpful information
*/
void help() {
Serial.print(F("GcodeCNCDemo4AxisV2 "));
Serial.println(VERSION);
Serial.println(F("Commands:"));
Serial.println(F("G00/G01 [X(steps)] [Y(steps)] [Z(steps)] [F(feedrate)]; - linear move"));
Serial.println(F("G04 P[seconds]; - delay"));
Serial.println(F("G90; - absolute mode"));
Serial.println(F("G91; - relative mode"));
Serial.println(F("G92 [X(steps)] [Y(steps)] [Z(steps)]; - change logical position"));
Serial.println(F("M18; - disable motors"));
Serial.println(F("M100; - this help message"));
Serial.println(F("M114; - report position and feedrate"));
}
/**
* Read the input buffer and find any recognized commands. One G or M command per line.
*/
void processCommand() {
int cmd = parsenumber('G',-1);
switch(cmd) {
case 0: // move linear
case 1: // move linear
feedrate(parsenumber('F',fr));
line( parsenumber('X',(mode_abs?px:0)) + (mode_abs?0:px),
parsenumber('Y',(mode_abs?py:0)) + (mode_abs?0:py),
parsenumber('Z',(mode_abs?pz:0)) + (mode_abs?0:pz));
break;
case 4: pause(parsenumber('P',0)*1000); break; // dwell
case 90: mode_abs=1; break; // absolute mode
case 91: mode_abs=0; break; // relative mode
case 92: // set logical position
position( parsenumber('X',0),
parsenumber('Y',0),
parsenumber('Z',0));
break;
default: break;
}
cmd = parsenumber('M',-1);
switch(cmd) {
case 18: // disable motors
// release();
break;
case 100: help(); break;
case 114: where(); break;
default: break;
}
}
/**
* prepares the input buffer to receive a new message and tells the serial connected device it is ready for more.
*/
void ready() {
sofar=0; // clear input buffer
Serial.print(F(">")); // signal ready to receive input
}
/**
* First thing this machine does on startup. Runs only once.
*/
void setup() {
Serial.begin(BAUD); // open coms
stepperX.setSpeed(60);
stepperY.setSpeed(60);
stepperZ.setSpeed(60);
help(); // say hello
position(0,0,0); // set staring position
feedrate(200); // set default speed
ready();
}
void stopX()
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
void stopY()
{
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void stopZ()
{
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
/**
* After setup() this machine will repeat loop() forever.
*/
void loop() {
// listen for serial commands
while(Serial.available() > 0) { // if something is available
char c=Serial.read(); // get it
Serial.print(c); // repeat it back so I know you got the message
if(sofar<MAX_BUF) buffer[sofar++]=c; // store it
if(buffer[sofar-1]==';') break; // entire message received
}
if(sofar>0 && buffer[sofar-1]==';') {
// we got a message and it ends with a semicolon
buffer[sofar]=0; // end the buffer so string functions work right
Serial.print(F("\r\n")); // echo a return character for humans
processCommand(); // do something with the command
ready();
}
}
/**
* This file is based of GcodeCNCDemo.
*
* GcodeCNCDemo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GcodeCNCDemo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/

Arduino: 1.6.11 (Windows 7)

sketch_oct25c:198: error: 'stopX' was not declared in this scope

sketch_oct25c:199: error: 'stopY' was not declared in this scope

sketch_oct25c:200: error: 'stopZ' was not declared in this scope

exit status 1
'stopX' was not declared in this scope

how to show the simulation of such types of project in proteus software?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment