Siirry takaisin
KYTKENTÄKAAVIOT
L293D
L293d veya L293b Motor Sürücüsü Kullananlar için Arduino Yaz?l?m?:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

#include
#include

#define LINE_BUFFER_LENGTH 512

const int kalem_kaldir = 130;
const int kalem_indir = 40;
const int kalem_servo_pin = 12;
const int adim_sayisi = 20;
const int hareket_hizi = 250;

Servo kalem_servo;

Stepper y_ekseni(adim_sayisi, 2,3,4,5);
Stepper x_ekseni(adim_sayisi, 6,7,8,9);

float x_adim_mm = 6.0;
float y_adim_mm = 6.0;

struct point {
float x;
float y;
float z;
};

struct point actuatorPos;

float StepInc = 1;
int StepDelay = 0;
int LineDelay = 50;
int penDelay = 50;
float Xmin = 0;
float Xmax = 40;
float Ymin = 0;
float Ymax = 40;
float Zmin = 0;
float Zmax = 1;

float Xpos = Xmin;
float Ypos = Ymin;
float Zpos = Zmax;
boolean verbose = false;


void setup() {
// Setup
Serial.begin( 9600 );
kalem_servo.attach(kalem_servo_pin);
kalem_servo.write(kalem_kaldir);
delay(200);
x_ekseni.setSpeed(hareket_hizi);
y_ekseni.setSpeed(hareket_hizi);
Serial.println("Çizim Yapan Mini CNC!");
Serial.print("X min ");
Serial.print(Xmin);
Serial.print(" den ");
Serial.print(Xmax);
Serial.println(" mm.");
Serial.print("Y min ");
Serial.print(Ymin);
Serial.print(" den ");
Serial.print(Ymax);
Serial.println(" mm.");
}

void loop()
{
delay(200);
char line[ LINE_BUFFER_LENGTH ];
char c;
int lineIndex;
bool lineIsComment, lineSemiColon;

lineIndex = 0;
lineSemiColon = false;
lineIsComment = false;

while (1) {

while ( Serial.available()>0 ) {
c = Serial.read();
if (( c == 'n') || (c == 'r') ) {
if ( lineIndex > 0 ) {
line[ lineIndex ] = '';
if (verbose) {
Serial.print( "Al?c? : ");
Serial.println( line );
}
processIncomingLine( line, lineIndex );
lineIndex = 0;
}
else {
// Empty or comment line. Skip block.
}
lineIsComment = false;
lineSemiColon = false;
Serial.println("ok");
}
else {
if ( (lineIsComment) || (lineSemiColon) ) {
if ( c == ')' ) lineIsComment = false;
}
else {
if ( c <= ' ' ) { } else if ( c == '/' ) { }
else if ( c == '(' ) { lineIsComment = true; }
else if ( c == ';' ) { lineSemiColon = true; }
else if ( lineIndex >= LINE_BUFFER_LENGTH-1 ) {
Serial.println( "Yazma Hatas?" );
lineIsComment = false;
lineSemiColon = false;
}
else if ( c >= 'a' && c <= 'z' ) {
line[ lineIndex++ ] = c-'a'+'A';
}
else {
line[ lineIndex++ ] = c;
}
}
}
}
}
}

void processIncomingLine( char* line, int charNB ) {
int currentIndex = 0;
char buffer[ 64 ];
struct point newPos;

newPos.x = 0.0;
newPos.y = 0.0;

while( currentIndex < charNB ) {
switch ( line[ currentIndex++ ] ) {
case 'U':
penUp();
break;
case 'D':
penDown();
break;
case 'G':
buffer[0] = line[ currentIndex++ ];
buffer[1] = '';

switch ( atoi( buffer ) ){
case 0:
case 1:
char* indexX = strchr( line+currentIndex, 'X' );
char* indexY = strchr( line+currentIndex, 'Y' );
if ( indexY <= 0 ) {
newPos.x = atof( indexX + 1);
newPos.y = actuatorPos.y;
}
else if ( indexX <= 0 ) { newPos.y = atof( indexY + 1); newPos.x = actuatorPos.x; } else { newPos.y = atof( indexY + 1); indexY = ''; newPos.x = atof( indexX + 1); } drawLine(newPos.x, newPos.y ); // Serial.println("ok"); actuatorPos.x = newPos.x; actuatorPos.y = newPos.y; break; } break; case 'M': buffer[0] = line[ currentIndex++ ]; buffer[1] = line[ currentIndex++ ]; buffer[2] = line[ currentIndex++ ]; buffer[3] = ''; switch ( atoi( buffer ) ){ case 300: { char* indexS = strchr( line+currentIndex, 'S' ); float Spos = atof( indexS + 1); if (Spos == 30) { penDown(); } if (Spos == 50) { penUp(); } break; } case 114: Serial.print( "Tam Pozisyon : X = " ); Serial.print( actuatorPos.x ); Serial.print( " - Y = " ); Serial.println( actuatorPos.y ); break; default: Serial.print( "Komut Tan?nmad? : M"); Serial.println( buffer ); } } } } void drawLine(float x1, float y1) { if (verbose) { Serial.print("fx1, fy1: "); Serial.print(x1); Serial.print(","); Serial.print(y1); Serial.println(""); } if (x1 >= Xmax) {
x1 = Xmax;
}
if (x1 <= Xmin) { x1 = Xmin; } if (y1 >= Ymax) {
y1 = Ymax;
}
if (y1 <= Ymin) {
y1 = Ymin;
}

if (verbose)
{
Serial.print("Xpos, Ypos: ");
Serial.print(Xpos);
Serial.print(",");
Serial.print(Ypos);
Serial.println("");
}

if (verbose)
{
Serial.print("x1, y1: ");
Serial.print(x1);
Serial.print(",");
Serial.print(y1);
Serial.println("");
}

x1 = (int)(x1*x_adim_mm);
y1 = (int)(y1*y_adim_mm);
float x0 = Xpos;
float y0 = Ypos;
long dx = abs(x1-x0);
long dy = abs(y1-y0);
int sx = x0 int sy = y0 dy) {
for (i=0; i=dx) {
over-=dx;
y_ekseni.step(sy);
}
delay(StepDelay);
}
}
else {
for (i=0; i=dy) {
over-=dy;
x_ekseni.step(sx);
}
delay(StepDelay);
}
}

