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.

MaximilianVINCENT

Black Hole Tutorial 04- So You Want to Write a Program

Recommended Posts

OpenComputers 1.51 for Minecraft 1.7.10

Black Hole Tutorial 04- So You Want to Write a Program

So you have an understanding of Computers, Robots and OpenOS and how everything fits together. Time to get them finally doing something. This tutorial is here to help you learn to design and write a simple program for a robot.

The Language of Lua
If you know little to nothing about Lua, start here: Lua 5.2 Reference Manual. Read what Lua is capable of. Have lua running on a computer and try out some of the example code provided.

OpenComputers mostly follows along with the above, read the wiki to see the differences.

There are many tutorials on Lua available, these are some I have used: TutorialsPoint, Lua- Really for Beginners, Lua-Users Wiki.

I am going to assume at this point that you have at least a basic knowledge of Lua. This should include:

  • Comments
  • Variables
  • Conditional Stuctures - if, then, else, ...
  • Loops - for, while, repeat, ...
  • Functions
  • Tables

Can you name at least six commands you can give a robot? If not, you need to learn more about how to control it.

  1. Look at the APIs available on the wiki, especially the Robot API.
  2. In the Lua interpreter l="" for k,v in pairs(robot) do l=l..k.." " end print(l) l=nil, will print a list of commands available for the Robot API. change 'robot' to other api's names to see what they have.
  3. I remember a command that gives us a string of documentation, but can't find it now. This is why you take lots of notes.

 

I thought for our first program, we should start by doing what everybody does when starting Minecraft: punch some wood.

-Plan Our Work-

The more specific you can be in your desired outcome, the easier it will be to translate that into software. These questions are not really necessary for such a simple program, but it is better to start thinking this way now.

Q: What do we want done?
Chop down a tree.

Q: What do we have to work with?
Lots of 1x1 trees.

Q: What else do we need for this to work?
A way to recharge the robot, eventually.

Q: What machine will best/can we afford to do this job?
We want it to move, so a Robot or Drone. We want it to grab a a treeful of wood, so a Drone could work if I knew how to program an EEPROM. I don't yet, so we will use a Robot. We will need at least one Inventory Upgrade to hold the wood we are chopping.
We aren't asking for much, so a minimal Tier 1 Robot should be able to do this.

 

  • Tier 1 Case
  • Tier 1 CPU
  • Tier 1.5 Memory
  • Tier 1 GPU
  • Tier 1 Hard Drive with OpenOS already installed on it
  • Tier 1 Screen
  • Keyboard
  • Inventory Upgrade

You could install a Tier 2 Upgrade Card as well. That would let you use a Generator or Solar Panel to help with our power requirements.


-Work Our Plan-

start a new file where you want, I suggest /usr/bin:
mkdir /usr/bin
edit /usr/bin/treeMuncher.lua


Some notes on what it was intended for, who wrote it and when are nice to have. Especially six months down the road when you won't even remember writing this program and have no idea what it does.

--[[
Tree Muncher v0.1
for OpenComputers v1.51
Written by Me
on 2015Mar03

 

Purpose:

To collect the trunk of a 1x1 tree and return to starting location.

How to use:
Put robot at the base of a 1x1 tree, facing the tree before starting program.

 

Needed:

minimum Tier 1 Robot
--]]


Save and Quit for now, we'll come back. We need to think of and test our program.

- Break it Down -

 

"This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface."  - Doug McIlroy

Make it run, then make it right, then make it fast”  - Kent Beck

 

Just like ye programmers of olde, you are working with machines of limited capablility. The above ideals are what was learned from that time and are just as valuable today. The Wikipedia page on UNIX Philosophy is a quick read and a great way to think about your programs. For a much longer read, The Art of Unix Programming is available.


Let us break down our program into 'dig into the tree, chop up tree, come back down'

1) Dig into the Tree

