40 #define KHI_ROBOT_WD002N "WD002N" 41 #define KHI_ROBOT_RS007L "RS007L" 42 #define KHI_ROBOT_RS007N "RS007N" 43 #define KHI_ROBOT_RS080N "RS080N" 44 #define KHI_KRNX_BUFFER_SIZE 4 45 #define KHI_KRNX_ACTIVATE_TH 0.02 46 #define KHI_KRNX_M2MM 1000 75 std::lock_guard<std::mutex> lock(
mutex_state[cont_no] );
82 std::lock_guard<std::mutex> lock(
mutex_state[cont_no] );
85 if ( *as_err_code != 0 )
87 warnPrint(
"AS returned %d by %s", *as_err_code, cmd );
101 errorPrint(
"%s returned -0x%X", name.c_str(), -ret );
124 for (
int ano = 0; ano < data.
arm_num; ano++ )
135 errorPrint(
"Please change Robot Controller's TEACH/REPEAT to REPEAT" );
140 errorPrint(
"Please change Robot Controller's TEACH LOCK to OFF" );
143 else if ( panel_info.
run_lamp != -1 )
145 errorPrint(
"Please change Robot Controller's RUN/HOLD to RUN" );
150 errorPrint(
"Please change Robot Controller's EMERGENCY to OFF" );
165 char msg[256] = { 0 };
180 char c_ip_address[64] = { 0 };
186 warnPrint(
"Cannot open cont_no:%d because it is already opend...", cont_no );
199 strncpy( c_ip_address, ip_address.c_str(),
sizeof(c_ip_address) );
200 infoPrint(
"Connecting to real controller: %s", c_ip_address );
220 char msg[1024] = { 0 };
234 snprintf( msg,
sizeof(msg),
"SW ZDBLREFFLT_MODSTABLE=ON" );
250 const int to_home_vel = 20;
251 const double timeout_sec_th = 5.0;
254 double timeout_sec_cnt = 0.0;
275 bool is_cycle =
true;
276 for (
int ano = 0; ano < arm_num; ano++ )
304 for (
int ano = 0; ano < arm_num; ano++ )
309 std::stringstream program;
310 program <<
"rb_rtc" << ano + 1;
311 if ( strcmp( program_info.
robot[ano].
program_name, program.str().c_str() ) == 0 )
336 for (
int ano = 0; ano < arm_num; ano++ )
341 std::stringstream program;
342 program <<
"rb_rtc" << ano + 1;
349 if ( timeout_sec_cnt > timeout_sec_th )
363 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
368 diff = data.
arm[ano].
home[jt]*conv - motion_data.
ang[jt];
410 char msg[1024] = { 0 };
424 for (
int ano = 0; ano < data.
arm_num; ano++ )
448 char robot_name[64] = { 0 };
449 char msg[256] = { 0 };
461 for (
int ano = 0; ano < arm_num; ano++ )
465 if ( strncmp( robot_name, data.
robot_name.c_str(), 6 ) != 0 )
483 snprintf( msg,
sizeof(msg),
"TYPE SYSDATA(ZROB.NOWAXIS,%d)", ano+1 );
504 rtcont_info.
cyc = (int)(
cont_info[cont_no].period/1e+6);
539 for (
int ano = 0; ano < arm_num; ano++ )
542 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
561 for (
int ano = 0; ano < arm_num; ano++ )
563 if ( !
getCurMotionData( cont_no, ano, &motion_cur[ano] ) ) {
return false; }
567 motion_data[cont_no][ano].erase( motion_data[cont_no][ano].begin() );
569 motion_data[cont_no][ano].push_back( motion_cur[ano] );
572 memcpy( ang[ano], &motion_cur[ano].ang,
sizeof(motion_cur[ano].ang) );
574 if ( motion_data[cont_no][ano].size() > 1 )
576 std::vector<TKrnxCurMotionData>::iterator it = motion_data[cont_no][ano].end();
580 vel[ano][jt] = motion_data[cont_no][ano].back().ang[jt] - it->ang[jt];
584 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
586 data.
arm[ano].
pos[jt] = (double)ang[ano][jt];
587 data.
arm[ano].
vel[jt] = (double)vel[ano][jt];
588 data.
arm[ano].
eff[jt] = (double)0;
591 if ( data.
arm[ano].
type[jt] == urdf::Joint::PRISMATIC )
618 data.
arm[0].
home[0] = -45.0f*M_PI/180;
619 data.
arm[0].
home[1] = 45.0f*M_PI/180;
620 data.
arm[1].
home[0] = 45.0f*M_PI/180;
621 data.
arm[1].
home[1] = -45.0f*M_PI/180;
627 for (
int ano = 0; ano < arm_num; ano++ )
629 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
636 for (
int ano = 0; ano < arm_num; ano++ )
638 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
654 char msg[1024] = { 0 };
655 char status[128] = { 0 };
659 bool is_primed =
true;
678 for (
int ano = 0; ano < arm_num; ano++ )
680 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
682 p_rtc_data->
comp[ano][jt] = (float)(data.
arm[ano].
cmd[jt] - data.
arm[ano].
home[jt]);
683 if ( data.
arm[ano].
type[jt] == urdf::Joint::PRISMATIC )
690 for (
int ano = 0; ano < arm_num; ano++ )
698 for (
int ano = 0; ano < arm_num; ano++ )
700 snprintf( msg,
sizeof(msg),
"[krnx_PrimeRtcCompData] ano:%d [jt]pos:vel:status ", ano+1 );
703 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
705 jt_pos = motion_data.
ang_ref[jt];
706 jt_vel = ( p_rtc_data->
comp[ano][jt] - p_rtc_data->
old_comp[ano][jt] )*(1e+9/
cont_info[cont_no].period);
707 if ( data.
arm[ano].
type[jt] == urdf::Joint::PRISMATIC )
712 snprintf( status,
sizeof(status),
"[%d]%.4f:%.4f:%d ", jt+1, jt_pos, jt_vel, p_rtc_data->
status[ano][jt] );
713 strcat( msg, status );
714 ROS_WARN(
"JT%d:%f,%f,%f,%f,%f,%f", jt+1, data.
arm[ano].
cmd[jt], data.
arm[ano].
home[jt]+p_rtc_data->
comp[ano][jt],p_rtc_data->
old_comp[ano][jt], p_rtc_data->
comp[ano][jt], data.
arm[ano].
home[jt], motion_data.
ang_ref[jt]);
715 ROS_WARN(
"JT%d:%f,%f,%f,%f,%f,%f", jt+1, data.
arm[ano].
cmd[jt]*180/M_PI, (data.
arm[ano].
home[jt]+p_rtc_data->
comp[ano][jt])*180/M_PI, p_rtc_data->
old_comp[ano][jt]*180/M_PI, p_rtc_data->
comp[ano][jt]*180/M_PI, data.
arm[ano].
home[jt]*180/M_PI, motion_data.
ang_ref[jt]*180/M_PI);
739 if ( state ==
ERROR )
741 for (
int ano = 0; ano < arm_num; ano++ )
755 for (
int ano = 0; ano < arm_num; ano++ )
758 if ( ( state !=
ERROR ) && ( error_lamp != 0 ) )
761 errorPrint(
"AS ERROR %d: ano:%d code:%d", cont_no, ano+1, error_code );
768 errorPrint(
"RTC SWITCH turned OFF %d: ano:%d", cont_no, ano+1 );
780 static bool buffer_enabled =
false;
782 int buffer_length = 0;
798 if ( buffer_length > 0 )
800 buffer_enabled =
true;
803 if ( buffer_enabled )
818 int last = str.find_first_of( del );
819 std::vector<std::string> list;
821 if ( first < str.size() )
823 std::string sub_str1( str, first, last - first );
824 list.push_back( sub_str1 );
826 std::string sub_str2( str, first, std::string::npos );
827 list.push_back( sub_str2 );
837 char tmplt[] =
"/tmp/khi_robot-rtc_param-XXXXXX";
838 char fd_path[128] = { 0 };
839 char file_path[128] = { 0 };
844 if ( ( fp = fdopen( fd,
"w" ) ) != NULL)
847 snprintf( fd_path,
sizeof(fd_path),
"/proc/%d/fd/%d", getpid(), fd );
848 rsize = readlink( fd_path, file_path,
sizeof(file_path) );
849 if ( rsize < 0 ) {
return false; }
854 fprintf( fp,
".PROGRAM rb_rtc1()\n" );
855 fprintf( fp,
" HERE #rtchome1\n" );
856 fprintf( fp,
" FOR .i = 1 TO 8\n" );
857 fprintf( fp,
" .acc[.i] = 0\n" );
858 fprintf( fp,
" END\n" );
859 fprintf( fp,
" L3ACCURACY .acc[1] ALWAYS\n" );
860 fprintf( fp,
" RTC_SW 1: ON\n" );
861 fprintf( fp,
"1 JMOVE #rtchome1\n" );
862 fprintf( fp,
" GOTO 1\n" );
863 fprintf( fp,
" RTC_SW 1: OFF\n" );
864 fprintf( fp,
".END\n" );
865 fprintf( fp,
".PROGRAM rb_rtc2()\n" );
866 fprintf( fp,
" HERE #rtchome2\n" );
867 fprintf( fp,
" FOR .i = 1 TO 8\n" );
868 fprintf( fp,
" .acc[.i] = 0\n" );
869 fprintf( fp,
" END\n" );
870 fprintf( fp,
" L3ACCURACY .acc[1] ALWAYS\n" );
871 fprintf( fp,
" RTC_SW 2: ON\n" );
872 fprintf( fp,
"1 JMOVE #rtchome2\n" );
873 fprintf( fp,
" GOTO 1\n" );
874 fprintf( fp,
" RTC_SW 2: OFF\n" );
875 fprintf( fp,
".END\n" );
879 fprintf( fp,
".PROGRAM rb_rtc1()\n" );
880 fprintf( fp,
" HERE #rtchome1\n" );;
881 fprintf( fp,
" ACCURACY 0 ALWAYS\n" );
882 fprintf( fp,
" RTC_SW 1: ON\n" );
883 fprintf( fp,
"1 JMOVE #rtchome1\n" );
884 fprintf( fp,
" GOTO 1\n" );
885 fprintf( fp,
" RTC_SW 1: OFF\n" );
886 fprintf( fp,
".END\n" );
906 for (
int ano = 0; ano < data.
arm_num; ano++ )
910 for (
int jt = 0; jt < data.
arm[ano].
jt_num; jt++ )
912 data.
arm[ano].
home[jt] = (double)motion_data.
ang[jt];
913 if ( data.
arm[ano].
type[jt] == urdf::Joint::PRISMATIC )
929 std::vector<std::string> vlist;
931 const char del =
' ';
945 if ( req.type ==
"as" )
949 dcode =
execAsMonCmd( cont_no, req.cmd.c_str(), resp,
sizeof(resp), &acode );
950 res.driver_ret = dcode;
952 res.cmd_ret = std::string(resp);
957 res.cmd_ret =
"IS TRANSITION STATE";
960 else if ( req.type ==
"driver" )
963 if ( req.cmd ==
"get_status" )
967 else if ( req.cmd ==
"hold" )
975 res.cmd_ret =
"NOT ACTIVE STATE";
978 else if ( req.cmd ==
"restart" )
986 res.cmd_ret =
"NOT INACTIVE/HOLDED/ERROR STATE";
989 else if ( req.cmd ==
"quit" )
996 if ( vlist.size() == 2 )
999 if ( api_cmd ==
"get_signal" )
1002 res.driver_ret = dcode;
1003 arg = std::atoi( vlist[1].c_str() );
1007 onoff = io.
io_do[(arg-1)/8] & ( 1 << (arg-1)%8 );
1013 onoff = io.
io_di[(arg-1)/8] & ( 1 << (arg-1)%8 );
1019 onoff = io.
internal[(arg-1)/8] & ( 1 << (arg-1)%8 );
1024 res.cmd_ret =
"INVALID ARGS";
1028 if ( onoff ) { res.cmd_ret =
"-1"; }
1029 else { res.cmd_ret =
"0";}
1032 else if ( api_cmd ==
"set_signal" )
1034 std::string as_cmd = req.cmd;
1035 as_cmd.replace( 0, strlen(
"set_signal"),
"SIGNAL" );
1036 dcode =
execAsMonCmd( cont_no, as_cmd.c_str(), resp,
sizeof(resp), &acode );
1037 res.driver_ret = dcode;
1043 res.cmd_ret =
"INVALID CMD";
1049 res.cmd_ret =
"INVALID ARGS";
1056 res.cmd_ret =
"INVALID TYPE";
DECLSPEC_IMPORT int WINAPI krnx_SetRtcInfo(int cont_no, TKrnxRtcInfo *rtc_info)
DECLSPEC_IMPORT int WINAPI krnx_ExecMon(int cont_no, const char *cmd, char *buffer, int buffer_sz, int *as_err_code)
std::vector< std::string > splitString(const std::string &str, const char &del)
DECLSPEC_IMPORT int WINAPI krnx_GetPanelInfo(int cont_no, int robot_no, TKrnxPanelInfo *panelinfo)
void errorPrint(const char *format,...)
DECLSPEC_IMPORT int WINAPI krnx_Open(int cont_no, char *hostname)
#define KRNX_MAX_CONTROLLER
bool initialize(const int &cont_no, const double &period, KhiRobotData &data, const bool in_simulation=false) override
bool setState(const int &cont_no, const int &state)
float comp[KRNX_MAX_ROBOT][KRNX_MAXAXES]
DECLSPEC_IMPORT int WINAPI krnx_GetRtcCompData(int cont_no, int robot_no, float *comp)
float old_comp[KRNX_MAX_ROBOT][KRNX_MAXAXES]
#define KHI_KRNX_ACTIVATE_TH
KhiRobotControllerInfo cont_info[KHI_MAX_CONTROLLER]
DECLSPEC_IMPORT int WINAPI krnx_Load(int cont_no, const char *filename)
char internal[KRNX_MAXSIGNAL/8]
DECLSPEC_IMPORT int WINAPI krnx_OldCompClear(int cont_no, int robot_no)
int status[KRNX_MAX_ROBOT][KRNX_MAXAXES]
char io_di[KRNX_MAXSIGNAL/8]
#define KHI_MAX_CONTROLLER
bool conditionCheck(const int &cont_no, const KhiRobotData &data)
DECLSPEC_IMPORT int WINAPI krnx_GetRobotName(int cont_no, int robot_no, char *robot_name)
bool readData(const int &cont_no, KhiRobotData &data) override
KhiRobotKrnxRtcData rtc_data[KRNX_MAX_CONTROLLER]
bool writeData(const int &cont_no, const KhiRobotData &data) override
double pos[KHI_MAX_JOINT]
DECLSPEC_IMPORT int WINAPI krnx_GetRtcBufferLength(int cont_no, int robot_no)
bool setRobotDataHome(const int &cont_no, KhiRobotData &data)
KhiRobotArmData arm[KHI_MAX_ARM]
DECLSPEC_IMPORT int WINAPI krnx_GetCurMotionData(int cont_no, int robot_no, TKrnxCurMotionData *md)
DECLSPEC_IMPORT int WINAPI krnx_Execute(int cont_no, int robot_no, const char *program, int exec_num, int step_num, int *as_err_code)
bool isTransitionState(const int &cont_no)
DECLSPEC_IMPORT int WINAPI krnx_GetRtcSwitch(int cont_no, int robot_no, int *rtc_sw)
int getState(const int &cont_no)
bool setStateTrigger(const int &cont_no, const int &state_trigger)
char msg_buf[KRNX_MSGSIZE]
std::string getStateName(const int &cont_no)
bool syncRtcPos(const int &cont_no, KhiRobotData &data)
bool getCurMotionData(const int &cont_no, const int &robot_no, TKrnxCurMotionData *p_motion_data)
DECLSPEC_IMPORT int WINAPI krnx_GetCurIoInfo(int cont_no, TKrnxIoInfo *ioinfo)
DECLSPEC_IMPORT int WINAPI krnx_Ereset(int cont_no, int robot_no, int *as_err_code)
DECLSPEC_IMPORT int WINAPI krnx_GetKrnxVersion(char *ver_text, int ver_len)
void jointPrint(std::string name, const KhiRobotData &data)
bool commandHandler(khi_robot_msgs::KhiRobotCmd::Request &req, khi_robot_msgs::KhiRobotCmd::Response &res) override
DECLSPEC_IMPORT int WINAPI krnx_SetRtcCompMask(int cont_no, int robot_no, int mask)
std::mutex mutex_state[KRNX_MAX_CONTROLLER]
int sw_dblrefflt[KRNX_MAX_CONTROLLER]
bool updateState(const int &cont_no, const KhiRobotData &data) override
DECLSPEC_IMPORT int WINAPI krnx_SendRtcCompData(int cont_no, unsigned short seq_no)
DECLSPEC_IMPORT int WINAPI krnx_GetCurErrorLamp(int cont_no, int robot_no, int *error_lamp)
TKrnxStepperInfo robot[KRNX_MAX_ROBOT]
int execAsMonCmd(const int &cont_no, const char *cmd, char *buffer, int buffer_sz, int *as_err_code)
ROSCPP_DECL bool del(const std::string &key)
bool deactivate(const int &cont_no, const KhiRobotData &data) override
DECLSPEC_IMPORT int WINAPI krnx_GetProgramInfo(int cont_no, int robot_no, TKrnxProgramInfo *proginfo)
DECLSPEC_IMPORT int WINAPI krnx_Close(int sd)
float ang_ref[KRNX_MAXAXES]
double eff[KHI_MAX_JOINT]
#define KHI_KRNX_BUFFER_SIZE
bool activate(const int &cont_no, KhiRobotData &data) override
double vel[KHI_MAX_JOINT]
bool setState(const int &cont_no, const int &state)
bool open(const int &cont_no, const std::string &ip_address, KhiRobotData &data) override
void infoPrint(const char *format,...)
double home[KHI_MAX_JOINT]
bool close(const int &cont_no) override
bool loadDriverParam(const int &cont_no, KhiRobotData &data)
DECLSPEC_IMPORT int WINAPI krnx_Kill(int cont_no, int robot_no, int *as_err_code)
bool hold(const int &cont_no, const KhiRobotData &data) override
DECLSPEC_IMPORT int WINAPI krnx_GetCurErrorInfo(int cont_no, int robot_no, int *error_code)
double cmd[KHI_MAX_JOINT]
bool getPeriodDiff(const int &cont_no, double &diff) override
bool retKrnxRes(const int &cont_no, const std::string &name, const int &ret, const bool error=true)
void warnPrint(const char *format,...)
DECLSPEC_IMPORT int WINAPI krnx_SetMonSpeed(int cont_no, int robot_no, float speed, int *as_err_code)
DECLSPEC_IMPORT int WINAPI krnx_Hold(int cont_no, int robot_no, int *as_err_code)
DECLSPEC_IMPORT int WINAPI krnx_PrimeRtcCompData(int cont_no, int robot_no, const float *comp, int *status)
bool loadRtcProg(const int &cont_no, const std::string &name)
char io_do[KRNX_MAXSIGNAL/8]
bool contLimitCheck(const int &cont_no, const int &limit)