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

OpenComputers 1.3, Robot Tree felling issues

Question

First, I love the Mod.


I use this mod to replace the CC variety because CC doesn't work well with my mod selections.OC is far better anyway.


So I set out to learn LUA programming yesterday, and read lots of API's and looked at lots of code.


I discovered that direct interaction with the robot means I need to be in the LUA prompt and to run the LUA scripts I make, I have to be in the shell.. this wasn't immediatly obvious and it took me a bot to figure that out.


Once I got the jest of how a LUA program for OC is put together, i started my first program last night and I got something that works but I have a bug that I can't track down, so I assume it's because I'm not allowed to have beginners luck.


The program is a simple tree felling program. the steps are simple:


Plant the saplings


move 1 space up


wait for the tree to be detected


drop 1 block down


chop the tree from bottom to top and back down.


rinse an repeat.


However, after the robot plants the saplings, it is just 1 block away like this: Rss


                                                                                                                         ss


First issue:


The saplings never grew. wide open sky, nothing above.


I moved the robot back from the saplings so there is a space between the robot and the first sapling, but this means the robot cannot detect the grown sapling from 2 blocks away.


I thought i'd add a timer to move the Robot forward 1 space to detect every 15 seconds and if it detected the tree, it would drop down 1 space, move forward one space and fell the tree as usual.


Second issue:


The second problem I had was after several trees were felled, the robot would lose track of the height variable so, while the robot was felling the second half of the tree, it would continue to dig into the ground as much as 5 blocks and muddle in that hole.


This bug seems random. and my thought is that the timer I added but be interfering with the height count?.. but being new to LUA and OpenComputers mod and programming in general.


Would anyone mind giving a guy a hand to *fix this second issue?


I will include my program code here:


[Edit] just realized how shabby the code looks at the end where I was working last. apologies.



local robot = require("robot")
local sides = require("sides")
local event = require("event")
 
local height = 0
 
function Harvest()
    robot.swing()
    robot.forward()
    robot.swing()
 
    while robot.detectUp() do
      robot.swingUp()
      robot.up()
      robot.swing()
      height = height + 1
    end
 
    robot.turnRight()
    robot.swing()
    robot.forward()
    robot.turnLeft()
 
    for i = 1,height do
      robot.swing()
      robot.swingDown()
      robot.down()
    end
 
    robot.swing()
    robot.turnAround()
    robot.forward()
    robot.turnRight()
    robot.forward()
    robot.turnRight()
    Saplings()
end
 
function Saplings()
    if not robot.detect() then do
      robot.select(12) -- Saplings here
      robot.forward()
      robot.place()
      robot.turnRight()
      robot.forward()
      robot.turnLeft()
      robot.place()
      robot.turnAround()
      robot.forward()
      robot.turnAround()
      robot.place()
      robot.turnLeft()
      robot.forward()
      robot.turnRight()
      robot.place()
      robot.up()
 robot.back()
 event.timer(15,onTimer,20)
end
  end
end
 
function onTimer()
robot.forward()
if robot.detect() then
Harvest()
else
robot.back()
end
 
end
-- event.timer(30, onTimer, 50)
 
Saplings()

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Hello and welcome! First, a timer needs event.pull() to pe called to work. Also you should look into optimizing the sapling function, it isnt very nice to read it and understand whats going on(this is important).

Edit: and writing lua in capital letters is not nercassy

Link to post
Share on other sites
  • 0

You are using the timer correctly, that is not the problem. But when you go down, you never reset your height to 0. So the next time you call Harvest, you first go down the blocks you need to go down, but then you also go down for the previous trees. You can solve this by setting height to 0 just after the loop where you go down. 

 

Also, it is better if you place a local before your function definitions. Then you won't clutter the environment as much and you free up some RAM after your program finished running. 

Link to post
Share on other sites
  • 0

You don't really need to count the height, you can do

function fellTree()
    robot.swing()
    robot.forward()
    while robot.detectUp() do
        robot.swingUp()
        robot.up()
    end
    while not robot.detectDown() do
        robot.down()
    end
end

This way you never need to count the height, it'll just stop at the top and come back down.

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
Answer this question...

×   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.