TEA Program Calling TEA Programs Example
Last Modified: 2006-11-06
find:

basket

Acroname Robotics PDF webpage version TEA Program Calling TEA Programs Example PDF

Related
Products

Product image for BrainStem Moto 1.0 Module
BrainStem Moto 1.0 Module
Product image for Brainstem GP 1.0 Module
Brainstem GP 1.0 Module

Contents

Introduction

A BrainStem module has a size limit of 1K for executable TEA programs.  However, a module is capable of running more than one program.  By using this feature, one program can call another program as a subroutine.  This makes it possible to write an application that is larger than the 1K limit. 

It shows how to write a program with subroutines and functions located in several different TEA Files.  The code takes advantage of some of the multi-tasking features of the BrainStem. 

Note

In early firmware versions, there was a bug that caused intermittent problems with the popcmd virtual machine instruction.  This problem only affects GP 1.0 build 11 and earlier and Moto 1.0 build 3 or earlier.  Here is the errata page and here is an example that shows a work-around if you are using a module affected by this bug. 

Theory

A program can call another program as a subroutine by using one of the aMulti_Run functions such as aMulti_Run00 .  The different forms of the aMulti_Run functions provide several styles of parameter passing.  The program acting as a subroutine must end with a call to the aMulti_Halt routine. 

Flowchart of aMulti_RunXX process.
Flowchart outlining a program execution when using the aMulti_RunXX routines.

When subroutines are small, several can be stored in a single TEA program.  A single case statement in the main routine can determine which subroutine to call.  The aMulti_Run functions are not as efficient as normal subroutine calls, but they provide the most straightforward way to expand a TEA project beyond the 1K limit per TEA file. 

This example application simply prints status messages indicating which routines are active.  It is only intended as a template for multi-file applications.  The programming for this example consists of five source files.  These files include a main calling program, functions to wrap the multitasking routines, two different TEA subtasks programs that execute as subroutines, and a TEA program that can be used by either the main program or the subtask TEA programs. 

Source Code - Wrapping the Shared Multitasking Routines

The "0mx.tea" file contains subroutine and function declarations that wrap the multi-tasking library routine calls.  Common defined values and include statements are also stored in this file since it will be included in all the source files for this example. 

/* filename: 0mx.tea */ #include <aCore.tea> #include <aMulti.tea> #include <aPrint.tea> #define TASKSPROC 1 #define FUNCSPROC 2 #define SUB1FILE 1 #define SUB2FILE 2 #define FUNCSFILE 3 int subtask1() { return aMulti_Run00(SUB1FILE,TASKSPROC); } int subtask2() { return aMulti_Run00(SUB2FILE,TASKSPROC); } int func0() { return aMulti_Run11(FUNCSFILE,FUNCSPROC,0,0); } int func1(int n) { return aMulti_Run11(FUNCSFILE,FUNCSPROC,1,n); }

Source Code - TEA Main Calling Routine

The "0m0.tea" program is the main routine.  It calls subtasks and functions located in three different TEA Files.  It should be compiled and loaded into file slot 0. 

The main program must be launched first.  It will automatically run in process slot 0 and launch the other programs.  The subtasks are hardcoded to run with process ID of 1.  If one subtask tries to call the other subtask there will be a process slot conflict.  An error will occur and the program will halt.  The functions should run with process ID of 2.  The main program and the subtasks can call any of the functions since they run in their own process slot. 

/* filename: 0m0.tea */ #include "0mx.tea" void main() { func0(); func1(100); subtask1(); subtask2(); }

Source Code - TEA Subtasks

The "0m1.tea" program contains subtask 1.  It should be compiled and loaded into file slot 1.  The "0m2.tea" program contains subtask 2.  It should be compiled and loaded into file slot 2. 

/* filename: 0m1.tea */ #include "0mx.tea" void main(char callingProc) { aPrint_String("subtask 1\n"); aCore_Sleep(20000); func0(); func1(101); aMulti_Halt(callingProc,0); }
/* filename: 0m2.tea */ #include "0mx.tea" void main(char callingProc) { aPrint_String("subtask 2\n"); aCore_Sleep(20000); func0(); func1(102); aMulti_Halt(callingProc,0); }

Source Code - TEA File with Parameter Input

The "0mf.tea" program contains some functions that can be used by the main ("0m0.tea") program or the subtasks ("0m1.tea" or "0m2.tea").  It should be compiled and loaded into file slot 3.  This program contains more than one function.  The wrapper functions include a parameter to identify which function to execute.  A case statement interpretes the parameter passed from the wrapping functions to launch the appropriate routine. 

/* filename: 0mf.tea */ #include "0mx.tea" int __func0() { aPrint_String("func0\n"); aCore_Sleep(10000); return 0; } int __func1(int n) { aPrint_String("func1 <- "); aPrint_IntDec(n); aPrint_Char('\n'); aCore_Sleep(10000); return 0; } void main(char callingProc, char c1, int n) { int retval=0; switch (c1) { case 0: retval = __func0(); break; case 1: retval = __func1(n); break; } aMulti_Halt(callingProc,retval); }

Revision History:

  • 2004-09-22: Example Created.
 

Related Examples:

Using Reflexes to Sequence TEA Programs Example

Robot Program with Two TEA Programs with Different Behaviors Example

voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy
© Copyright 1994-2008 Acroname, Inc., Boulder, Colorado. All rights reserved.