-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionsCozmo.py
148 lines (115 loc) · 6.49 KB
/
functionsCozmo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import cozmo
import asyncio
from cozmo.util import degrees, distance_mm, speed_mmps
import random
#!/usr/bin/env python3
# Copyright (c) 2016 Anki, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the file LICENSE.txt or at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Make Cozmo turn toward a face.
This script shows off the turn_towards_face action. It will wait for a face
and then constantly turn towards it to keep it in frame.
'''
import asyncio
import time
import cozmo
import faces
def songright(robot: cozmo.robot.Robot):
# scales is a list of the words for Cozmo to sing
scales = ["Doe", "Ray", "Mi", "Fa", "So", "La", "Ti", "Doe"]
# Find voice_pitch_delta value that will range the pitch from -1 to 1 over all of the scales
voice_pitch = -1.0
voice_pitch_delta = 20.0 / (len(scales) - 1)
# Move head and lift down to the bottom, and wait until that's achieved
robot.move_head(-5) # start moving head down so it mostly happens in parallel with lift
robot.set_lift_height(0.0).wait_for_completed()
robot.set_head_angle(degrees(-25.0)).wait_for_completed()
# Start slowly raising lift and head
robot.move_lift(0.15)
robot.move_head(0.15)
# "Sing" each note of the scale at increasingly high pitch
for note in scales:
robot.say_text(note, voice_pitch=voice_pitch, duration_scalar=0.3).wait_for_completed()
voice_pitch += voice_pitch_delta
def cozmo_singing(robot: cozmo.robot.Robot):
# Create an array of SongNote objects, consisting of all notes from C2 to C3_Sharp
notes = [
cozmo.song.SongNote(cozmo.song.NoteTypes.C2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.C2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.G2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.G2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.A2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.A2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.G2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.G2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.F2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.F2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.E2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.E2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.D2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.D2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.C2, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.Rest, cozmo.song.NoteDurations.Half)]
# Play the ascending notes
robot.play_song(notes, loop_count=1).wait_for_completed()
# Create an array of SongNote objects, consisting of the C3 pitch with varying durations
notes = [
cozmo.song.SongNote(cozmo.song.NoteTypes.C3, cozmo.song.NoteDurations.Half),
cozmo.song.SongNote(cozmo.song.NoteTypes.C3, cozmo.song.NoteDurations.ThreeQuarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.Rest, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.C3, cozmo.song.NoteDurations.Quarter),
cozmo.song.SongNote(cozmo.song.NoteTypes.C3, cozmo.song.NoteDurations.Whole)]
# Play the notes with varying durations
#robot.play_song(notes, loop_count=1).wait_for_completed()
# The main loop to our program. Runs after all the initialization.
def cozmo_squares(robot: cozmo.robot.Robot):
# Use a "for loop" to repeat the indented code 4 times
# Note: the _ variable name can be used when you don't need the value
for _ in range(4):
robot.drive_straight(distance_mm(150), speed_mmps(50)).wait_for_completed()
robot.turn_in_place(degrees(90)).wait_for_completed()
def running(robot: cozmo.robot.Robot):
# Drive forwards for 150 millimeters at 50 millimeters-per-second.
robot.drive_straight(distance_mm(150), speed_mmps(50)).wait_for_completed()
# Turn 90 degrees to the left.
# Note: To turn to the right, just use a negative number.
robot.turn_in_place(degrees(90)).wait_for_completed()
def motion_step(robot: cozmo.robot.Robot):
# grab a list of animation triggers
all_animation_triggers = robot.anim_triggers
# randomly shuffle the animations
random.shuffle(all_animation_triggers)
# select the first three animations from the shuffled list
triggers = 3
chosen_triggers = all_animation_triggers[:triggers]
print('Playing {} random animations:'.format(triggers))
# play the three random animations one after the other, waiting for each to complete
for trigger in chosen_triggers:
print('Playing {}'.format(trigger.name))
robot.play_anim_trigger(trigger).wait_for_completed()
# grab animation triggers that have 'WinGame' in their name
chosen_triggers = [trigger for trigger in robot.anim_triggers if 'WinGame' in trigger.name]
# play the three random animations one after the other, waiting for each to complete
for trigger in chosen_triggers:
print('Playing {}'.format(trigger.name))
robot.play_anim_trigger(trigger).wait_for_completed()
def cozmo_nod(robot: cozmo.robot.Robot):
robot.move_head(-5).wait_for_completed
robot.move_head(10).wait_for_completed
robot.move_head(-5).wait_for_completed
def cozmo_refuse(robot: cozmo.robot.Robot):
robot.turn_in_place(degrees(45)).wait_for_completed
robot.turn_in_place(degrees(-90)).wait_for_completed
robot.turn_in_place(degrees(45)).wait_for_completed
def cozmo_spin(robot: cozmo.robot.Robot):
robot.turn_in_place(degrees(360)).wait_for_completed