Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
284 f9daq 1
unit ST_function;
2
 
3
interface
4
uses Windows, SysUtils{, Dialogs};
5
 
6
const
7
  ErrNotLoadDLL=32768;
8
type
9
 
10
  TUSMC_Devices = packed record
11
                NOD: DWORD;
12
                Serial: Pointer; //array of PChar;
13
                Version: Pointer; //array of PChar;
14
        end; {USMC_Devices}
15
 
16
  TUSMC_Parameters =record
17
    AccelT : Single;       // 'Acceleration time (in ms)
18
    DecelT : Single;       // 'Deceleration time (in ms)
19
    PTimeout : Single;     //'Time (in ms) after which current will be reduced to 60% of normal
20
    BTimeout1 : Single;    //'Time (in ms) after which speed of step motor rotation will be equal to the one specified at
21
                           // 'BTO1P field (see below). (This parameter is used when controlling step motor using buttons)
22
    BTimeout2 : Single;   //'
23
    BTimeout3 : Single;   //  '
24
    BTimeout4 : Single;   // '
25
    BTimeoutR : Single;   //  'Time (in ms) after which reset command will be performed
26
    BTimeoutD : Single;   //  'This field is reserved for future use
27
    MinP : Single ;       //  'Speed (steps/sec) while performing reset operation. (This parameter is used when controlling
28
                          //  'step motor using buttons)
29
    BTO1P : Single;       // 'Speed (steps/sec) after BTIMEOUT 1 time have p:sed. (This parameter is used when controlling
30
                          //  'step motor using buttons)
31
    BTO2P : Single;       //  '
32
    BTO3P : Single ;      //  '
33
    BTO4P : Single ;      //  '
34
    MaxLoft : WORD;       //  'Value in full steps that will be used performing backl:h operation
35
    StartPos : DWORD;     // 'This field is reserved for future use
36
    RTDelta : WORD;    //  'Revolution distance – number of full steps per one full revolution
37
    RTMinError : WORD;    // 'Number of full steps missed to raise the error flag
38
    MaxTemp : Single;      // 'Maximum allowed temperature (Celsius)
39
    SynOUTP : Byte;       // 'Duration of the output synchronization pulse
40
    LoftPeriod : Single;  // 'Speed of the last phase of the backlash operation.
41
    EncMult: Single;      // Encoder step multiplier. Should be <Encoder Steps per Revolution>
42
                          //  / <SM Steps per Revolution> and should be integer multiplied by 0.25
43
    Reserved: array [1..16] of Byte;    // <Unused> File padding
44
  end; {USMC_Parameters}
45
 
46
  TUSMC_StartParameters = record
47
    SDivisor: byte;    //'Step is divided by this factor (1,2,4,8)
48
    DefDir:  LongBool;    //  'Direction for backlash operation (relative)
49
    LoftEn:  LongBool;    //  'Enable automatic backlash operation (works if slow start/stop mode is off)
50
    SlStart:  LongBool;   //  'If TRUE slow start/stop mode enabled.
51
    WSyncIN:  LongBool;   //  'If TRUE controller will wait for input synchronization signal to start
52
    SyncOUTR:  LongBool;  //  'If TRUE output synchronization counter will be reset
53
    ForceLoft:  LongBool; // 'If TRUE and destination position is equal to the current position backlash operation will be performed.
54
    Reserved: array [1..4] of Byte;  // <Unused> File padding
55
  end; {USMC_StartParameters}
56
 
57
  TUSMC_Mode = record
58
    PMode : LongBool;      // 'Turn off buttons (TRUE - buttons disabled)
59
    PReg : LongBool;       // 'Current reduction regime (TRUE - regime is on)
60
    ResetD : LongBool;     // 'Turn power off and make a whole step (TRUE - apply)
61
    EmReset : LongBool;    // 'Quick power off
62
    Tr1T : LongBool;       // 'Trailer 1 TRUE state (TRUE : +3/+5?; FALSE : 0?)
63
    Tr2T : LongBool;       // 'Trailer 2 TRUE state (TRUE : +3/+5?; FALSE : 0?)
64
    RotTrT : LongBool;     // 'Rotary Transducer TRUE state (TRUE : +3/+5?; FALSE : 0?)
65
    TrSwap : LongBool;     // 'If TRUE, Trailers are treated to be swapped
66
    Tr1En : LongBool;      // 'If TRUE Trailer 1 Operation Enabled
67
    Tr2En : LongBool;      // 'If TRUE Trailer 2 Operation Enabled
68
    RotTeEn : LongBool;    // 'If TRUE Rotary Transducer Operation Enabled
69
    RotTrOp : LongBool;    // 'Rotary Transducer Operation Select (stop on error for TRUE)
70
    Butt1T : LongBool;     // 'Button 1 TRUE state (TRUE : +3/+5?; FALSE : 0?)
71
    Butt2T : LongBool;     // 'Button 2 TRUE state (TRUE : +3/+5?; FALSE : 0?)
72
    ResetRT : LongBool;    // 'Reset Rotary Transducer Check Positions (need one full revolution before it can detect error)
73
    SyncOUTEn : LongBool;  // 'If TRUE output syncronization enabled
74
    SyncOUTR : LongBool;   // 'If TRUE output synchronization counter will be reset
75
    SyncINOp : LongBool;   // 'Synchronization input mode:
76
                        //'True - Step motor will move one time to the destination position
77
                        //'False - Step motor will move multiple times by steps equal to the value destination position
78
    SyncCount : DWORD; // 'Number of steps after which synchronization output sygnal occures
79
    SyncInvert : LongBool; // 'Set to TRUE to invert output synchronization signal
80
    EncoderEn : LongBool; // Enable Encoder on pins {SYNCIN,ROTTR} - disables Synchronization input and Rotary Transducer
81
    EncoderInv : LongBool; //  Invert Encoder Counter Direction
82
    ResBEnc : LongBool; //  Reset <EncoderPos> and <ECurPos> to 0
83
    ResEnc : LongBool; //  Reset <ECurPos> to <EncoderPos>
84
    Reserved: array [1..8] of Byte;  // <Unused> File padding
85
  end; {USMC_Mode}
86
 
87
  TUSMC_State = record
88
    CurPos: integer;      //  'Current position (in microsteps)
89
    Temp: single;         //  'Current temperature of the driver
90
    SDivisor:  byte;   //  'Step is divided by this factor
91
    Loft:  LongBool;      //  'Indicates backlash status
92
    FullPower:  LongBool; //  'If TRUE then full power.
93
    CW_CCW:  LongBool;    //  'Current direction. Relatively!
94
    Power:  LongBool;     //  'If TRUE then Step Motor is ON.
95
    FullSpeed:  LongBool; //  'If TRUE then full speed. Valid in "Slow Start" mode only.
96
    AReset:  LongBool;    //  'TRUE After Device reset, FALSE after "Set Position".
97
    RUN:  LongBool;       //  'Indicates if step motor is rotating
98
    SyncIN:  LongBool;    //  'Logical state directly from input synchronization PIN
99
    SyncOUT:  LongBool;   //  'Logical state directly from output synchronization PIN
100
    RotTr:  LongBool;     //  'Indicates current rotary transducer press state
101
    RotTrErr:  LongBool;  //  'Indicates rotary transducer error flag
102
    EmReset:  LongBool;   //  'Indicates state of emergency disable button (local control)
103
    Trailer1:  LongBool;  //  'Indicates trailer 1 logical state.
104
    Trailer2:  LongBool;  //  'Indicates trailer 2 logical state.
105
    Voltage:  Single;     //  'Input power source voltage (6-39V) -=24 version 0nly=-
106
    Reserved: array [1..8] of Byte;  // <Unused> File padding
107
  end; {USMC_State}
108
 
109
        TUSMC_EncoderState = record
110
    EncoderPos : integer;  // Current position measured by encoder
111
    ECurPos : integer;  // Current position (in Encoder Steps) - Synchronized with request call
112
    Reserved: array [1..8] of Byte;  // <Unused> File padding
113
  end;  {USMC_EncoderState}
114
 
115
  D_USMC_Init = function (out USMC_Devices: TUSMC_Devices) : Longword; cdecl;
116
  D_USMC_GetState = function (Device: Longword;     // IN - Device number
117
                                                                            out Str: TUSMC_State  // OUT - Structure representing divice state
118
                                                                            ) : Longword ; cdecl;
119
  D_USMC_SaveParametersToFlash = function (Device: Longword         // IN - Device number
120
                                                                            ) : Longword ; cdecl;
121
  D_USMC_GetMode = function ( Device: Longword;                             // IN - Device number
122
                                                                      out Str: TUSMC_Mode                                 // OUT - Structure representing some of divice parameters
123
                                                                        ): Longword ; cdecl;
124
  D_USMC_SetMode= function ( Device: Longword;                              // IN - Device number
125
                                                                      var Str: TUSMC_Mode                                       // IN/OUT Structure representing some of divice parameters
126
                                                                      ): Longword ; cdecl;
