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

Multithreading in drones

Question

I'm trying to write a script to control a worker drone. This drone will interact with plants on specific coordinates that are being fed to him by mainframe via wireless modem. I've planned to have those commands:
1. Add/remove new coordinates.
2. Print out all registered coordinates.
3. Finish the job, return to charger and wait for further commands. 
4. ...

The initial solution included using two threads: a worker thread that executes requested and routine actions and a controller thread that listens for new modem messages, updates drone status and generates responses for the mainframe. The problem is - drones have no access to Thread API. I believe there should be a simple solution for this task but I don't see any suitable way to provide such level of responsiveness without proper threading.

Rough example for drone :
 

...
thread.waitForAny({
  thread.create(function()  -- Worker
    while true do
      if state == "work" then
        for _, pos in pairs(locations) do
          moveto(pos)
          dostuff()
          if state ~= "work" then break end
        end
      elseif state == "stop" then
        sleep(5)
      end
    end
  end),
  thread.create(function() -- Controller
    while true do
      local message = table.pack(computer.pullSignal())
      if message[1] == "modem_message" then
        local command = serialization.deserialize(message[5])
        if command.text == "stop" then 
          state = "stop" 
        elseif command.text == "resume" then
          state = "work"
      end
    end
}})

 

I am sure somebody already did this kind of stuff on their server, I just can't come up with anything effective and simple without surpassing the 4KiB threshold. Do you have any suggestions?

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 1

aye, the threading library is part of openos. without installing it, you've got not thread api

My solution to your issue would be to put a timeout on computer.pullSignal. If it times, execute the next pending command (if there is one)

 

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.