Python Create Library Overview

Your Python program uses the    
create
   library.
It supplies functions that send commands:
to your laptop’s Bluetooth radio,
then to the BAM on the Create,
then to the Create’s controller.
Ditto for the reverse path.
The   
create
   module
(library)
 
 
 
 
 
r
o
b
o
t
.
g
o
(
2
0
,
 
0
)
<
n
e
x
t
 
s
t
a
t
e
m
e
n
t
>
 
.
.
.
<
n
e
x
t
 
s
t
a
t
e
m
e
n
t
>
 
.
.
.
<
m
u
c
h
 
f
u
r
t
h
e
r
 
i
n
 
p
r
o
g
r
a
m
>
 
.
.
.
The robot “commands” in the Python create
module simply 
send messages
 
to the robot
Almost all of the robot commands in the create module do NOT BLOCK.
That is, as soon as the message is sent, they continue to the next statement
(long before the message has arrived to the robot).
The only BLOCKING commands are:
the 
constructor
 (waits for an acknowledgment that the connection exists), and
getSensor
 (waits for the sensor value to arrive).
This causes some
commands to behave in
ways you might not expect.
The   
create
   module
(library)
Students:  The rest of these slides present some of
the most common methods in the 
create
 module:
Connecting and disconnecting
Motion
Sensors
Singing
 
Don’t memorize the details, but DO remember the
basics and refer back to this document and others
as needed.
Connecting and Disconnecting
port = 
18
robot = create.Create(port)
robot.toFullMode()
   ...
robot.shutdown()
Set the port per your Bluetooth setup.
Or use 
'sim' 
for the simulator.
If the program breaks at this point with a message
AttributeError:
'Create' object has no attribute 'ser'
then it failed to make the Bluetooth connection.
In this case, try:
Turn the robot off.
Wait 10 seconds.
Turn the robot on.
After the robot beeps, run your program again
(several times if necessary).
In the worst case, switch robots (but keep the
same BAM).
Without this, the robot will start ignoring
commands when you pick it up or
otherwise make it think that it is about to
fall off a cliff.  This is usually NOT what you
want, so run in full mode.
Try VERY hard to ensure that your program executes this at the
end of each of its runs.  It ensures that the robot shuts down in
a “clean” state, which greatly increases the chances that (a) you
can reconnect, and (b) future sensor data is not garbage.
Motion
robot.go(
20
, 
0
)
time.sleep(
3.5
)
robot.stop()
 
robot.driveDirect(
20
, -
40
)
time.sleep(
3.5
)
robot.stop()
Start moving linearly at
20 cm per second and
start rotating at 0
degrees per second.
Positive indicates forward, counter-clockwise; negative the reverse.
Maximum speeds are something like 50 cm per second and 180 degrees per second.
Start moving with the
left wheel turning 20 cm
per second and the right
wheel turning -40 cm
per second.
Give’s the computer’s processor
to other processes for 3.5
seconds (but remember, the
robot is still moving).  Then tells
the robot to stop.  So the robot
will go ABOUT 70 cm.
Sensors
robot.getSensor(
'DISTANCE'
)
 
robot.go(
20
, 
0
)
time.sleep(
3.0
)
robot.stop()
 
distance = robot.getSensor(
'DISTANCE'
)
print
(distance)
front_left = robot.getSensor(
'CLIFF_FRONT_LEFT_SIGNAL'
)
is_playing = robot.getSensor(
'SONG_PLAYING'
)
for
 key 
in
 create.SENSORS:
    value = robot.getSensor(key)
    
print
(key + 
':'
, value)
The 
getSensor
 method returns whatever sensor value(s) you ask for;
you must spell the sensor-string exactly right.
The 
DISTANCE 
sensor uses the wheel encoders to estimate how far the
robot has traveled linearly since the last time you asked it for the
distance.  The response is in 
millimeters
 (not centimeters).  To measure
distance traveled, call the method before the motion to initialize the
sensor, then again after the motion to get the distance traveled.
See the robot
documenation for details
about the sensors.
This snippet gets and prints
all the current sensor values.
Singing – What’s a song?
def
 
robot_make_song():
    song = []
 
    
for
 k 
in
 range(
40
, 
100
, 
5
):
        duration = 
16
        note = (k, duration)
        song.append(note)
    
return
 song
song = make_robot_song()
 
robot.playSong(song)
A 
song
 is a list of up to 16 
notes
.
 
A 
note
 is a 2-tuple (
pitch
, 
duration
)
where 
pitch
 is between about 
31
and about 
127
 and 
duration
 is the
duration of the note in 
64
th
s of a
second.
 
For example, 
(120, 32) 
is a high
note played for about ½ of a second.
The 
playSong
 method sends a message asking
the robot to start playing the given song (i.e.,
the given sequence of notes).
Singing – Leaving time to play
def
 
robot_sing2(robot, song):
    
total_duration = 
0
    
for
 k 
in
 range(len(song)):
        note = song[k]
        duration = note[
1
]
        total_duration = total_duration + (duration / 
64
)
    robot.playSong(song)
    time.sleep(total_duration + 
1.0
)
def
 
robot_sing1(robot, song):
    
robot.playSong(song)
Singing – 
Blocked
 versus 