127
  D_USMC_GetParameters = function ( Device: Longword;                   // IN - Device number
128
                                                                                  out Str: TUSMC_Parameters       // OUT - Structure representing some of divice parameters
129
                                                                                  ) : Longword ; cdecl;
130
  D_USMC_SetParameters = function ( Device: Longword;                   // IN - Device number
131
                                                                                  var Str: TUSMC_Parameters       // IN/OUT Structure representing some of divice parameters
132
                                                                                   ): Longword ; cdecl;
133
  D_USMC_GetStartParameters = function ( Device: Longword;          // IN - Device number
134
                            out Str: TUSMC_StartParameters      // OUT - Structure representing start function parameters
135
                            ) : Longword ; cdecl;
136
  D_USMC_Start = function ( Device: Longword;                                 // IN - Device number
137
                                                                      DestPos: integer;                                 // IN - Destination position
138
                            var Speed: single;                                  // IN/OUT - Speed of rotation
139
                            out Str: TUSMC_StartParameters      // IN/OUT - Structure representing start function parameters
140
                                                                      ) : Longword ; cdecl;
141
  D_USMC_Stop = function ( Device: Longword                                           // IN - Device number
142
                                                                      ) : Longword ; cdecl;
143
  D_USMC_SetCurrentPosition = function ( Device: Longword;                                    // IN - Device number
144
                                                                      Position: integer                                 // IN - New current position in 1/8 steps (5 least significant bits are ignored)
145
                            ) : Longword ; cdecl;
146
  D_USMC_GetEncoderState =  function ( Device: Longword;                        // IN - Device number
147
                                                                                  var Str: TUSMC_EncoderState     //IN/OUT Structure containing encoder state
148
                                                                                  ) : Longword ; cdecl;
149
  D_USMC_GetLastErr = procedure (ErrString: PChar; Size: Integer) cdecl;
150
  D_USMC_Close = function : Longword ; cdecl;
151
 
152
        TStandaClass = class(TObject)
153
    protected
154
        HandleStandaDLL: THandle;
155
                public
156
                        bDllLoadOK: Boolean;
157
      USMC_Init: D_USMC_Init;
158
        USMC_GetState: D_USMC_GetState;
159
      USMC_SaveParametersToFlash: D_USMC_SaveParametersToFlash;
160
        USMC_GetMode: D_USMC_GetMode;
161
        USMC_SetMode: D_USMC_SetMode;
162
        USMC_GetParameters: D_USMC_GetParameters;
163
        USMC_SetParameters: D_USMC_SetParameters;
164
        USMC_GetStartParameters: D_USMC_GetStartParameters;
165
        USMC_Start: D_USMC_Start;
166
        USMC_Stop: D_USMC_Stop;
167
      USMC_SetCurrentPosition: D_USMC_SetCurrentPosition;
168
      USMC_GetEncoderState: D_USMC_GetEncoderState;
169
        USMC_GetLastErr: D_USMC_GetLastErr;
170
      USMC_Close: D_USMC_Close;
171
                        function LoadLibAndFuncs : Integer;
172
                        function UnLoadLibAndFuncs : Integer;
173
        constructor Create;
174
                        destructor Destroy; override;
175
    end;
176
(******************************************************************************)
177
 
178
implementation
179
 
180
constructor TStandaClass.Create;
181
var
182
        iError: Integer;
183
begin
184
        inherited Create;
185
        iError := LoadLibAndFuncs;
186
        if iError <> 0 then
187
        begin
188
                bDllLoadOK := false;
189
    if iError=ErrNotLoadDLL then        //ShowMessage('TStandaClass.Create: Error "can`t Load DLL" ')
190
    else ;//ShowMessage('TStandaClass.Create: Error ''' + IntToStr(iError) + ''' from "LoadLibAndFuncs"');
191
        end else
192
        begin
193
                bDllLoadOK := true;
194
        end;
195
end;
196
 
197
destructor TStandaClass.Destroy;
198
var
199
        iError: Integer;
200
begin
201
        iError := UnLoadLibAndFuncs;
202
        if iError <> 0 then
203
        begin
204
//              ShowMessage('TStandaClass.Destroy: Error from "UnLoadLibAndFuncs"');
205
        end;
206
        inherited Destroy;
207
end;
208
 
209
 
210
function TStandaClass.LoadLibAndFuncs : Integer;
211
begin
212
        Result := 0;
