Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Gorzoid

Drone Follow Script

Recommended Posts

Changed it up, it now uses navigation upgrade instead of motion sensor so you can now walk anywhere and drone will follow as long as you are close enough to both it and a labled waypoint. Sadly though it is alot laggier because of the delay in navigation.findWaypoints() I could implement code to use it less frequently by translating positions of waypoints after moving but for now this is fine.

Code:

 

EEPROM:

--Follow Script EEPROM

local PORT_FOLLOW = 0xbf01
local MAX_DIST = 100

local drone = component.proxy(component.list("drone")())
local modem = component.proxy(component.list("modem")())
local nav = component.proxy(component.list("navigation")())

modem.open(PORT_FOLLOW)

local function findClient()
	while true do
		local evt,_,sender,port,dist,msg = computer.pullSignal()
		if evt == "modem_message" and port == PORT_FOLLOW and dist < MAX_DIST and msg == "FOLLOW_REQUEST_LINK" then
			drone.setStatusText("Linked: "..sender:sub(1,3))
			modem.send(sender,port,"FOLLOW_LINK")
			return sender
		end
	end
end

local function getNearbyNodes(justLabels)
	local waypoints = nav.findWaypoints(MAX_DIST)
	local nodes = {}
	for i = 1,waypoints.n do
		if justLabels then
			nodes[i] = waypoints[i].label
		else
			local wp = waypoints[i]
			nodes[wp.label] = wp.position
		end
	end
	return nodes
end

local client,nodes,noResponse = findClient(),getNearbyNodes()
local timeout = computer.uptime() + 10

while true do
	local evt,_,sender,port,dist,msg,label,ox,oy,oz = computer.pullSignal(timeout - computer.uptime())
	if moving and drone.getOffset() < 0.5 then
		moving = false
		nodes = getNearbyNodes()
	end
	if not evt then
		if noResponse then
			return
		end
		noResponse = true;
		modem.send(client,PORT_FOLLOW,"HEARTBEAT_REQUEST")
		timeout = timeout + 1
	elseif evt == "modem_message" and sender == client and port == PORT_FOLLOW and dist < MAX_DIST then
		if msg == "HEARTBEAT_RESPONSE" then
			noResponse = false
		elseif msg == "HEARTBEAT_REQUEST" then
			modem.send(sender,port,"HEARTBEAT_RESPONSE")
		elseif msg == "POS_UPDATE" and not moving then
			local node = nodes[label]
			if not node then
				modem.send(sender,port,"UPDATE_NODES",label,table.unpack(getNearbyNodes(true)))
			else
				drone.move(node[1] - ox, node[2] - oy, node[3] - oz)
				moving = true
				modem.send(sender,port,"OK")
			end
		end
		timeout = computer.uptime() + 10
	end
end

Client

--Follow Script Client

local PORT_FOLLOW = 0xbf01

local component = require("component")
local event = require("event")
local os = require("os")

local modem = component.modem
local nav = component.navigation

modem.open(PORT_FOLLOW)

local function findDrone()
	modem.broadcast(PORT_FOLLOW,"FOLLOW_REQUEST_LINK")
	local _,_,sender,port,_,msg = event.pull("modem_message",nil,nil,PORT_FOLLOW,nil,"FOLLOW_LINK")
	return sender
end

local drone = findDrone()

local function heartbeatHook(_,_,sender,port,_,msg)
	if sender == drone and port == PORT_FOLLOW and msg == "HEARTBEAT_REQUEST" then
		modem.send(sender,port,"HEARTBEAT_RESPONSE")
	end
end


event.listen("modem_message",heartbeatHook)

modem.send(drone,PORT_FOLLOW,"POS_UPDATE")
local nodes = {select(7,event.pull("modem_message",nil,drone,PORT_FOLLOW,nil,"UPDATE_NODES"))}

local function getNode()
	local tbl = nav.findWaypoints(100)
	for i=1,tbl.n do
		local label = tbl[i].label
		for i2=1,#nodes do
			if label == nodes[i2] then
				return label,table.unpack(tbl[i].position)
			end
		end
	end
end

while true do
        local label,x,y,z = getNode()
        print(label,x,y,z)
	modem.send(drone,PORT_FOLLOW,"POS_UPDATE",label,x,y,z)
	local args = {select(6,event.pull("modem_message",nil,drone,PORT_FOLLOW,nil))}
	if table.remove(args,1) == "UPDATE_NODES" then
		nodes = args
	end
	os.sleep(0.1)
