Scripting 101 - #3, more on functions

C Scripting questions and answers
Post Reply
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4586
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Scripting 101 - #3, more on functions

Post by ArtF »


  So , as left our hero's they were in dire straights.. oh..right..functions..

  We were speaking of how when calling functions you need to consider time
and the ramifications your function may have on running programs and other scripts.
The more scripts you run in parallel, the more conscious you'll need to be on this topic.
  There are a few things to consider, first, is this a function being called in linear time,
that is, is this in a GCode type of situation where you are OK to waste time as the
next line of the program wont be called till your done anyway. If so, your in the
drivers seat, BUT you still want to allow Auggie to process what you want to do,
so if you figure what your doing is complex find a way to perform yield() every
once in a  while. But you can also do a block. A "block" is a special command
which really translates to "Im gonna have a nap, wake me when the following occurs".

  Auggie has several built in block signals that you can wait for, and I add more all the time,
I will publish a list of them soon. So consider a script in the Gcode that is called as in..

G0X10Y10Z5
{CutSquare(5,5);}
m30

  the function CutSquare is a linear time call, its in the drivers seat and the next line
M30 will not be executed until the CutSquare ends. But depending on the square,
it may take some time to cut, planners being what they are, so its smart after
commanding your minions to do a job, to have a nap till their done.

global CutSquare = function( width,height )
{
    FeedTo( width , 0 );
    FeedTo( width, height);
    FeedTo( 0, height );
    FeedTo( 0,0);
}

    The above function would work fine ( assuming there is a
library function names FeedTo(..) in your system.). The 4 lines would
be added to the planner and the script would end allowing the m30 to execute.

  But what if you wanted to activate some hardware function after each line ends
its motion? Common occurrence.. and to wait around is impossible as you'd lock up
your system, so in this case. you can use the block..

global CutSquare = function( width,height )
{
    FeedTo( width , 0 );
    block("MotionStill");
    //do some hardware function here
    FeedTo( width, height);
    block("MotionStill");
    //do some hardware function here
    FeedTo( 0, height );
    block("MotionStill");
    //do some hardware function here
    FeedTo( 0,0);
    block("MotionStill");
    //do some hardware function here
}

    The above forces the script to have a nap after commanding the system
minions to make a move to the new location. During this time it uses no CPU time and is
a very efficient way of waiting for something. "MotionStill" is one of many
internal calls, but you can also make them yourselves. If, you have some script out
there monitoring a switch pin for example, it can simple emit a signal when it see's
a pin go active. A signal is just the wakeup call for a block. All scipts blocked on a
signal name get woken up when they hear signal yell it out..

Here is a small script that may monitor a pin for example..

global function ButtonPressed = function()
{
    while( true )
    {
        if( Pokeys1.GetPinDig( 5 ) == 1 )
        {
            signal("HumanPushedButton");
        }
        Sleep(1);
    }
}

    This function could be running on your system all the time after being called from
your mainscreen init script. It wakes up every second, see's if a button is pressed,
and if so does a signal("HumanPushedButton"); If in our previous example, we had
blocked on that phrase...

global CutSquare = function( width,height )
{
    FeedTo( width , 0 );
    block("HumanPushedButton");
    //do some hardware function here
    FeedTo( width, height);
    block("HumanPushedButton");
    //do some hardware function here
    FeedTo( 0, height );
    block("HumanPushedButton");
    //do some hardware function here
    FeedTo( 0,0);
    block("HumanPushedButton");

  Then the script would move the tool, conveyor, 3d extruder, robot..whatever the heck FeedTo is doing..
and then wait till the other script sees a button get pressed. The Gcode program running will not complete until
this script ends.. so one line can do a lot of work. A single file loaded into Auggie can represent not just a cutting
job but an entire never ending process as well, all up to the scripter, and the functions used. You should
note that really no motion at all is required to use Auggie as an automation scripter,  a hundred threads could
all be running each blocking and signalling on various inputs and outputs and other threads can be waking up to
turn down thermostats, open greenhouse windows and other quite complex tasks based on fairly complex
logic. While my current needs are based on motion, others may have use for never ending process, so the
system is setup for either.

  Enough for lesson 3 I think.. Ask when confused, and Ill try to confuse you more..

Art




Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests