00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00032 #include <des70_10.h>
00033
00041 int InitDES_communication(std::string com_device, int* port)
00042 {
00043 int result = true;
00044
00045
00046
00047 struct termios newParams;
00048 int ret;
00049
00050 memset(&newParams,0,sizeof(newParams));
00051
00052 printf("Opening Comm for DES 70/10..."); fflush(stdout);
00053 *port = open(com_device.c_str(), O_RDWR | O_NONBLOCK );
00054 tcflush(*port, TCIFLUSH);
00055
00056 if(*port == -1)
00057 {
00058 printf("Error\n");
00059 printf("Failed to open Port %s for device DES 70/10\n", com_device.c_str());
00060 result = false;
00061 }else{
00062 printf("Done\n");
00063
00064 newParams.c_cflag = B38400 | CS8 | CLOCAL | CREAD;
00065 newParams.c_iflag = IGNPAR;
00066 newParams.c_oflag = 0;
00067
00068
00069 ret = tcsetattr(*port, TCSANOW, &newParams);
00070
00071 if(ret < 0)
00072 {
00073 perror("Set serial communication parameters failed:");
00074 result = false;
00075 };
00076 }
00077
00078 return result;
00079 };
00080
00088 int calc_crc_16(TYPE_msg_frame* msg)
00089 {
00090 msg->size = (msg->data[0]+1)*2+1;
00091 msg->crc = 0x00;
00092 msg->crc = update_crc_ccitt(msg->crc, msg->OpCode);
00093
00094 msg->crc = update_crc_ccitt(msg->crc, msg->data[0]);
00095 for(int i=1; i< msg->size; i+=2)
00096 {
00097
00098 msg->crc = update_crc_ccitt(msg->crc, msg->data[i+1]);
00099
00100 msg->crc = update_crc_ccitt(msg->crc, msg->data[i]);
00101
00102 };
00103
00104
00105 msg->data[msg->size] = (msg->crc & 0xff);
00106 msg->data[msg->size+1] = ((msg->crc >> 8) & 0xff);
00107 return 1;
00108 }
00109
00119 int write_msg_inbuf(const int port, unsigned char* data, int size)
00120 {
00121 int result = true;
00122
00123
00124 {
00125 int n = write(port, data, size);
00126 data++;
00127 if(n<0 || n!= size)
00128 {
00129 perror("\terror transmiting");
00130 result = false;
00131 };
00132 };
00133
00134 return result;
00135 };
00136
00146 int check_msg_aceptance(const int port, float wait_time, unsigned char* msg)
00147 {
00148 int n=0;
00149 int result = 1;
00150 unsigned char byteread[20];
00151 char finished = true;
00152 c_timer mid_timer;
00153
00154
00155 mid_timer.tic(0);
00156 while(finished)
00157 {
00158 n = read(port, &byteread[0], 1);
00159
00160 if(n>0)
00161 {
00162 msg[0] = byteread[n-1];
00163 finished = false;
00164
00165 }else if(n==-1)
00166 {
00167 result = 0;
00168 finished = false;
00169 *msg=20;
00170 }
00171 mid_timer.toc(0);
00172 if((mid_timer.get_toc(0)> wait_time))
00173 {
00174 result = -1;
00175 finished = false;
00176 *msg=20;
00177 }
00178 }
00179 return result;
00180 };
00181
00192 int send_OpCode(const int port, unsigned char tries, unsigned char OpCode, char flg)
00193 {
00194
00195 char result = 0;
00196 unsigned char tmp[2];
00197 unsigned char num=0;
00198 int err;
00199 #define WAIT 0.05
00200 c_timer timer0;
00201
00202 tmp[0] = 0;
00203
00204
00205
00206 while(num < tries)
00207 {
00208 write_msg_inbuf(port, &OpCode, 1);
00209
00210 err = check_msg_aceptance(port, WAIT, &tmp[0]);
00211 tcflush(port, TCIFLUSH);
00212
00213
00214
00215 switch(err)
00216 {
00217 case 1:
00218 {
00219 if(tmp[0] == 'O')
00220 {
00221 result = 1;
00222 num = tries+20;
00223 }else if(tmp[0] == 'F')
00224 {
00225 result = -1;
00226 }
00227 break;
00228 }
00229 case -1:
00230 {
00231 result=0;
00232
00233 }
00234 };
00235
00236 if(flg)
00237 {
00238 printf("### OpCode - 0x%02x###\n", OpCode);
00239 if(result == 1)
00240 {
00241 printf("OpCode understood\n");
00242 }else if(result == -1)
00243 {
00244 printf("OpCode not understood\n");
00245 }
00246
00247 }
00248 num++;
00249 };
00250
00251
00252 return result;
00253 };
00254
00264 int send_data(const int port, TYPE_msg_frame* msg, char flg)
00265 {
00266 int result = 0;
00267 unsigned char tmp[2];
00268 int err;
00269 #define WAIT 0.05
00270
00271 tmp[0] = 'N';
00272
00273 write_msg_inbuf(port, &msg->data[0], 1);
00274 write_msg_inbuf(port, &msg->data[1], msg->size-1);
00275 write_msg_inbuf(port, &msg->data[msg->size], 2);
00276 tcflush(port, TCIFLUSH);
00277
00278 err = check_msg_aceptance(port, WAIT, &tmp[0]);
00279
00280 switch(err)
00281 {
00282 case 1:
00283 {
00284 if(tmp[0] == 'O')
00285 {
00286 result = 1;
00287
00288 }else if(tmp[0] == 'F')
00289 {
00290 result = -1;
00291
00292 }
00293 break;
00294 }
00295 default:
00296 {
00297 result=0;
00298
00299 }
00300 }
00301
00302 if(flg)
00303 {
00304 printf("*\tdata[0]\n");
00305 for(int k=1;k<msg->size;k +=2)
00306 printf("*\tdata[%d,%d]:0x%02x,0x%02x\n", k, k+1, msg->data[k], msg->data[k+1]);
00307
00308 printf("*\tdata[crc]:0x%04x\n", msg->crc);
00309 if(result == 1)
00310 {
00311 printf("msg sent without error\n");
00312 }else if(result == -1)
00313 {
00314 printf("msg sent but failed(crc or serial conection lost)\n");
00315 }
00316 };
00317
00318 return result;
00319 };
00320
00330 int read_buffer(const int port, TYPE_msg_frame* msg, char flg)
00331 {
00335 #define MAX_WAIT_TIME 0.023
00336 int result = true;
00337 unsigned char byteread[20];
00338 int n;
00339 int count_bytes;
00340 char finished = false;
00341 unsigned char tmp;
00342 int crc_val=0x00;
00343 unsigned char non_opcode = 0xff;
00344 count_bytes = 0;
00345 c_timer timer0;
00346
00347 timer0.tic(0);
00348
00349
00350 while(!finished)
00351 {
00352 timer0.toc(0);
00353 if (timer0.get_toc(0) > MAX_WAIT_TIME)
00354 {
00355 result = false;
00356 finished = true;
00357 printf("LIB_DES exceed waiting time:%d\n", result);
00358 }
00359 n = read(port, &byteread[0] , 1);
00360
00361 if(n>0)
00362 {
00363 switch(count_bytes)
00364 {
00365 case 0:
00366 {
00367 if(byteread[n-1] == 0x00)
00368 {
00369 msg->OpCode = byteread[n-1];
00370 tmp = 'O';
00371 write_msg_inbuf(port, &tmp, 1);
00372 }else
00373 {
00374 non_opcode = byteread[n-1];
00375 tmp = 'F';
00376 write_msg_inbuf(port, &tmp, 1);
00377 finished = true;
00378 result = false;
00379 };
00380 break;
00381 };
00382 case 1:
00383 {
00384 msg->data[0] = byteread[n-1];
00385 msg->size = (msg->data[0]+1)*2+1;
00386 break;
00387 };
00388 default:
00389 {
00390 if(count_bytes < (msg->size+2))
00391 {
00392 msg->data[count_bytes-1] = byteread[n-1];
00393 }else
00394 {
00395 msg->data[count_bytes-1] = byteread[n-1];
00396 finished = true;
00397 }
00398 }
00399 }
00400 count_bytes++;
00401 }else if(n == -1)
00402 {
00403 result = false;
00404 finished = true;
00405 perror("error receiving msg");
00406 };
00407 }
00408
00409 if (result)
00410 {
00411 crc_val = (msg->data[msg->size])+(msg->data[msg->size+1] << 8);
00412 calc_crc_16(msg);
00413
00414 if(crc_val == msg->crc)
00415 {
00416 tmp = 'O';
00417 write_msg_inbuf(port, &tmp, 1);
00418 }else
00419 {
00420 tmp = 'F';
00421 write_msg_inbuf(port, &tmp, 1);
00422 result = false;
00423 };
00424 };
00425
00426 if(flg)
00427 {
00428 printf("#### Read Buffer ####\n");
00429 if(result)
00430 {
00431 printf("msg->OpCode:0x%02x\n", msg->OpCode);
00432 printf("msg->data[0]:0x%02x | msg->size:%d\n", msg->data[0], msg->size);
00433 for(int i = 1; i < msg->size; i += 2)
00434 {
00435 printf("msg->data[%d]:0x%02x msg->data[%d]:0x%02x\n", i, msg->data[i], i+1, msg->data[i+1]);
00436 };
00437 printf("msg->data[%d]:0x%02x + msg->data[%d]:0x%02x crc: 0x%04x\n", msg->size, msg->data[msg->size], msg->size+1, msg->data[msg->size+1], crc_val);
00438 printf("obtained msg->crc:0x%04x\n", msg->crc);
00439 }else
00440 {
00441 printf("OpCode not known[0x%02x] or obtained crc!=DES_crc [0x%04x!=0x%04x]\n", non_opcode, msg->crc, crc_val);
00442 }
00443 }
00444
00445 return result;
00446 };
00447
00457 int send_msg_frame(const int port, TYPE_msg_frame* msg, char flg)
00458 {
00459 int result = true;
00460
00461
00462 calc_crc_16(msg);
00463
00464
00465 int res = send_OpCode(port, 1, msg->OpCode, flg);
00466
00467 if((res == -1) || (res == 0))
00468 {
00469 result = 0;
00470
00471 }
00472
00473
00474 if(result)
00475 {
00476 res = send_data(port, msg, flg);
00477
00478 if((res == -1) || (res == 0))
00479 {
00480 result = false;
00481
00482 }
00483 };
00484
00485
00486 return result;
00487 };
00488
00498 int DES_ST_read_sys_status(const int port, int* sys_status, char flg)
00499 {
00500 char result = false;
00501 TYPE_msg_frame send_msg, received_msg;
00502
00503 send_msg.OpCode = 0x01;
00504 send_msg.data[0] = 0x00;
00505 send_msg.data[1] = 0x00;
00506 send_msg.data[2] = 0x00;
00507
00508 if(flg)
00509 printf("\n\n\tread system status\n");
00510
00511
00512 if(send_msg_frame(port, &send_msg, flg))
00513 {
00514 result = true;
00515
00516 };
00517
00518 if(result && (read_buffer(port, &received_msg, flg)))
00519 {
00520
00521 *sys_status = (received_msg.data[1] + (received_msg.data[2]<<8));
00522 }else
00523 {
00524
00525 result = false;
00526 };
00527
00528 return result;
00529 };
00530
00531
00541 int DES_ST_read_error(const int port, int* errors, char flg)
00542 {
00543 int result = false;
00544 TYPE_msg_frame send_msg, received_msg;
00545
00546 send_msg.OpCode = 0x02;
00547 send_msg.data[0] = 0x00;
00548 send_msg.data[1] = 0x00;
00549 send_msg.data[2] = 0x00;
00550
00551 if(flg)
00552 printf("\n\n\tread error\n");
00553
00554 if(send_msg_frame(port, &send_msg, flg))
00555 {
00556 result = true;
00557 };
00558
00559 if(result && (read_buffer(port, &received_msg, flg)))
00560 {
00561 *errors = (received_msg.data[received_msg.size-1] + (received_msg.data[received_msg.size-2]<<8));
00562 }else
00563 {
00564 result = false;
00565 }
00566
00567 return result;
00568 };
00569
00570
00579 int DES_ST_clear_errors(const int port, char flg)
00580 {
00581 int result = false;
00582
00583 TYPE_msg_frame send_msg;
00584
00585 send_msg.OpCode = 0x03;
00586 send_msg.data[0] = 0x00;
00587 send_msg.data[1] = 0x00;
00588 send_msg.data[2] = 0x00;
00589
00590 if(flg)
00591 printf("\n\n\tclear errors\n");
00592
00593 if(send_msg_frame(port, &send_msg, flg))
00594 {
00595 result = true;
00596 };
00597
00598 return result;
00599 };
00600
00609 int DES_ST_reset(const int port, char flg)
00610 {
00611 int result = false;
00612
00613 TYPE_msg_frame send_msg;
00614
00615 send_msg.OpCode = 0x04;
00616 send_msg.data[0] = 0x00;
00617 send_msg.data[1] = 0x00;
00618 send_msg.data[2] = 0x00;
00619
00620 if(flg)
00621 printf("\n\n\trestart servoamplifier with default values\n");
00622
00623 if(send_msg_frame(port, &send_msg, flg))
00624 {
00625 result = true;
00626 };
00627
00628 return result;
00629
00630 };
00631
00641 int DES_ST_enable(const int port, int* newState, char flg)
00642 {
00643 int result = false;
00644
00645 TYPE_msg_frame send_msg;
00646
00647 send_msg.OpCode = 0x05;
00648 send_msg.data[0] = 0x00;
00649 send_msg.data[1] = (*newState & 0xff);
00650 send_msg.data[2] = ((*newState >> 8) & 0xff);
00651
00652 if(flg)
00653 printf("\n\n\tEnable servo %d\n", *newState);
00654
00655 if(send_msg_frame(port, &send_msg, flg))
00656 {
00657 result = true;
00658 };
00659
00660 return result;
00661 };
00662
00663
00675 int DES_SPF_read_temp_param(const int port, int paramNb, int dataFormat, int* response, char flg)
00676 {
00677 int result = false;
00678
00679 TYPE_msg_frame send_msg, received_msg;
00680
00681 send_msg.OpCode = 0x14;
00682 send_msg.data[0] = 0x01;
00683 send_msg.data[1] = (paramNb & 0xff);
00684 send_msg.data[2] = ((paramNb >> 8) & 0xff);
00685 send_msg.data[3] = (dataFormat & 0xff);
00686 send_msg.data[4] = ((dataFormat >> 8) & 0xff);
00687
00688 if(flg)
00689 printf("\n\n\tread parameter: %d\n", paramNb);
00690
00691 if(send_msg_frame(port, &send_msg, flg))
00692 {
00693 result = true;
00694 };
00695
00696 if(result && (read_buffer(port, &received_msg, flg)))
00697 {
00698 *response = received_msg.data[1] + (received_msg.data[2]<<8);
00699 }else
00700 {
00701 result = false;
00702 }
00703
00704 return result;
00705 }
00706
00718 int DES_SPF_set_temp_param(const int port, int paramNb, int dataFormat, int* newValue, char flg)
00719 {
00720 int result = false;
00721
00722 TYPE_msg_frame send_msg;
00723
00724 send_msg.OpCode = 0x15;
00725 send_msg.data[1] = (paramNb & 0xff);
00726 send_msg.data[2] = ((paramNb >> 8) & 0xff);
00727 send_msg.data[3] = (dataFormat & 0xff);
00728 send_msg.data[4] = ((dataFormat >> 8) & 0xff);
00729
00730 if(dataFormat == 0)
00731 {
00732 send_msg.data[0] = 0x02;
00733 send_msg.data[5] = (*newValue & 0xff);
00734 send_msg.data[6] = ((*newValue >> 8) & 0xff);
00735 result = true;
00736 }else if(dataFormat == 1)
00737 {
00738 send_msg.data[0] = 0x03;
00739 unsigned int low_word = (*newValue & 0xffff);
00740 unsigned int higher_word = (*newValue >> 16) & 0xffff;
00741 send_msg.data[5] = (higher_word & 0xff);
00742 send_msg.data[6] = ((higher_word >> 8) & 0xff);
00743 send_msg.data[7] = (low_word & 0xff);
00744 send_msg.data[8] = ((low_word >> 8) & 0xff);
00745 result = true;
00746 }
00747
00748 if(flg)
00749 printf("\n\n\tSet parameter: %d\n", paramNb);
00750
00751 if(result && send_msg_frame(port, &send_msg, flg))
00752 {
00753 result = true;
00754 }else
00755 result = false;
00756
00757 return result;
00758 };
00759
00769 int DES_SPF_read_all_temp_param(const int port, TYPE_des_sysparam* sysparam, char flg)
00770 {
00771 int result = false;
00772
00773 TYPE_msg_frame send_msg, received_msg;
00774
00775 send_msg.OpCode = 0x18;
00776 send_msg.data[0] = 0x00;
00777 send_msg.data[1] = 0x00;
00778 send_msg.data[2] = 0x00;
00779
00780 if(flg)
00781 printf("\n\n\tread all parameters\n");
00782
00783 if(send_msg_frame(port, &send_msg, flg))
00784 {
00785 result = true;
00786 }
00787
00788 if(result && (read_buffer(port, &received_msg, flg)))
00789 {
00790 unsigned char* data0;
00791 unsigned char* data1;
00792 unsigned char index0, index1;
00793 index0 = 1; index1 = 2;
00794 data0 = &received_msg.data[0];
00795 data1 = &received_msg.data[0];
00796
00797 sysparam->baudrate = (data0[index0]) + ((data1[index1])<<8);
00798 index0 += 2; index1 += 2;
00799
00800 sysparam->sys_config = (data0[index0]) + ((data1[index1])<<8);
00801 index0 += 2; index1 += 2;
00802
00803 sysparam->cur_reg_gain_p = (data0[index0]) + ((data1[index1])<<8);
00804 index0 += 2; index1 += 2;
00805
00806 sysparam->max_cur_output = (data0[index0]) + ((data1[index1])<<8);
00807 index0 += 2; index1 += 2;
00808
00809 sysparam->speed_reg_gain_p = (data0[index0]) + ((data1[index1])<<8);
00810 index0 += 2; index1 += 2;
00811
00812 sysparam->speed_reg_gain_i = (data0[index0]) + ((data1[index1])<<8);
00813 index0 += 2; index1 += 2;
00814
00815 sysparam->internal_param1 = (data0[index0]) + ((data1[index1])<<8);
00816 index0 += 2; index1 += 2;
00817
00818 sysparam->internal_param2 = (data0[index0]) + ((data1[index1])<<8);
00819 index0 += 2; index1 += 2;
00820
00821 sysparam->internal_param3 = (data0[index0]) + ((data1[index1])<<8);
00822 index0 += 2; index1 += 2;
00823
00824 sysparam->max_speed_error = (data0[index0]) + ((data1[index1])<<8);
00825 index0 += 2; index1 += 2;
00826
00827 sysparam->setting_unit_gain = (data0[index0]) + ((data1[index1])<<8);
00828 index0 += 2; index1 += 2;
00829
00830 sysparam->max_speed_error = (data0[index0]) + ((data1[index1])<<8);
00831 index0 += 2; index1 += 2;
00832
00833 sysparam->setting_unit_gain = (data0[index0]) + ((data1[index1])<<8);
00834 index0 += 2; index1 += 2;
00835
00836 sysparam->setting_unit_offset = (data0[index0]) + ((data1[index1])<<8);
00837 index0 += 2; index1 += 2;
00838
00839 sysparam->setting_unit_delay = (data0[index0]) + ((data1[index1])<<8);
00840 index0 += 2; index1 += 2;
00841
00842 sysparam->peak_current = (data0[index0]) + ((data1[index1])<<8);
00843 index0 += 2; index1 += 2;
00844
00845 sysparam->max_cont_current = (data0[index0]) + ((data1[index1])<<8);
00846 index0 += 2; index1 += 2;
00847
00848 sysparam->max_speed = (data0[index0]) + ((data1[index1])<<8);
00849 index0 += 2; index1 += 2;
00850
00851 sysparam->acceleration = (data0[index0]) + ((data1[index1])<<8);
00852 index0 += 2; index1 += 2;
00853
00854 sysparam->speed_constant = (data0[index0]) + ((data1[index1])<<8);
00855 index0 += 2; index1 += 2;
00856
00857 sysparam->enc_resolution = (data0[index0]) + ((data1[index1])<<8);
00858 index0 += 2; index1 += 2;
00859
00860 sysparam->pole_pair_number = (data0[index0]) + ((data1[index1])<<8);
00861 index0 += 2; index1 += 2;
00862
00863 sysparam->internal_param4 = (data0[index0]) + ((data1[index1])<<8);
00864 index0 += 2; index1 += 2;
00865
00866 sysparam->rpm2qc_factor = (data0[index0]) + ((data1[index1])<<8);
00867 index0 += 2; index1 += 2;
00868
00869 sysparam->index_offset = (data0[index0]) + ((data1[index1])<<8);
00870 index0 += 2; index1 += 2;
00871
00872 sysparam->pwm_period = (data0[index0]) + ((data1[index1])<<8);
00873 index0 += 2; index1 += 2;
00874
00875 sysparam->max_duty_cycle = (data0[index0]) + ((data1[index1])<<8);
00876 index0 += 2; index1 += 2;
00877
00878 sysparam->cur_det_ph_u_offset = (data0[index0]) + ((data1[index1])<<8);
00879 index0 += 2; index1 += 2;
00880
00881 sysparam->cur_det_ph_v_offset = (data0[index0]) + ((data1[index1])<<8);
00882 index0 += 2; index1 += 2;
00883
00884 sysparam->ad_conv_offset = (data0[index0]) + ((data1[index1])<<8);
00885 index0 += 2; index1 += 2;
00886
00887 sysparam->can_module_id = (data0[index0]) + ((data1[index1])<<8);
00888 index0 += 2; index1 += 2;
00889
00890 sysparam->can_service_id = (data0[index0]) + ((data1[index1])<<8);
00891 index0 += 2; index1 += 2;
00892
00893 sysparam->can_rx_pdo_id = (data0[index0]) + ((data1[index1])<<8);
00894 index0 += 2; index1 += 2;
00895
00896 sysparam->can_tx_pdo_id = (data0[index0]) + ((data1[index1])<<8);
00897 index0 += 2; index1 += 2;
00898
00899 sysparam->can_bcr1 = (data0[index0]) + ((data1[index1])<<8);
00900 index0 += 2; index1 += 2;
00901
00902 sysparam->can_bcr2 = (data0[index0]) + ((data1[index1])<<8);
00903 index0 += 2; index1 += 2;
00904
00905 sysparam->can_op_mode = (data0[index0]) + ((data1[index1])<<8);
00906 index0 += 2; index1 += 2;
00907
00908 sysparam->can_rx_sdo_id = (data0[index0]) + ((data1[index1])<<8);
00909 index0 += 2; index1 += 2;
00910
00911 sysparam->can_tx_sdo_id = (data0[index0]) + ((data1[index1])<<8);
00912 index0 += 2; index1 += 2;
00913
00914 sysparam->can_rtr0_id = (data0[index0]) + ((data1[index1])<<8);
00915 index0 += 2; index1 += 2;
00916
00917 sysparam->can_rtr1_id = (data0[index0]) + ((data1[index1])<<8);
00918 index0 += 2; index1 += 2;
00919
00920 sysparam->can_config = (data0[index0]) + ((data1[index1])<<8);
00921 index0 += 2; index1 += 2;
00922
00923 sysparam->internal_param5 = (data0[index0]) + ((data1[index1])<<8);
00924 index0 += 2; index1 += 2;
00925
00926 sysparam->error_proc = (data0[index0]) + ((data1[index1])<<8);
00927 index0 += 2; index1 += 2;
00928
00929 sysparam->max_speed_curr = (data0[index0]) + ((data1[index1])<<8);
00930 index0 += 2; index1 += 2;
00931
00932 sysparam->hall_angle_offs = (data0[index0]) + ((data1[index1])<<8);
00933 index0 += 2; index1 += 2;
00934
00935 if(flg)
00936 {
00937 printf("baudrate:0x%04x\n", sysparam->baudrate);
00938 printf("sys_config:0x%04x\n", sysparam->sys_config);
00939 printf("cur_reg_gain_p:0x%04x\n", sysparam->cur_reg_gain_p);
00940 printf("max_cur_output:0x%04x\n", sysparam->max_cur_output);
00941 printf("speed_reg_gain_p:0x%04x\n", sysparam->speed_reg_gain_p);
00942 printf("speed_reg_gain_i:0x%04x\n", sysparam->speed_reg_gain_i);
00943 printf("internal_param1:0x%04x\n", sysparam->internal_param1);
00944 printf("internal_param2:0x%04x\n", sysparam->internal_param2);
00945 printf("internal_param3:0x%04x\n", sysparam->internal_param3);
00946 printf("max_speed_error:0x%04x\n", sysparam->max_speed_error);
00947 printf("setting_unit_gain:0x%04x\n", sysparam->setting_unit_gain);
00948 printf("max_speed_error:0x%04x\n", sysparam->max_speed_error);
00949 printf("setting_unit_gain:0x%04x\n", sysparam->setting_unit_gain);
00950 printf("setting_unit_offset:0x%04x\n", sysparam->setting_unit_offset);
00951 printf("setting_unit_delay:0x%04x\n", sysparam->setting_unit_delay);
00952 printf("peak_current:0x%04x\n", sysparam->peak_current);
00953 printf("max_cont_current:0x%04x\n", sysparam->max_cont_current);
00954 printf("max_speed:0x%04x\n", sysparam->max_speed);
00955 printf("acceleration:0x%04x\n", sysparam->acceleration);
00956 printf("speed_constant:0x%04x\n", sysparam->speed_constant);
00957 printf("enc_resolution:0x%04x\n", sysparam->enc_resolution);
00958 printf("pole_pair_number:0x%04x\n", sysparam->pole_pair_number);
00959 printf("internal_param4:0x%04x\n", sysparam->internal_param4);
00960 printf("rpm2qc_factor:0x%04x\n", sysparam->rpm2qc_factor);
00961 printf("index_offset:0x%04x\n", sysparam->index_offset);
00962 printf("pwm_period:0x%04x\n", sysparam->pwm_period);
00963 printf("max_duty_cycle:0x%04x\n", sysparam->max_duty_cycle);
00964 printf("cur_det_ph_u_offset:0x%04x\n", sysparam->cur_det_ph_u_offset);
00965 printf("cur_det_ph_v_offset:0x%04x\n", sysparam->cur_det_ph_v_offset);
00966 printf("ad_conv_offset:0x%04x\n", sysparam->ad_conv_offset);
00967 printf("can_module_id:0x%04x\n", sysparam->can_module_id);
00968 printf("can_service_id:0x%04x\n", sysparam->can_service_id);
00969 printf("can_rx_pdo_id:0x%04x\n", sysparam->can_rx_pdo_id);
00970 printf("can_tx_pdo_id:0x%04x\n", sysparam->can_tx_pdo_id);
00971 printf("can_bcr1:0x%04x\n", sysparam->can_bcr1);
00972 printf("can_bcr2:0x%04x\n", sysparam->can_bcr2);
00973 printf("can_op_mode:0x%04x\n", sysparam->can_op_mode);
00974 printf("can_rx_sdo_id:0x%04x\n", sysparam->can_rx_sdo_id);
00975 printf("can_tx_sdo_id:0x%04x\n", sysparam->can_tx_sdo_id);
00976 printf("can_rtr0_id:0x%04x\n", sysparam->can_rtr0_id);
00977 printf("can_rtr1_id:0x%04x\n", sysparam->can_rtr1_id);
00978 printf("can_config:0x%04x\n", sysparam->can_config);
00979 printf("internal_param5:0x%04x\n", sysparam->internal_param5);
00980 printf("error_proc:0x%04x\n", sysparam->error_proc);
00981 printf("max_speed_curr:0x%04x\n", sysparam->max_speed_curr);
00982 printf("hall_angle_offs:0x%04x\n", sysparam->hall_angle_offs);
00983 };
00984 }else
00985 {
00986 result = false;
00987 }
00988
00989 return result;
00990 };
00991
01001 int DES_SF_set_velocity(const int port, int newVelocity, char flg)
01002 {
01003 int result = false;
01004
01005 TYPE_msg_frame send_msg;
01006
01007 send_msg.OpCode = 0x21;
01008 send_msg.data[0] = 0x00;
01009 send_msg.data[1] = (newVelocity & 0xff);
01010 send_msg.data[2] = ((newVelocity >> 8) & 0xff);
01011
01012
01013 if(flg)
01014 printf("\n\n\tset velocity: %d\n", newVelocity);
01015
01016 if(send_msg_frame(port, &send_msg, flg))
01017 {
01018 result = true;
01019 };
01020
01021
01022 return result;
01023 };
01024
01034 int DES_SF_set_current(const int port, int newCurrent, char flg)
01035 {
01036 int result = false;
01037
01038 TYPE_msg_frame send_msg;
01039
01040 send_msg.OpCode = 0x22;
01041 send_msg.data[0] = 0x00;
01042 send_msg.data[1] = (newCurrent & 0xff);
01043 send_msg.data[2] = ((newCurrent >> 8) & 0xff);
01044
01045 if(flg)
01046 printf("\n\n\tset current: %d\n", newCurrent);
01047
01048 if(send_msg_frame(port, &send_msg, flg))
01049 {
01050 result = true;
01051 };
01052
01053 return result;
01054 };
01055
01066 int DES_SF_stop_motion(const int port, char flg)
01067 {
01068 int result = false;
01069
01070 TYPE_msg_frame send_msg;
01071
01072 send_msg.OpCode = 0x23;
01073 send_msg.data[0] = 0x00;
01074 send_msg.data[1] = 0x00;
01075 send_msg.data[2] = 0x00;
01076
01077 if(flg)
01078 printf("\n\n\tstop motion\n");
01079
01080 if(send_msg_frame(port, &send_msg, flg))
01081 {
01082 result = true;
01083 };
01084
01085 return result;
01086 };
01087
01088
01100 int DES_MF_read_velocity_is_must(const int port, int vel_type, int* velocity, int* requested_vel, char flg)
01101 {
01102 int result = false;
01103
01104 TYPE_msg_frame send_msg, received_msg;
01105
01106 send_msg.OpCode = 0x28;
01107 send_msg.data[0] = 0x00;
01108 send_msg.data[1] = (vel_type & 0xff);
01109 send_msg.data[2] = ((vel_type >> 8) & 0xff);
01110
01111 if(flg)
01112 printf("\n\n\trequesting velocity\n");
01113
01114 if(send_msg_frame(port, &send_msg, flg))
01115 {
01116 result = true;
01117 }
01118 if(result && (read_buffer(port, &received_msg, flg)))
01119 {
01120 *velocity = received_msg.data[1] | ((char)received_msg.data[2]<<8);
01121 *requested_vel = received_msg.data[3] | (received_msg.data[4]<<8);
01122 }else
01123 {
01124 result = false;
01125 printf("LIB_DES: reading velocity success:%s\n", (result==false?"false":"true"));
01126 }
01127 return result;
01128 }
01129
01130
01142 int InitDES(const int port, TYPE_des_sysparam* newSysParam, char enable_des, char brake_des, char flg)
01143 {
01144 int result = 1;
01145 int des_err;
01146 TYPE_DES_status_var aDES_status_var;
01147 TYPE_des_sysparam aDES_sysparam;
01148 int enable = 1;
01149 c_timer timer0;
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160 if(!DES_SPF_set_temp_param(port, SYS_CONFIG, 0, &newSysParam->sys_config, flg))
01161 {
01162 result=0;
01163 }
01164
01165 if(result)
01166 result=DES_SPF_set_temp_param(port, ENC_RESOLUTION, 0, &newSysParam->enc_resolution, flg);
01167
01168 if(result)
01169 result=DES_SPF_set_temp_param(port, MAX_SPEED, 0, &newSysParam->max_speed, flg);
01170
01171 if(result)
01172 result=DES_SPF_set_temp_param(port, MAX_CONT_CURRENT, 0, &newSysParam->max_cont_current, flg);
01173
01174 if(result)
01175 result=DES_SPF_set_temp_param(port, PEAK_CURRENT, 0, &newSysParam->peak_current, flg);
01176
01177 if(result)
01178 result=DES_SPF_set_temp_param(port, SPEED_REG_GAIN_P, 0, &newSysParam->speed_reg_gain_p, flg);
01179
01180 if(result)
01181 result=DES_SPF_set_temp_param(port, SPEED_REG_GAIN_I, 0, &newSysParam->speed_reg_gain_i, flg);
01182
01183
01184 char finished = false;
01185 if(result)
01186 {
01187 char f1,f2;
01188 f1=false;
01189 f1=false;
01190
01191 timer0.tic(0);
01192 while(!finished)
01193 {
01194 result = DES_ST_read_sys_status(port, &aDES_status_var.sys_op_status, flg);
01195
01196
01197 if((aDES_status_var.sys_op_status & 0b0010000000000000) && brake_des)
01198 {
01199 DES_SF_stop_motion(port, flg);
01200 }else
01201 f1 = true;
01202
01203 if(!(aDES_status_var.sys_op_status & 0b0000010000000000) && enable_des)
01204 {
01205 DES_ST_enable(port, &enable, flg);
01206 }else
01207 f2 = true;
01208 timer0.toc(0);
01209 if(timer0.get_toc(0)>1.5)
01210 {
01211
01212 result = 0;
01213 finished = true;
01214 }else
01215 {
01216 usleep(10000);
01217 };
01218 finished = (f1 && f2);
01219 };
01220 };
01221
01222
01223 if((aDES_status_var.sys_op_status & 0b0000001000000000) && result)
01224 {
01225 timer0.tic(1);
01226 finished = false;
01227 while(!finished)
01228 {
01229 DES_ST_read_error(port, &des_err, flg);
01230 result = (0b0010000111111111 & des_err);
01231 if(des_err & 0b1000000000000000)
01232 {
01233 DES_ST_clear_errors(port, flg);
01234 finished = true;
01235 }
01236 };
01237 }
01238
01239 if(flg)
01240 {
01241 DES_SPF_read_temp_param(port, SYS_CONFIG, 0, &aDES_sysparam.sys_config, 1);
01242 printf("requested sys_config:0x%04x\n", newSysParam->sys_config);
01243 printf("specified at DES sys_config:0x%04x\n", aDES_sysparam.sys_config);
01244
01245 if(des_err & 0b0000000000000001)
01246 printf("Error during offset detection\n");
01247 if(des_err & 0b0000000000000010)
01248 printf("Index processing error\n");
01249 if(des_err & 0b0000000000000100)
01250 printf("Index not received\n");
01251 if(des_err & 0b0000000000001000)
01252 printf("Error by initial position detection\n");
01253 if(des_err & 0b0000000000010000)
01254 printf("Over-Current Error\n");
01255 if(des_err & 0b0000000000100000)
01256 printf("Over-Voltage Error\n");
01257 if(des_err & 0b0000000001000000)
01258 printf("Over-Speed in current control mode\n");
01259 if(des_err & 0b0000000010000000)
01260 printf("Supply voltage too low for operation\n");
01261 if(des_err & 0b0000000100000000)
01262 printf("Angle Detection error\n");
01263 if(des_err & 0b0010000000000000)
01264 printf("Parameter out of range\n");
01265 };
01266
01267 return result;
01268 };
01269
01270
01271 int stopDES(const int port, char flg)
01272 {
01273 int result = false;
01274 c_timer timer0;
01275 TYPE_DES_status_var aDES_status_var;
01276 char finished = false;
01277 int enable = 0;
01278 char f1 = false;
01279 char f2 = true;
01280
01281
01282 timer0.tic(0);
01283
01284 while(!finished)
01285 {
01286
01287 DES_SF_set_velocity(port, 0, flg);
01288
01289
01290 if(!DES_ST_read_sys_status(port, &aDES_status_var.sys_op_status, flg))
01291 {
01292 aDES_status_var.sys_op_status = 0b0010000000000000;
01293 }
01294
01295
01296
01297 if(!(aDES_status_var.sys_op_status & 0b0010000000000000))
01298 {
01299 DES_SF_stop_motion(port, flg);
01300 f1 = true;
01301 }
01302
01303
01304 DES_ST_enable(port, &enable, flg);
01305
01306
01307 finished = (f1 && f2);
01308 }
01309 return result;
01310
01311 };
01312
01313
01314 int DES_set_new_baud(int* port, int baud, char flg)
01315 {
01316 int result = true;
01317 struct termios newParams;
01318 int ret;
01319
01320 bzero(&newParams, sizeof(newParams));
01321
01322
01323 printf("Set new Baudrate for DES 70/10..."); fflush(stdout);
01324
01325 DES_SPF_set_temp_param(*port, BAUDRATE, 0, &baud, flg);
01326
01327 newParams.c_cflag = (B115200 | CS8 | CLOCAL | CREAD);
01328 ret = tcsetattr(*port, TCSANOW, &newParams);
01329
01330 if(ret < 0)
01331 {
01332 perror("Set serial communication parameters failed:");
01333 result = false;
01334 };
01335
01336 return result;
01337 };