if (verbose)
{
Serial.print("dx, dy:");
Serial.print(dx);
Serial.print(",");
Serial.print(dy);
Serial.println("");
}

if (verbose)
{
Serial.print("Going to (");
Serial.print(x0);
Serial.print(",");
Serial.print(y0);
Serial.println(")");
}

delay(LineDelay);

Xpos = x1;
Ypos = y1;
}


void penUp() {
kalem_servo.write(kalem_kaldir);
delay(LineDelay);
Zpos=Zmax;
if (verbose) {
Serial.println("Pen up!");
}
}
void penDown() {
kalem_servo.write(kalem_indir);
delay(LineDelay);
Zpos=Zmin;
if (verbose) {
Serial.println("Pen down.");
}
}
Adafruit Motor Shield Kullananlar ?çin Arduino Yaz?l?m? :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

Processing Yaz?l?m?:


import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import processing.serial.*;

Serial port = null;

// select and modify the appropriate line for your operating system
// leave as null to use interactive port (press 'p' in the program)
String portname = null;
//String portname = Serial.list()[0]; // Mac OS X
//String portname = "/dev/ttyUSB0"; // Linux
//String portname = "COM6"; // Windows

boolean streaming = false;
float speed = 0.001;
String[] gcode;
int i = 0;

void openSerialPort()
{
if (portname == null) return;
if (port != null) port.stop();

port = new Serial(this, portname, 9600);

port.bufferUntil('n');
}

void selectSerialPort()
{
String result = (String) JOptionPane.showInputDialog(frame,
"Select the serial port that corresponds to your Arduino board.",
"serial port seç",
JOptionPane.QUESTION_MESSAGE,
null,
Serial.list(),
0);

if (result != null) {
portname = result;
openSerialPort();
}
}

void setup()
{
size(500, 250);
openSerialPort();
}

void draw()
{
background(0);
fill(255);
int y = 24, dy = 12;
text("Menuler", 12, y); y += dy;
text("p: serial port seç", 12, y); y += dy;
text("1: h?z 0.001 inch", 12, y); y += dy;
text("2: h?z 0.010 inch", 12, y); y += dy;
text("3: h?z 0.100 inch", 12, y); y += dy;
text("Yön Tu?lar?: x-y Düzlemi", 12, y); y += dy;
text("Sayfa a?a??-yukar?: z eksen ", 12, y); y += dy;
text("$: grbl ayarlar", 12, y); y+= dy;
text("h: Ba?lang?ç Konumu", 12, y); y += dy;
text("0: Güncel Konum", 12, y); y += dy;
text("g: g-code dosya seç ", 12, y); y += dy;
text("x: Çizimi Durdur", 12, y); y += dy;
y = height - dy;
text("güncel h?z: " + speed + " ad?m", 12, y); y -= dy;
text("güncel serial port: " + portname, 12, y); y -= dy;
}

void keyPressed()
{
if (key == '1') speed = 0.001;
if (key == '2') speed = 0.01;
if (key == '3') speed = 0.1;

if (!streaming) {
if (keyCode == LEFT) port.write("G91nG20nG00 X-" + speed + " Y0.000 Z0.000n");
if (keyCode == RIGHT) port.write("G91nG20nG00 X" + speed + " Y0.000 Z0.000n");
if (keyCode == UP) port.write("G91nG20nG00 X0.000 Y" + speed + " Z0.000n");
if (keyCode == DOWN) port.write("G91nG20nG00 X0.000 Y-" + speed + " Z0.000n");
if (keyCode == KeyEvent.VK_PAGE_UP) port.write("G91nG20nG00 X0.000 Y0.000 Z" + speed + "n");
if (keyCode == KeyEvent.VK_PAGE_DOWN) port.write("G91nG20nG00 X0.000 Y0.000 Z-" + speed + "n");
if (key == 'h') port.write("G90nG20nG00 X0.000 Y0.000 Z0.000n");
if (key == 'v') port.write("$0=75n$1=74n$2=75n");
//if (key == 'v') port.write("$0=100n$1=74n$2=75n");
if (key == 's') port.write("$3=10n");
if (key == 'e') port.write("$16=1n");
if (key == 'd') port.write("$16=0n");
if (key == '0') openSerialPort();
if (key == 'p') selectSerialPort();
if (key == '$') port.write("$$n");
}

if (!streaming && key == 'g') {
gcode = null; i = 0;
File file = null;
println("Loading file...");
selectInput("Select a file to process:", "fileSelected", file);
}

if (key == 'x') streaming = false;
}

void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
gcode = loadStrings(selection.getAbsolutePath());
if (gcode == null) return;
streaming = true;
stream();
}
}

void stream()
{
if (!streaming) return;

while (true) {
if (i == gcode.length) {
streaming = false;
return;
}

if (gcode[i].trim().length() == 0) i++;
else break;
}

println(gcode[i]);
port.write(gcode[i]+ 'n');
i++;
}

void serialEvent(Serial p)
{
String s = p.readStringUntil('n');
println(s.trim());

if (s.trim().startsWith("ok")) stream();
if (s.trim().startsWith("error")) stream(); // XXX: really?
}
Tiedostoluettelo: