Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
364 f9daq 1
// C++ Example for Thorlabs LTS stages;
2
// 2023-09-06; 
3
// 2023-09-06;
4
// C++20;
5
// Kinesis 1.14.37;
6
// ==================
7
// This example uses the Kinesis C++ API to move and control Thorlabs integrated stepper motor, LTS, K10CR1, and MLJ stages.
8
 
9
 
10
//CVI Add dll path to: Options>Environment>Include Path> C:\Program Files\Thorlabs\Kinesis
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <stdbool.h>
14
#include <windows.h> 
15
#include <utility.h>
16
 
17
 
18
#include "ThorlabsMotionControlIntegratedStepperMotors.h"
19
 
20
 
21
void waitMove(char serialNo[]);
22
void waitHome(char serialNo[]);
23
char serialNumbers[][9] = {"45388004", "45387934","45388034"};  
24
 
25
// Set number of microsteps, found in APT communications protocol. Should be changed to 136533 if using K10CR1
26
int conversionRate = 409600;
27
 
28
void LTS_Home(int i){
29
 
30
//Set up homing parameters. 
31
                MOT_HomingParameters homingParams;
32
                ISC_GetHomingParamsBlock(serialNumbers[i], &homingParams);
33
                homingParams.direction = MOT_Reverse;
34
                ISC_SetHomingParamsBlock(serialNumbers[i], &homingParams);
35
 
36
                //Clear existing messages in the hardware buffer.
37
                ISC_ClearMessageQueue(serialNumbers[i]);
38
 
39
                //Home the stage and wait for the return message before continuing.
40
                ISC_Home(serialNumbers[i]);
41
                printf( "Homing...\n");
42
                waitHome(serialNumbers[i]);
43
                printf( "Homed...\n");
44
}
45
 
46
int LTS_Init(int i){
47
 
48
  int errorReturn = ISC_Open(serialNumbers[i]);
49
  Sleep(1000);
50
  if (errorReturn == 0){
51
          printf( "Device %d (%s) Connected...\n",i, serialNumbers[i] );
52
          //Settings are loaded based on Stage Name. The integrated stepper class holds LTS and K10CR1 Information.
53
          ISC_StartPolling(serialNumbers[i], 50);
54
          Sleep(1000);
55
  } else {
56
          printf( "Error connecting device %d (%s)\n",i, serialNumbers[i] );  
57
  }
58
  return errorReturn;
59
}
60
 
61
void LTS_Close(int i){
62
//Close the stage
63
        ISC_StopPolling(serialNumbers[i]);
64
        ISC_Close(serialNumbers[i]);
65
        printf( "Device Disconnected...\n");
66
}
67
 
68
void LTS_MoveAbsolute(int i, int position){
69
        ISC_SetMoveAbsolutePosition(serialNumbers[i], conversionRate * position);
70
    ISC_MoveAbsolute(serialNumbers[i]);
71
 
72
}
73
 
74
void LTS_MoveRelative(int i, int distance){
75
        ISC_SetMoveRelativeDistance(serialNumbers[i], distance);
76
    ISC_MoveRelativeDistance(serialNumbers[i]);
77
}
78
 
79
 
80
int LTS_GetPosition(int i){
81
  return ISC_GetPosition(serialNumbers[i]);
82
}
83
 
84
 
85
//Waits should only be used for Home commands. The home command has a different status return.
86
void waitHome(char serialNo[])// Waits until a single axis is homed.
87
{
88
        WORD messageType;
89
        WORD messageId;
90
        DWORD messageData;
91
        ISC_WaitForMessage(serialNo, &messageType, &messageId, &messageData);
92
        while (messageType != 2 || messageId != 0)
93
        {
94
                ISC_WaitForMessage(serialNo, &messageType, &messageId, &messageData);
95
                printf( "pos: %d\n" ,     ISC_GetPosition(serialNo) );
96
 
97
        }
98
}
99
 
100
void waitMove(char serialNo[])// Waits until axis is stopped.
101
{
102
        WORD messageType;
103
        WORD messageId;
104
        DWORD messageData;
105
        ISC_WaitForMessage(serialNo, &messageType, &messageId, &messageData);
106
        while (messageType != 2 || messageId != 1)
107
        {
108
                ISC_WaitForMessage(serialNo, &messageType, &messageId, &messageData);
109
                printf( "pos: %d\n",      ISC_GetPosition(serialNo) );      
110
        }
111
}
112
 
