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

run program on robot from computer

Question

I have a few questions about programming. I have wireless card on both, open port 123 on the modem and know to use =event.pull("modem") to wait for a message on the robot and then use component.modem.broadcast(123, "hello") to broadcast a message. but the message its only a message. I am wondering how I can make the robot understand it is a command or to execute a program on it. 

second question is the command robot.forward(), it lags when i use that same command multiple times in a row.. my robot moves 4 blocks forward and then go up and turn a few times to go through a maze to a room. right now i am typing robot.forward()  4 times to make it move 4 blocks forward.  i see on the robot API page for moving forward is  robot.forward(): boolean[, string] .  Don't know what boolean or string is, not that you need to explain. I just don't know how to input the amount of forward movement I want.

third question is, I want the robot to check slot 1, and see if there are 3 wither skeleton skulls, if there are then proceed to move into the room.  i tried getStackInInternalSlot(1), I don't know how to use that info to compare the item in the slot. i am really clueless figuring out how it is suppose to work. Tried looking at other people's program to use in mine but can't understand them. not sure how to make robot recognize an item like "wither skeleton skull".  

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1

To answer you first question 

On 6/8/2017 at 9:23 AM, nosho said:

how I can make the robot understand it is a command or to execute a program on it.

We have to understand that messages in OpenComputers are only the basic types: strings, numbers, booleans, and NOT tables and/or functions. This means that in order to have a robot take "commands" from another computer it has to have a special program that waits for special, known commands (a protocol) and executes either a simple command or maybe a more complex command like running another program from the filesystem and awaiting another command after it has finished the program.

Here we'll make 2 very basic programs to do simple control of a robot. One for the client and one for the robot.

-- # Client.lua
local term = require "term"
local component = require "component"
local modem = component.modem

assert(modem, "You need a modem dummy.")

local port, robot_address = 15, "34eb7b28-14d3-4767-b326-dd1609ba92e"

modem.open(port)

while true do
  print "Enter robot command."
  local command = term.read()
  modem.send(robot_address, port, command)
end
-- # END Client.lua

-- # Robo.lua
local component = require "component"
local event = require "event"
local robot = require "robot"
local modem = component.modem

modem.open(15) -- # listen on port 15 like the client

local commands = {
  ["printFuel"] = function() print("fuel lvl:", component.computer.energy(), "/", component.computer.maxEnergy()) end
}

setmetatable(commands, {__index = robot})

while true do
  print "Awaiting next command.."
  local msg = table.pack(event.pull("modem_message"))
  local command = msg[6]
  
  if commands[command] then
    print("Executing command [" .. command .. "]..")
    local ok, err = pcall(commands[command])
    if not ok then print("ERROR: ", err) end
  end
end
-- # END Robo.lua

DISCLAIMER: This is a proof of concept program and hasn't been tested but, with tweaking for any errors, should work as intended.

To answer your second question, robot.forward and other movement methods are relatively slow without an experienced experience upgrade component.

If you want to move multiple times without typing robot.forward X amount of times then you need to look into functions and how to use and write them.

-- # This is a naive approach but works as intended. Simply.
local function forward(times)
  times = times or 1
  for i = 1, times do robot.forward() end
end

As for your third question... Theres quite a bit pack into it. Firstly you need an inventory_controller upgrade to manipulate and get info about inventories. The information returned about item stacks in slots is a table. Look into 'tables' in lua. They're fundamental, you'll need em sooner or later. 

Lemme know if you need some more information or clarification.

Link to post
Share on other sites
  • 0

I'm at work so I cant give details right now but I would highly suggest you look at the "types" of values you can use to program, such as booleans or strings. These are paramount to the basics of programming and understanding documentation so you can help yourself better.

Link to post
Share on other sites
  • 0

For the people who experience issues add this

command = command:gsub("%s+", "")

just after this.

.....
  print "Awaiting next command.."
  local msg = table.pack(event.pull("modem_message"))
  local command = msg[6]

 So like that

.....
  print "Awaiting next command.."
  local msg = table.pack(event.pull("modem_message"))
  local command = msg[6]
  command = command:gsub("%s+", "")
	

 When you sent string over network card. It adds many space after the text. So with the command I have added it removes all the spaces and let it works as it should do.

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.