Reading and Writing with Files

Step 7. Making real use of data from a file

If you write to a file "Time Ping 124" you are going to have a very hard time using it in another script. If you separate it with commas so that it is written as "Time,Ping,124" it is going to make it easier for a script to read that line of data and make sense of it. Because you could script reading data after the second comma, or any other place in the line because the data is separate by commas which is what allows a script to read the data in an understandable way.

But be careful how you use the commas. If your file output ends up being "Time , Ping , 124 " those spaces will probably throw off the usability of those values, because while you know the value for the ping sensor is "124", what your robot will read it as is " 124 " which renders the data nearly useless because of the extra spaces before and after the numerical value of 124.

Of course you could simply write ping data like this to a single line with only the ping sensor value, just "124" with nothing else.

Having said all that, let's make sure your file output from "WriteTest3" script is clean with a slight modification. Lets clean up our script with the FileDelete line add back. Modify the WriteTest3 script as shown, save it and run it.


#WriteTest3
$filename = "c:\temp\myfile2.txt"
FileReadClose($filename)
FileDelete($filename)

$PingValue = 124
FileWriteLine($filename,"Time:," +$time + ",Ping," + $PingValue)

sleep(100)

After running the script, open the "myfile2" and make sure it looks like this:

User-inserted image

If it does, close the file. If it does not, review the previous steps to see what you might have missed.

There is a sample script named "ReadFields" that we could use to see how to read comma separated text files, but I'm going to show you a slightly different example. Add a new script control and name it "ReadCommaFile" and enter the following code, then save the script.


# Reads a file and seperates each field by a comma.
# The Split() function will return a
# specified field index

$filename = "c:\temp\myfile2.txt"

if (FileExists($filename) = false)
  print("File does not exist")
  Halt()
endif

FileReadReset($filename)

$cnt = 1

:START

  $txt = FileReadLine($filename)

  $TimeField = Split($txt, ",", 1)
  $SensorField = Split($txt, ",", 2)
  $SensorValueField = Split($txt, ",", 3)

  print("Row: " + $cnt)
  print("Time: " + $TimeField)
  print("Sensor: " + $SensorField)
  print("SensorValue: " + $SensorValueField)

  if (FileReadEnd($filename) = true)
      FileReadClose($filename)
    print("End Of File")
    goto(END)
  endif

  $cnt = $cnt + 1

goto(START)

:END

This reads the line of data from our file and separates out the data by commas so it can be used. The data in our file was:

Time:,16:35:54,Ping,124

Going by comma position, the data breaks down as follows: Time: = 0 16:35:54 = 1 Ping = 2 124 = 3

So the line: $SensorValueField = Split($txt, ",", 3) is reading the data after the 3rd comma. And $SensorValueField is the variable we are using to store that value.

Now you can use this value elsewhere in other scripts if you need to. Lets add a new Script control and name it "NearOrFar", then enter the following code and save the script.


if ($SensorValueField > 100)
Print("I was far away from something.")
else 
Print("I was near something.")
endif

Run the script. Based on the value being 124, the script should print "I was far away from something.".

Now go back to your "WriteTest3" script and change the value from 124 to 80. Save the script and run it.

Now run the "ReadCommaFile" script again.

Now run the "NearOrFar" script again. This time the script should print "I was near something.".


ARC Pro

Upgrade to ARC Pro

Discover the limitless potential of robot programming with Synthiam ARC Pro – where innovation and creativity meet seamlessly.

#1  

can the read text be a script?

#2  

Josh, I'm not sure I understand your question.  You would want to read the text contained within an EZ-Script?

#3  

well I was thinking of a script in the notepad file , so for example if you had questions and answers worked out but you also wanted a movement to accompany a response. Like eye brows raise when answering.   Maybe im not explaining it clearly , but I was imagining using word files as large memory for responses instead of placed inline with all the other scripts and code so ARC doesnt have to pass through them every cycle making it slower the larger amount of scripting there.

#4   — Edited

I think I see where you are going.

Let me add this to the "reading" of the files and data, while I can have a txt file with data in and have an EZ-Script read it does not mean my robot has "understanding" of it.  To create the connections and understanding to understand that a word like "Justin" or "Josh" relates to a person and who that person is requires more scripting, which I think is what your point is.

Your idea is instead of adding that extra scripting in an EZ-Script is add it to your text file or note pad file.

That is a pretty cool idea and I would say you can do it, to a certain level of difficulty.  I don't think you would want to try to build out an advanced AI full of scripts in text files because I think that approach would become unmanageable.

But like you said, if you want certain actions or scripts to active when lines are read in a text a file, you could make a comma separated file and insert your scripted commands among text separated by commas, but be mindful of any commas in your command control or action script call names so you tell ReadFile to split the read back at the spots.

#5  

So I did a quick experiment and here are some issues, you can add scripted data in your text file or comma separated file for an EZ-Script to take action on, but as you read in the lines you will assign them out to variables - which you then need to translate to an action or ez-script.  I don't see a way for ARC to understand its own scripting language from a file or to directly re-translate it.

This is why in the past I have used my own short hand commands in text files for the robot to read such as:

Justin Ratliff,Smile Josh,Wave DJ,Faint

That data might be in text file for people my robot knows and my scripts then use IF-THEN logic to carry out other scripts and actions based on those indicators in the text file like "Smile, Wave, Faint, etc." as examples.

PRO
Synthiam
#6  

You can make an ARC skill plugin control that loads txt files and executes them. It'll be pretty easy to do - but i'm not sure why you'd want to do that yet. Can you give me a use-case? Is it to avoid having lots of script windows? Or to avoid having a script manager with lots of items in it?

#7  

yes basically not have a ton of script windows open

#8  

it seems like it could be cleaner if you could congregate them in a memo txt doc

PRO
Synthiam
#9  

What about the script manager? Wouldn't that be the same thing?