Aldebaran Robots Programmbeispiele

Aus TippvomTibb
Zur Navigation springen Zur Suche springen

Allgemeines

[Schlagwoerter: NAO Examples Beispiele ChatBot Chat Bot] Ich weiß noch nicht so recht wie ich diese Seite(n) gestalten soll. Ich habe schon viele Programme aus dem Aldebaran Store, von Github und anderen Quellen ausprobiert. Der Erfolg war durchwachsen. Ich will den Erstellern keine Vorwuerfe machen, es ist ja sehr lobenswert, dass sie den Quellcode ueberhaupt zur Verfuegung stellen, aber sind doch nicht selten Anpassungen notwendig um ein Programm ueberhaupt zum Laufen zu bewegen. Manchmal war der Aufwand sogar so groß, oder das Programm so bescheiden dokumentiert, dass sich jeder weitere Aufwand kaum lohnt.

Ich koennte mir vorstellen, jedes Programm hier im Wiki mit einer Art Steckbrief zu versehen, so dass man moeglichst schnell einen Eindruck des Umfangs und der Leistung bekommt. Dazu koennte ich mir vorstellen einen Parser für die CRG-Files zu schreiben der einem das Grundgeruest des Steckbriefes liefert.

Ich fang mal an einzelne Codeschnipsel zu kommentieren.

AIDA-bot (Github)

So sah das Original aus.

Screenshot 20230224 115407.png

Was ein bisschen Umsortieren zum Verstaendnis beitraegt:-))

Screenshot 20230224 200953.png

Ablauf:

-SetLanguage English
-StandInit
-AIDA-Bot ->onWelcome
    def onInput_onWelcome(self):
        setMessage('WELCOME_MSG') # im template dict hinterlegt
        self.speech(zora['answer'])
        self.Movement(zora['action'])
AIDA-Bot erzeugt ein speech- und Movement-String
'Say Text'-Box stoppt die das parallel angestoszene Movement.(?)
'Motion'-Box Hauptrichtung 'Welcome'-Box oder Exit 'GoodBye'-Box mit 'Crouch'-Box
'Welcome'-Box spielt per Zufall eine der drei Motions (keine Behaviers) ab 'open arms', 'Bow' und 'WideGather'
'Stand'-Box im Hauptpfad ein individueller Ausgang neben success, failure ist dstart # Warum hier dstart gebraucht wird ist mir nicht ganz klar; Die Dialog-Box haette man wohl auch an success haengen koennen
-failure wird nicht ausgewertet
-success und dstart aktivieren geleichzeitig
'Nao_bored'-Box und 'Dialog'-Box
'Dialog'-Box Languages: American English (Add to the package content as collaborative dialog) An example of multilanguage dialog implementation.
'Nao_bored'-Box ist eine Boxgruppe
-active ist mit einer Verzoegerung 'Delay'-Box von 20 Sekunden
-stop
-allstop Wirkung ?



Random-Box

Steckt in der Welcome-Box, in der Nao_bored->Actions-Box und in der GoodBye-Box.

Screenshot 20230223 220616.png

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self)

    def onLoad(self):
        #put initialization code here
        pass

    def onUnload(self):
        #put clean-up code here
        pass

    def onInput_onStart(self):
        #self.onStopped() #activate the output of the box
        pass
        animations = [self.action1,self.action2,self.action3]
        action = animations[int(random.randint(0, len(animations) - 1))]
        action()

    def onInput_onStop(self):
        self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
        self.onStopped() #activate the output of the box

Interessant sind dabei die 3 folgenden Zeilen:

       animations = [self.action1,self.action2,self.action3]
       action = animations[int(random.randint(0, len(animations) - 1))]
       action()

animations wird eine Liste mit den EInsprungpunkten der drei "Ausgangs-Bangern". action bekommt per Zufall eine der drei Funktionen zugewiesen und ruft diese mit action() dann auf.

Screenshot 20230224 100155.png

Auf die Idee waere ich so nicht gekommen. Ich haette es mit random und nachfolgend einem switch ralisiert.

GetImage

Hole einen Schnappschuss von einem ueber das Netzwerk erreichbaren NAOs.

 1 #! /usr/bin/env python
 2 # -*- encoding: UTF-8 -*-
 3 
 4 """Example: Shows how images can be accessed through ALVideoDevice"""
 5 
 6 import qi
 7 import argparse
 8 import sys
 9 import time
