This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
func.sh
executable file
·354 lines (329 loc) · 8.38 KB
/
func.sh
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/bash
MENDEL_DEV=/dev/arduino
#
# this file is designed to be sourced into your current shell like this:
#
# source ./func.sh
#
# and then used like this:
#
# $ mendel_cmd G1 X100
# $ mendel_cmd M250
#
# {X:4200,Y:0,Z:0,E:0,F:300,c:19334400}
# {X:4200,Y:0,Z:0,E:0,F:300,c:0}
# Q1/1E
# $ mendel_readsym_uint8 mb_head
# 1
# $ mendel_readsym_target startpoint
# X: 2100
# Y: 0
# Z: 0
# E: 0
# F: 300
# $ mendel_readsym_mb
# [0] {
# eX: 0 eY: 0 eZ: 0 eE: 0 eF: 0
# flags: 0
# dX: 0 dY: 0 dZ: 0 dE: 0
# cX: 0 cY: 0 cZ: 0 cE: 0
# ts: 0
# c: 0 ec: 0 n: 0
# }
# [HEAD,TAIL:1] {
# eX: 4200 eY: 0 eZ: 0 eE: 0 eF: 300
# flags: 120
# dX: 4200 dY: 0 dZ: 0 dE: 0
# cX: -2100 cY: -2100 cZ: -2100 cE: -2100
# ts: 4200
# c: 19334400 ec: 0 n: 0
# }
# [2] {
# eX: 0 eY: 0 eZ: 0 eE: 0 eF: 0
# flags: 0
# dX: 0 dY: 0 dZ: 0 dE: 0
# cX: 0 cY: 0 cZ: 0 cE: 0
# ts: 0
# c: 0 ec: 0 n: 0
# }
# [3] {
# eX: 0 eY: 0 eZ: 0 eE: 0 eF: 0
# flags: 0
# dX: 0 dY: 0 dZ: 0 dE: 0
# cX: 0 cY: 0 cZ: 0 cE: 0
# ts: 0
# c: 0 ec: 0 n: 0
# }
# [4] {
# eX: 0 eY: 0 eZ: 0 eE: 0 eF: 0
# flags: 0
# dX: 0 dY: 0 dZ: 0 dE: 0
# cX: 0 cY: 0 cZ: 0 cE: 0
# ts: 0
# c: 0 ec: 0 n: 0
# }
# [5] {
# eX: 0 eY: 0 eZ: 0 eE: 0 eF: 0
# flags: 0
# dX: 0 dY: 0 dZ: 0 dE: 0
# cX: 0 cY: 0 cZ: 0 cE: 0
# ts: 0
# c: 0 ec: 0 n: 0
# }
# [6] {
# eX: 0 eY: 0 eZ: 0 eE: 0 eF: 0
# flags: 0
# dX: 0 dY: 0 dZ: 0 dE: 0
# cX: 0 cY: 0 cZ: 0 cE: 0
# ts: 0
# c: 0 ec: 0 n: 0
# }
# [7] {
# eX: 0 eY: 0 eZ: 0 eE: 0 eF: 0
# flags: 0
# dX: 0 dY: 0 dZ: 0 dE: 0
# cX: 0 cY: 0 cZ: 0 cE: 0
# ts: 0
# c: 0 ec: 0 n: 0
# }
# Initialize serial port settings
mendel_setup() {
stty 115200 raw ignbrk -hup -echo ixoff < $MENDEL_DEV
}
# Reset the arduino by dripping DTR
mendel_reset() {
stty hup < $MENDEL_DEV
stty hup < $MENDEL_DEV
mendel_setup
}
#Connect, and type in commands and see the response yourself.
#Basically, a lightweight terminal implementation
mendel_talk() {
# If there is already a 'cat' process associated with this terminal,
# don't kill it.
if ps | grep 'cat$' >/dev/null; then
skip_kill_cat=1
fi
echo "press ctrl+D to exit"
( cat <&3 & cat >&3; kill $! ; ) 3<>$MENDEL_DEV
# You're supposed to use "^D" to exit. If somebody uses "^C" instead,
# it leaves the "cat" process connected between the terminal and $MENDEL_DEV
# detect this condition and kill that process.
if [ "$skip_kill_cat" == "" ]; then
kill `ps | grep 'cat$'| cut -d " " -f -1` 2>/dev/null
fi
}
# Send a command, printing the reply.
mendel_cmd() {
(
local IFS=$' \t\n'
local RSC=0
local cmd="$*"
echo "$cmd" >&3;
local REPLY=""
while ! [[ "$REPLY" =~ ^OK ]] && ! [[ "$REPLY" =~ ^ok ]]
do
read -u 3
echo "${REPLY##ok }"
if [[ "$REPLY" =~ ^RESEND ]] || [[ "$REPLY" =~ ^rs ]]
then
if [ "$RSC" -le 3 ]
then
echo "$cmd" >&3
RSC=$(( $RSC + 1 ))
else
REPLY="OK"
echo "Too many retries: aborting" >&2
fi
fi
done
) 3<>$MENDEL_DEV;
}
#Send a command, printing both the command and the reply, prefix so you can tell which is which.
mendel_cmd_hr() {
(
local IFS=$' \t\n'
local cmd="$*"
local RSC=0
echo "$cmd" >&3
echo "S> $cmd"
local REPLY=""
while ! [[ "$REPLY" =~ ^OK ]] && ! [[ "$REPLY" =~ ^ok ]]
do
read -u 3
echo "<R $REPLY"
if [[ "$REPLY" =~ ^RESEND ]] || [[ "$REPLY" =~ ^rs ]]
then
if [ "$RSC" -le 3 ]
then
echo "$cmd" >&3
echo "S> $cmd"
RSC=$(( $RSC + 1))
else
REPLY="OK"
echo "Too many retries: aborting" >&2
fi
fi
done
) 3<>$MENDEL_DEV;
}
# Print a gcode file. Echos commands and replies.
mendel_print() {
(
for F in "$@"
do
local IFS=$'\n'
for L in $(< $F)
do
mendel_cmd_hr "$L"
done
done
)
}
# Print a gcode file. Press a key after each line. Echos commands and replies.
mendel_print_interactive() {
(
for F in "$@"
do
local IFS=$'\n'
for L in $(< $F)
do
mendel_cmd_hr "$L"
read
done
done
)
}
# Use the debug interface to directly read memory.
# Usage:
# mendel_readsym 0x<address>(:<size>)
# mendel_readsym <name>
# determines address and size of "name" from mendel.sym
mendel_readsym() {
(
local IFS=$' \t\n'
local sym=$1
if [ -n "$sym" ]
then
if [[ "$sym" =~ ^(0?x?[0-9A-Fa-f]+)(:([0-9]+))?$ ]]
then
local ADDR=$(( ${BASH_REMATCH[1]} ))
local SIZE=$(( ${BASH_REMATCH[3]} ))
if [ "$SIZE" -le 1 ]
then
SIZE=1
fi
mendel_cmd "M253 S$ADDR P$SIZE"
else
make mendel.sym &>/dev/null
if egrep -q '\b'$sym'\b' mendel.sym
then
local ADDR=$(( $(egrep '\b'$sym'\b' mendel.sym | cut -d\ -f1) ))
local SIZE=$(egrep '\b'$sym'\b' mendel.sym | cut -d+ -f2)
mendel_cmd "M253 S$ADDR P$SIZE"
else
echo "unknown symbol: $sym"
fi
fi
else
echo "what symbol?" > /dev/fd/2
fi
)
}
mendel_readsym_uint8() {
local sym=$1
local val=$(mendel_readsym $sym)
perl -e 'printf "%u\n", hex "0x".$ARGV[0]' $val
}
mendel_readsym_int8() {
local sym=$1
local val=$(mendel_readsym $sym)
perl -e 'printf "%d\n", ((hex "0x".$ARGV[0]) & 0x7F) - (((hex "0x".$ARGV[0]) & 0x80)?0x80:0)' $val
}
mendel_readsym_uint16() {
local sym=$1
local val=$(mendel_readsym $sym)
perl -e '$ARGV[0] =~ m#(..)(..)# && printf "%u\n", hex "0x$2$1"' $val
}
mendel_readsym_int16() {
local sym=$1
local val=$(mendel_readsym $sym)
perl -e '$ARGV[0] =~ m#(..)(..)# && printf "%d\n", ((hex "0x$2$1") & 0x7FFF) - (((hex "0x$2$1") & 0x8000)?0x8000:0)' $val
}
mendel_readsym_uint32() {
local sym=$1
local val=$(mendel_readsym $sym)
perl -e '$ARGV[0] =~ m#(..)(..)(..)(..)# && printf "%u\n", hex "0x$4$3$2$1"' $val
}
mendel_readsym_int32() {
local sym=$1
local val=$(mendel_readsym $sym)
perl -e '$ARGV[0] =~ m#(..)(..)(..)(..)# && printf "%d\n", hex "0x$4$3$2$1"' $val
}
mendel_readsym_target() {
local sym=$1
local val=$(mendel_readsym "$sym")
if [ -n "$val" ]
then
perl -e '@a = qw/X Y Z E F/; $c = 0; while (length $ARGV[0]) { last unless $ARGV[0] =~ s#^(..)(..)(..)(..)##; printf "%s: %d\n", $a[$c], hex "0x$4$3$2$1"; $c++; }' "$val"
fi
}
mendel_readsym_mb() {
local val=$(mendel_readsym movebuffer)
local mbhead=$(mendel_readsym mb_head)
local mbtail=$(mendel_readsym mb_tail)
perl - <<'ENDPERL' -- $val $mbhead $mbtail
$i = -1;
@a = qw/eX 4 eY 4 eZ 4 eE 4 eF 4 flags 9 dX 12 dY 4 dZ 4 dE 4 cX 12 cY 4 cZ 4 cE 4 ts 12 c 12 rs 4 sn 4 cm 4 n 4 rs 1/;
$c = 0;
$c = 1234567;
while (length $ARGV[1]) {
if ($c > ($#a / 2)) {
$i++;
$c = 0;
printf "\n}\n"
if ($i > 0);
printf "[%s%d] {\n", (($i == $ARGV[2])?"HEAD":"").(($ARGV[2] == $ARGV[3] && $ARGV[2] == $i)?",":"").(($i == $ARGV[3])?"TAIL":"").(($i == $ARGV[2] || $i == $ARGV[3])?":":""), $i
}
if ($a[$c * 2 + 1] & 8) {
printf "\n";
}
if (($a[$c * 2 + 1] & 7) == 4) {
$ARGV[1] =~ s#^(..)(..)(..)(..)##;
printf "\t%s: %d", $a[$c * 2], hex "0x$4$3$2$1";
}
elsif (($a[$c * 2 + 1] & 7) == 1) {
$ARGV[1] =~ s#^(..)##;
printf "\t%s: %d", $a[$c * 2], hex "0x$1";
}
$c++;
}
printf "\n}\n";
ENDPERL
}
# Read status of PID routines.
mendel_heater_pid() {
local P=$(mendel_readsym_int16 heater_p)
local I=$(mendel_readsym_int16 heater_i)
local D=$(mendel_readsym_int16 heater_d)
local PF=$(mendel_readsym_int32 p_factor)
local IF=$(mendel_readsym_int32 i_factor)
local DF=$(mendel_readsym_int32 d_factor)
local O=$(mendel_readsym_uint8 0x27)
local T=$(mendel_cmd M105 | cut -d\ -f2 | cut -d/ -f1)
echo "P=$P pf=$PF r="$(($P * $PF))
echo "I=$I if=$IF r="$(($I * $IF))
echo "D=$D df=$DF r="$(($D * $DF))
echo "R="$(( $(($P * $PF)) + $(($I * $IF)) + $(($D * $DF)) )) / 1024
echo "R="$(( $(( $(($P * $PF)) + $(($I * $IF)) + $(($D * $DF)) )) / 1024 ))
echo "R="$(( $(( $(( $(($P * $PF)) + $(($I * $IF)) + $(($D * $DF)) )) / 1024 )) + 128 ))
echo "O=$O T=$T"
}
if [[ "$0" =~ ^mendel_(setup|reset|talk|cmd|readsym|heater_pid|print) ]]
then
eval "$0" "$@"
fi
if [[ "$1" =~ ^mendel_(setup|reset|talk|cmd|readsym|heater_pid|print) ]]
then
eval "$@"
fi