指令流激光能量跟随比例系数模式

指令流激光能量跟随比例系数模式例程
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// LaserProDuty.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

// 该例程仅用于功能演示,请保证安全的情况下使用

// 测试功能:激光能量跟对功能示例,比例系数模式,输出能量为pwm的占空比
// 测试平台:网络型运动控制器
// 测试环境:Windows
// 测试流程:
//           (1)初始化控制器
//           (2)Group初始化
//           (3)压指令流数据
//           (4)启动指令流,等待指令流执行完成
//           (5)执行完成,关闭控制器
// 注意事项:
//           (1)本例程使用的“例程专用.xml”、“例程专用.cfg”,仅用于本例程
//           (2)实际使用时,需要使用MotionStudio生成网络配置xml
//           (3)实际使用时,必须确认网络上接了支持激光功能的从站!!!

// 加载固高运动控制库头文件
#include "gxn.h"
// 动态加载固高运动控制gxn.lib库
#pragma comment(lib,"gxn.lib")

/**
 * @brief 指令出错打印函数
 * @param command 打印信息字符串
 * @param error 错误码
 * @return 错误码
*/
short CommandHandler(char* command, short error)
{
    printf("%s = %d\n", command, error);
    getchar();

    return error;
}

/**
 * @brief 初始化运动控制器(开卡 + 初始化网络拓扑 + 初始化轴)
 * @param core 需要初始化的核号,从1开始
 * @param axis 需要初始化的轴起始索引,从1开始
 * @param axisCount 需要初始化的轴数量,从起始索引axis开始计数,必须大于0
 * @return 0表示初始化成功,非0表示初始化失败
*/
short InitMc(short core,short axis,short axisCount)
{
    short rtn;
    short overTime;
    long status;

    // 打开运动控制器
    rtn = GTN_OpenCard(CHANNEL_PCIE,NULL,NULL);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_OpenCard",rtn);
    }
    printf("Open Card Success !\n");

    // 初始化网络
    // 注意:(1)“例程专用.xml”仅用于本例程
    //       (2)实际使用时,需要使用MotionStudio生成对应的网络配置文件
    // overTime:网络初始化超时时间,单位:秒
    overTime = 120;
    rtn = GTN_NetInit(NET_INIT_MODE_XML_STRICT,"例程专用.xml",overTime,&status);
    if ( 0 != rtn )
    {
        printf("status = %d\n",status);
        return CommandHandler("GTN_NetInit",rtn);
    }
    printf("Init Net Success !\n");

    // 加载配置文件到控制器
    // 注意:(1)“例程专用.cfg”仅用于本例程
    //       (2)实际使用时,需要使用MotionStudio生成对应的配置文件
    rtn = GTN_LoadConfig(core,"例程专用.cfg");
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_LoadConfig(\"例程专用.cfg\")",rtn);
    }

    // 清除轴状态
    rtn = GTN_ClrSts(core,axis,axisCount);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_ClrSts",rtn);
    }
    printf("Init Mc Config Success !\n");

    return rtn;
}

