Cozmo人工智慧機器人SDK使用筆記(1)-基礎部分basics

zhangrelay發表於2019-01-08

APP和SDK有對應關係

如(3.0.0和1.4.6)或(3.1.0和1.4.7)。不嚴格對應,無法正常使用SDK。

cozmosdk.anki.com/docs/

Cozmo SDK經常更新,以便提供最佳的使用者體驗。SDK和Cozmo應用程式並不總是以相同的頻率更新。檢查此列表以檢視要使用的SDK版本是否與正在使用的應用程式版本匹配。

應用版本# SDK版本#
1.0.1 0.7.0
1.0.2 0.8.0
1.0.3 0.8.1
1.1.0 0.9.0
1.1.0 0.10.0
1.2.0 0.11.0
1.3.0 0.12.0
1.4.1 0.13.0
1.5.0 0.14.0
1.6.0 0.15.0
1.7.1 0.15.1
1.7.1 0.16.0
2.0.0 1.0.0
2.0.1 1.0.1
2.0.1 1.1.0
2.1.0 1.2.0
2.1.0 1.2.1
2.2.0 1.3.0
2.3.0 1.3.1
2.4.0 1.3.2
2.5.0 1.4.0
2.6.0 1.4.1
2.7.0 1.4.2
2.8.0 1.4.3
2.9.0 1.4.4
2.10.0 1.4.5
3.0.1 1.4.6
3.1.0 1.4.7

如果有不相容的應用版本和SDK,請同步更新應用和SDK。


部分CSDN關於Czomo博文彙總:

手機連線Cozmo人工智慧機器人玩具:

Cozmo機器人脫離智慧手機使用的不完全攻略:

人工智慧基礎(高中版)教材補充和資源分享之番外篇 Cozmo+Python+ROS+AI:


SDK簡要說明:

本文簡要介紹一下教程tutorials中,基礎部分01_basics:

使用前連結正確:


01_hello_world.py

Cozmo說出對應句子。

核心程式碼:robot.say_text("Hello World.").wait_for_completed()

可以替換如Hi Cozmo!

import cozmo


def cozmo_program(robot: cozmo.robot.Robot):
    robot.say_text("Hi Cozmo!").wait_for_completed()


cozmo.run_program(cozmo_program)

執行結果,聽到cozmo說出hi cozmo:


02_drive_and_turn.py

基本運動指令。

核心程式碼:

直線運動:

robot.drive_straight(distance_mm(150), speed_mmps(50)).wait_for_completed()

轉彎運動:

robot.turn_in_place(degrees(90)).wait_for_completed()

import cozmo
from cozmo.util import degrees, distance_mm, speed_mmps


