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

Archived

This topic is now archived and is closed to further replies.

  • 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".  

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.