先简单总结一下使用笔记1-3:

  1. 基础部分介绍了一些常用功能,比如运动控制、LED显示和扬声器交互等
  2. 人机接口显示部分--输出,cozmo面部显示屏输出一些基本信息
  3. 人机接口视觉部分--输入,cozmo摄像头获取环境信息并处理等

接着,就自然过渡到第四部分----立方体和物体任务部分,共有13个项目专题,非常有趣。

Cozmo环顾四周,找寻充电底座图标,然后行驶到附近,任务完成。


1. go to pose

给定Cozmo目标位置和角度,然后行驶到对应位置和角度。

先设定目标,然后如果将relative_to_robot设置为true,就是相对量,当年机器人为原点,给定目标相对原点的距离和角度。

核心代码如下:

def cozmo_program(robot: cozmo.robot.Robot):robot.go_to_pose(Pose(100, 100, 0, angle_z=degrees(45)), relative_to_robot=True).wait_for_completed()

2. create wall

虚拟墙,并绕过行驶到目标。

核心代码:

import cozmo
from cozmo.util import degrees, Posedef cozmo_program(robot: cozmo.robot.Robot):fixed_object = robot.world.create_custom_fixed_object(Pose(100, 0, 0, angle_z=degrees(0)),10, 100, 100, relative_to_robot=True)if fixed_object:print("fixed_object created successfully")robot.go_to_pose(Pose(200, 0, 0, angle_z=degrees(0)), relative_to_robot=True).wait_for_completed()cozmo.run_program(cozmo_program, use_3d_viewer=True)

3. go to object

Cozmo找到一个立方体(1-3皆可以),然后行驶到对应立方体附近。

def go_to_object_test(robot: cozmo.robot.Robot):'''The core of the go to object test program'''# Move lift down and tilt the head uprobot.move_lift(-3)robot.set_head_angle(degrees(0)).wait_for_completed()# look around and try to find a cubelook_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)cube = Nonetry:cube = robot.world.wait_for_observed_light_cube(timeout=30)print("Found cube: %s" % cube)except asyncio.TimeoutError:print("Didn't find a cube")finally:# whether we find it or not, we want to stop the behaviorlook_around.stop()if cube:# Drive to 70mm away from the cube (much closer and Cozmo# will likely hit the cube) and then stop.action = robot.go_to_object(cube, distance_mm(70.0))action.wait_for_completed()print("Completed action: result = %s" % action)print("Done.")

4. stack or roll

让Cozmo依据找到的立方体的数量执行不同的任务。

超时时间默认为10s,环顾四周,找寻立方体,根据数量进行操作:

0---恩,它很生气

1---翻滚立方体

2---堆叠立方体

import cozmodef cozmo_program(robot: cozmo.robot.Robot):lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)cubes = robot.world.wait_until_observe_num_objects(num=2, object_type=cozmo.objects.LightCube, timeout=10)print("Found %s cubes" % len(cubes))lookaround.stop()if len(cubes) == 0:robot.play_anim_trigger(cozmo.anim.Triggers.MajorFail).wait_for_completed()elif len(cubes) == 1:robot.run_timed_behavior(cozmo.behavior.BehaviorTypes.RollBlock, active_time=60)else:robot.run_timed_behavior(cozmo.behavior.BehaviorTypes.StackBlocks, active_time=60)cozmo.run_program(cozmo_program)

5. cube stack

Cozmo堆叠立方体。Cozmo将等到它看到两个立方体,然后拿起一个并将其放在另一个立方体上。它会拿起看到的第一个,然后把它放在第二个上。

