This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
BlockSmith's profile picture

Automining with custom navigation API issues

Started by BlockSmith, 20 May 2014 - 09:03 AM
BlockSmith #1
Posted 20 May 2014 - 11:03 AM
This mining program is intended to use my custom API to move the turtle from A to B and back if need be for refueling and banking.

When the turtle gets to it's target position the Target variables seem to lose all their values.

Solution:
your problem seems to be at one of 3 lines, you're assigning xTarget to be a nil value on either line 357, line 366, or line 432.

I shall continue to investigate and report back if I find anything more… hopefully the above will begin pointing you in the direction of your error though.

SIDE NOTE: i do suggest you make use of Vector API, it can help reduce some of your variables.


EDIT: okay so it looks like the problem is in goJob you're assigning xTarget to be the same value as xJob which doesn't exist


The automining program will not allow me to access the navigation.status or navigation.yCurrent variables, but still uses navigation functions fine.

Solution:
Added get and set methods for variables.

Once the turtle arrives at its destination whilst printing and updating its status it should begin to mine while continuing to update itself using functions from the custom API.


No errors, just improper placement.

http://pastebin.com/trYQRVm4

Here's my code for the mining program.

http://pastebin.com/wjUB0Ctc

Updated: links to code
Updated: Thanks to theoriginalbit on helping with my first issue. New issue.
Edited on 22 May 2014 - 06:28 AM
apemanzilla #2
Posted 20 May 2014 - 02:14 PM
The code is private?
Zudo #3
Posted 20 May 2014 - 05:11 PM
:D/> we can't read your script!
BlockSmith #4
Posted 20 May 2014 - 08:49 PM
Fixed the links. Thanks for getting back to me so quickly. :D/>
theoriginalbit #5
Posted 21 May 2014 - 10:14 AM
your problem seems to be at one of 3 lines, you're assigning xTarget to be a nil value on either line 357, line 366, or line 432.

I shall continue to investigate and report back if I find anything more… hopefully the above will begin pointing you in the direction of your error though.

SIDE NOTE: i do suggest you make use of Vector API, it can help reduce some of your variables.

EDIT: okay so it looks like the problem is in goJob you're assigning xTarget to be the same value as xJob which doesn't exist
Edited on 21 May 2014 - 08:18 AM
BlockSmith #6
Posted 21 May 2014 - 10:23 AM
Ah. That makes sense. I decided to switch from xTarget to xJob when I started turning this into an API. Thanks. I'll go dive in now.

Update: Error fixed! New issue though.

The automining program will not allow me to access the navigation.status or navigation.yCurrent variables, but still uses navigation functions fine.

Update: Error fixed!
Edited on 22 May 2014 - 06:29 AM
BlockSmith #7
Posted 25 May 2014 - 09:13 AM
So I decided to take theoriginalbit's advice and use vectors for my API. I also made some major changes to my mining program to make it far more efficient. However, when I try to move with this API the turtle will start up fine, go through all of my mining program's startups, get his home and the target, display his outputs, and bank. Once he goes into the goTo function in my API all of the target values are reset to 0 for some reason.

NEW ERROR: drill:96: attempt to index ? (a nil value)


--Code cut from drill program


line 95: function getCoords()
line 96:	current = new.vector(navigation.getCurrent())
line 97:	heading = navigation.getCurrentH()
line 98: end


--Code cut from navigation API


function setCurrent(x, y, z, head) --set the current vector and current heading = 0, 1, 2, 3 / S, W, N, E
current = vector.new(x, y, z)   --set the current vector
heading = head  --set the current heading = 0, 1, 2, 3 / S, W, N, E
end
function getCurrent()
return current  --get the current vector
end
function getCurrentH()
	return heading  --get the current heading
end


Here is the API

http://pastebin.com/V5TwuFeH – Updated with code comments and seems to work fine now. I was missing some conditionals and whatnot.


Here is the drill program

http://pastebin.com/h6NsSQMJ – Also updated with some code comments.
Edited on 29 May 2014 - 07:07 AM
theoriginalbit #8
Posted 25 May 2014 - 09:35 AM
okay firstly, you should have continued this in the other thread, we generally like it when you keep all questions related to the same code in the same thread.

okay so for the problem. in your goHome function you invoke setTarget, which is passed the return value of getHome here is problem 1, its returning Home, but your variable is declared home, or maybe hHome, I'm not too sure, you've got variables that are too similarly named so I can't completely extrapolate their purpose. anyway so this means it returns nil, where it should be returning a vector. now lets assume you fix that and it does return a vector, well you'll stumble across another problem, and that problem is that you're giving setTarget 2 vectors on line 261 but when you declare setTarget you specify that you want an x, y, z and h. meaning that x & y contain vectors, and z & h are nil.

