#!/bin/bash
# TinyG initialization after a successful reset to defaults ($defa=1)
# 2021 CKO, Embeon SDE

function cmd {
	echo $1
	echo $1 > /dev/ttyUSB0
	echo $1 >> tinyinit-out.cmd
	sleep 0.1
}

stty -F /dev/ttyUSB0 115200 crtscts
echo "; Initializing System"

cmd "\$ex=1"	# xonxoff
#cmd "\$ex=2"	#rtscts

cmd "\$sv=0"	#disable
#cmd "\$sv=2"	# status report verbosity = verbose

cmd "\$ej=0"
cmd "\$jv=0"
cmd "\$mt=864000"

echo /dev/ttyUSB0

sleep 1
echo "; Motor 1, X-Axis"

cmd "\$1sa=0.9"
cmd "\$1mi=8"
cmd "\$1tr=39.93"
cmd "\$1pm=2"
cmd "\$xvm=30000"
cmd "\$xfr=10000"
cmd "\$xtn=0"
cmd "\$xtm=1000"
cmd "\$xjm=5000"
cmd "\$xjh=5000"
cmd "\$xsn=3"
cmd "\$xsx=2"
cmd "\$xsv=3000"
cmd "\$xlv=500"
cmd "\$xlb=5"
cmd "\$xzb=5"

echo /dev/ttyUSB0

sleep 1
echo "; Motor 2, Y-Axis"

cmd "\$2sa=0.9"
cmd "\$2mi=8"
cmd "\$2tr=39.877"
cmd "\$2pm=2"
cmd "\$yvm=25000"
cmd "\$yfr=10000"
cmd "\$ytn=0"
cmd "\$ytm=1000"
cmd "\$yjm=5000"
cmd "\$yjh=5000"
cmd "\$ysn=3"
cmd "\$ysx=2"
cmd "\$ysv=3000"
cmd "\$ylv=500"
cmd "\$ylb=5"
cmd "\$yzb=5"

echo /dev/ttyUSB0

sleep 1
echo "; Motor 3, Z-Axis"

cmd "\$3sa=1.8"
cmd "\$3mi=8"
cmd "\$3tr=8"
cmd "\$3pm=2"
cmd "\$zvm=5000"
cmd "\$zfr=5000"
cmd "\$ztn=0"
cmd "\$ztm=100"
cmd "\$zjm=2000"
cmd "\$zjh=2000"
cmd "\$zsn=2"
cmd "\$zsx=3"
cmd "\$zsv=1000"
cmd "\$zlv=200"
cmd "\$zlb=1"
cmd "\$zzb=10"

echo /dev/ttyUSB0

sleep 1
echo "; Motor 4, A-Axis"

cmd "\$4ma=3"
cmd "\$4sa=0.9"
cmd "\$4tr=160"
cmd "\$4mi=8"
cmd "\$4po=0"
cmd "\$4pm=2"
cmd "\$aam=3"
cmd "\$avm=100000"
cmd "\$afr=100000"
cmd "\$atn=-1"
cmd "\$atm=-1"
cmd "\$ajm=57600"
cmd "\$ajh=57600"
cmd "\$ajd=0.05"
cmd "\$ara=57.2957795"
cmd "\$asn=0"
cmd "\$asx=0"
cmd "\$asv=21600"
cmd "\$alv=21600"
cmd "\$alb=5"
cmd "\$azb=2"

echo /dev/ttyUSB0

sleep 1
echo "; done."