113
 
114
 
115
#ifdef THORLABS_MAIN
116
 
117
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
118
                       LPSTR lpszCmdLine, int nCmdShow) {
119
  int i,j;
120
  int nlines,atline;
121
  int evcontrl,evpanel,p1_h,ierr,type;
122
  int rstat,n1,n2, n3, nid;
123
  int comled;
124
  int lid[16]= {P1_L1,P1_L2,P1_L3,P1_L4,P1_L5,P1_L6,P1_L7,P1_L8,
125
                P1_L9,P1_L10,P1_L11,P1_L12,P1_L13,P1_L14,P1_L15,P1_L16
126
               };
127
  short int ns2, port;
128
  char tline[80];
129
 
130
  if (InitCVIRTE (hInstance, 0, 0) == 0) return -1; /* out of memory */
131
 
132
//  p1_h = LoadPanel (0,"ThorlabsLTS_ui.uir", P1);
133
  p1_h = BuildP1 (0);
134
  ierr = DisplayPanel (p1_h);
135
  ierr = SetActiveCtrl (p1_h,P1_B1);
136
  ierr = SetActiveCtrl (p1_h,P1_B2);
137
  ierr = SetActiveCtrl (p1_h,P1_B3);
138
 
139
  while (1) {
140
    ierr = GetUserEvent (1, &evpanel, &evcontrl);
141
    if (evcontrl == P1_B1) break;
142
    GetCtrlVal (p1_h, P1_COMLED, &comled);
143
    if (!comled) {
144
      GetCtrlVal (p1_h, P1_PORT, &port);
145
      if (MIKRO_Open (port)) continue;
146
      MIKRO_Init(1,0);
147
      MIKRO_Init(2,0);
148
      MIKRO_Init(3,0);
149
      SetCtrlVal (p1_h, P1_COMLED, 1);
150
    }
151
    switch (evcontrl) {
152
      case P1_BL:
153
        MoveRelative(0,1000);
154
        break;
155
      case P1_BR:
156
        MoveRelative(1,1000);
157
        break;
158
      case P1_BU:
159
        MoveRelative(2,1000);
160
        break;
161
      case P1_BD:
162
        MoveRelative(0,-1000);
163
        break;
164
      case P1_BF:
165
        MoveRelative(1,-1000);
166
        break;
167
      case P1_BB:
168
        MoveRelative(2,-1000);
169
        break;
170
      case P1_HO:
171
        ierr = GetCtrlVal (p1_h, P1_N3, &nid);
172
        LTS_Home(nid);
173
        break;
174
      case P1_GX:
175
        GetCtrlVal (p1_h, P1_XG, &n2);
176
                LTS_MoveAbsolute(0,n2);
177
        break;
178
      case P1_GY:
179
        GetCtrlVal (p1_h, P1_YG, &n2);
180
        LTS_MoveAbsolute(1,n2);
181
        break;
182
      case P1_GZ:
183
        GetCtrlVal (p1_h, P1_ZG, &n3);
184
        LTS_MoveAbsolute(2,n2);
185
        break;
186
      case P1_G:
187
        GetCtrlVal (p1_h, P1_XG, &n2);
188
        LTS_MoveAbsolute(0,n2);
189
        GetCtrlVal (p1_h, P1_YG, &n2);
190
        LTS_MoveAbsolute(1,n2);
191
        GetCtrlVal (p1_h, P1_ZG, &n3);
192
        LTS_MoveAbsolute(2,n3);
193
      case P1_B2:
194
      case P1_S1:
195
                  /*
196
        ierr = GetCtrlVal (p1_h, P1_S1, MIKRO_Send);
197
        nout = strlen (MIKRO_Send);
198
        MIKRO_Send[nout++]=13;
199
        rstat = FlushInQ (MIKRO_Port);
200
        rstat = ComWrt (MIKRO_Port, MIKRO_Send, nout);
201
        if ((nin = ComRdTerm (MIKRO_Port, MIKRO_Receive, 30, 0xa))!=0)
202
          nin = ComRdTerm (MIKRO_Port, MIKRO_Receive, 30, 0xa);
203
        if (nin!=0) nin--;
204
        MIKRO_Receive[nin]=0;
205
        switch (nin) {
206
          case 0:
207
            ierr = SetCtrlVal (p1_h, P1_N2, 0);
208
            break;
209
          case 9:
210
            nout = sscanf (MIKRO_Receive, "%x %hx", &n1,&ns2);
211
            ierr = SetCtrlVal (p1_h, P1_N2, ns2);
212
            break;
213
          case 13:
214
            nout = sscanf (MIKRO_Receive, "%x %x", &n1,&n2);
215
            ierr = SetCtrlVal (p1_h, P1_N2, n2);
216
            break;
217
          default:
218
            break;
219
        }
220
        ierr = SetCtrlVal (p1_h, P1_N1, nin);
221
        ierr = SetCtrlVal (p1_h, P1_S2, MIKRO_Receive);
222
                  */
223
        break;
224
      case P1_B3: // reset
225
                /*
226
                  ierr = GetCtrlVal (p1_h, P1_N3, &nid);
227
        ierr = GetCtrlVal (p1_h, P1_STAGETYPE, &type);
228
        MIKRO_Reset(nid);
229
        rstat = GetNumTextBoxLines (p1_h, P1_T1, &atline);
230
        rstat = InsertTextBoxLine (p1_h, P1_T1, atline, MIKRO_Receive);
231
        MIKRO_Init(nid, type);
232
                */
233
        break;
234
      case P1_B4: // status
235
                  /*
236
        ierr = GetCtrlVal (p1_h, P1_N3, &nid);
237
        n2=MIKRO_GetStat(nid);
238
        ierr = SetCtrlVal (p1_h, P1_N2, n2);
239
        ierr = SetCtrlVal (p1_h, P1_N1, nin);
240
        ierr = SetCtrlVal (p1_h, P1_S2, MIKRO_Receive);
241
        for (i=0; i<16; i++) {
242
          ierr = SetCtrlVal (p1_h, lid[i], n2 & 1);
243
          n2>>=1;
244
        }
245
                  */
246
        break;
247
      case P1_EN:
248
                  /*
249
        ierr = GetCtrlVal (p1_h, P1_N3, &nid);
250
        MIKRO_Cmd(nid,"en");
251
                  */
252
        break;
253
      default:
254
        break;
255
    }
256
    Delay(0.1);
257
    n2 = LTS_GetPosition(0);
258
 
259
    SetCtrlVal (p1_h, P1_XP, n2);
260
    n2 = LTS_GetPosition(1);
261
    SetCtrlVal (p1_h, P1_YP, n2);
262
    n2 = LTS_GetPosition(2);
263
    SetCtrlVal (p1_h, P1_ZP, n2);
264
  }
265
 
266
  GetCtrlVal (p1_h, P1_COMLED, &comled);
267
  if (comled) for (int i=0;i<3;i++) LTS_Close (i);
268
 
269
  return 0;
270
}
271
 