/**
 * @brief 初始化一个三轴正交模型的group
 * @param core 需要初始化的核号,从1开始
 * @param axis 需要初始化的轴起始索引,从1开始
 * @param axisCount 需要初始化的轴数量,从起始索引axis开始计数,必须大于0
 * @param group 需要初始化的group号,从1开始
 * @return 0表示初始化成功,非0表示初始化失败
*/
short InitGroup(short core,short axis,short axisCount,short group)
{
    short rtn;
    short i;
    TScaleParameter scaleParameter;
    memset(&scaleParameter,0,sizeof(scaleParameter));
    TAxisMotionConstraint axisMotionConstraint;
    memset(&axisMotionConstraint,0,sizeof(axisMotionConstraint));

    // 设置轴当量
    scaleParameter.count = 1;
    scaleParameter.alpha[0] = 1;
    scaleParameter.beta[0] = 1000;

    // 设置轴运动约束
    axisMotionConstraint.velMax = 2000;
    axisMotionConstraint.accMax = 5000;
    axisMotionConstraint.decMax = 5000;
    axisMotionConstraint.jerkMax = 1000000;
    axisMotionConstraint.dvMax = 10;
    axisMotionConstraint.reverseLimitMode = 0;

    for (i=axis;i<(axis+axisCount);++i)
    {
        rtn = GTN_SetScaleParameter(core,MC_PROFILE,i,&scaleParameter,0);
        if ( 0 != rtn )
        {
            return CommandHandler("GTN_SetScaleParameter",rtn);
        }

        rtn = GTN_SetAxisMotionConstraint(core,i,&axisMotionConstraint,0);
        if ( 0 != rtn )
        {
            return CommandHandler("GTN_SetAxisMotionConstraint",rtn);
        }
    }

    TGroupMotionConstraint groupMotionConstraint;
    memset(&groupMotionConstraint,0,sizeof(groupMotionConstraint));
    TVelProfileMode velProfileMode;
    memset(&velProfileMode,0,sizeof(velProfileMode));
    TGroupStopParameter stopPrm;
    memset(&stopPrm,0,sizeof(stopPrm));
    TGroupStopParameter abruptStopPrm;
    memset(&abruptStopPrm,0,sizeof(abruptStopPrm));
    TKinematicTransform kinematicTransform;
    memset(&kinematicTransform,0,sizeof(TKinematicTransform));

    // 设置group运动约束
    groupMotionConstraint.velMax = 500;
    groupMotionConstraint.accMax = 1000;
    groupMotionConstraint.decMax = 1000;

    // 设置group速度规划模式
    velProfileMode.mode = VEL_PROFILE_MODE_SMOOTH;
    velProfileMode.parameter.smooth.accTime = 40;
    velProfileMode.parameter.smooth.k = 0;

    // 设置group平滑停止参数
    stopPrm.deceleration = 2000;
    stopPrm.jerk = 4000;

    // 设置group紧急停止参数
    abruptStopPrm.deceleration = 2000;
    abruptStopPrm.jerk = 5000;

    // 设置group运动学模型
    kinematicTransform.type = KIN_TYPE_ORTHOGONAL;

    // 建立三维正交模型group
    for (i=1;i<=3;++i)
    {
        rtn = GTN_AddAxisToGroup(core,group,i+axis,i);
        if ( 0 != rtn )
        {
            return CommandHandler("GTN_AddAxisToGroup",rtn);
        }
    }

    rtn = GTN_SetGroupMotionConstraint(core,group,&groupMotionConstraint);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetGroupMotionConstraint",rtn);
    }

    rtn = GTN_SetGroupVelProfileMode(core,group,&velProfileMode);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetGroupVelProfileMode",rtn);
    }

    rtn = GTN_SetGroupStopParameter(core,group,0,&stopPrm);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetGroupStopParameter",rtn);
    }

    rtn = GTN_SetGroupStopParameter(core,group,1,&abruptStopPrm);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetGroupStopParameter",rtn);
    }

    rtn = GTN_SetGroupKinematicTransform(core,group,&kinematicTransform,0);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetGroupKinematicTransform",rtn);
    }

    rtn = GTN_GroupEnable(core,group);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_GroupEnable",rtn);
    }
    printf("Init Group Success !\n");

    return rtn;
}