EDIT: I've also noticed you have a lot of unused functions in this script!
Edited on 25 May 2014 - 07:35 AM
BlockSmith #9
Posted 25 May 2014 - 11:22 AM
Thanks for the response. I noticed getCoords was unused in the drill program, thanks for that. Now I'm having issue using it. Once that issue is resolved I think the miner and API should be done until I update the API for GPS
theoriginalbit #10
Posted 25 May 2014 - 11:24 AM
honestly I didn't even look in the drill program, I looked at the API first and discovered the problem there so didn't bother looking at drill. there's functions you declare and just don't use in the API.
BlockSmith #11
Posted 25 May 2014 - 11:25 AM
I don't use hardly any from the API in the API because I'll be calling them from other programs to use. Figured that'd be best for flexibility.
Edited on 25 May 2014 - 09:26 AM
Lyqyd #12
Posted 25 May 2014 - 12:11 PM
Threads merged.
BlockSmith #13
Posted 25 May 2014 - 08:49 PM
Appreciated. Anyone have any advice. I'm spinning my wheels at this point lol.
theoriginalbit #14
Posted 26 May 2014 - 12:12 AM
NEW ERROR: drill:96: attempt to index ? (a nil value)

line 96:	current = new.vector(navigation.getCurrent())


current = new.vector(navigation.getCurrent())


new.vector(navigation.getCurrent())


new.vector()


new.vector

Still not seeing the problem?

vector.new
Edited on 25 May 2014 - 10:12 PM
BlockSmith #15
Posted 26 May 2014 - 05:06 AM
xD. It's always something so small and obvious… Just goes to show that sometimes, you should just take a break.
Edited on 26 May 2014 - 03:07 AM
BlockSmith #16
Posted 29 May 2014 - 07:35 AM
I'm almost there! I've added in gps functionality and am trying to transmit jobs via rednet. For some reason I'm losing all my values…again.

This is the section of code I'm using to send the data I get from the user. I've tested all values until this function is called and they retain their values.


function setTarget(t, x, y, z, head)
    rednet.close("right")
    rednet.open("right")
   
    job = {x, y, z, head}
    print("xTarget = "..job[1])
    sleep(10)
    sJob = textutils.serialize(job)
    rednet.send(t, sJob)
   
    rednet.close("right")
   
    CtargetX = x
    CtargetY = y
    CtargetZ = z
   
    getNextJob(x, z)
end

This is the code I use to receive the values sent above


function BOSSsetTarget()
    rednet.close("right")
    rednet.open("right")
    while boss == nil do
	    rednet.broadcast("Need Work")
	    term.clear()
	    term.setCursorPos(1, 1)
	    print("Need Work")
	    boss, tLoc, dist = rednet.receive(10)
	    if boss ~= nil then
		    print("Orders Received...")
		    sendCurrent()
		    print("Sending Position...")
	    end
    end
    location = textutils.unserialize(tLoc)
    targetX = location[1]
    targetY = location[2]
    targetZ = location[3]
    targetHeading = location[4]
    rednet.close("right")
end

nil values for all items.

Please help.
BlockSmith #17
Posted 29 May 2014 - 07:10 PM
Anyone…Ideas?
Lyqyd #18
Posted 29 May 2014 - 07:47 PM
Get rid of the sleep(10) in the sending code.
BlockSmith #19
Posted 03 June 2014 - 09:13 AM
Losing another value somehow…


function goY(nY)
	term.clear()
	term.setCursorPos(1, 1)
	print(yCurrent) -- This prints 63, the current Y position and is accurate.
	if yCurrent > nY then -- This throws an "attempt to compare __lt on nil and number." error
		while yCurrent ~= nY do
			down()
		end
	elseif yCurrent < nY then  
		term.clear()
		term.setCursorPos(1, 1)
		print(yCurrent)
		while yCurrent ~= nY do
			up()
		end
	end
end


Additional information…

This error does not occur on the first iteration.
The code works fine if I tell it to start at a y value greater than it's home y value.



function down()
	refuel()
	while turtle.detectDown() and mine do
		turtle.digDown()
		movement = "Digging Down"
		sendPosition()
		display()
	end
	while not turtle.detectDown() and not turtle.down() do
		movement = "Attacking Down"
		sendPosition()
		display()
		turtle.attackDown()
	end
	movement = "Down"
	yCurrent = yCurrent - 1
	sendPosition()
	display()
end

The error also only gets thrown if I'm using a GPS setup to get the original coordinates.


function GPSsetCurrent()
	print("Getting GPS coordinates")
	isOn()  --Forces modem on if it's off
	local x, y, z = gps.locate()
	isOff()  --Forces modem off if it's on
	setCurrent(x, y, z)  --Sets xCurrent, yCurrent, and zCurrent to x, y, z from gps.
	setHome(x, y, z)  --Sets xHome, yHome, and zHome to x, y, z from gps.
end

Fixed: Pass by copy derp on my part. Silly lua
Edited on 03 June 2014 - 08:30 AM
theoriginalbit #20
Posted 03 June 2014 - 11:21 AM
Fixed: Pass by copy derp on my part. Silly lua
yep, variables are pass-by-value, the only thing in Lua that's pass-by-reference is tables.
Bomb Bloke #21
Posted 03 June 2014 - 11:35 AM
… and functions.
Lyqyd #22
Posted 03 June 2014 - 03:26 PM
And threads, of course. Tables are where the phenomenon is most noticeable, as you can change the internal values and see the changes elsewhere.
theoriginalbit #23
Posted 04 June 2014 - 02:03 AM
precisely, which is the main reason I left functions and threads off the list as in this case, since tables are the only noticeable time.