Not Blocked
def
 
robot_sing3(robot, song):
    
robot.playSong(song)
 
    
while
 
True
:
        
if
 
not
 robot.getSensor(
'SONG_PLAYING'
):
            
break
def
 
robot_sing1(robot, song):
    
robot.playSong(song)
while True:
    ...
    if 
the event of interest occurred
:
        
break
    ...
The 
Wait-Until-Event
pattern
Slide Note
Embed
Share

This Python program uses the Create library to send commands, enabling communication between your laptop's Bluetooth radio, the BAM on the Create robot, and the Create's controller. The library facilitates non-blocking commands for controlling the robot's movements and sensors, with specific instructions for connecting, disconnecting, motion control, and maintaining proper shutdown procedures. Explore the common methods and remember the basics for efficient robot control.

  • Python
  • Create library
  • Robot commands
  • Bluetooth communication
  • Motion control

Uploaded on Oct 07, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Your Python program uses the create library. It supplies functions that send commands: to your laptop s Bluetooth radio, then to the BAM on the Create, then to the Create s controller. Ditto for the reverse path. The create module (library)

  2. The robot commands in the Python create module simply send messagesto the robot robot.go(20, 0) <next statement> ... <next statement> ... <much further in program> ... Almost all of the robot commands in the create module do NOT BLOCK. That is, as soon as the message is sent, they continue to the next statement (long before the message has arrived to the robot). This causes some commands to behave in ways you might not expect. The only BLOCKING commands are: the constructor (waits for an acknowledgment that the connection exists), and getSensor (waits for the sensor value to arrive).

  3. The create module (library) Students: The rest of these slides present some of the most common methods in the create module: Connecting and disconnecting Motion Sensors Singing Don t memorize the details, but DO remember the basics and refer back to this document and others as needed.

  4. Connecting and Disconnecting Set the port per your Bluetooth setup. Or use 'sim' for the simulator. If the program breaks at this point with a message AttributeError: 'Create' object has no attribute 'ser' then it failed to make the Bluetooth connection. In this case, try: Turn the robot off. Wait 10 seconds. Turn the robot on. After the robot beeps, run your program again (several times if necessary). In the worst case, switch robots (but keep the same BAM). port = 18 robot = create.Create(port) robot.toFullMode() ... robot.shutdown() Try VERY hard to ensure that your program executes this at the end of each of its runs. It ensures that the robot shuts down in a clean state, which greatly increases the chances that (a) you can reconnect, and (b) future sensor data is not garbage. Without this, the robot will start ignoring commands when you pick it up or otherwise make it think that it is about to fall off a cliff. This is usually NOT what you want, so run in full mode.

  5. Motion Start moving linearly at 20 cm per second and start rotating at 0 degrees per second. Give s the computer s processor to other processes for 3.5 seconds (but remember, the robot is still moving). Then tells the robot to stop. So the robot will go ABOUT 70 cm. robot.go(20, 0) time.sleep(3.5) robot.stop() Start moving with the left wheel turning 20 cm per second and the right wheel turning -40 cm per second. robot.driveDirect(20, -40) time.sleep(3.5) robot.stop() Positive indicates forward, counter-clockwise; negative the reverse. Maximum speeds are something like 50 cm per second and 180 degrees per second.

  6. Sensors The getSensor method returns whatever sensor value(s) you ask for; you must spell the sensor-string exactly right. front_left = robot.getSensor('CLIFF_FRONT_LEFT_SIGNAL') is_playing = robot.getSensor('SONG_PLAYING') See the robot documenation for details about the sensors. robot.getSensor('DISTANCE') The DISTANCE sensor uses the wheel encoders to estimate how far the robot has traveled linearly since the last time you asked it for the distance. The response is in millimeters (not centimeters). To measure distance traveled, call the method before the motion to initialize the sensor, then again after the motion to get the distance traveled. robot.go(20, 0) time.sleep(3.0) robot.stop() distance = robot.getSensor('DISTANCE') print(distance) for key in create.SENSORS: value = robot.getSensor(key) print(key + ':', value) This snippet gets and prints all the current sensor values.

  7. Singing Whats a song? def robot_make_song(): song = [] A song is a list of up to 16 notes. A note is a 2-tuple (pitch, duration) where pitch is between about 31 and about 127 and duration is the duration of the note in 64ths of a second. for k in range(40, 100, 5): duration = 16 note = (k, duration) song.append(note) return song For example, (120, 32) is a high note played for about of a second. song = make_robot_song() The playSong method sends a message asking the robot to start playing the given song (i.e., the given sequence of notes). robot.playSong(song)

  8. Singing Leaving time to play def robot_sing1(robot, song): robot.playSong(song) def robot_sing2(robot, song): total_duration = 0 for k in range(len(song)): note = song[k] duration = note[1] total_duration = total_duration + (duration / 64) robot.playSong(song) time.sleep(total_duration + 1.0)

  9. Singing Blocked versus Not Blocked The Wait-Until-Event pattern def robot_sing1(robot, song): robot.playSong(song) def robot_sing3(robot, song): robot.playSong(song) while True: ... if the event of interest occurred: break ... while True: if not robot.getSensor('SONG_PLAYING'): break

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#