Emilia Clarke Emilia Clarke - 1 month ago 11
Python Question

iRobot Create - Playing two songs

Here's my code:

import create
robot = create.Create(3)
robot.toFullMode()

robot.setSong(0,[(36,16),(36,16),(38,32),(36,32),(53,32),(52,32),\
(36,16),(36,16),(38,32),(36,32),(55,32),(41,32),\
(36,16),(36,16),(48,32),(33,32)])

robot.setSong(1,[(53,32),(40,32),(38,32),(34,16),(34,16),\
(33,32),(41,32),(43,32),(41,32)])

robot.playSongNumber(0)
robot.playSongNumber(1)


The first song plays, but the second one won't...any ideas as to why?
This is being coded with the
create.py
Python interface to the iRobot Create:

Answer

Without seeing any documentation or source code, this is a wild guess, but…

I'll bet the playSongNumber function just sends the robot the play command, without waiting for it to finish, and when you send it a play command while it's already playing something, it ignores you.

If I'm right, then hopefully the API provides some way to wait until it's done. If it doesn't provide that, but it does provide a way to poll the current state, you can do that in a loop (sleeping briefly between each check) until it's done. If it doesn't even provide that, then you have to work out how long the song will take by counting the number of notes (or adding the durations, if one of the numbers in each of those pairs is a duration) and sleep that long.

Comments