About Threads

ftComputing : Programme für die fischertechnik-Interfaces und -konstruktionskästen
  
ftComputing.de
Home
Back
Sitemap
Index
Links
Impressum
Mail
 

General

umFish30.DLL separates the access to the interface resources and the access of the application. Therefore umFish30 can be used with threads. Separation is done by placing the interface accesses in a special poll thread. The poll thread is controlled by the MultiMediaTimer. The poll thread accesses interface in fix intervals and places the belonging values in a special (internal) control block (the ftiDCB). The application reads and writes this ftiDCB asynchronously.

This technique anables different threads to access the interface (if it is needed : with extension module) without problems. The original access to the unique resource only happens once by the one poll thread. The access to single E-Inputs / M-Outputs can be done without additional work. Writing to all M-Outputs with one function (SetMotors) requieres an masking to save the M-Outputs of other threads.

The following examples refer to C++Builder programs with thread classes inherited from VCL class TThread. Working directly with Win-API functions is possible in the same manner, there are some additional possibilties (look to Robot-Plant).

Program RobStamp

The Programs AmpelThread and RobStamp are constructed with the same schema. AmpelThread is very much easier to handle (there is robot to crash against the stamp) and should be the first program to try. RobStamp is more interesting, therefor it is discussed here. The actual program AmpelThread uses the Intelligent Interface with extension module connected to COM1, RobStamp uses COM1 and COM2 (corresponding to the exsisting models).

frmThreadMain

Der main thread containing the GUI and the general control
ftR Instance of the class TFishFace for robot control
ftS Instance of the class TFishFace stamp control
umFish30.DLL At the background : the Poll Thread
RobReady Event : Robot has place one piece
Standard Security, autoReset, not signaled
StanzeReady Event : Stamp can process the next piece
Standard Security, autoReset, not signaled
RobThread Thread for robot control
StanzThread Thread for stamp control

TfrmThreadMain::cmdActionClick

  • Create ftR and ftS instances :
    ftR = new TFishFace(false, false, 0);
    ftS = new TFishFace(false, false, 0);
    no AnalogScan, no Slave, with default PollInterval
  • Connection to the interfaces :
    ftR->OpenInterface("COM2"), false);
    ftS->OPenInterface("COM1", false);
    no interrupt by Application.ProcessMessages();
    using a try - catch construct to detect open errors. Some more are useful, but you always will hear it, if it crashes.
  • Create the threads for Rob/Stamp
    RobThread = new TRobThread(true);
    StanzThread = new TStanzThread(true);
    without starting them
  • Thread end functions
    RobThread->OnTerminate = RobEnde;
    StanzThread->OnTerminate = StanzEnde;
  • Starting the threads
    RobThread->Suspended = false;
    StanzThread->Suspended = false;

TRobThread::Execute   

Working method of the threads.

  • Drive to Home Position
    ftR->SetMotor(mSaule, ftiLinks, 15, 999);
    ftR->SetMotor(mArmV, ftiLinks, 15, 999);
    ftR->SetMotor(mArmH, ftiLinks, 15, 999);
    ftR->SetMotor(mGreifer, ftiLinks, 15, 999);
    ftR->WaitForMotors(0, mSaule, mArmV, mArmH, mGreifer);
    The four motors of the robot are started simultanously with full speed (15) to run for 999 impulses or until reaching the end switch. The real maximum of impulses may be 200, that means end switch will come first in any case. WaitForMotors is waiting for all end switches are true.
  • Processing loop
    do {} while(!Terminated)
    runs until the thread geht from outside the thread an end request.
  • Inside the processing loop
    frmThreadMain->lblStatus->Caption = "...."
    Anzeige des aktuellen Status
    ftR->SetMotor(...
    ftR->SetMotor(...
    ft->WaitForMotors(0, ...);
    Processing the single steps : fetch the piece, go to deposit place on the feeder.
  • Wait for StanzeReady
    while(!StanzeReady-WaitFor(100) == wrSignaled);
    Deposit the piece on the feeder after signal
    then signal RobReady.
    RobReady->SetEvent();
    Back to the store without a stop.
  • If receiving a Terminate-Request
    Drive to a resting positition.

TStanzThread::Execute

Working method of the threads, similar to TRobThread.

  • Drive to Home Position
  • Processing loop
  • Inside the processing loop
    At the beginning Wait for RobReady and there after signal StanzeReady
  • Continue the processing loop
  • After getting a Terminate request
    Clear the lamps.

TfrmThreadMain::cmdEndeClick

An artificial ending the stamp processing :

  • Terminate request to robot
    RobThread-Terminate();
    StanzeReady-SetEvent();
    RobThread-WaitFor();
    The terminate request is not noticed by the thread, because the thread is waiting for StanzeReady. That is done by setting a special StanzeReady. After that the RobThread will end the thread normaly.
  • Terminate request to Stamp
    analog robot
    remember : one piece is requested for 
    ftS->WaitForLow(ePhotoV);
    ftS->Pause(1234);
  • Cleaning

Notes

Using threads : umFish30.DLL see above. VCL (Windows GUI) mostly, sometimes it is better to use  Synchronize(.....); or the use of special components (e.g. TThreadListe ...).

Global Variables : frmThreadMain, ftR, ftS are some and very nice to use and therefore nothing for OOP purists. frmThreadMain comes from VCL, but ftR, ftS can be an parameter for the thread constructor.

Last Update : 10.08.2004