def cozmo_program(robot: cozmo.robot.Robot):# Attempt to stack 2 cubes# Lookaround until Cozmo knows where at least 2 cubes are:lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)cubes = robot.world.wait_until_observe_num_objects(num=2, object_type=cozmo.objects.LightCube, timeout=60)lookaround.stop()if len(cubes) < 2:print("Error: need 2 Cubes but only found", len(cubes), "Cube(s)")else:# Try and pickup the 1st cubecurrent_action = robot.pickup_object(cubes[0], num_retries=3)current_action.wait_for_completed()if current_action.has_failed:code, reason = current_action.failure_reasonresult = current_action.resultprint("Pickup Cube failed: code=%s reason='%s' result=%s" % (code, reason, result))return# Now try to place that cube on the 2nd onecurrent_action = robot.place_on_object(cubes[1], num_retries=3)current_action.wait_for_completed()if current_action.has_failed:code, reason = current_action.failure_reasonresult = current_action.resultprint("Place On Cube failed: code=%s reason='%s' result=%s" % (code, reason, result))returnprint("Cozmo successfully stacked 2 blocks!")

6. pickup furthest

让Cozmo拿起最远的立方体。此实例演示对象位置姿态的简单数学运算。

(dst = translation.position.x ** 2 + translation.position.y ** 2)

机器人试图在视觉中找到三个立方体,可以在脚本打开的摄像头窗口中看到运行过程。每个立方体将显示轮廓和立方体编号。机器人等待直到找到3个立方体,然后尝试拿起最远的一个。

import cozmodef cozmo_program(robot: cozmo.robot.Robot):lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)cubes = robot.world.wait_until_observe_num_objects(num=3, object_type=cozmo.objects.LightCube, timeout=60)lookaround.stop()max_dst, targ = 0, Nonefor cube in cubes:translation = robot.pose - cube.posedst = translation.position.x ** 2 + translation.position.y ** 2if dst > max_dst:max_dst, targ = dst, cubeif len(cubes) < 3:print("Error: need 3 Cubes but only found", len(cubes), "Cube(s)")else:robot.pickup_object(targ, num_retries=3).wait_for_completed()cozmo.run_program(cozmo_program, use_viewer=True)

7. look around

Cozmo环顾四周,做出反应,然后拿起并放下一个立方体。

import asyncioimport cozmo
from cozmo.util import degreesdef cozmo_program(robot: cozmo.robot.Robot):look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)# try to find a blockcube = Nonetry:cube = robot.world.wait_for_observed_light_cube(timeout=30)print("Found cube", cube)except asyncio.TimeoutError:print("Didn't find a cube :-(")finally:# whether we find it or not, we want to stop the behaviorlook_around.stop()if cube is None:robot.play_anim_trigger(cozmo.anim.Triggers.MajorFail)returnprint("Yay, found cube")cube.set_lights(cozmo.lights.green_light.flash())anim = robot.play_anim_trigger(cozmo.anim.Triggers.BlockReact)anim.wait_for_completed()action = robot.pickup_object(cube)print("got action", action)result = action.wait_for_completed(timeout=30)print("got action result", result)robot.turn_in_place(degrees(90)).wait_for_completed()action = robot.place_object_on_ground_here(cube)print("got action", action)result = action.wait_for_completed(timeout=30)print("got action result", result)anim = robot.play_anim_trigger(cozmo.anim.Triggers.MajorWin)cube.set_light_corners(None, None, None, None)anim.wait_for_completed()cozmo.run_program(cozmo_program)

8. drive to charger

让Cozmo行驶到充电底座附近。

import asyncio
import timeimport cozmo
from cozmo.util import degrees, distance_mm, speed_mmpsdef drive_to_charger(robot):'''The core of the drive_to_charger program'''# If the robot was on the charger, drive them forward and clear of the chargerif robot.is_on_charger:# drive off the chargerrobot.drive_off_charger_contacts().wait_for_completed()robot.drive_straight(distance_mm(100), speed_mmps(50)).wait_for_completed()# Start moving the lift downrobot.move_lift(-3)# turn around to look at the chargerrobot.turn_in_place(degrees(180)).wait_for_completed()# Tilt the head to be levelrobot.set_head_angle(degrees(0)).wait_for_completed()# wait half a second to ensure Cozmo has seen the chargertime.sleep(0.5)# drive backwards away from the chargerrobot.drive_straight(distance_mm(-60), speed_mmps(50)).wait_for_completed()# try to find the chargercharger = None# see if Cozmo already knows where the charger isif robot.world.charger:if robot.world.charger.pose.is_comparable(robot.pose):print("Cozmo already knows where the charger is!")charger = robot.world.chargerelse:# Cozmo knows about the charger, but the pose is not based on the# same origin as the robot (e.g. the robot was moved since seeing# the charger) so try to look for the charger firstpassif not charger:# Tell Cozmo to look around for the chargerlook_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)try:charger = robot.world.wait_for_observed_charger(timeout=30)print("Found charger: %s" % charger)except asyncio.TimeoutError:print("Didn't see the charger")finally:# whether we find it or not, we want to stop the behaviorlook_around.stop()if charger:# Attempt to drive near to the charger, and then stop.action = robot.go_to_object(charger, distance_mm(65.0))action.wait_for_completed()print("Completed action: result = %s" % action)print("Done.")cozmo.robot.Robot.drive_off_charger_on_connect = False  # Cozmo can stay on charger for now
cozmo.run_program(drive_to_charger, use_viewer=True, force_viewer_on_top=True)