272
 
273
#else
274
// *************************************
275
 
276
int main(int argc, char ** argv)
277
{
278
        //Sets up simulations. Comment in if running on physical hardware. 
279
        //TLI_InitializeSimulations();
280
 
281
        //Input serial number. Change for your specific device. 
282
        //serials = ["45388004", "45387934","45388034"] 
283
 
284
 
285
        //Build Device List and open device.
286
        TLI_BuildDeviceList();
287
        TLI_DeviceInfo info;
288
        int err=0;
289
        for (int i=0;i<3;i++){
290
          TLI_GetDeviceInfo(serialNumbers[i], &info);
291
          printf("%s\n", info.description);
292
          Sleep(1000);
293
          err |= LTS_Init(i);
294
        }  
295
 
296
 
297
        for (int i=0;i<3;i++){
298
 
299
                //Move the stage and wait for the return message before continuing.
300
                for (int k=0;k<10;k++){
301
                  LTS_MoveAbsolute(i,k);
302
                  printf( "Moving ...\n");
303
                  waitMove(serialNumbers[i]);
304
                  printf( "Move Complete...\n");
305
                }
306
 
307
 
308
        }
309
 
310
        for (int i=0;i<3;i++){
311
           LTS_Close(i);
312
        }
313
 
314
        //Closes simulations. Comment if running on physical hardware. 
315
        //TLI_UninitializeSimulations();
316
        Delay(10);
317
        return 0;
318
}
319
#endif