Search code examples
python-2.7loggingnao-robotchoregraphe

How to write and log to a file on my NAO robot


I'm using Choregraphe on Windows to implement programms with Python for my NAO robot. I have two problems which I can't solve by myself:

  1. I want to create a textfile on NAO robot and write information in it. Later I want to store it to my computer. Leading to this article - reading a textfile

I used the following code in a Python Box:

import logging
 filepath = os.path.join(os.path.dirname(ALFrameManager.getBehaviorPath(self.behaviorId)), "fileName.txt")
maybeContains = None
try:
    with open(filepath, "r") as textfile:
        maybeContains = textfile.readlines()
except:
    pass
with open(filepath, "a") as textfile:
    if maybeContains == "":
        agenda = "type1;type2;\n"
        textfile.write(agenda)
        textfile.write(storedData)
    else:
        textfile.write(storedData)
self.onStopped()

When I try to download the file "fileName.txt" via Connection > Advanced > File Transfer there isn't that file in one of those listed orders.

  1. I also want to create a textfile on the robot to log informations from the coding, so I can check the actions of the robot. As in 1. I want to download the log fiel to the computer.

I added to the onLoad() method of a "Say Text"-box the following code:

     def onLoad(self):
    self.logging.basicConfig(filename="20180712.log", format='%(asctime)s %(levelname)s-8s [%(filename)s:%(lineno)d]%(message)s', level=logging.DEBUG)
self.logger = self.logging.getLogger("Behavior - Box") `

    Before a command, which should be logged I call
    `    self.logger("what happened here")

Solution

  • the "Connection > Advanced > File Transfer" open a file at a specific location. Depending on the version of your robot. Some years ago it was "/var/www" or "~/ftp/" ...

    On my current NAO (2.1), it's in "/home/nao".

    So the good way in my case is to create the file at this place:

    filepath = "/home/nao/myfile.txt"
    

    That being said, there's better way to get a file from your robot, on windows, you can use winscp (gui) or pscp (cli), it's far more convenient than Choregraphe...

    Good luck.