def cozmo_program(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()

前行150mm+轉向90。


03_count.py

數數字,從1到10。

核心程式碼:迴圈使用for i in range(10):

import cozmo


def cozmo_program(robot: cozmo.robot.Robot):
    # A "for loop" runs for each value i in the given range - in this example
    # starting from 0, while i is less than 5 (so 0,1,2,3,4).
    for i in range(10):
        # Add 1 to the number (so that we count from 1 to 5, not 0 to 4),
        # then convert the number to a string and make Cozmo say it.
        robot.say_text(str(i+1)).wait_for_completed()


cozmo.run_program(cozmo_program)

cozmo開始數數字。


04_drive_square.py

02和03示例組合,走完成一個正方形!

import cozmo
from cozmo.util import degrees, distance_mm, speed_mmps


def cozmo_program(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()


cozmo.run_program(cozmo_program)

運動指令和迴圈指令結合,當然也可以讓他同時大聲數數字。

加入???


05_motors.py

移動頭部,手臂和履帶。

import time

import cozmo


def cozmo_program(robot: cozmo.robot.Robot):
    # Tell the head motor to start lowering the head (at 5 radians per second)
    robot.move_head(-5)
    # Tell the lift motor to start lowering the lift (at 5 radians per second)
    robot.move_lift(-5)
    # Tell Cozmo to drive the left wheel at 25 mmps (millimeters per second),
    # and the right wheel at 50 mmps (so Cozmo will drive Forwards while also
    # turning to the left
    robot.drive_wheels(25, 50)

    # wait for 3 seconds (the head, lift and wheels will move while we wait)
    time.sleep(3)

    # Tell the head motor to start raising the head (at 5 radians per second)
    robot.move_head(5)
    # Tell the lift motor to start raising the lift (at 5 radians per second)
    robot.move_lift(5)
    # Tell Cozmo to drive the left wheel at 50 mmps (millimeters per second),
    # and the right wheel at -50 mmps (so Cozmo will turn in-place to the right)
    robot.drive_wheels(50, -50)

    # wait for 3 seconds (the head, lift and wheels will move while we wait)
    time.sleep(3)


cozmo.run_program(cozmo_program)

06_sing_scales.py

03示例擴充套件!學唱歌???

import cozmo
from cozmo.util import degrees


def cozmo_program(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 = 2.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


cozmo.run_program(cozmo_program)

07_backpack_lights.py

燈光設定:紅色--綠色--藍色--白色--關閉

import time

import cozmo


def cozmo_program(robot: cozmo.robot.Robot):
    # set all of Cozmo's backpack lights to red, and wait for 2 seconds
    robot.set_all_backpack_lights(cozmo.lights.red_light)
    time.sleep(2)
    # set all of Cozmo's backpack lights to green, and wait for 2 seconds
    robot.set_all_backpack_lights(cozmo.lights.green_light)
    time.sleep(2)
    # set all of Cozmo's backpack lights to blue, and wait for 2 seconds
    robot.set_all_backpack_lights(cozmo.lights.blue_light)
    time.sleep(2)
    # set just Cozmo's center backpack lights to white, and wait for 2 seconds
    robot.set_center_backpack_lights(cozmo.lights.white_light)
    time.sleep(2)
    # turn off Cozmo's backpack lights and wait for 2 seconds
    robot.set_all_backpack_lights(cozmo.lights.off_light)
    time.sleep(2)

08_animation.py

播放動畫動作(表情系列)

import cozmo


def cozmo_program(robot: cozmo.robot.Robot):
    # Play an animation via a Trigger - see:
    # http://cozmosdk.anki.com/docs/generated/cozmo.anim.html#cozmo.anim.Triggers
    # for a list of available triggers.
    # A trigger can pick from several appropriate animations for variety.
    print("Playing Animation Trigger 1:")
    robot.play_anim_trigger(cozmo.anim.Triggers.CubePounceLoseSession).wait_for_completed()

    # Play the same trigger, but this time ignore the track that plays on the
    # body (i.e. don't move the wheels). See the play_anim_trigger documentation
    # for other available settings.
    print("Playing Animation Trigger 2: (Ignoring the body track)")
    robot.play_anim_trigger(cozmo.anim.Triggers.CubePounceLoseSession, ignore_body_track=True).wait_for_completed()

    # Play an animation via its Name.
    # Warning: Future versions of the app might change these, so for future-proofing
    # we recommend using play_anim_trigger above instead.
    # See the remote_control_cozmo.py example in apps for an easy way to see
    # the available animations.
    print("Playing Animation 3:")
    robot.play_anim(name="anim_poked_giggle").wait_for_completed()


cozmo.run_program(cozmo_program)

09_cube_lights.py

三個能量方塊分別為:紅色/綠色/藍色

import time

import cozmo
from cozmo.objects import LightCube1Id, LightCube2Id, LightCube3Id


def cozmo_program(robot: cozmo.robot.Robot):
    cube1 = robot.world.get_light_cube(LightCube1Id)  # looks like a paperclip
    cube2 = robot.world.get_light_cube(LightCube2Id)  # looks like a lamp / heart
    cube3 = robot.world.get_light_cube(LightCube3Id)  # looks like the letters 'ab' over 'T'

    if cube1 is not None:
        cube1.set_lights(cozmo.lights.red_light)
    else:
        cozmo.logger.warning("Cozmo is not connected to a LightCube1Id cube - check the battery.")

    if cube2 is not None:
        cube2.set_lights(cozmo.lights.green_light)
    else:
        cozmo.logger.warning("Cozmo is not connected to a LightCube2Id cube - check the battery.")

    if cube3 is not None:
        cube3.set_lights(cozmo.lights.blue_light)
    else:
        cozmo.logger.warning("Cozmo is not connected to a LightCube3Id cube - check the battery.")

    # Keep the lights on for 10 seconds until the program exits
    time.sleep(10)

        


10_play_sound.py

手機播放聲音:

import time

import cozmo

def cozmo_program(robot: cozmo.robot.Robot):
    # Play a sound that ends on its own
    robot.play_audio(cozmo.audio.AudioEvents.SfxGameWin)
    time.sleep(1.0)

    # Play a sound for us to interrupt after two seconds
    # This sound "MusicStyle80S1159BpmLoop" is:
    #   - "80S" style music #"1", at "159Bpm" (beats per minute)
    #   - if the song is played repeatedly, the beginning and end
    #     line up making it possible to play in a "Loop"
    robot.play_audio(cozmo.audio.AudioEvents.MusicStyle80S1159BpmLoop)

    # Most sounds have an accompanying (name + "Stop") event to cancel it
    # before it finishes.
    time.sleep(2.0)
    robot.play_audio(cozmo.audio.AudioEvents.MusicStyle80S1159BpmLoopStop)

    # Start the tiny orchestra system.
    # By itself, the tiny orchestra system will not make any sound, but
    # allows us to turn synchronized audio channels on and off until
    # we tell it to stop.
    robot.play_audio(cozmo.audio.AudioEvents.MusicTinyOrchestraInit)

    # Turn on the bass_mode_1 channel in the tiny orchestra system.
    robot.play_audio(cozmo.audio.AudioEvents.MusicTinyOrchestraBassMode1)

    # After 5 seconds...
    time.sleep(5.0)

    # Turn on the strings_mode_3 channel.
    robot.play_audio(cozmo.audio.AudioEvents.MusicTinyOrchestraStringsMode3)

    # After 5 seconds...
    time.sleep(5.0)

    # Stop the tiny orchestra system.
    # This will cause all tinyOrchestra music to stop playing.
    robot.play_audio(cozmo.audio.AudioEvents.MusicTinyOrchestraStop)

11_play_song.py

cozmo播放音樂:比手機音效zha。

import asyncio
import time

import cozmo

def cozmo_program(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_Sharp, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.D2, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.D2_Sharp, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.E2, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.F2, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.F2_Sharp, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.G2, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.G2_Sharp, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.A2, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.A2_Sharp, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.B2, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.C3, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.C3_Sharp, cozmo.song.NoteDurations.Quarter),
        cozmo.song.SongNote(cozmo.song.NoteTypes.Rest, cozmo.song.NoteDurations.Quarter) ]

    # 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()

cozmo.run_program(cozmo_program)

12_random_animation.py

import cozmo
import random

def cozmo_program(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()

cozmo.run_program(cozmo_program)

Fin


 

相關文章