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

"too long without yielding" in robot farming loop

Question

Today i was writing a program for my Mystical Agriculture Farm.

The program is supposed to steer the Robot in random directions and check the Robot's energy after each cycle.

After a certain threshold (now 10%) is reached, it sends the robot to a charger.

The energy check and recharging works like a charm, but as soon as i add the while-loop and the random movements, i get a "too long without yielding" error (the full error is pasted at the end of the github file).

github: The Code

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Thanks for the quick response.

I went ahead and added the line right before the last "end".

Sadly it doesn't seem to fix the issue (or if it does it opens another), as the robot now just freezes without making a move.

After i cancelled the program after approximately 1 minute  (Ctrl+Alt+C) this error appeared:

interrupted:
stack traceback:
		[C]: in function 'error'
		/lib/event.lua:222: in function </lib/event.lua:203>
		(...tail calls...)
		boot/20_os.lua:58: in function 'sleep'
		/home/farmer.lua:87: in main chunk
		(...tail calls...)
		[C]: in function 'xpcall'
		machine:751: in function 'xpcall'
		lib/process.lua:84: in function </lib/process.lua:80>

 

EDIT:  it seems the error was caused by the server.

After i added os.sleep(0) to the end of my main-loop the program worked.

Thanks for your answer and also thanks payonel for your solution for loops with long event intervals.

 

Link to post
Share on other sites
  • 0

you have to look at your code and think -- is there anywhere in this loop that could run for more than 5 seconds without sleep/yield? If so, add a os.sleep(0), perhaps once per loop, or use a timeout yourself and sleep every 2 seconds.

I have a similar sleep in the terminal code when printing out a very large file to the screen, i do this:

  local last_sleep = computer.uptime()
  while true do
    if uptime() - last_sleep > 1 then
      os.sleep(0)
      last_sleep = computer.uptime()
    end

 

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.