Cool thing: just like you, a robot does not need an axe to chop a tree down.
Put your robot down facing a tree and turn it on. Enter the lua interpreter with lua and try robot.swing()
Cool, we got a block. You may have noticed a delay after getting the block. That is there to simulate the time it normally would have taken to chop down that block. Put that log back in the tree and put an axe into the tool slot (Bottom-left corner of Robot's inventory with a wrench icon). Press the up arrow to show the last command you entered and enter to do it again.

Was the delay less using the axe? Look at the axe, you are indeed using it. Durability of your tools is something you have to take into account in future programs. To make it worse, the only way to change tools is with the Tier 2 Inventory Controller Upgrade. And if we wanted the Robot to craft it's own axes, that requires a Crafting Upgrade. At this point, we need a Tier 2 Robot.

2) Chop Up Tree
We got a block, now we want a tree. Let 's use Beck's above quote and show how that idea can translate into reality.

"Make it run"
We can move and swing, that should be enough to make it work. Trees are usually less then 10 blocks tall, so
robot.swing()

robot.forward()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
robot.swingUp()

robot.up()
will work. It is also very wasteful of space and hard to read. Also I noticed during testing the trees I chop are never more then 8 blocks tall.

"then make it right"
robot.swing()
robot.forward()
for i=1,7 -- seven because we already took the bottom block
do
  robot.swingUp()
  robot.up()
end

is much easier to understand. I can think of one more change, though.

 

robot.swing()
robot.forward()
while robot.swingUp() do robot.up() end

Small, simple and easy to read. Good code can be a thing of beauty.
This has the added benefit of stopping when there is nothing more to chop.

"then make it fast"
This program is so simple, we can't really do much to make it faster. Maybe a diamond axe?

3) Come Back Down

If you have been testing these programs as we go along (good for you!), you noticed the robot has done the job we want: the tree is chopped. Of course, it is now more than a few blocks up in the air.

One last step before we are done: have our robot come back to where it started. This is not just for an easily retrieval. By returning to its original coordinates and facing, this makes it easier for us to keep track of where the robot is in your program and in the world. Once your robot is working on several projects, having a home base (usually next to a Charger) makes things simpler. Simple is good.

Let's finish this:
robot.swing() -- make a hole in the base of the tree
robot.forward() -- move into the tree
while robot.swingUp() do robot.up() end -- while we get a block swinging up, keep moving up
while not robot.detectDown() do robot.down() end -- until we see a block belows us, keep moving down
robot.back() -- back to start location


Try these commands in lua and make sure they work, Testing is good. Then exit the interpreter and get back to your tree muncher program.
edit /usr/bin/treeMuncher.lua

after the comment header, add

local r = require("robot")

to have access to the Robot API. (I use a single letter for the variable name because I will have to type it a lot and is easy to remember. Saves a little space on your file system as well. Changing 'r' to 'robot' or 'rbt' or even 'GrrlPower' works as well, if a bit silly.)

and then our program above:
r.swing()
r.forward()
while r.swingUp() do r.up() end
while not r.detectDown() do r.down() end
r.back()


and that's that, your first simple program. Check for mistakes in typing, Save and Quit. We'll be back later to make this program more useful.

 

Actually, almost done. Do you have a way to recharge your robot? If your robot sits for long periods of time waiting, a Solar Panel Upgrade would make sense. Since we are collecting lots of wood, we could burn some small portion in a Generator Upgrade.

 

Bonus: Auto-Chop

At the shell, type cp /usr/bin/treeMuncher.lua /autorun.lua

Now take your robot to the base of another tree and turn it on. Robot Chop!

After booting the robot, OpenOS will automatically run autorun or autorun.lua in the root directory if available.

 

Least Signfigant Bits

  • Your turn: Can you add to this program to plant a seedling and wait until a new tree is grown before cutting down again?
  • If you are having problems, post it on the Support Sub-Forum. Additional information, constructive critism or praise of this tutorial should be posted below

End of Line.

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.