-- ****************initalize the pins to there states of listen or switching******
-- int the pins
gpio.mode(1, gpio.OUTPUT) --motor A power ON
gpio.mode(2, gpio.OUTPUT) -- motor B power ON
gpio.mode(3, gpio.OUTPUT) -- polarity for motor A
gpio.mode(4, gpio.OUTPUT) -- polarity for motor B
-- comment out as nessary below for fewer servos we are testing alot to see if we crash
gpio.mode(5, gpio.OUTPUT)
gpio.mode(6, gpio.OUTPUT)
gpio.mode(7, gpio.OUTPUT)
gpio.mode(8, gpio.OUTPUT)
--****************list of prams for calling wifi module and connect to a router***************
wifi.setmode(wifi.STATION)
wifi.setphymode(wifi.PHYMODE_G)
station_cfg={}
station_cfg.ssid="sex" --dont pass on a blank password like pss="" cause lua is !@#$
station_cfg.save=true
wifi.sta.config(station_cfg)
cfg={}
cfg.ip="192.168.42.165"
cfg.netmask="255.255.255.0"
cfg.gateway="192.168.42.1"
wifi.sta.setip(cfg)
wifi.sta.connect()
--**************************globalVARS****************************
port = 9876
cmd = ""
currentstep=0
wantedstep=0
pola=1
polb=1
s5=1500 -- micro seconds for each servo to be at center...standard stuff. nums are same as PINS
s6=1500
s7=1500
s8=1500
s0=1500
prefix=""
--*********************functions and events below*****************
--A timer event for servos and stepper
tmr.alarm(1, 2000, tmr.ALARM_AUTO, function() -- this is infact a call to a C module that has a callback.... SET TIMER EVENT
-- any C module call will be excuted after lua code, after we leave this code it will be exicuted in a event/task que same as the timer. no there is no way to know when it will process or fail.
-- print(s7) --debug
-- print(s8) --debug
gpio.serout(6,gpio.HIGH,{s6,100},1,function()
print("servo call 1") gpio.serout(7,gpio.HIGH,{s7,100},1,function()
print("servo call 2") gpio.serout(8,gpio.HIGH,{s8,100},1,function()
print("servo call 3") end) end)
end)
--****check if stepper needed to move
if(currentstep~=wantedstep) then -- was not done so eval the direction and then the steps
print(currentstep.." "..wantedstep)
if(currentstep>wantedstep) then
if((pola==1) and (polb==1)) then --WAS A=high B=high polarity pins OK???? we want to change the polarity
gpio.write(3,gpio.LOW)
gpio.write(4,gpio.HIGH)
pola=0
polb=1
elseif(pola<polb) then --WAS A=low B=high
gpio.write(3,gpio.LOW)
gpio.write(4,gpio.LOW)
pola=0
polb=0
elseif((pola==0) and (polb==0)) then --WAS A=low B=low
gpio.write(3,gpio.HIGH)
gpio.write(4,gpio.LOW)
pola=1
polb=0
elseif(pola>polb) then -- --WAS A=high B=low -- do the fourth step and incrament the counter fourth step
gpio.write(3,gpio.HIGH)
gpio.write(4,gpio.HIGH)
pola=1
polb=1
currentstep=currentstep-1 -- other direction
end
end
if(currentstep<wantedstep) then -- clockwise?
if((pola==1) and (polb==1)) then --WAS A=high B=high polarity pins OK???? we are want to change the polarity
gpio.write(3,gpio.HIGH)
gpio.write(4,gpio.LOW)
pola=1
polb=0
elseif(pola>polb) then --WAS A=low B=high
gpio.write(3,gpio.LOW)
gpio.write(4,gpio.LOW)
pola=0
polb=0
elseif((pola==0) and (polb==0)) then --WAS A=low B=low
gpio.write(3,gpio.LOW)
gpio.write(4,gpio.HIGH)
pola=0
polb=1
elseif(pola<polb) then --WAS A=high B=low -- do the fourth step and incrament the counter fourth step
gpio.write(3,gpio.HIGH)
gpio.write(4,gpio.HIGH)
pola=1
polb=1
currentstep=currentstep+1 -- other direction
end
end
else
--tmr.stop(1) -- nothing needed to be done so we turn off the timer uncomment timer start below if you want to stop telling the servos what to do as they ONLY need one pulse to move to the commanded position
print("the last ELSE for the move stepper check") --debuging
end -- the if AND the else share the same end for no $#@ reason
end) -- timver event end
--************************************
--**********************global functions*****************************************
function stringStarts(a,b)
return string.sub(a,1,string.len(b))==b -- if string wanted witch is b in this case return true if( string to check ,index1,index2(b's length)==b)
end
function stringEnds(a,b)
return b=='' or string.sub(a,-string.len(b))==b
end
function meta(a,b)
return b=='' or string.sub(a,-string.len(b))==b
end
--***proccesss comand with the above 3 functions and the below one
function exeCmd(st) -- example: "servo 1500"
prefix = string.sub(st,1,1) -- only do this once! we are wasting a varible but not rerunning code so..meh
if(prefix == "8") then s8=tonumber(string.sub(st,8,-1)) end
if(prefix == "7") then s7=tonumber(string.sub(st,8,-1)) end
if(prefix == "6") then s6=tonumber(string.sub(st,8,-1)) end
if(prefix == "5") then s5=tonumber(string.sub(st,8,-1)) end
if(prefix == "0") then s0=tonumber(string.sub(st,8,-1)) end
if(prefix == "9") then s9=tonumber(string.sub(st,8,-1)) end
if(prefix =="a") then wantedstep = tonumber(string.sub(st,8,-1)) end
if(prefix =="b") then end
if(prefix =="c") then end
end
--*******call back for a packet???
function receiveData(conn, data) -- not nessary to add local here in prams it's local only for prams automatic
cmd = cmd .. data -- connotate string this+that
local a, b = string.find(cmd, "\n", 1, true)
while a do
exeCmd( string.sub(cmd, 1, a-1) )
cmd = string.sub(cmd, a+1, string.len(cmd))
a, b = string.find(cmd, "\n", 1, true)
end
--tmr.start(1)
end
-- set up tcp, change later to upd some how
srv=net.createServer(net.TCP, 28800)
srv:listen(port,function(conn)
print("RoboRemo connected")
conn:send("dbg connected ok\n")
conn:on("receive",receiveData)
conn:on("disconnection",function(c)
print("RoboRemo disconnected")
end)
end)