9. custom objects

自定义对象,包括大小和形状等。

import timeimport cozmo
from cozmo.objects import CustomObject, CustomObjectMarkers, CustomObjectTypesdef handle_object_appeared(evt, **kw):# This will be called whenever an EvtObjectAppeared is dispatched -# whenever an Object comes into view.if isinstance(evt.obj, CustomObject):print("Cozmo started seeing a %s" % str(evt.obj.object_type))def handle_object_disappeared(evt, **kw):# This will be called whenever an EvtObjectDisappeared is dispatched -# whenever an Object goes out of view.if isinstance(evt.obj, CustomObject):print("Cozmo stopped seeing a %s" % str(evt.obj.object_type))def custom_objects(robot: cozmo.robot.Robot):# Add event handlers for whenever Cozmo sees a new objectrobot.add_event_handler(cozmo.objects.EvtObjectAppeared, handle_object_appeared)robot.add_event_handler(cozmo.objects.EvtObjectDisappeared, handle_object_disappeared)# define a unique cube (44mm x 44mm x 44mm) (approximately the same size as a light cube)# with a 30mm x 30mm Diamonds2 image on every facecube_obj = robot.world.define_custom_cube(CustomObjectTypes.CustomType00,CustomObjectMarkers.Diamonds2,44,30, 30, True)# define a unique cube (88mm x 88mm x 88mm) (approximately 2x the size of a light cube)# with a 50mm x 50mm Diamonds3 image on every facebig_cube_obj = robot.world.define_custom_cube(CustomObjectTypes.CustomType01,CustomObjectMarkers.Diamonds3,88,50, 50, True)# define a unique wall (150mm x 120mm (x10mm thick for all walls)# with a 50mm x 30mm Circles2 image on front and backwall_obj = robot.world.define_custom_wall(CustomObjectTypes.CustomType02,CustomObjectMarkers.Circles2,150, 120,50, 30, True)# define a unique box (60mm deep x 140mm width x100mm tall)# with a different 30mm x 50mm image on each of the 6 facesbox_obj = robot.world.define_custom_box(CustomObjectTypes.CustomType03,CustomObjectMarkers.Hexagons2,  # frontCustomObjectMarkers.Circles3,   # backCustomObjectMarkers.Circles4,   # topCustomObjectMarkers.Circles5,   # bottomCustomObjectMarkers.Triangles2, # leftCustomObjectMarkers.Triangles3, # right60, 140, 100,30, 50, True)if ((cube_obj is not None) and (big_cube_obj is not None) and(wall_obj is not None) and (box_obj is not None)):print("All objects defined successfully!")else:print("One or more object definitions failed!")returnprint("Show the above markers to Cozmo and you will see the related objects ""annotated in Cozmo's view window, you will also see print messages ""everytime a custom object enters or exits Cozmo's view.")print("Press CTRL-C to quit")while True:time.sleep(0.1)cozmo.run_program(custom_objects, use_3d_viewer=True, use_viewer=True)

10. object moving

检测移动立方体并实时跟踪其速度等。

