-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsimple_stream.m
143 lines (100 loc) · 2.9 KB
/
simple_stream.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
% this is a simple gcode sender for matlab
% Samuel Freitas 5/31/2022
warning('off','serialport:serialport:ReadlineWarning');
clear all
close all force hidden
GRBL_com_port = "COM3";
gcode_path = "grbl_test.gcode";
BAUD_RATE = 115200;
gcode = importdata(gcode_path);
gcode_string = string(gcode);
ser = serialport(GRBL_com_port,BAUD_RATE);
ser.Timeout = 0.1;
send_wake_up(ser)
for i = 1:length(gcode_string)
this_string = gcode_string(i);
cleaned_line = char(remove_eol_chars(remove_comment(this_string)));
if ~(isempty(cleaned_line))
disp(string(cleaned_line))
writeline(ser,cleaned_line);
wait_for_movement_completion(ser,cleaned_line);
end
end
function out = remove_comment(this_string)
comment_idx = find(char(this_string) == ';', 1);
if isempty(comment_idx)
out = this_string;
else
temp = char(this_string);
out = string(temp(1:comment_idx-1));
end
end
function send_wake_up(ser)
% # Wake up
% # Hit enter a few times to wake the Printrbot
writeline(ser, char("\r\n\r\n"))
pause(2) % Wait for Printrbot to initialize
flush(ser,"input") % Flush startup text in serial input
end
function out = encode_string(in_string)
% found that you just need to send chars to the GRBL controller as a line
out = double(char(in_string));
end
function out = decode_unicode(in_unicode)
out = string(char(in_unicode));
end
function out = remove_eol_chars(in_string)
out = strip(in_string);
end
function wait_for_movement_completion(ser,cleaned_line)
pause(1)
if ~isequal(cleaned_line,'$X') && ~isequal(cleaned_line,'$$')
idle_counter = 0;
while 1
% # Event().wait(0.01)
flush(ser,"input")
command = char(['?\n']);
writeline(ser,command)
grbl_out = readline(ser);
grbl_response = decode_unicode(strip(grbl_out));
if ~isequal(grbl_response,'ok')
if contains(grbl_response,'Idle')
idle_counter = idle_counter + 1;
end
end
if idle_counter > 10
break
end
pause(0.2) % in the GRBL documentation it reccomends a 5Hz rate for the '?' command
end
end
end
function send_grbl_command(ser,command)
this_string = command;
cleaned_line = char(remove_eol_chars(remove_comment(this_string)));
if ~(isempty(cleaned_line))
writeline(ser,cleaned_line);
wait_for_movement_completion(ser,cleaned_line);
disp(this_string);
end
end
function read_grbl_output(ser)
out = readline(ser);
for i = 1:100
out = readline(ser);
if isempty(out)
break
else
disp(out)
end
end
end
function send_grbl_command_read_output(ser,command)
send_grbl_command(ser,command)
read_grbl_output(ser)
end
function ser = easy_grbl_setup(GRBL_com_port,BAUD_RATE)
ser = serialport(GRBL_com_port,BAUD_RATE);
ser.Timeout = 0.1;
send_wake_up(ser)
end