Fanuc Tcp/Ip app with Karel

  • Greetings,


    I want to make Python app that would communicate with Fanuc robot via TCP/IP. The goal is to select a program to run on TP.

    I believe that I need to use Karel to read data from Python app. Is it possible to get some small piece of code as an example of how to do that?

    I am totally new in Karel so it would be very helpful just to try some simple example of reading data and calling program in Karel via TCP/IP.


    Thank You!

  • Damek,


    Its not a simple process. You will need to rely heavily on the Fanuc manual for Karel.

    Here is an overview:

    1. You must have Karel and User Socket Messaging software option on the robot

    2. Configure a Socket Messaging server in the Host Comm settings of the robot. Pay attention to the SERVER_PORT setting.

    3. Create the Karel program using the Socket commands. These are in the Karel manual. These will look like the following:


    sock_var : FILE

    STATUS : INTEGER


    MSG_CONNECT('S3', STATUS) --Where S3 is the Server you configured for socket messaging.

    OPEN FILE sock_var ('rw', 'S3') --Where sock_var is a variable you declared of type type FILE.

    You will also need the commands READ, WRITE, BYTES_AHEAD, and MSG_DISCO.

  • Thank You. I am currently trying to establish a connection between the Fanuc robot in Roboguide and my Python app.

    Is this even possible? I want the robot to be a client and the app needs to act as a server. Is it possible to simulate that kind of connection in Roboguide? Or it needs to be done on a real robot?

    Thank You!

  • I managed to establish a connection. My next question is how to run the Karel program for connecting to the server and collecting data in the background? I don't want to call it from TP. I want to run it every time I start the controller. Is this possible?

  • I managed to establish a connection and run a simple program in Karel and in TP in parallel.

    But I have a problem with Roboguide. It keeps crashing every time I manage to establish a connection. Also when I run Karel script parallel with a simple TP program (movements between two points) everything starts lagging and robot movements are very slow and in big steps.

    What am I doing wrong? I attached Karel program and TP program.


    -------------------------------

    PROGRAM kc

    %STACKSIZE = 4000

    %NOLOCKGROUP

    %NOPAUSE=ERROR+COMMAND+TPENABLE

    %ENVIRONMENT uif

    %ENVIRONMENT sysdef

    %ENVIRONMENT kclop

    %ENVIRONMENT bynam

    %ENVIRONMENT fdev

    %ENVIRONMENT flbt

    %INCLUDE klevccdf

    %INCLUDE klevkeys

    %INCLUDE klevkmsk



    VAR

    file_var : FILE

    tmp_int : INTEGER

    tmp_str : STRING[128]

    status : INTEGER

    entry : INTEGER

    loop1 : BOOLEAN


    BEGIN

    label::

    SET_FILE_ATR(file_var, ATR_IA)

    SET_VAR(entry, '*SYSTEM*','$HOSTC_CFG[1].$SERVER_PORT',8000,status)

    WRITE(' VAR status = ',status,CR)


    MSG_CONNECT('C1:',status)

    WRITE(' Connect status = ',status,CR)

    IF status = 1 THEN

    GOTO label_2

    ENDIF

    GOTO label


    label_2::

    MSG_PING('C1:',status)

    IF status = 0 THEN

    MSG_DISCO('C1:',status)

    WRITE('Disconnect status=',status,CR)

    GOTO label

    ENDIF


    GOTO label_2


    END kc

    ----------------------------------------------------



    tp program:

    ---------------------------------------------------

    RUN kc


    LBL[1]


    J PR[1] FINE 100%

    J PR[2] FINE 100%


    JLBL[1]

  • It looks like you created a loop with your GOTO statements without any DELAY in the loop. In KAREL you need a DELAY in a loop or it will take all of the CPU resources and lockup the controller. Try adding DELAY 50.

  • Thank You, I didn't know that. However, I tried to add some delays into the code but it still doesn't work correctly.

    Every time when my loop is iterating in Karel the robot moves with lag. If I put a delay to 3000 for example, the lag occurs every 3 s. Any advice on how to solve that? (I am doing everything in Roboguide for now so I don't know if a real robot will behave in the same way).

  • Do you have only one delay in your code? Where exactly.

    Post your actual code. Should look like:

  • PROGRAM kc

    %STACKSIZE = 4000

    %NOLOCKGROUP

    %NOPAUSE=ERROR+COMMAND+TPENABLE

    %ENVIRONMENT uif

    %ENVIRONMENT sysdef

    %ENVIRONMENT kclop

    %ENVIRONMENT bynam

    %ENVIRONMENT fdev

    %ENVIRONMENT flbt

    %INCLUDE klevccdf

    %INCLUDE klevkeys

    %INCLUDE klevkmsk



    VAR

    file_var : FILE

    tmp_int : INTEGER

    tmp_str : STRING[128]

    status : INTEGER

    entry : INTEGER

    loop1 : BOOLEAN

    s : STRING[25]


    BEGIN

    MSG_DISCO('C1:',status)


    label::


    DELAY 100


    SET_FILE_ATR(file_var, ATR_IA)

    SET_VAR(entry, '*SYSTEM*','$HOSTC_CFG[1].$SERVER_PORT',8000,status)

    WRITE(' VAR status = ',status,CR)

    MSG_CONNECT('C1:',status)

    WRITE(' Connect status = ',status,CR)


    DELAY 100


    IF status = 0 THEN

    WRITE(' spojeno',status,CR)

    MSG_DISCO('C1:',status)

    DELAY 100

    WRITE(' Odspojeno',status,CR)

    ENDIF


    DELAY 100


    GOTO label


    END kc


    ---------------------------------------


    Yes, I tried with a few delays but I still get the same behavior.

  • Here is the full code. The idea is that the Karel program works in the background and on the Python app demands changes DO and get register values. I tested locally and everything works. The problem is only that when I run simultaneously Karel and TP program (a simple program that moves the robot through few points) the robot in Roboguide starts lagging and moving in big steps. I tried with a few delays but that didn't help. Any other ideas?


    Thank you!

    -----------------------------------------------------


    PROGRAM kc_test

    %STACKSIZE = 4000

    %NOLOCKGROUP

    %NOPAUSE=ERROR+COMMAND+TPENABLE

    %ENVIRONMENT uif

    %ENVIRONMENT sysdef

    %ENVIRONMENT kclop

    %ENVIRONMENT bynam

    %ENVIRONMENT fdev

    %ENVIRONMENT flbt

    %ENVIRONMENT REGOPE

    %INCLUDE klevccdf

    %INCLUDE klevkeys

    %INCLUDE klevkmsk



    VAR

    file_var : FILE

    tmp_int : INTEGER

    tmp_str : STRING[128]

    status : INTEGER

    entry : INTEGER

    loop1 : BOOLEAN

    broj_kom : INTEGER

    status_2 : INTEGER

    broj_kom_str : STRING[128]

    r_val : REAL

    rflag : BOOLEAN


    BEGIN


    MSG_DISCO('C1:',status)


    label::


    DELAY 100


    SET_FILE_ATR(file_var, ATR_IA)

    SET_VAR(entry, '*SYSTEM*','$HOSTC_CFG[1].$SERVER_PORT',8000,status)

    WRITE(' VAR status = ',status,CR)

    MSG_CONNECT('C1:',status)

    WRITE(' Connect status = ',status,CR)

    loop1 = TRUE

    IF status = 0 THEN

    WHILE loop1 = TRUE DO

    DELAY 100

    WRITE('Opening file...',CR)

    OPEN FILE file_var('rw','C1:')

    status = IO_STATUS(file_var)

    IF status = 0 THEN

    WRITE('Waiting to read from server...')

    DELAY 100

    READ file_var(tmp_str::2)

    WRITE('Read: ',tmp_str::2,CR)

    DELAY 100


    IF tmp_str = 'P1' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[5] = ON

    DELAY 500

    ENDIF


    IF tmp_str = 'P2' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[6] = ON

    ENDIF


    IF tmp_str = 'P3' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[7] = ON

    ENDIF


    IF tmp_str = 'P6' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[8] = ON

    ENDIF


    IF tmp_str = 'P9' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[9] = ON

    ENDIF


    IF tmp_str = '12' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[10] = ON

    ENDIF


    IF tmp_str = 'S6' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[11] = ON

    ENDIF


    IF tmp_str = 'S9' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[12] = ON

    ENDIF


    IF tmp_str = '13' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[13] = ON

    ENDIF


    IF tmp_str = '14' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[14] = ON

    ENDIF


    IF tmp_str = 'F6' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[15] = ON

    ENDIF


    IF tmp_str = '15' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[16] = ON

    ENDIF


    IF tmp_str = 'F9' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[17] = ON

    ENDIF


    IF tmp_str = 'CO' THEN

    FOR tmp_int = 1 TO 18 DO

    DOUT[tmp_int] = OFF

    ENDFOR

    DOUT[1] = ON

    DOUT[2] = ON

    ENDIF


    IF tmp_str = '99' THEN

    DOUT[4] = ON

    CLOSE FILE file_var

    MSG_DISCO('C1:',status)

    GOTO label

    ENDIF


    IF tmp_str = 'BR' THEN

    GET_REG(1, rflag, broj_kom, r_val, status_2)

    CNV_INT_STR(broj_kom, 0, 0, broj_kom_str)

    WRITE file_var(broj_kom_str)

    CLOSE FILE file_var

    ENDIF


    IF tmp_str = 'RS' THEN

    SET_REAL_REG(1,0, status_2)

    DELAY 50

    GET_REG(1, rflag, broj_kom, r_val, status_2)

    CNV_INT_STR(broj_kom, 0, 0, broj_kom_str)

    WRITE file_var(broj_kom_str)

    CLOSE FILE file_var

    ENDIF


    CLOSE FILE file_var

    ELSE

    GOTO label

    ENDIF

    ENDWHILE

    ENDIF

    DELAY 200

    GOTO label


    END kc_test

  • I don't think there is anyway to do it "out of the box". I think you would have to use the socket messaging option and implement TCP/IP on your own. They way I have done it in the past is use a library that implemented Ethernet/IP messaging and communicated directly to the robot that way. I didn't use python but I have used C# and C++. I'm sure there's probably a python library out there.


    c# implementation:

    https://github.com/rossmann-engineering/EEIP.NET

Advertising from our partners