10 import vision_definitions
11 from PIL import Image
12 
13 
14 
15 def main(session):
16     """
17     This is just an example script that shows how images can be accessed
18     through ALVideoDevice in Python.
19     Nothing interesting is done with the images in this example.
20     """
21     # Get the service ALVideoDevice.
22 
23     video_service = session.service("ALVideoDevice")
24 
25     # Register a Generic Video Module
26     resolution = vision_definitions.kQQVGA
27     colorSpace = vision_definitions.kYUVColorSpace
28     fps = 20
29 
30     nameId = video_service.subscribeCamera("python_GVM",0, resolution, colorSpace, fps)
31 
32     print ('getting images in remote')
33     for i in range(0, 2):
34         print ("getting image " + str(i))
35         t0 = time.time()
36         naoImage = video_service.getImageRemote(nameId)
37         t1 = time.time()
38         print ("acquisition delay ", t1 - t0)
39 
40         # Now we work with the image returned and save it as a PNG  using ImageDraw
41         # package.
42 
43         # Get the image size and pixel array.
44         imageWidth = naoImage[0]
45         imageHeight = naoImage[1]
46         array = bytes(naoImage[6])
47 
48         # Create a PIL Image from our pixel array.
49         #im = Image.frombytes("RGB", (imageWidth, imageHeight), array, 'raw', 'BGR', 0, -1)
50         #im = Image.frombytes("RGB", (imageWidth, imageHeight), array)
51         # Save the image.
52         #im.save("camImage"+str(i)+".png", "PNG")
53         
54         #imsaved = Image.open(r"camImage"+str(i)+".png")
55         imsaved = Image.open(r"index.jpeg")
56         imsaved.show("index.jpeg")
57 
58     video_service.unsubscribe(nameId)
59 
60 if __name__ == "__main__":
61     parser = argparse.ArgumentParser()
62     parser.add_argument("--ip", type=str, default="127.0.0.1",
63                         help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
64     parser.add_argument("--port", type=int, default=9559,
65                         help="Naoqi port number")
66 
67     args = parser.parse_args()
68     session = qi.Session()
69     try:
70         session.connect("tcp://" + args.ip + ":" + str(args.port))
71     except RuntimeError:
72         print ("Can't connect to Naoqi at ip \"" + args.ip + "\" on port " + str(args.port) +".\n"
73                "Please check your script arguments. Run with -h option for help.")
74         sys.exit(1)
75     main(session)
colors = [(0,128,0), (255,48,29), (48,48,48)]
colors = [bytes(color) for color in colors]
print(colors)

# Some fake image data
data = [[(u+v) % 3 for u in range(4)] for v in range(3)]
print(data)

# Three ways to convert the palette numbers in `data` to RGB bytes

# By concatenation 
img_str = b''
for row in data:
    for col in row:
        img_str += colors[col]
print(img_str)

# By joining the RGB bytes objects
a = b''.join([colors[col] for row in data for col in row])
print(a)

# By splitting the RGB bytes objects and building 
# a new bytes object of the whole image
b = bytes(k for row in data for col in row for k in colors[col])
print(b)    

output

[b'\x00\x80\x00', b'\xff0\x1d', b'000']
[[0, 1, 2, 0], [1, 2, 0, 1], [2, 0, 1, 2]]
b'\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000'
b'\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000'
b'\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000\x00\x80\x00\xff0\x1d000\x00\x80\x0

GetVideo

Ein erster Versuch remote auf die Kameras des NAO zuzugreifen. Ich bin auf die Bottom ausgewichen, da die Top-Kamera von einem naoqi-Prozess besetzt (Device or Resource busy) war.

$ fuser /dev/video0
/dev/video0: 1871m
$ ps axl | grep 1871
gst-launch-0.10 -v v4l2src device=/dev/video-bottom ! video/x-raw-yuv,width=640,height=480,framerate=30/1 ! ffmpegcolorspace ! jpegenc ! multipartmux! tcpserversink port=3000 
vlc tcp://ip.of.the.robot:3000

Screenshot 20230226 101740.png

Allerdings kommt das Video mit extremer Verzoegerung!

Request for Comments


Kommentar hinzufügen
TippvomTibb freut sich über alle Kommentare. Sofern du nicht anonym bleiben möchtest, registriere dich bitte oder melde dich an.