1. Home
    1. Dashboard
    2. Search
  2. Forum
    1. Unresolved Threads
    2. Members
      1. Recent Activities
      2. Users Online
      3. Team Members
      4. Search Members
      5. Trophys
  3. Articles
  4. Blog
  5. Videos
  6. Jobs
  7. Shop
    1. Orders
  • Login or register
  • Search
This Thread
  • Everywhere
  • This Thread
  • This Forum
  • Articles
  • Pages
  • Forum
  • Blog Articles
  • Products
  • More Options
  1. Robotforum - Support and discussion community for industrial robots and cobots
  2. Forum
  3. Industrial Robot Support and Discussion Center
  4. Fanuc Robot Forum
Your browser does not support videos RoboDK Software for simulation and programming
Visit our Mainsponsor
IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Sponsored Ads

Creating a logfile with Karel?

  • ThePainter
  • September 22, 2014 at 12:42 PM
  • Thread is Resolved
  • ThePainter
    Reactions Received
    1
    Trophies
    3
    Posts
    48
    • September 22, 2014 at 12:42 PM
    • #1

    Hi, I have no experience of Karel programing. I need to make a program that store values from a couple of registers and put them in logfile(txt) with date stamps. Would this even be possible to do in Karel? If so, do anyone have an example of something similar?

    Thanks in advance!

  • rf103
    Reactions Received
    17
    Trophies
    4
    Posts
    264
    • September 24, 2014 at 11:24 AM
    • #2

    This is certainly possible with Karel and should not be too difficult to implement.

    You could write something that runs a simple WHILE loop (with an appropriate termination condition and DELAY statement), and periodically writes out the values of the registers you are interested in.

    Couple of pointers:


      • mark your logging program as not containing any motion: use the %NOLOCKGROUP directive for this. This would also allow you to keep your logging program running (in the background) in case of any (motion related) errors (see %NOPAUSE and %NOBUSYLAMP fi)

      • you can get the current time with the GET_TIME routine. Convert this to a string using CNV_TIME_STR. One issue is that GET_TIME returns a value with a resolution of 2 seconds. This might be an issue, depending on your requirements. If you need sub-second logging, GET_USEC_TIM might do the trick.

      • Accesing registers: GET_POS_REG, GET_REG, GET_STR_REG, etc.

      • Don't forget to terminate the lines you send to WRITE with a CR: not only does this lead to very long lines, but depending on configuration of your file descriptor, CR is actually used to signal to the interpreter that the buffer should be flushed to the device.

      • I'd install an ABORT handler, which delays aborting of your Karel program, so you can properly close your logfile. Depending on circumstances, not closing (and flushing) of FILE descriptors can lead to data loss. See Chapter 6 (Condition Handlers) of the Karel Reference Manual for this.

      • Be sure to build in some kind of limiting system: you don't want your logfile to eat up all the free space on your MD or RD device. I've noticed Fanuc controllers can suffer from memory fragmentation, where the total amount of free memory seems ok, but the largest free block is very small. This can lead to performance issues with other subsystems (such as motion).

    Hope this helps.

    Edited once, last by rf103 (September 24, 2014 at 11:30 AM).

  • scotty
    Reactions Received
    43
    Trophies
    5
    Posts
    497
    • October 1, 2014 at 3:49 PM
    • #3

    Maybe it can help you. It is simple test program to create XML file. And after you can get this file from LAN into pc and parse it.

    Code
    PROGRAM xmlt
    
    
    %COMMENT = 'cool job'
    %ENVIRONMENT REGOPE
    %NOPAUSESHFT
    %NOPAUSE = ERROR + TPENABLE + COMMAND
    
    
    CONST
    frst = '<?xml VERSION="1.0" encoding="utf-8"?>'
    opendata = '<catalog>'
    closedata = '</catalog>'
    file_name = 'test2\file_table.xml'
    VAR
    xml_file : FILE
    STATUS : INTEGER
     sr1, sr2 : STRING[5]
    BEGIN
    
    GET_STR_REG(2, sr2, STATUS)
    WHILE  ( sr2 = '1') DO
    	DELAY (500)
    	OPEN FILE xml_file ('RW', file_name)	
    	GET_STR_REG(1, sr1, STATUS)
    	GET_STR_REG(2, sr2, STATUS)
    	WRITE xml_file ( frst, CR)
    	WRITE xml_file ( opendata, CR)
    	WRITE xml_file ( '<costumer id = "1"> SR[1] =' + sr1 + '</costumer>',CR)
    	WRITE xml_file ( closedata, CR)
    	CLOSE FILE xml_file
    ENDWHILE
    
    
    END xmlt
    Display More
  • aleinen18
    Trophies
    3
    Posts
    10
    • June 28, 2016 at 6:31 PM
    • #4

    Hello,
    I know this is an old topic...but I was wondering why I am not getting the Seconds to output with the following code:

    GET_TIME(intTime)
    CNV_TIME_STR(intTime, time_str)
    WRITE ('Date: ' , SUB_STR(time_str, 1, 9), CR)
    WRITE ('Time: ' , SUB_STR(time_str,11,5), CR)
    WRITE('Full String: ',time_str, CR)

    My output reads:

    Date: 28-JUN-16
    Time: 11:26
    Full String: 28-JUN-16 11:26

    Any ideas?
    Thank you

  • skalactik
    Reactions Received
    13
    Trophies
    3
    Posts
    111
    • June 28, 2016 at 6:42 PM
    • #5
    Quote

    A.4.34 CNV_TIME_STR Built-In Procedure

    Purpose: Converts an INTEGER representation of time to a STRING

    Syntax : CNV_TIME_STR(source, target)

    Input/Output Parameters :

    [in] source :INTEGER

    [out] target :STRING

    %ENVIRONMENT Group :TIM

    Details:

    l The GET_TIME Built-In Procedure is used to determine the INTEGER representation of time. CNV_TIME_STR is used to convert source to target , which will be displayed in ``DD-MMM-YYY HH:MM:'' format.

    Display More


    from what i read, the routine does not convert the values for seconds

  • aleinen18
    Trophies
    3
    Posts
    10
    • June 28, 2016 at 6:50 PM
    • #6

    Skalactik, I read that too, and makes me think it is not possible. But if you read under the GET_TIME Built-In Procedure it states, "Use the CNV_TIME_STR built-in procedure to convert the INTEGER into the "DD-MMM-YYY-HH:MM:SS" STRING format. The manual is not really clear, but from testing I'm thinking I cannot get the seconds to output.

  • skalactik
    Reactions Received
    13
    Trophies
    3
    Posts
    111
    • June 29, 2016 at 1:00 PM
    • #7

    Tried it with Roboguide, produced the same result.
    I guess your best bet is to retrieve the seconds from the integer value out of GET_TIME.

  • jjessup
    Reactions Received
    4
    Trophies
    3
    Posts
    42
    • June 29, 2016 at 6:20 PM
    • #8

    We're been doing this for a couple of months now, with good success.
    I've got a karel program that gets called while sending the robot home. It then stores the previous job number, timestamp, resin and glass weights, and if we're spraying the whole cart, or only a section of the cart.

    PROGRAM spray_log
    VAR
    file_var : FILE
    glass_weight : REAL
    resin_weight : REAL
    glass_status : INTEGER
    resin_status : INTEGER
    gtemp : INTEGER
    rtemp : INTEGER
    programname : STRING[5]
    now : INTEGER
    timestamp : STRING[20]
    wallside4 : STRING[2]
    wtemp : REAL
    wstatus : INTEGER
    wallreg : INTEGER
    BEGIN
    GET_TIME(now)
    CNV_TIME_STR(now,timestamp)
    GET_REG(1,TRUE,rtemp,resin_weight,resin_status)
    GET_REG(2,TRUE,gtemp,glass_weight,glass_status)
    GET_REG(20,FALSE,wallreg,wtemp,wstatus)
    CNV_INT_STR(wallreg,2,0,wallside4)
    IF NOT UNINIT(programname) THEN
    OPEN FILE file_var('AP', 'fr:spray_log.xml')
    WRITE file_var ('<part><ts>',timestamp,'</ts><job>',programname+wallside4,'</job><rw>', resin_weight::9::2 ,'</rw><gw>', glass_weight::9::2,'</gw></part>' ,CR)
    CLOSE FILE file_var
    ENDIF
    programname=$TP_DEFPROG
    END spray_log

  • Fabrice
    Trophies
    3
    Posts
    3
    • August 25, 2016 at 11:51 AM
    • #9

    Hello,

    I want to log different data during production. I know how to log the data now but does any body know how to check the size of the logfile?
    I want to delete the log file if size becomes to big to avoid that the memory becomes full.

    Thanks in advance,
    Regards,
    Fabrice

  • scotty
    Reactions Received
    43
    Trophies
    5
    Posts
    497
    • August 25, 2016 at 10:55 PM
    • #10

    If you are using xml type, then you can previously make deseriallization of your data file and after that check how much records do you have. It should pretty much equal to your requirement. Also you need to check how much records satisfy your requirement for file size. And after you can decide do you want to delete the file or not. I mixed few sentences but I hope you understand.

  • Fabrice
    Trophies
    3
    Posts
    3
    • August 29, 2016 at 9:54 AM
    • #11

    Hi Scotty,

    Thanks for the information.
    Is it really needed to use xml file, what will you mean by deseriallization?

    If I follow you idea I can maybe use this instruction GET_FILE_POS to check the amount of records.

    Reagrds,
    Fabrice

  • scotty
    Reactions Received
    43
    Trophies
    5
    Posts
    497
    • August 29, 2016 at 7:33 PM
    • #12

    Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

    If you want to have file with structured data type then I recommend to make xml structure and take a look to XML topic in KAREL Reference Manual. In opposite case you can use any other solution.

    Recommendation from Fanuc:
    GET_FILE_POS is only supported for files opened on the RAM Disk device. Do not use GET_FILE_POS on another device; otherwise, you could injure personnel and damage equipment.

    Regards,
    Scotty

    Edited once, last by scotty (September 1, 2016 at 3:43 PM).

  • JT
    Reactions Received
    5
    Trophies
    3
    Posts
    10
    • August 31, 2016 at 10:56 AM
    • #13

    To check the file size you can use the FILE_LIST built-In procedure:

    ----------------------------------------------------------
    --- Display a directory listing of files on the MC drive:
    file_spec = 'MC:\*.*' -- All files in MC drive
    n_skip = 0 -- First time do not skip any files
    format = 4 -- Return list in filename.filetype size date time - format
    WRITE ('Doing FILE list',CR)
    REPEAT -- UNTIL all files have been listed
    FILE_LIST(file_spec, n_skip, format, ary_nam, n_files, STATUS)
    IF (STATUS <>0 ) THEN -- Error occurred
    WRITE ('FILE_LIST builtin failed WITH STATUS = ',STATUS,CR)
    ELSE
    displ_list(n_files) -- Write the names to the TP USER menu
    n_skip = n_skip + n_files -- Skip the files we already got.
    ENDIF
    UNTIL (ARRAY_LEN(ary_nam) <> n_files) -- When n_files does not equal
    ----------------------------------------------------------------------------

    The file size is written in:
    ary_nam :ARRAY[100] OF STRING[40] -- Returned names format: filename.filetype size date time
    and always starts with char 21

    Date and time is only supported if the device supports timestamping. I guess this is related to a controller SW-version...

  • Fabrice
    Trophies
    3
    Posts
    3
    • September 2, 2016 at 11:52 AM
    • #14

    Thanks for you replies.

    I will use the following code and write in the ram memory.

    str_fn = 'rd:\CycleTime.txt'
    CLOSE FILE logfile
    OPEN FILE logfile('AP',str_fn) -- Appends data
    FileSize=GET_FILE_POS(logfile)
    IF FileSize>1000000 THEN
    CLOSE FILE logfile
    OPEN FILE logfile('RW',str_fn) -- Rewrites - Delete existing data
    ENDIF

  • JT
    Reactions Received
    5
    Trophies
    3
    Posts
    10
    • September 2, 2016 at 2:59 PM
    • #15

    Or you could use a PIP-file in case memory-management is an issue, it offers following functionality:
    -Read and write data at the same time. (a Karel program fills the buffer while another retrieves information of interest)
    -Rolling buffer

    The max buffer size is determined by the PERM cmos memory (menu>statu>memory) if 'cmos_flag=TRUE' is enabled. 700kb equals +- 20 000 lines of 40 char logging data.


    PROGRAM pip_write
    %NOLOCKGROUP
    %NOPAUSE= COMMAND + TPENABLE + ERROR

    VAR
    out_file:FILE
    console:FILE
    cmos_flag: BOOLEAN
    n_sectors: INTEGER
    record_size: INTEGER
    form_dict: STRING[32]
    form_ele: INTEGER
    STATUS:INTEGER
    i: INTEGER

    BEGIN
    FORCE_SPMENU(TP_PANEL,SPI_TPUSER,1)--activate user
    WRITE(CHR(128),CR)--clear user screen

    cmos_flag=TRUE -- CMOS_flag if set to TRUE will put the pipe data in CMOS. By default pipe data is in DRAM.
    -- If CMOS_FLG = False; n_sectors will stay default 8 ==> 8 * 1024 = 8192 characters
    -- Put CMOS_FLG = TRUE to change the n_sectors. (example 32 ===> 32 * 1024 = 32768 bytes of logfile)
    n_sectors = 700 --n_sectors number of 1024 byte sectors to allocate to the pipe. The default is 8
    record_size = 0 --record_size the size of a binary record in a pipe. If set to 0 the pipe is treated as ASCII
    form_dict = 'test'
    form_ele = 1
    PIPE_CONFIG( 'PIP:JT_1.dat', cmos_flag, n_sectors, record_size, form_dict, form_ele, STATUS) --need to be called before file is open

    OPEN FILE out_file ('rw', 'PIP:JT_1.dat')-- open file for write to device PIP:
    STATUS = IO_STATUS(out_file)
    WRITE TPDISPLAY('status open file for write',STATUS,CR)
    FOR i = 0 TO 20005 DO
    WRITE out_file ('nr', i , ' - Data that I am logging', CR)
    ENDFOR
    STATUS = IO_STATUS(out_file)
    WRITE TPDISPLAY('status after writing',STATUS,CR)
    CLOSE FILE out_file

    END pip_write

  • rozzy323
    Trophies
    1
    Posts
    6
    • February 15, 2025 at 9:26 PM
    • #16

    Do you need to have the (txt) file loaded to the controll or does it create the file itself? I am trying something similar, I want to track and log whenever (2) registers change value and include a timestamp in the log and I've been having a time with it. I want this to run as a bg program or simultaneously with my main program to track at all times when the registers change. The program will execute whether I call or run but I can't find the log file where things are being saved. I included a .TP for looping changing these registers for testing purposes.

    PROGRAM TRACK_REGS
    %NOLOCKGROUP
    %NOPAUSE = ERROR + COMMAND + TPENABLE

    -- DECLARATIONS

    CONST
    -- SERVERITY CONSTANTS
    SUCCESS = 0
        
    VAR
    REG95_VALUE : INTEGER
    REG100_VALUE : INTEGER
    PREV95_VALUE : INTEGER
    PREV100_VALUE : INTEGER
    FILE_ID : FILE
    status : INTEGER
    time_str : STRING[24]
    sLogFileName : STRING [24]
    iStatusOpen : INTEGER
    iStatusWrite : INTEGER
    bool : BOOLEAN
    rel : REAL
    current_time : INTEGER
        
    --function to get current time as string
    ROUTINE get_current_time(time_str : STRING)
    VAR
    current_time : INTEGER
    BEGIN
    GET_TIME(current_time)
    CNV_TIME_STR(current_time, time_str)
    END get_current_time

    BEGIN
    --initialize variables
    PREV95_VALUE = -99999
    PREV100_VALUE = -99999
        
    WRITE TPERROR (CHR(128))
    sLogFileName = 'RD:REGISTER_LOG.TXT'
        
    --open log file
    OPEN FILE file_id ('RW', sLogFileName)
    iStatusOpen = IO_STATUS(file_id)
    IF (iStatusOpen <> SUCCESS) THEN
    WRITE TPERROR ('[TRACK_REGISTER_CHANGES_TEST] cant open file:', iStatusOpen, CR)
    ABORT
    ENDIF
        
    --MAIN LOOP
    REPEAT
    --GET THE CURRENT VALUES OF REGISTER 95 AND 100
    GET_REG(95, bool, REG95_VALUE, rel, status)
    GET_REG(100, bool, REG100_VALUE, rel, status)
            
    --compare with previous values and log changes
    IF REG95_VALUE <> PREV95_VALUE THEN
    --get current time
    get_current_time(time_str)
                
    --log the change for register 95
    WRITE FILE_ID ('register95 changed to:', reg95_value, 'at', time_str, CR)
                
    PREV95_VALUE = REG95_VALUE
    ENDIF
            
    --compare with previous values and log changes
    IF REG100_VALUE <> PREV100_VALUE THEN
    --get current time
    get_current_time(time_str)
                
    --log the change for register 95
    WRITE FILE_ID ('register100 changed to:', reg100_value, 'at', time_str, CR)
                
    PREV100_VALUE = REG100_VALUE
    ENDIF
            
    DELAY (2000)
    UNTIL (REG95_VALUE = 10000)
        
    CLOSE FILE FILE_ID
    END TRACK_REGS
            
    1: ;
    2: RUN TRACK_REGS ;
    3: ;
    4: LBL[1] ;
    5: R[100]=3 ;
    6: R[95]=35 ;
    7: R[95]=R[95]+1 ;
    8: ;
    9: R[100]=15 ;
    10: JMP LBL[1] ;
    /POS
    /END

  • YakawFaBB
    Reactions Received
    12
    Trophies
    1
    Posts
    72
    • February 17, 2025 at 8:15 AM
    • #17

    Hi rozzy323,

    Normally the file should create itself, but you need to change your instruction below:

    If the file doesn't exist before you can change RW to AP for "APPEND"

    Code
    OLD:
    OPEN FILE file_id ('RW', sLogFileName)
    NEW:
    OPEN FILE file_id ('AP', sLogFileName)

  • rozzy323
    Trophies
    1
    Posts
    6
    • February 17, 2025 at 2:39 PM
    • #18

    YakawFaBB


    I found that my program is working but it is overwriting the same line in the log file, any thoughts on how to fix that?

  • Online
    panic mode
    Reactions Received
    1,278
    Trophies
    11
    Posts
    13,079
    • February 17, 2025 at 3:48 PM
    • #19

    In any programming language use "append" to access file when goal is to add something to a file, but not lose any of existing content.

    Pseudo code:

    Code
    Log(string msg)
    If  not fileexists(filename) then
        Write_txt(filename, msg); // file is missing, so. Create new one, using WRITE
    Else
        Append_txt(filename,msg); // file already exists, assume it contains something and use APPEND
    Endif
    
    End // end of LOG()

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

Advertising from our partners

IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Advertise in Robotics
Advertise in Robotics

Job Postings

  • Anyware Robotics is hiring!

    yzhou377 February 23, 2025 at 4:54 AM
  • How to see your Job Posting (search or recruit) here in Robot-Forum.com

    Werner Hampel November 18, 2021 at 3:44 PM
Your browser does not support videos RoboDK Software for simulation and programming

Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Thread Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000
  1. Privacy Policy
  2. Legal Notice
Powered by WoltLab Suite™
As a registered Member:
* You will see no Google advertising
* You can translate posts into your local language
* You can ask questions or help the community with your knowledge
* You can thank the authors for their help
* You can receive notifications of replies or new topics on request
* We do not sell your data - we promise

JOIN OUR GREAT ROBOTICS COMMUNITY.
Don’t have an account yet? Register yourself now and be a part of our community!
Register Yourself Lost Password
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on Google Play
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on the App Store
Download