import timeimport cozmodef handle_object_moving_started(evt, **kw):# This will be called whenever an EvtObjectMovingStarted event is dispatched -# whenever we detect a cube starts moving (via an accelerometer in the cube)print("Object %s started moving: acceleration=%s" %(evt.obj.object_id, evt.acceleration))def handle_object_moving(evt, **kw):# This will be called whenever an EvtObjectMoving event is dispatched -# whenever we detect a cube is still moving a (via an accelerometer in the cube)print("Object %s is moving: acceleration=%s, duration=%.1f seconds" %(evt.obj.object_id, evt.acceleration, evt.move_duration))def handle_object_moving_stopped(evt, **kw):# This will be called whenever an EvtObjectMovingStopped event is dispatched -# whenever we detect a cube stopped moving (via an accelerometer in the cube)print("Object %s stopped moving: duration=%.1f seconds" %(evt.obj.object_id, evt.move_duration))def cozmo_program(robot: cozmo.robot.Robot):# Add event handlers that will be called for the corresponding eventrobot.add_event_handler(cozmo.objects.EvtObjectMovingStarted, handle_object_moving_started)robot.add_event_handler(cozmo.objects.EvtObjectMoving, handle_object_moving)robot.add_event_handler(cozmo.objects.EvtObjectMovingStopped, handle_object_moving_stopped)# keep the program running until user closes / quits itprint("Press CTRL-C to quit")while True:time.sleep(1.0)cozmo.robot.Robot.drive_off_charger_on_connect = False  # Cozmo can stay on his charger for this example
cozmo.run_program(cozmo_program)

11. dock with cube

行驶到立方体附近,并对接立方体,但是并不拿起立方体。

import cozmo
from cozmo.util import degreesasync def dock_with_cube(robot: cozmo.robot.Robot):await robot.set_head_angle(degrees(-5.0)).wait_for_completed()print("Cozmo is waiting until he sees a cube.")cube = await robot.world.wait_for_observed_light_cube()print("Cozmo found a cube, and will now attempt to dock with it:")# Cozmo will approach the cube he has seen# using a 180 approach angle will cause him to drive past the cube and approach from the opposite side# num_retries allows us to specify how many times Cozmo will retry the action in the event of it failingaction = robot.dock_with_cube(cube, approach_angle=cozmo.util.degrees(180), num_retries=2)await action.wait_for_completed()print("result:", action.result)cozmo.run_program(dock_with_cube)

12. roll cube

翻滚立方体。

import cozmo
from cozmo.util import degreesasync def roll_a_cube(robot: cozmo.robot.Robot):await robot.set_head_angle(degrees(-5.0)).wait_for_completed()print("Cozmo is waiting until he sees a cube")cube = await robot.world.wait_for_observed_light_cube()print("Cozmo found a cube, and will now attempt to roll with it:")# Cozmo will approach the cube he has seen and roll it# check_for_object_on_top=True enforces that Cozmo will not roll cubes with anything on topaction = robot.roll_cube(cube, check_for_object_on_top=True, num_retries=2)await action.wait_for_completed()print("result:", action.result)cozmo.run_program(roll_a_cube)

13. pop a wheelie

借助立方体,将Cozmo身体直立起来。

import cozmoasync def pop_a_wheelie(robot: cozmo.robot.Robot):print("Cozmo is waiting until he sees a cube")cube = await robot.world.wait_for_observed_light_cube()print("Cozmo found a cube, and will now attempt to pop a wheelie on it")action = robot.pop_a_wheelie(cube, num_retries=2)await action.wait_for_completed()cozmo.run_program(pop_a_wheelie)

Fin


