This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
JGBrands's profile picture

Redstone in Motion + ComputerCraft Elevator Issue

Started by JGBrands, 25 October 2013 - 08:15 AM
JGBrands #1
Posted 25 October 2013 - 10:15 AM
Title: Redstone in Motion + ComputerCraft Elevator Issue

Greetings,

I've been working on recreating an elevator on my SMP server that I have earlier built (succesfully) in SSP.

The set up is quite simple. There's a Computer at the top and bottom platforms, which have the programs 'down' and 'up. Both of which are a very simple 2 line program.


rednet.open("back")
rednet.broadcast("down")

And 'up' for the up program, naturally.

The workhorse is on the computer that controls the Elevator's carriage. The program's code is a little dirty and could definitely be written better, but my priority is to have something working first, I can clean it up later.
Here;

local id
local msg
local rv
local emsg
-- Open for rednet.
rednet.open("top")
-- Do we have an order?
if fs.exists("/var/move") then
  fh = fs.open("/var/move", "r")
  direction = fh.readLine()
  -- We got an order, move!
  if direction == "up" then
    peripheral.call("left", "move", 1, false, false)
  elseif direction == "down" then
    peripheral.call("left", "move", 0, false, false)
  else
    print("ERR:/var/move: invalid")
  end
 
  -- Errors?
  if rv == false then
    print("ERR: " .. emsg)
    rednet.broadcast("done")
    fh.close()
  end
end
-- Wait for a new order.
while true do
  print("Waiting for new order...")
  id, msg = rednet.receive()
  print("recv: " .. id .. ": " .. msg)
 
  -- Check if this is what we want.
  if id == 4 then
    if msg == "up" or msg == "down" then
	  print("recv order: " .. msg)
	  fh = fs.open("/var/move", "w")
	  fh.writeLine(msg)
	  fh.close()
	  print("write: " .. msg .. " >> /var/move")
	  os.reboot()
    else
	  print("err: invalid order")
    end
  else
    print("err: invalid controller")
  end
end

This script is naturally called 'startup'.

Here's the issue. The script works fine, however once the Elevator is put in motion, the computer doesn't reboot anymore. It goes about 2 steps in the direction it's meant to go (meaning it has rebooted successfully at least once), and then it has to be right clicked to work.

Is this a bug or have I missed something really obvious?
Engineer #2
Posted 25 October 2013 - 10:51 AM
The computer can only reboot when there is a message received, and it checks if the message is "up" or "down". That means that you have to constantly send rednet messages from another computer. Maybe it missed a message because of the reboot, I suggest something like this as startup:

if fs.exists( "f" ) then
   local file = fs.open( "f", "r" )
   local direction = file.readAll()
   file.close()
   os.queueEvent( "rednet_message", 4, direction, 1 )
end

while true do
    local id, message = rednet.receive()
    if id == 4 and ( message == "up" or message == "down" ) then
       if message == "up" then
          local file = fs.open( "f", "w" )
          file.write( "up" )
          file.close()
          peripheral.call("left", "move", 1, false, false)
          fs.delete( "f" )
       else -- We checked if message is up or down, so I use else instead, just cuz I can :P/>
          local file = fs.open( "f", "w" )
          file.write( "down" )
          file.close()
          peripheral.call("left", "move", 0, false, false)
          fs.delete( "f" )
       end
end
I hope this is server-restart proof, but it should work essentially.
I think you will understand this, but dont hesitate to ask questions if you dont. I will gladly help you, not only me, but some others as well :)/>
JGBrands #3
Posted 25 October 2013 - 11:00 AM
This is how I had it earlier, but it crashed the computers. I might try and have the elevator controller send an ACK every second to the carriage controller, see if that fixes it; and have it stop when the computer send done.
Engineer #4
Posted 25 October 2013 - 02:34 PM
How did that code crash the computer? I'm willing to figure it out with you, because I'm curious :P/>

I would use that last only if we cannot fix the code. It still is an option though.
JGBrands #5
Posted 25 October 2013 - 07:10 PM
How did that code crash the computer? I'm willing to figure it out with you, because I'm curious :P/>

I would use that last only if we cannot fix the code. It still is an option though.

Sadly that did not work. It was a very patchy fix to begin with, but it caused the elevator to stop working all together because it tried to handle the ACK packets I sent it.

The program is actually slightly more complicated than I posted it here, the real way this thing works is that the controllers at the platforms send a request to a server computer which controls all elevators. It also keeps track of what floor the elevator is at and if it's currently in motion or not. So basically the traffic goes as Elevator <–> Server <–> Controller.

This isn't the issue though, the issue is that the computer won't restart after moving, I literally have to keep right clicking the computer to keep the elevator moving, which is tedious when you're moving 60 levels down.
Engineer #6
Posted 25 October 2013 - 07:37 PM
-snip-
Please check your PM
Bomb Bloke #7
Posted 26 October 2013 - 05:25 AM
I've a hunch that there's nothing wrong with your code, but something up between CC and RiM. I've not tried RiM myself, but if I get the gist of it right, if you're using that to move the computer itself then that computer should reboot automatically when shifted (hence why only one rednet message "should" see it move all the way up or down) - only there's at least one report of that not working reliably. Reading on from there throughout the next page will hopefully ring some bells with you.
Engineer #8
Posted 26 October 2013 - 06:53 AM
We were talking over PM and confirmed that it's something about the server.

He went with a more appropriate way of fixing his problem with Redstone and a little bit of computers.

So this is solved?
JGBrands #9
Posted 26 October 2013 - 12:41 PM
Yeah this is solved. Well sort of.

I'll report the problem to the author of RiM, since the problem has nothing to do with ComputerCraft, but rather is an issue of RiM on SMP.

Thanks for the help, lovely ComputerCraft community. That said, I'd just like to thank the author(s) for this mod. This thing has completely reinvigorated my love for Minecraft and gives me another excuse to bring my profession into my private life hahahahaha!