00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "PhysicalLinearActuator.h"
00015
00016
00017
00018
00019
00020
00021 PhysicalLinearActuator::PhysicalLinearActuator()
00022 {
00023
00024 la.posvel = la.jogvel = 5;
00025 la.jogacc=la.posacc=la.posdec=20;
00026 la.mtt=5000000;
00027 la.portfd= 0;
00028 }
00029
00030 PhysicalLinearActuator::~PhysicalLinearActuator()
00031 {
00032
00033
00034
00035 }
00036
00037
00038
00039
00040
00041 int PhysicalLinearActuator::TravelDistance(double param_distance)
00042 {
00043
00044 char *buffer;
00045 buffer = (char *) malloc(256);
00046 int InchesToSteps=25400;
00047 int DistanceSteps=static_cast<int>(floor(param_distance
00048 *static_cast<double>(InchesToSteps)));
00049 say(la.portfd, "MR11");
00050 if (DistanceSteps < 0)
00051
00052 { sprintf(buffer, "DI%d\rFL\rSS-", -DistanceSteps);
00053 say(la.portfd, buffer);
00054 }
00055
00056 else
00057
00058 { sprintf(buffer, "DI%d\rFL\rSS+", DistanceSteps);
00059 say(la.portfd, buffer);
00060 }
00061 return (0);
00062 }
00063
00064 int PhysicalLinearActuator::TravelPosition(double param_position)
00065 {
00066 char buffer[256];
00067 const int InchesToSteps=25400;
00068
00069 int PositionSteps= static_cast<int>(floor(param_position
00070 *static_cast<double>(InchesToSteps)));
00071 int LowerLimitSteps=static_cast<int>(floor(LowerLimit
00072 *static_cast<double>(InchesToSteps)));
00073 int TravelSteps=static_cast<int>(floor(Travel
00074 *static_cast<double>(InchesToSteps)));
00075 say(la.portfd, "MR11");
00076 sprintf(buffer, "DI%d\rFP", LowerLimitSteps+(TravelSteps/2)+PositionSteps);
00077 say(la.portfd, buffer);
00078
00079 return (0);
00080
00081 }
00082
00083 int PhysicalLinearActuator::SetVelocity(double velocity)
00084 {
00085 if (velocity < .025)
00086 velocity = .025;
00087 char buffer[256];
00088 say(la.portfd, "MR11\r");
00089 sprintf(buffer, "CS%f\r", velocity);
00090
00091 say(la.portfd, buffer);
00092
00093 return (0);
00094 }
00095
00096 int PhysicalLinearActuator::StartJog(int direction)
00097 {
00098 if (direction != 0)
00099 {
00100 char buffer[256];
00101 sprintf(buffer, "DI%d\r", direction);
00102 say(la.portfd, buffer);
00103 say(la.portfd, "CJ\r");
00104 hear(la.portfd,buffer, 80, 0, 300000,1);
00105 return(0);
00106 }
00107 else
00108 {
00109 say(la.portfd, "JE\r");
00110 say(la.portfd, "JS1\r");
00111 return(0);
00112 }
00113 }
00114
00115 int PhysicalLinearActuator::Stop()
00116 {
00117 say(la.portfd, "ST\r");
00118 return (0);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 int PhysicalLinearActuator::Initialize(int action, char *port)
00143 {
00144 char *buffer,
00145 *ptr;
00146 int dir;
00147 int limflag=0;
00148
00149 la.portfn = port;
00150 buffer = (char *)malloc(256);
00151 init_serial(la.portfn, B9600, &(la.portfd));
00152 hear(la.portfd, buffer, 80, 0, 300000, 1);
00153
00154
00155
00156 if (action&LAIT_COMM) {
00157
00158 say(la.portfd, "RS");
00159
00160
00161
00162
00163 buffer[0] = 0;
00164 hear(la.portfd, buffer, 10, 0, 300000, '\r');
00165
00166
00167
00168
00169
00170 switch(buffer[0]) {
00171 case 'M':
00172 case 'W':
00173 case 'T':
00174 close(la.portfd);
00175 return 1;
00176
00177 case 'E':
00178 close(la.portfd);
00179 return 2;
00180
00181 case 'R':
00182 break;
00183
00184 default:
00185 close(la.portfd);
00186 return 3;
00187 }
00188 }
00189
00190 if (action&LAIT_PRGP) {
00191
00192 sprintf(buffer,"VE%d", la.posvel);
00193 say(la.portfd, buffer);
00194 sprintf(buffer,"AC%d\r\nDE%d", la.posacc, la.posdec);
00195 say(la.portfd, buffer);
00196 sprintf(buffer,"JS%d\r\nJA%d", la.jogvel, la.jogacc);
00197 say(la.portfd, buffer);
00198 }
00199
00200
00201 say(la.portfd, "IS");
00202 hear(la.portfd, buffer, 30, 12, 300000, '\r');
00203 int old_position=atoi(buffer);
00204
00205
00206
00207
00208
00209
00210 if (action&LAIT_JPOS) {
00211
00212 say(la.portfd, "IS");
00213 hear(la.portfd, buffer, 30, 12, 300000, '\r');
00214
00215
00216
00217
00218 ptr = strstr(buffer, "IS=");
00219
00220 if (ptr?ptr[4] != '0':1) {
00221 say(la.portfd, "DI1");
00222 say(la.portfd, "CJ");
00223 say(la.portfd, "SS+");
00224 hear(la.portfd, buffer, 5, 1, la.mtt, '\r');
00225 }
00226 }
00227
00228 if (action&LAIT_JNEG) {
00229
00230 say(la.portfd, "IS");
00231 hear(la.portfd, buffer, 30, 12, 300000, '\r');
00232 ptr = strstr(buffer, "IS=");
00233 if (ptr?ptr[3] != '0':1) {
00234 say(la.portfd, "DI-1");
00235 say(la.portfd, "CJ");
00236 say(la.portfd, "SS-");
00237 hear(la.portfd, buffer, 30, 1, la.mtt, '\r');
00238 }
00239 }
00240
00241
00242
00243
00244
00245
00246
00247 if (action&LAIT_CTRA) {
00248
00249 say(la.portfd, "IS");
00250 hear(la.portfd, buffer, 30, 12, 300000, '\r');
00251 ptr = strstr(buffer, "IS=");
00252 if (!ptr) {
00253 close(la.portfd);
00254 return(4);
00255 }
00256 if (ptr[3]=='0') limflag = 1;
00257 if (ptr[4]=='0') limflag = 2;
00258
00259
00260
00261
00262
00263
00264
00265
00266 if (!limflag) {
00267 if ((la.curpos - la.lo_l) <
00268 (la.hi_l - la.curpos)) {
00269 say(la.portfd, "DI-1");
00270 limflag = 1;
00271 } else {
00272 say(la.portfd, "DI1");
00273 limflag = 2;
00274 }
00275 say(la.portfd, "CJ");
00276 say(la.portfd, "SS-");
00277 hear(la.portfd, buffer, 30, 0, la.mtt, '\r');
00278 }
00279
00280
00281
00282 say(la.portfd, "SP");
00283 hear(la.portfd, buffer, 30, 12, 300000, '\r');
00284
00285
00286
00287
00288 if (limflag==2) {
00289 dir = 0;
00290 if (!(ptr = strstr(buffer, "SP="))) {
00291 close(la.portfd);
00292 return(4);
00293 }
00294 la.hi_l = atoi(ptr+3);
00295 } else {
00296 if (limflag==1) {
00297 dir = 1;
00298 if (!(ptr = strstr(buffer, "SP="))) {
00299 close(la.portfd);
00300 return(4);
00301 }
00302 la.lo_l = atoi(ptr+3);
00303 } else {
00304 close(la.portfd);
00305 return(5);
00306 }
00307 }
00308
00309
00310 sprintf(buffer, "DI%s1", ((char *)dir)?"":"-");
00311 say(la.portfd, buffer);
00312 say(la.portfd, "CJ");
00313 say(la.portfd, "SS.");
00314 hear(la.portfd, buffer, 10, 0, la.mtt, '\r');
00315 if (!buffer[0]) {
00316 say(la.portfd, "SJ");
00317 close(la.portfd);
00318 return(5);
00319 }
00320
00321
00322
00323
00324
00325
00326 say(la.portfd, "SP");
00327 hear(la.portfd, buffer, 30, 12, 300000, '\r');
00328 if (!(ptr = strstr(buffer, "SP="))) {
00329 close(la.portfd);
00330 return(4);
00331 }
00332 if (dir) la.hi_l = atoi(ptr+3);
00333 else la.lo_l = atoi(ptr+3);
00334
00335 la.travel = la.hi_l - la.lo_l;
00336 sprintf(buffer, "DI%s%d", ((char*)dir)?"-":"", LAIT_SLIM);
00337
00338 say(la.portfd, buffer);
00339 if (dir) la.curpos = (la.hi_l - la.lo_l) - LAIT_SLIM;
00340 else la.curpos = LAIT_SLIM;
00341 }
00342
00343 if (action&LAIT_MOLD) {
00344
00345 sprintf(buffer, "DI%d\rFP", old_position);
00346 say(la.portfd, buffer);
00347 hear(la.portfd, buffer, 10, 2, la.mtt, '\r');
00348 la.curpos = old_position;
00349 }
00350
00351 if (action&LAIT_MPOS) {
00352
00353
00354 sprintf(buffer, "DI%d\rFP\rSS+", la.hi_l - LAIT_SLIM);
00355 say(la.portfd, buffer);
00356 hear(la.portfd, buffer, 10, 2, la.mtt, '\r');
00357 la.curpos = la.travel-LAIT_SLIM;
00358 }
00359
00360 if (action&LAIT_MNEG) {
00361
00362 sprintf(buffer, "DI%d\rFP\rSS-", la.lo_l + LAIT_SLIM);
00363 say(la.portfd, buffer);
00364 hear(la.portfd, buffer, 10, 2, la.mtt, '\r');
00365 la.curpos = LAIT_SLIM;
00366 }
00367
00368
00369
00370
00371
00372 UpperLimit=(static_cast <double>(la.hi_l))/25400.0;
00373 LowerLimit=(static_cast <double>(la.lo_l))/25400.0;
00374 CurrentPosition=(static_cast <double>(la.curpos))/25400.0;
00375 Travel=(static_cast <double>(la.travel))/25400.0;
00376
00377 return(0);
00378
00379 }
00380
00381
00382 int PhysicalLinearActuator::Deinitialize() {
00383
00384 close(portfd);
00385 return 0;
00386 }
00387
00388
00389
00390
00391
00392 double PhysicalLinearActuator::QueryCurrentPosition()
00393 {
00394 char *buffer;
00395 buffer = (char *) malloc(256);
00396 say(la.portfd, "SP");
00397 hear(la.portfd, buffer, 30, 12, 300000, '\r');
00398 int currentposition=atoi(buffer);
00399 CurrentPosition=currentposition/25400.0;
00400 return CurrentPosition;
00401 }
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419