Cozmo人工智能机器人SDK使用笔记(4)-任务部分cubes_and_objects相关推荐

  1. Cozmo人工智能机器人SDK使用笔记(1)-基础部分basics

    APP和SDK有对应关系 如(3.0.0和1.4.6)或(3.1.0和1.4.7).不严格对应,无法正常使用SDK. cozmosdk.anki.com/docs/ Cozmo SDK经常更新,以便提 ...

  2. Cozmo人工智能机器人SDK使用笔记(2)-显示部分face

    这篇博文针对SDK教程中的第二部分cozmo_face进行简单介绍,如下: face是cozmo显示的核心部分: 来学习一下,如何操作吧- 分为3个文件,如上图所示. 1. face image co ...

  3. Cozmo人工智能机器人SDK使用笔记(3)-视觉部分vision

    关于机器人感知-视觉部分,有过一次公开分享,讲稿全文和视屏实录,参考如下CSDN链接: 机器人感知-视觉部分(Robotic Perception-Vision Section): https://b ...

  4. Cozmo人工智能机器人SDK使用笔记(6)-并行部分Parallel_Action

    Cozmo并行动作示例. 此示例演示如何并行(而不是按顺序)执行动作. import sys import timetry:from PIL import Image except ImportErr ...

  5. Cozmo人工智能机器人SDK使用笔记(5)-时序部分async_sync

    Cozmo首先寻找一个立方体. 找到立方体后,立方体的灯以循环方式绿色闪烁,然后等待轻敲立方体. 此时,程序分别为同步和异步两种类型,注意区分. 1. 同步 立方体闪烁同步示例 import asyn ...

  6. Vector人工智能机器人SDK使用笔记

    Cozmo是2016年推出的,2两年后的2018年Vector上市,具备语音助手和更多功能,元件数由300+升级到700+. Vector的SDK具体说明在:developer.anki.com/ve ...

  7. ROS2GO之手机连接Cozmo人工智能机器人玩具

    ROS2GO之手机连接Cozmo人工智能机器人玩具 参考anki的开发者页面,硬件基础: 1. Cozmo机器人:: 2. 兼容Cozmo APP的手机或平板:: 3. 运行Windows.MacOS ...

  8. Cozmo人工智能机器人玩具/教具完整版中文说明书和介绍(附应用下载链接)

    Cozmo(Anki)人工智能AI机器人Robot完整版中文说明书和介绍 (附应用下载链接)https://download.csdn.net/download/zhangrelay/10854427 ...

  9. 曾推出Anki Drive和Cozmo人工智能机器人的独角兽企业Anki谢幕

    Anki DRIVE | 极客东东 听说 Anki 出事了,即将在未来的一周关闭,公司近200名员工将获得一周薪酬补偿.很遗憾这家小林很欣赏的初创公司,前后融资多达2亿美元,其实他们已经很有钱了,但却 ...

最新文章

  1. CentOS安装jdk的三种方法
  2. js三进制计算机,js 笔记 - 二进制位运算符
  3. C++ 用迭代的方式实现归并排序
  4. 决策树-熵计算-ID3算法(转)
  5. 干货:从0到1搭建「推荐策略产品」的深度思考
  6. java学习(70):GUL图形用户界面初识
  7. iphone-common-codes-ccteam源代码 CCNSArray.m
  8. Eclipse Pydev 技巧汇编
  9. C语言简明数据类型指南
  10. 对Boost.Asio中异步事件循环的理解
  11. linux下物理内存不足,vm中linux物理内存不足解决方案
  12. msg1500说明书_瑞斯康达MSG1500 路由 刷机 保姆级教程
  13. chrome 清理缓存
  14. 2.证券投资基金的概述
  15. 马化腾是该全面反思腾讯战略了:吃老本不能让腾讯变得伟大!
  16. 《贝叶斯统计》最大后验密度可信集(HPD)Python实现
  17. 心理测试单机小软件,十个有趣的心理小测试 好玩的心理测试题
  18. 自认为隐藏在计算机补码中的秘密(原来这么简单)
  19. 早上空腹喝酸奶好吗?
  20. 2019-11-11

热门文章

  1. 【TCP/IP】流量控制和拥塞控制
  2. 计算机专业范文推荐信,计算机专业留学推荐信范文
  3. DEMUX(解扰解复用)
  4. 图像处理中拟合直线的几种方法
  5. 新木桶理论与信息安全
  6. 捍卫小崔, 因为民众有不靠谱的权利
  7. Ubuntu 22.04 Ubuntu 22.10 解决按照官方教程无法使用搜狗输入法
  8. B2C网站设计中微交互研究
  9. navigator 与语言相关的属性在各大浏览器的差异,及获取浏览器语言的正解
  10. Dell PowerEdge R730服务器使用