213
 
214
        try
215
                HandleStandaDLL := LoadLibrary(pChar('USMCDLL.dll'));
216
 
217
                if HandleStandaDLL <> 0 then
218
                begin
219
                        @USMC_Init := GetProcAddress(HandleStandaDLL, 'USMC_Init');
220
                        @USMC_GetLastErr := GetProcAddress(HandleStandaDLL, 'USMC_GetLastErr');
221
                        @USMC_GetState := GetProcAddress(HandleStandaDLL, 'USMC_GetState');
222
                        @USMC_GetStartParameters := GetProcAddress(HandleStandaDLL, 'USMC_GetStartParameters');
223
                        @USMC_Start := GetProcAddress(HandleStandaDLL, 'USMC_Start');
224
                        @USMC_Stop := GetProcAddress(HandleStandaDLL, 'USMC_Stop');
225
                        @USMC_GetParameters := GetProcAddress(HandleStandaDLL, 'USMC_GetParameters');
226
                        @USMC_SetParameters := GetProcAddress(HandleStandaDLL, 'USMC_SetParameters');
227
                        @USMC_GetMode := GetProcAddress(HandleStandaDLL, 'USMC_GetMode');
228
                        @USMC_SetMode := GetProcAddress(HandleStandaDLL, 'USMC_SetMode');
229
      @USMC_SaveParametersToFlash := GetProcAddress(HandleStandaDLL, 'USMC_SaveParametersToFlash');
230
      @USMC_SetCurrentPosition := GetProcAddress(HandleStandaDLL, 'USMC_SetCurrentPosition');
231
      @USMC_GetEncoderState := GetProcAddress(HandleStandaDLL, 'USMC_GetEncoderState');
232
      @USMC_Close := GetProcAddress(HandleStandaDLL, 'USMC_Close');
233
 
234
                        if @USMC_Init = nil then Result := Result + 1;
235
                        if @USMC_GetLastErr = nil then  Result := Result + 2;
236
                        if @USMC_GetState = nil then Result := Result + 4;
237
                        if @USMC_GetStartParameters = nil then  Result := Result + 8;
238
                        if @USMC_Start = nil then       Result := Result + 16;
239
                        if @USMC_Stop = nil then        Result := Result + 32;
240
                        if @USMC_GetParameters = nil then       Result := Result + 64;
241
                        if @USMC_SetParameters = nil then       Result := Result + 128;
242
                        if @USMC_GetMode = nil then     Result := Result + 256;
243
                        if @USMC_SetMode = nil then     Result := Result + 512;
244
      if @USMC_SaveParametersToFlash = nil then Result := Result + 1024;
245
      if @USMC_SetCurrentPosition = nil then    Result := Result + 2048;
246
      if @USMC_GetEncoderState = nil then       Result := Result + 4096;
247
      if @USMC_Close = nil then Result := Result + 8192;
248
 
249
                end else
250
                begin
251
                        Result := ErrNotLoadDLL;
252
                end;
253
        except
254
                on EExternalException do
255
                        Result := Result + 100000;
256
                else
257
                        Result := Result + 200000;      { unexpected }
258
        end;
259
end; {LoadLibAndFuncs}
260
 
261
function TStandaClass.UnLoadLibAndFuncs : Integer;
262
var
263
        bOK : Boolean;
264
begin
265
        Result := 0;
266
 
267
        if HandleStandaDLL <> 0 then
268
        begin
269
                bOK := FreeLibrary(HandleStandaDLL);
270
                if bOK = true then
271
                begin
272
                        HandleStandaDLL := 0;
273
 
274
                        USMC_Init := nil;
275
                        USMC_GetLastErr := nil;
276
                        USMC_GetState := nil;
277
                        USMC_GetStartParameters := nil;
278
                        USMC_Start := nil;
279
                        USMC_Stop := nil;
280
                        USMC_GetParameters := nil;
281
                        USMC_SetParameters := nil;
282
                        USMC_GetMode := nil;
283
                        USMC_SetMode := nil;
284
      @USMC_SaveParametersToFlash := nil;
285
      @USMC_SetCurrentPosition := nil;
286
      @USMC_GetEncoderState := nil;
287
      @USMC_Close := nil;
288
                end else
289
                begin
290
                        // further: exact info by 'GetLastError()'
291
                        Result := 1;
292
                end; {if bOK = true then}
293
        end; {if HandleStandaDLL <> 0 then}
294
end; {UnLoadLibAndFuncs}
295
 
296
 
297
end.