OpenDRO User's Guide

8.4 Examples

Below are some examples of using the automation interface to do some useful operations.

8.4.1 User Speed Keys

An easy trick is to use a user function for your own speed key.  For example, suppose you want to have a speed key to quickly jump to machine setup:

function user1()
func.run('setup','mach')
end

Now when you press FUNC-FUNC-1, OpenDRO will jump straight to the machine setup.  You can do this for any function by supplying the proper group and function names in the func.run() call as documented above.

8.4.2 Auto-Execute from SD Card

OpenDRO can automatically execute a script on boot up as previously described but let's say you want this script to come from a file on the SD card named 'autoexec.lua' instead.  This is easily accomplished by writing a one line auto-execute script to run the SD card file:

OpenDRO> luash.execwr()
Press Ctrl-D to end
> dofile('autoexec.lua')
>
OpenDRO>

Note that the SD card is only supported on the DRO-550.  You can also extend this concept to the autoexec.lua file itself where each user function is its own script on the SD card.

autoexec.lua

function user1() dofile('polar.lua') end
function user2() dofile('threads.lua') end
function user3() dofile('calc.lua') end
function user4() dofile('angle.lua') end

An approach like this has the added benefit of conserving memory since all of the scripts reside on the SD card instead of in RAM and is strongly recommended for the DRO-550.  The DPU-550 has twice the amount of RAM as the DRO-550 so its lack of SD card support is much less of an issue.

8.4.3 Polar Conversion

The following example demonstrates how to convert a polar coordinate into a Cartesian coordinate that the DRO can use.

display.menuset(1,'enter')
display.menuset(2,'length')
local length = keypad.float(3)
display.menuset(2,'angle')
local angle = keypad.float(3)
angle = angle * math.pi / 180
axis.preseti('x', length * math.cos(angle))
axis.preseti('y', length * math.sin(angle))
machine.inc()

The first two lines show a message on the main display to enter the length.  The third line waits for the user to enter a floating point number on the keypad for the length.  Note the 'local' keyword in front of the length variable.  This keyword makes the length variable local in scope instead of global.  This conserves memory since when the function is finished, the memory for the variable is freed.  You should get in the habit of adding this in front of all variables.  You only need to add it to the first declaration of the variable and not on subsequent use of the variable.

The next two lines prompt and wait for the angle.  The angle is then converted from degrees to radians before performing the conversion to X and Y coordinates with sine and cosine.  The axis.preseti() lines set incremental preset values using the X and Y coordinates while the last line switches to incremental mode to show the results.

8.4.4 Tap Drill Chart

The following example demonstrates how to show information to the user via a selection menu.  In this case, we will show the tap drill sizes for five screw sizes.

local screws = { '2-56', '4-40', '6-32', '8-32', '10-24' }
local drills = { 0.0730, 0.960, 0.1160, 0.1440, 0.1610 }
display.menuset(1,'select')
display.menuset(2,'screw')
local index = keypad.select(3, screws);
display.menuset(1,'tap')
display.menuset(2,'drill')
display.menuset(3,tostring(drills[index]))
keypad.get()

The first two lines define Lua tables for the screw names and their respective drill sizes.  Note the 'local' keyword as previously discussed.  The script shows a select screw message before prompting for a selection.  The selection works by rotating through the table given as the second parameter with the preset and zero keys.  When a selection is made with the enter key, the index corresponding to the selection is returned.  We then display a result message by looking up the size in the drills table with the returned index.  Lastly, we wait for any key press to continue.

8.4.5 Memory Usage

Here we will demonstrate how to minimize memory usage and troubleshoot memory problems.  To minimize memory usage, always follow the following guidelines:

  1. Use local variables and local functions whenever possible.
  2. When you must use globals, set their value to nil when you are done with them so that they are freed.
  3. Store scripts on the SD card instead of in memory.
  4. Use Lua tables sparingly since they use a lot of memory.

You can watch memory usage with the mem.show() command.  The last line, 'Lua free', shows the amount of free memory available for Lua.  Keep in mind that Lua is a garbage collected language so don't be concerned if the number jumps around a bit.  You should be more interested in the trend.

OpenDRO> mem.show()
 :               :
Lua free      : 7687

Now let's define a function in memory:

OpenDRO> function polar()
OpenDRO>> local screws = { '2-56', '4-40', '6-32', '8-32', '10-24' }
OpenDRO>> local drills = { 0.0730, 0.960, 0.1160, 0.1440, 0.1610 }
OpenDRO>> display.menuset(1,'select')
OpenDRO>> display.menuset(2,'screw')
OpenDRO>> local index = keypad.select(3, screws);
OpenDRO>> display.menuset(1,'tap')
OpenDRO>> display.menuset(2,'drill')
OpenDRO>> display.menuset(3,tostring(drills[index]))
OpenDRO>> keypad.get()
OpenDRO>> end
OpenDRO>

Now, let's check the memory again:

OpenDRO> mem.show()
 :               :
Lua free      : 6646

You can see that the function is taking a little over 1000 bytes of memory because it is now a global function.  We can free the memory consumed by the function by setting it to nil.

OpenDRO> polar=nil
OpenDRO> mem.show()
 :               :
Lua free      : 7794

We are now back and slightly above our starting memory usage.  Again, the reason we are above is because of the effects of garbage collection.  If we instead save this script on the SD card and run it with dofile(), then it would only use memory when it is running and would immediately free it when done.