| ME 135 - Spring 2005 - Lab 7 | ||||
| Lab 7 - Design using state transition logic, Implementation using TranRunJ (Updated at 1530 on 03-28-2005) NOTES: Download the TranRunJ documentation here. Extract it and open index.html. There's a lot of information there -- you don't need to know it all in detail, but use it as a reference. CTask, ContinuousTask, PeriodicTask, and EventTask will come in especially handy. Many people learn best by example. If that's you, please check out Steve's example of a "Latching Connector" from the March 15th lecture (see below). If you have any questions understanding it, please ask me or Steve. If you understand that example and you understand Lab 6, this lab will be no problem! INSTRUCTIONS: Step 1: Design First, design all your tasks and determine what states they'll have and what action/entry/transtion each state will have. Do this on paper before you start coding anything. Ask me if you have any questions at this point. The most important part of this lab is to understand the concepts behind "state transition logic," not the details of implementation in TranRunJ. Step 2: Implementation Create a new project in Netbeans. Create a new directory for this lab where all your files will be stored. Mount this directory. Download TranRunJ (tranrunj.zip) from the class web page and mount it in Netbeans. Mount MechControllAndDLL. Set your working directory to "DLLs files". Begin your implementation: A good place to start for this lab is Steve's "Latch Connector" example from the March 15th lecture. Download, unzip, and mount his sample code in Netbeans and take a look at it. You can base your project on his code. The following is an overview of a typical program that uses TranRunJ: -> Run.java: This is the "user" class -- in other words, it has the main(...) method in it that Netbeans will execute when you want to run your program. The main(...) method simply calls the start(...) method in Run.java. Run.java also sets up the processes, tasks, and timers that will be used in your program and tells them to start running. Run.java should extend TRJMain, which gives it the magical functionality that has already been programmed in TranRunJ -- you get to use it for free! In Steve's "Latching Connector" example (see above), his Run.java file is a good, simple example of what your user class should look like. -> PWMTask.java, ControlLogicTask.java, MotorControllerTask.java, etc.: Create one class for each task you'll need (each task is in a separate .java file). Each of these tasks should extend the appropriate TranRunJ base class, as follows: ContinuousTask: TranRunJ will make this task get CPU time as often as possible (whenever nothing else is running). These tasks are LOW priority. PeriodicTask: TranRunJ will make this task get CPU time at regular time intervals (which you specify in the constructor as the interval argument). These tasks are HIGH priority. EventTask: TranRunJ will make this task get CPU time whenever a certain event occurs. You specify whether you want this task to get CPU time by writing (overriding) a method called checkEvent() that returns true or false (corresponding to run or don't run). These tasks are HIGH priority. Within each task, declare each state that belongs to that task. Each state is of type CState. Each state is actually a "sub-class" that you'll declare within the task to which it belongs. It should extend CState. More About States: Each state subclass can have the following three methods, corresponding to the "state transition logic" methodology you learned in class: An entry method executes once when the state is first entered: protected void entry() { ... } An action method gets executed over and over as long as the task remains in this state: protected void action() { ... idle(); } A transition test method gets executed over and over by TranRunJ to determine if you're ready to move to the next state. If you want to remain in the current state then return null. If you want to go to the next state, return that state: protected CState transitionTest() { if (some_test) return null; else if (some_other_test) return nextState; idle(); } The following applies to IntermittentTasks only (i.e., EventTask and PeriodicTask): Notice that you must call idle() in the action and transitionTest methods -- if you don't, TranRunJ will devote all its CPU time to that method and it won't give any time to any other tasks or methods! In Steve's "Latching Connector" example (see above), his PWM.java task is a good, simple example of what a simple task (with two states, in this case) should look like. Task Messages: In TranRunJ, each task (CTask) has the ability to send and receive messages. This allows tasks to communicate. For example, a user interface task can tell a PWM task to set its duty cycle to 0.5. Read TranRunJ documentation for CTask to see what methods are available (e.g., createMessageInbox, checkForTaskMessage, readTaskMessageInt, readTaskMessageDouble, sendTaskMessage). Not only can a task send a message to another task, but a task can send a "global" message that all other tasks can see. In the CTask documentation, the following methods are useful: createGlobalData and copyToDataBox. Optional: National Instruments Data Acqusition Board (BNC-2120): Steve mentioned in the lab instructions that you can use the National Instruments data acquisition board instead of a GUI for your user input. To get started, look in MechControlAndDLL\Diagnostic\NItest |
||||