end

 

 

Instructions for install:

  1. Insert an empty eeprom into your computer
  2. edit /dev/eeprom
  3. Paste the eeprom code and save.
  4. Paste the client code into either /usr/ or your mounted disk drive if you are using floppy disk of openos.
  5. Create a drone with navigation upgrade and wireless network card and the eeprom you made. (If you already have drone put it in crafting grid with your eeprom)

Instructions for use:

  1. Start drone first
  2. Then open client
  3. Done. (You gotta use Ctrl+Alt+C to escape because I am lazy)

 

Tell me what you guys would prefer?

  • Motion sensors with limited range and portability but good accuracy and good speed.
  • Navigation getPosition with very good range, medium portability and good speed.
  • Navigation waypoints with good range, very high portability, and bad speed.
Link to post
Share on other sites

I am not a OpenComputers programmer i just like to play around with open computers and opencomputers programs so can you please tell me how i can get this working?

 Ehh this is quite old, not sure if it works, but I think you need to download the file at https://gist.github.com/fnuecke/6bcbd66910b946b54ec7 and use the flash command to flash that file onto an eeprom (you can see how on the drones tutorial here https://www.youtube.com/watch?v=W5JTRKdup7s) then download this and save to client.lua

make sure a wireless card is on the drone and the computer and u also need a motion sensor on the computer.

once you have done that you should be able to turn on the drone and then run the program, be sure to edit the client.lua to set the positions of the motion sensor and drone otherwise it will not move to u properly

Link to post
Share on other sites

@Gorzoid Is it possible for you, or me when I'm not feeling lazy, to modify this to use the Navigation Upgrade inside of a tablet? (If the Navigation Upgrade works in a tablet, I don't remember.)

I think I actually made a test version of what you are talking about, I never finished it but if I may start up MC and try make it again, it would work alot better

Link to post
Share on other sites

Gorzoid could you finish it for latest version because its very cool. When i use this old version i need to fix coords thing :

like it was -231drone

you have to make it : -231 drone.

and when i change it to sensor too the last coord it attached to function and when i do : -214 function

it gives me an error. please fix the program that would be awesome!!!

Link to post
Share on other sites

I have also gotten to the point where the tablet just says "NIL"- four times a line, each line all at once. The Drone says that it is "Linked: 3b" (possibly cutoff, I might change the code to just say "Lnk:") after client.lua runs, but the tablet used starts with 6c, so I am not sure what the drone is suggesting that it is linked to. I would like a drone to do something, but I can barely read the code. I understand what NIL implies, but as far as I can see it means that the tablet and the drone are not communicating. 

Using V1.6.2.7 of OpenComputers, not latest but should be more recent than than the code (or the updated code). For SkyFactory 3.0.15. Attaching a shot of NILs.

Any advice or help?

2017-11-30_19.30.43.png

 

Edit: Tried running from different devices, including a robot. The "Linked:" changes code, but still doesn't reflect the address of the computer/hard drive. Also still nothing but NILs. Also-also, confirmed that all of this is taking place on the map- should that even matter.

Edited by Ender of Games
Updated from yesterday, more clarification given on issues
Link to post
Share on other sites
On 12/1/2017 at 7:50 AM, Ender of Games said:

I have also gotten to the point where the tablet just says "NIL"- four times a line, each line all at once. The Drone says that it is "Linked: 3b" (possibly cutoff, I might change the code to just say "Lnk:") after client.lua runs, but the tablet used starts with 6c, so I am not sure what the drone is suggesting that it is linked to. I would like a drone to do something, but I can barely read the code. I understand what NIL implies, but as far as I can see it means that the tablet and the drone are not communicating. 

Using V1.6.2.7 of OpenComputers, not latest but should be more recent than than the code (or the updated code). For SkyFactory 3.0.15. Attaching a shot of NILs.

Any advice or help?

2017-11-30_19.30.43.png

 

Edit: Tried running from different devices, including a robot. The "Linked:" changes code, but still doesn't reflect the address of the computer/hard drive. Also still nothing but NILs. Also-also, confirmed that all of this is taking place on the map- should that even matter.


you need to set up waypoints with the name    i2

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.