Lesson 14 NAO Robot Walking - levizzzle/CS490 GitHub Wiki

Lesson 14: Involved using the Choregraphe software to simulate robot actions of a NAO robot. For this lesson we were tasked with having NAO walk in a square and a triangle. Followed by writing a python script to walk in a square or triangle. We started off using multiple blocks to visualize the movements and get NAO moving but after recognizing the pattern and understanding the code we were able to combine it all into individual blocks. For the square block we are walking forward and then turning 90 degrees four times. For the triangle we are walking forward and turning 120 degrees three times. Lastly in our square or triangle block we added the random function to randomly determine which whether to walk a square or triangle. We also added code from the say block to have NAO say which shape he's walking.

Software used:

  • Choregraphe NAO robot simulation

Blocks used

  • Say
  • Move to
  • Python Scripted - Square block
  • Python Scripted - Triangle block
  • Python Scripted - Square or Triangle block

Screenshot of Choregraph block diagram

Screenshots of NAO bot walking in a square

Screenshot of NAO bot walking in a triangle

Python code for Square or Triangle block

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self, False)
        self.tts = ALProxy('ALTextToSpeech')
        self.ttsStop = ALProxy('ALTextToSpeech', True)
        self.motion = ALProxy("ALMotion")
        self.positionErrorThresholdPos = 0.05
        self.positionErrorThresholdAng = 0.05


    def onLoad(self):
        self.bIsRunning = False
        self.ids = []

    def onUnload(self):
        for id in self.ids:
            try:
                self.ttsStop.stop(id)
            except:
                pass
        while( self.bIsRunning ):
            time.sleep( 0.2 )
        self.motion.moveToward(0.0, 0.0, 0.0)


    def onInput_onStart(self):
        import almath
        import random
        # The command position estimation will be set to the sensor position
        # when the robot starts moving, so we use sensors first and commands later.
        self.bIsRunning = True
        initPosition = almath.Pose2D(self.motion.getRobotPosition(True))
        targetDistance = almath.Pose2D(self.getParameter("Distance X (m)"),
            self.getParameter("Distance Y (m)"),
            self.getParameter("Theta (deg)") * almath.PI / 180)
        expectedEndPosition = initPosition * targetDistance
        enableArms = self.getParameter("Arms movement enabled")
        self.motion.setMoveArmsEnabled(enableArms, enableArms)

        rand = random.randint(0,1)

        if rand == 0:
            for x in range(3):
                self.speak(rand)
                self.motion.moveTo(0.1,0,0 * almath.PI / 180)
                self.motion.moveTo(0,0,120 * almath.PI / 180)
        else:
            for x in range(4):
                self.speak(rand)   
                self.motion.moveTo(0.1,0,0 * almath.PI / 180)
                self.motion.moveTo(0,0,90 * almath.PI / 180)
        # The move is finished so output
        realEndPosition = almath.Pose2D(self.motion.getRobotPosition(False))
        positionError = realEndPosition.diff(expectedEndPosition)
        positionError.theta = almath.modulo2PI(positionError.theta)
        if (abs(positionError.x) < self.positionErrorThresholdPos
            and abs(positionError.y) < self.positionErrorThresholdPos
            and abs(positionError.theta) < self.positionErrorThresholdAng):
            self.onArrivedAtDestination()
        else:
            self.onArrivedAtDestination()

    def onInput_onStop(self):
        self.onUnload()

    def speak(self, rand):
        try:
            sentence = "\RSPD="+ str(100) + "\ "
            sentence += "\VCT="+ str(100) + "\ "
            if rand == 1:
                sentence += str("I'm walking in a square!")
            elif rand == 0:
                sentence += str("I'm walking in a triangle!")

            sentence +=  "\RST\ "
            id = self.tts.post.say(str(sentence))
            self.ids.append(id)
            self.tts.wait(id, 0)
        finally:
            try:
                self.ids.remove(id)
            except:
                pass
            if( self.ids == [] ):
                self.bIsRunning = False

Link to Youtube Video

Things Learned

Lesson 14 taught us how to use NAO bot motor functions to him walk. We first learned how to combine move to blocks for moving forward and turning a certain amount of degrees. After learning how the code worked for the move to function we were able to create singular blocks for moving in a square or triangle by looping through move functions and adding a random generator. Most importantly we learned how to combine code from other functions which is why we adding a say function inside of our Square or Triangle block.