索尼 toio™ 应用创意开发征文|微生物行为模拟

Toio™ Q宝小机器人的灵活性和可编程性使其成为了一个令人兴奋的工具,让孩子可以将编程与实际操作相结合,在玩乐中增长编程知识,加强动手能力,对计算机学科产生更大的兴趣。

本文就会介绍一个使用Q宝机器人在宏观世界中模拟微生物活动的例子。细胞生物虽然结构简单,但也具备向环境中的食物自动靠拢,最终吃掉食物的能力。我们可以使用Q宝小机器人来模拟这一过程,让孩子发现电子计算机操作的程序设备也可以模仿生物行为的现象,并对生物与机械的关系作出更多思考。

首先我们需要连接两个toio™核心立方体,一个代表微生物(CellCube),另一个代表食物(FoodCube)。这段代码首先使用connect_to_toio(device_name)函数连接到指定名称的toio™核心立方体。在这里,我们连接了名为"CellCube"和"FoodCube"的两个立方体,并分别将它们分配给Cell_cube和food_cube变量。

async def connect_to_toio(device_name):
    device_list = await BLEScanner.scan(1)
    for device in device_list:
        if device.name == device_name:
            cube = ToioCoreCube(device.interface)
            await cube.connect()
            return cube
    return None

# 连接微生物和食物 Toio 立方体
Cell_cube = await connect_to_toio("CellCube")
food_cube = await connect_to_toio("FoodCube")

然后我们将移动微生物(CellCube)向食物(FoodCube)靠近,模拟微生物的觅食行为。这段代码定义了move_to_random_position(cube, x, y)函数,用于将toio™核心立方体移动到指定的位置(x, y)。在游戏循环game_loop()中,我们首先连接两个立方体,然后生成一个随机位置作为食物的目标位置。接下来,我们根据当前位置和目标位置,逐步移动微生物向食物靠近,直到微生物吃掉食物。

async def move_to_random_position(cube, x, y):
    await cube.api.motor.motor_control_target(
        timeout=5,
        movement_type=MovementType.Linear,
        speed=Speed(max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),
        target=TargetPosition(cube_location=CubeLocation(point=Point(x=x, y=y), angle=0),
                              rotation_option=RotationOption.AbsoluteOptimal),
    )

async def game_loop():
    # ...(连接设备和生成目标位置的代码)

    # 模拟微生物向食物移动
    while Cell_x != random_x or Cell_y != random_y:
        # ...(计算下一步移动方向的代码)

        # 移动微生物到下一步位置
        await Cell_cube.api.motor.motor_control_target(
            timeout=1,
            movement_type=MovementType.Linear,
            speed=Speed(max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),
            target=TargetPosition(cube_location=CubeLocation(point=Point(x=Cell_x, y=Cell_y), angle=0),
                                  rotation_option=RotationOption.AbsoluteOptimal),
        )

    print("微生物吃掉了食物!")

    # 断开连接
    await Cell_cube.disconnect()
    await food_cube.disconnect()

    # 延迟一段时间后继续下一轮游戏
    await asyncio.sleep(3)

其中微生物(CellCube)通过toio™核心立方体的移动控制逐步靠近食物(FoodCube),并在吃掉食物后结束游戏然后开始下一轮的操作!

在这里插入图片描述

完整代码

import asyncio
import random
from toio import *

async def connect_to_toio(device_name):
    device_list = await BLEScanner.scan(1)
    for device in device_list:
        if device.name == device_name:
            cube = ToioCoreCube(device.interface)
            await cube.connect()
            return cube
    return None

async def move_to_random_position(cube, x, y):
    await cube.api.motor.motor_control_target(
        timeout=5,
        movement_type=MovementType.Linear,
        speed=Speed(max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),
        target=TargetPosition(cube_location=CubeLocation(point=Point(x=x, y=y), angle=0),
                              rotation_option=RotationOption.AbsoluteOptimal),
    )

async def game_loop():
    while True:
        # 连接两个 Toio 核心立方体,一个表示微生物,一个表示食物
        Cell_cube = await connect_to_toio("CellCube")
        food_cube = await connect_to_toio("FoodCube")

        if Cell_cube is None or food_cube is None:
            print("未找到设备")
            return

        # 生成随机位置作为食物的目标位置
        random_x = random.randint(0, 300)  # 随机生成 x 坐标
        random_y = random.randint(0, 300)  # 随机生成 y 坐标

        # 移动食物到随机位置
        await move_to_random_position(food_cube, random_x, random_y)

        # 微生物的当前位置
        Cell_x, Cell_y = 150, 150  # 初始位置

        # 微生物每次移动的步长
        step = 10

        # 模拟微生物缓慢移动
        while Cell_x != random_x or Cell_y != random_y:
            # 计算下一步移动的方向
            if Cell_x < random_x:
                Cell_x += step
            elif Cell_x > random_x:
                Cell_x -= step

            if Cell_y < random_y:
                Cell_y += step
            elif Cell_y > random_y:
                Cell_y -= step

            # 移动微生物到下一步位置
            await Cell_cube.api.motor.motor_control_target(
                timeout=1,
                movement_type=MovementType.Linear,
                speed=Speed(max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),
                target=TargetPosition(cube_location=CubeLocation(point=Point(x=Cell_x, y=Cell_y), angle=0),
                                      rotation_option=RotationOption.AbsoluteOptimal),
            )

        print("微生物吃掉了食物!")

        # 断开连接
        await Cell_cube.disconnect()
        await food_cube.disconnect()

        # 延迟一段时间后继续下一轮游戏
        await asyncio.sleep(3)

async def main():
    await game_loop()

if __name__ == "__main__":
    asyncio.run(main())

孩子如果感兴趣,还可以对上述代码进行修改,控制食物在不同位置出现的概率。之后孩子会发现,无论食物出现在哪里,微生物都会自动移动到食物旁边。家长可以借机向孩子讲述自然界中微生物的觅食能力,并启发孩子思考进化与编程之间的关系。家长甚至可以同时让孩子使用显微镜观察真实微生物的觅食活动,并与Q宝机器人的觅食操作对比,进一步加深自然界与人造程序之间区别与联系的认知。总之,Q宝机器人可以充当非常合适的教学工具,让孩子在充满乐趣的体验中学习和思考更多知识。

  • 55
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
memcpy_toio是一个函数,用于将数据从内存复制到IO空间。它是在memcpy函数的基础上进行了封装,通过定义宏来实现。具体来说,memcpy_toio的定义如下:#define memcpy_toio(a,b,c) memcpy(__io_virt(a),(b),(c)) 。在这个宏的定义中,__io_virt是一个用于将物理地址映射到虚拟地址的函数。因此,memcpy_toio会将数据从源内存地址复制到目标IO地址。 需要注意的是,memcpy_toio函数主要用于访问IO空间,而不是一般的内存空间。这是因为IO空间与内存空间的访问方式不同。在处理IO设备时,需要使用特殊的访问方法,以确保数据的正确传输和处理。因此,当需要将数据从内存复制到IO空间时,应该使用memcpy_toio函数而不是普通的memcpy函数。 总结起来,memcpy_toio函数是一个用于将数据从内存复制到IO空间的函数,通过封装memcpy函数以实现对IO空间的访问。它主要用于处理IO设备数据的传输和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [memcpy vs memcpy_toio](https://blog.csdn.net/qq_41592865/article/details/129560905)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [I/O资源读写接口 writel/readl/memcpy_toio](https://blog.csdn.net/u012294613/article/details/129118222)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

几何心凉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值