/**
 * @brief 获取指令流执行状态
 * @param core 核号,从1开始
 * @param list 指令流号,从1开始
 * @param group group号,从1开始
 * @param pExecute 获取的指令流执行状态
 * @return 0表示获取成功,非0表示获取失败
*/
short GetListExecuteInfo(short core,short list,short group,short *pExecute)
{
    short rtn;
    TCommandListStatus listStatus;
    TGroupStatus groupStatus;
    double acsPos[8];

    rtn = GTN_GetCommandListStatus(core,list,&listStatus);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_GetCommandListStatus",rtn);
    }

    rtn = GTN_GetGroupStatus(core,group,&groupStatus);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_GetGroupStatus",rtn);
    }

    rtn = GTN_GetGroupProfilePos(core,group,1,acsPos,8,COORD_SYSTEM_ACS);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_GetGroupProfilePos",rtn);
    }

    printf("List: Execute = %d, Remainder = %d, acsPos[0] = %lf, acsPos[1] = %lf\r",
        listStatus.execute,listStatus.remainderSegCount,acsPos[0],acsPos[1]);

    *pExecute = listStatus.execute;

    if ( 0 == listStatus.execute )
    {
        if ( 0 == listStatus.remainderSegCount )
        {
            printf("\nList Execute Success !\n");
        }
        else
        {
            printf("\nList Execute Error !\n");
        }
    }

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    short rtn;                         // 指令返回值
    short core;                        // 需要执行例程的运动控制器核号
    short axis;                        // 需要初始化的轴起始索引号
    short axisCount;                   // 需要初始化的轴数量,从轴起始索引号开始算起

    // 初始化运动控制器
    // 开卡 + 初始化网络拓扑 + 初始化核1的1-8轴
    core = 1;
    axis = 1;
    axisCount = 8;
    rtn = InitMc(core,axis,axisCount);
    if ( 0 != rtn )
    {
        return rtn;
    }

    short group;                       // group号
    short dir[8]={0,0,0,0,0,0,0,0};    // group方向参数
    double pos[8]={0,0,0,0,0,0,0,0};   // group位置参数
    TGroupMoveParameter groupMoveParameter;
    memset(&groupMoveParameter,0,sizeof(groupMoveParameter));

    // 初始化group
    group = 1;
    groupMoveParameter.velocity = 10;
    groupMoveParameter.acceleration = 500;
    groupMoveParameter.deceleration = 500;
    groupMoveParameter.overrideSelect = 0;
    rtn = InitGroup(core,axis,axisCount,group);;
    if ( 0 != rtn )
    {
        return rtn;
    }

    short list;                        // 指令流号
    TListInfo listInfo;
    memset(&listInfo,0,sizeof(listInfo));

    // 初始化指令流
    list = 1;
    listInfo.list = list;
    listInfo.modal = 0;
    listInfo.segNum = 0;
    rtn = GTN_ClearCommandListData(core,list,0);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_ClearCommandListData",rtn);
    }

    rtn = GTN_ClearCommandListStatus(core,list,0);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_ClearCommandListStatus",rtn);
    }

    short laserChannel;                // 激光通道号
    double laserFrequency;             // 激光pwm信号输出频率
    double laserOnDelay;               // 激光开光延时
    double laserOffDelay;              // 激光关光延时
    TLaserPwmPrmPro laserPwmPrmPro;    // 激光pwm信号能量值限制参数
    TLaserFollowPrmPro laserFollowPrmPro;

    laserChannel = 0;
    laserPwmPrmPro.minDuty = 0;
    laserPwmPrmPro.maxDuty = 80;
    laserPwmPrmPro.minFrequency = 0;
    laserPwmPrmPro.maxFrequency = 50;
    laserPwmPrmPro.minPulseWidth = 0;
    laserPwmPrmPro.maxPulseWidth = 20;

    laserFollowPrmPro.powerType = LASER_FOLLOW_POWER_TYPE_DUTY;
    laserFollowPrmPro.group = group;
    laserFollowPrmPro.source = LASER_FOLLOW_SYNCH_VEL_SOURCE_PROFILE;
    laserFollowPrmPro.coordSystem = COORD_SYSTEM_ACS;
    laserFollowPrmPro.ratio = 1.0;
    laserFollowPrmPro.minPower = 5;
    laserFollowPrmPro.maxPower = 90;

    laserOnDelay = 0;
    laserOffDelay = 100;
    laserFrequency = 20;

    // 压指令流数据
    pos[0] = 10;
    pos[1] = 0;
    listInfo.modal = 1;
    ++ listInfo.segNum;
    rtn = GTN_MoveLinearAbsolute(core,group,pos,dir,&groupMoveParameter,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_MoveLinearAbsolute",rtn);
    }

    listInfo.modal = 0;
    ++ listInfo.segNum;
    rtn = GTN_SetLaserPwmPrmPro(core,laserChannel,&laserPwmPrmPro,&listInfo);
    ++ listInfo.segNum;
    rtn += GTN_SetLaserDelayPro(core,laserChannel,laserOnDelay,laserOffDelay,&listInfo);
    ++ listInfo.segNum;
    rtn += GTN_SetLaserPwmFrequencyPro(core,laserChannel,laserFrequency,&listInfo);
    ++ listInfo.segNum;
    rtn += GTN_SetLaserFollowPrmPro(core,laserChannel,&laserFollowPrmPro,&listInfo);
    ++ listInfo.segNum;
    rtn += GTN_SetLaserFollowEnablePro(core,laserChannel,1,&listInfo);
    ++ listInfo.segNum;
    rtn += GTN_SetLaserEnablePro(core,laserChannel,1,LASER_ON_MODE_DEFAULT,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("SetLaserPrm",rtn);
    }

    pos[0] = 20;
    pos[1] = 0;
    listInfo.modal = 1;
    ++ listInfo.segNum;
    rtn = GTN_MoveLinearAbsolute(core,group,pos,dir,&groupMoveParameter,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_MoveLinearAbsolute",rtn);
    }

    pos[0] = 20;
    pos[1] = 10;
    listInfo.modal = 1;
    ++ listInfo.segNum;
    rtn = GTN_MoveLinearAbsolute(core,group,pos,dir,&groupMoveParameter,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_MoveLinearAbsolute",rtn);
    }

    pos[0] = 10;
    pos[1] = 10;
    listInfo.modal = 1;
    ++ listInfo.segNum;
    rtn = GTN_MoveLinearAbsolute(core,group,pos,dir,&groupMoveParameter,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_MoveLinearAbsolute",rtn);
    }

    pos[0] = 10;
    pos[1] = 0;
    listInfo.modal = 1;
    ++ listInfo.segNum;
    rtn = GTN_MoveLinearAbsolute(core,group,pos,dir,&groupMoveParameter,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_MoveLinearAbsolute",rtn);
    }

    listInfo.modal = 0;
    ++ listInfo.segNum;
    rtn = GTN_SetLaserFollowEnablePro(core,laserChannel,0,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetLaserFollowEnablePro",rtn);
    }

    ++ listInfo.segNum;
    rtn = GTN_SetLaserEnablePro(core,laserChannel,0,LASER_OFF_MODE_POWER_OFF,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetLaserEnablePro",rtn);
    }

    pos[0] = 0;
    pos[1] = 0;
    listInfo.modal = 1;
    ++ listInfo.segNum;
    rtn = GTN_MoveLinearAbsolute(core,group,pos,dir,&groupMoveParameter,&listInfo);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_MoveLinearAbsolute",rtn);
    }

    rtn = GTN_CommandListDataEnd(core,list);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_CommandListDataEnd",rtn);
    }
    printf("Push List Data Finish !\n");

    rtn = GTN_StartCommandList(core,listInfo.list);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_StartCommandList",rtn);
    }
    printf("Start List !\n");

    short execute;
    execute = 0;
    do 
    {
        rtn = GetListExecuteInfo(core,listInfo.list,group,&execute);
        if ( 0 != rtn )
        {
            return rtn;
        }
    } while ( 1 == execute );

    // 关闭控制器
    rtn = GTN_Close();
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_Close",rtn);
    }

    printf("Press Any Key To Exit !\n");
    getchar();

    return 0;
}