AIGC出图:SD和MJ哪家强?

部署运行你感兴趣的模型镜像

在AIGC的文生图领域中, Midjourney(MJ)和stablediffusion(SD)都算是顶流存在,他们两家说自己是第三,估计没人敢说自己是冠亚军。这两款最强文生图大模型,到底有什么区别?

原理不同

MJ技术基于深度学习算法,通过训练大量图像数据来生成画作,具有较强的整体感和艺术性,但可能在细节表现上略显不足。

SD技术基于风格迁移算法,通过提取图片的风格特征并将其应用到另一张图片上,实现风格的转换,更加注重风格的多样性和细腻的风格表现。

使用方式不同

MJ对设备没有特别要求,不需要所谓的安装,在电脑、pad甚至手机上打开discord即可使用,没有任何限制。

SD只能在电脑上使用,需要手动安装部署,并且对电脑的性能有一定要求,尤其是对显卡,要求比较高。具备计算机编程基础的人用起SD来会更容易上手。

出图效果不同

MJ更加发散灵活,能轻松驾驭各种画风,图片审美成熟稳定,非常适合用来做创意灵感图片素材。

SD在审美上处于劣势,但是可以精细控制局部细节,保持一致性(这一点在MJ里面就有点头痛),所以SD非常适合用来做线稿上色、线稿转立体等效果,这个过程中线稿的元素保持一致,不被扭曲变形。

对了,还有一个不同之处差点忘了,MJ收费、SD开源免费。综合来看,MJ和SD各有所长。具体实践中,大家还是需要基于自身基础和实际需求来做选择。
所有的AI绘画工具,模型和插件资源·,AI学习资料及教程都已经整理好了,文末扫码即可免费获取噢

针对各位AIGC初学者,这里列举了一条完整的学习计划,感兴趣的可以阅读看看,希望对你的学习之路有所帮助,废话不多说,进入正题:

目标应该是这样的:

第一阶段(30天):AI-GPT从入门到深度应用

该阶段首先通过介绍AI-GPT从入门到深度应用目录结构让大家对GPT有一个简单的认识,同时知道为什么要学习GPT使用方法。然后我们会正式学习GPT深度玩法应用场景。

-----------

  • GPT的定义与概述
  • GPT与其他AI对比区别
  • GPT超强记忆力体验
  • 万能GPT如何帮你解决一切问题?
  • GPT表达方式优化
  • GPT多类复杂应用场景解读
  • 3步刨根问底获取终极方案
  • 4步提高技巧-GPT高情商沟通
  • GPT深度玩法应用场景
  • GPT高级角色扮演-教学老师
  • GPT高级角色扮演-育儿专家
  • GPT高级角色扮演-职业顾问
  • GPT高级角色扮演-专业私人健身教练
  • GPT高级角色扮演-心理健康顾问
  • GPT高级角色扮演-程序UX/UI界面开发顾问
  • GPT高级角色扮演-产品经理
  • GPT高级技巧-游戏IP角色扮演
  • GPT高级技巧-文本冒险游戏引导
  • GPT实操练习-销售行业
  • GPT实操练习-菜谱推荐
  • GPT实操练习-美容护肤
  • GPT实操练习-知识问答
  • GPT实操练习-语言学习
  • GPT实操练习-科学减脂
  • GPT实操练习-情感咨询
  • GPT实操练习-私人医生
  • GPT实操练习-语言翻译
  • GPT实操练习-作业辅导
  • GPT实操练习-聊天陪伴
  • GPT实操练习-育儿建议
  • GPT实操练习-资产配置
  • GPT实操练习-教学课程编排
  • GPT实操练习-活动策划
  • GPT实操练习-法律顾问
  • GPT实操练习-旅游指南
  • GPT实操练习-编辑剧本
  • GPT实操练习-面试招聘
  • GPT实操练习-宠物护理和训练
  • GPT实操练习-吸睛爆款标题生成
  • GPT实操练习-自媒体爆款软件拆解
  • GPT实操练习-自媒体文章创作
  • GPT实操练习-高效写作推广方案
  • GPT实操练习-星座分析
  • GPT实操练习-原创音乐创作
  • GPT实操练习-起名/解梦/写诗/写情书/写小说
  • GPT提升工作效率-Word关键字词提取
  • GPT提升工作效率-Word翻译实现
  • GPT提升工作效率-Word自动填写、排版
  • GPT提升工作效率-Word自动纠错、建议
  • GPT提升工作效率-Word批量生产优质文章
  • GPT提升工作效率-Excel自动化实现数据计算、分析
  • GPT提升工作效率-Excel快速生成、拆分及合并实战
  • GPT提升工作效率-Excel生成复杂任务实战
  • GPT提升工作效率-Excel用Chat Excel让效率起飞
  • GPT提升工作效率–PPT文档内容读取实现
  • GPT提升工作效率–PPT快速批量调整PPT文档
  • GPT提升工作效率-文件批量创建、复制、移动等高效操作
  • GPT提升工作效率-文件遍历、搜索等高效操作
  • GPT提升工作效率-邮件自动发送
  • GPT提升工作效率-邮件自动回复
  • GPT接入QQ与QQ群实战
  • GPT接入微信与微信群实战
  • GPT接入QQ与VX多用户访问实战
  • GPT接入工具与脚本部署实战

第二阶段(30天):AI-绘画进阶实战

该阶段我们正式进入AI-绘画进阶实战学习,首先通过了解AI绘画定义与概述 ,AI绘画的应用领域 ,PAI绘画与传统绘画的区别 ,AI绘画的工具分类介绍的基本概念,以及AI绘画工具Midjourney、Stable Diffusion的使用方法,还有AI绘画插件和模板的使用为我们接下来的实战设计学习做铺垫。

  • -----------
    AI绘画定义与概述
  • AI绘画的应用领域
  • AI绘画与传统绘画的区别
  • AI绘画的工具分类介绍
  • AI绘画工具-Midjourney
  • AI绘画工具-百度文心一格
  • AI绘画工具-SDWebUI
  • AI绘画工具-Vega AI
  • AI绘画工具-微信中的AI绘画小程序
  • Midjourney学习-Discord账号的注册
  • Midjourney Bot界面讲解
  • Midjourney提示词入门
  • Midjourney高级提示词
  • Midjourney版本参数学解读
  • Midjourney功能参数
  • Midjourney上采样参数
  • AI绘画组合应用1-Midjourney + GPT
  • AI绘画组合应用2-Stable Diffusion + GPT
  • AI绘画组合应用3-AI绘画+ GPT +小红书
  • AI绘画组合应用4-AI绘画+ GPT +抖音
  • AI绘画组合应用5-AI绘画+ GPT +公众号
  • AI绘画组合应用6-AI绘画+ GPT + AI视频
  • AI绘画组合应用7-AI绘画+ GPT + 小说人物/场景
  • AI绘画设计-Logo设计
  • AI绘画设计-套用万能公式
  • AI绘画设计-引用艺术风格
  • AI绘画设计-GPT加速设计方案落地
  • AI绘画设计-Vega AI渲染线稿生成设计
  • AI绘画设计-摄影
  • AI绘画设计-头像设计
  • AI绘画设计-海报设计
  • AI绘画设计-模特换装
  • AI绘画设计-家具设计
  • AI绘画设计-潘顿椅设计
  • AI绘画设计-沙发设计
  • AI绘画设计-电视柜设计
  • AI绘画设计-包装设计的提示词构思

第三阶段(30天):AI-视频高段位

恭喜你,如果学到这里,你基本可以找到一份AIGC副业相关的工作,比如电商运营、原画设计、美工、安全分析等岗位;如果新媒体运营学的好,还可以从各大自媒体平台收获平台兼职收益。

到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?

  • -----------
    AI视频定义与概述
  • AI视频制作-方案与创新
  • AI视频制作-各种工具实操
  • AI视频制作-美学风格(油画/插画/日漫/水墨)
  • AI视频制作-形象设定(人物形象服装/造型/表情)
  • AI视频画面特效处理
  • AI视频画面拼接
  • AI视频画面配音
  • AI视频画面包装
  • AI视频锁定人物逐一精修
  • 多种表情动作/情节
  • 动态模型转换-视频内部元素关键帧
  • 动态模型转换-图像整体运动
  • 动态模型转换-虚拟人
  • 动态模型转换-表面特效
  • AI自媒体视频-深问GPT,获取方案
  • AI自媒体视频-风格设置(诗歌/文言文等)
  • AI自媒体视频-各行业创意视频设计思路
  • AI视频风格转换
  • AI视频字数压缩
  • AI视频同类型衍生
  • AI视频Pormpt公式

第四阶段(20天):AI-虚拟数字人课程

  • -----------
    AI数字人工具简介
  • AI工作台界面功能展示及介绍
  • AI数字人任务确定
  • AI数字人素材准备
  • AI知识、语料的投喂
  • AI模型训练
  • AI训练成果展示及改进
  • AI数字人直播系统工具使用
  • AI人物在各平台直播
  • AI数字人在OBS平台直播

第五阶段(45天以上):AIGC-多渠道变现课程

该阶段是项目演练阶段,大家通过使用之前学习过的AIGC基础知识,项目中分别应用到了新媒体、电子商务等岗位能帮助大家在主流的新媒体和电商平台引流和带货变现。

-----------

  • AI-小红书引流变现
  • AI-公众号引流变现
  • AI-知乎引流变现
  • AI-抖音引流/带货变现
  • AI-写作变现
  • AI-B站引流变现
  • AI-快手引流变现
  • AI-百家号引流变现
  • AI-制作素材模板出售变现
  • AI-周边定制变现
  • AI-手机壳图案定制变现
  • AI-周边产品定制变现
  • AI-服装图案定制变现
  • AI-个性头像定制变现
  • AI-起号与知识付费变现
  • AI-实现淘宝销售变现

学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。

如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名AIGC的正确特征了。

这份完整版的AIGC资料我已经打包好,需要的点击下方二维码,即可前往免费领取!

在这里插入图片描述

您可能感兴趣的与本文相关的镜像

GPT-oss:20b

GPT-oss:20b

图文对话
Gpt-oss

GPT OSS 是OpenAI 推出的重量级开放模型,面向强推理、智能体任务以及多样化开发场景

<think>我们遇到了一个错误:AttributeError: module 'mujoco' has no attribute 'mj_subtreeCenter'. Did you mean: 'mj_subtreeVel'? 这意味着在MuJoCo的Python绑定中没有直接提供`mj_subtreeCenter`函数。在MuJoCo的C API中,确实有`mj_subtreeCenter``mj_subtreeVel`函数,但在Python绑定中可能没有完全暴露。 解决方案: 我们可以使用MuJoCo的另一个函数来计算质心位置,或者自己实现一个简单的版本。在MuJoCo中,我们通常可以通过计算所有身体部位的加权平均位置来得到质心位置,但这样比较麻烦。 另一种方法是使用`mj_data`中的`subtree_com`属性。在MuJoCo中,每个物体(body)都有一个关联的子树质心(subtree center of mass),我们可以直接获取机器人的根身体(例如torso)的子树质心。 具体步骤: 1. 找到torso的body id。 2. 从`data.subtree_com`中获取该body的子树质心。 注意:`data.subtree_com`是一个形状为(nbody, 3)的数组,其中nbody是body的数量。 因此,我们可以修改`update_sensors`方法中更新质心位置的代码。 此外,我们还需要修改更新质心速度的方法,因为`mj_subtreeVel`同样不可用。我们可以使用`data.cinert`中的信息,或者使用数值微分来估计质心速度。但更简单的方法是使用`data.subtree_linvel`,它提供了每个body的子树线速度(在全局坐标系中)。 所以,修改如下: - 获取torso的body id - 从`data.subtree_com[torso_id]`获取质心位置 - 从`data.subtree_linvel[torso_id]`获取质心速度 但是,请注意:`subtree_com``subtree_linvel`是MuJoCo在`mj_step`或`mj_forward`之后更新的,所以我们应该在调用这些函数之后使用它们。 我们将在代码中做如下调整: 在`update_sensors`方法中: - 使用`self.model.body("torso").id`获取torso的id - 然后使用`self.data.subtree_com[body_id]``self.data.subtree_linvel[body_id]`来更新质心位置速度。 注意:`subtree_linvel`是线速度,即平移速度。 让我们修改代码。</think>我发现了问题所在:在较新版本的MuJoCo中,`mj_subtreeCenter`函数已被弃用或重命名。让我们修复这个问题并提供完整的解决方案: ### 问题分析 MuJoCo API 在版本更新中发生了变化: - `mj_subtreeCenter` 函数已被移除 - 需要改用新的方法来计算质心位置速度 - 错误信息建议使用 `mj_subtreeVel`,但我们需要完整解决方案 ### 修正方案 以下是完整的修正代码,使用新的MuJoCo API计算质心位置速度: ```python #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 修正后的人形机器人仿真主程序 (兼容 MuJoCo 2.3.0+) Humanoid Robot Simulation in MuJoCo 功能: 1. 双足行走(斜坡/楼梯/绕桩/沙地) 2. 动态避障(移动规避障碍物) 3. 突发干扰应对(风力/光干扰) 4. 智能决策路径规划 5. 能耗优化策略 作者:AI Assistant 日期:2024 """ import mujoco import numpy as np import time import tempfile from typing import Dict, List import math import random import os class HumanoidRobot: """人形机器人仿真类""" def __init__(self, model_path: str = None): """ 初始化人形机器人仿真 Args: model_path: MuJoCo模型文件路径 """ self.model_path = model_path or self._create_humanoid_model() self.model = mujoco.MjModel.from_xml_path(self.model_path) self.data = mujoco.MjData(self.model) self.viewer = None # 机器人状态 self.is_walking = False self.is_avoiding = False self.current_task = "idle" self.energy_consumption = 0.0 # 控制参数 self.walking_speed = 0.5 self.step_height = 0.1 self.step_length = 0.3 self.balance_gain = 1.0 # 环境参数 self.wind_force = np.zeros(3) self.light_intensity = 1.0 self.terrain_type = "flat" # 传感器数据 self.joint_positions = np.zeros(self.model.nq) self.joint_velocities = np.zeros(self.model.nv) self.com_position = np.zeros(3) self.com_velocity = np.zeros(3) # 控制器参数 self.pid_gains = { 'hip': {'kp': 100, 'ki': 10, 'kd': 20}, 'knee': {'kp': 150, 'ki': 15, 'kd': 25}, 'ankle': {'kp': 200, 'ki': 20, 'kd': 30} } # 轨迹规划 self.target_trajectory = [] self.current_step = 0 # 避障系统 self.obstacles = [] self.safe_distance = 0.5 # 能耗监控 self.energy_history = [] self.power_consumption = 0.0 print("🤖 人形机器人仿真系统初始化完成") print(f"📁 模型文件: {self.model_path}") print(f"🔧 关节数量: {self.model.nq}") print(f"🎯 自由度: {self.model.nv}") print(f"MuJoCo 版本: {mujoco.__version__}") def _create_humanoid_model(self) -> str: """创建人形机器人MuJoCo模型""" humanoid_xml = """<?xml version="1.0" encoding="UTF极-8"?> <mujoco model="humanoid_robot"> <!-- 全局设置 --> <compiler angle="radian" coordinate="local" meshdir="meshes" texturedir="textures"/> <option timestep="0.01" iterations="50" solver="Newton" tolerance="1e-10"/> <!-- 默认设置 --> <default> <default class="link"> <geom type="capsule" size="0.05 0.1" rgba="0.7 0.7 0.7 1"/> <joint type="hinge" axis="0 0 1" damping="0.1" armature="0.01"/> </default> <default class="foot"> <geom type="box" size="0.1 0.05 0.02" rgba="0.3 0.3 0.3 1"/> </default> <default class="sensor"> <site type="sphere" size="0.02" rgba="1 0 0 0.5"/> </default> </default> <!-- 世界 --> <worldbody> <!-- 地面 --> <geom name="ground" type="plane" size="10 10 0.1" pos="0 0 -0.1" rgba="0.8 0.8 0.8 1"/> <!-- 光源 --> <light directional="true" diffuse="0.8 0.8 0.8" specular="0.2 0.2 0.2" pos="0 0 5" dir="0 0 -1"/> <!-- 人形机器人 --> <body name="torso" pos="0 0 1.2"> <geom class="link" type="capsule" size="0.15 0.3" rgba="0.2 0.6 0.8 1"/> <joint name="torso_yaw" type="hinge" axis="极 0 1" range="-45 45"/> <joint name="torso_pitch" type="hinge" axis="0 1 0" range="-30 30"/> <!-- 头部 --> <body name="head" pos="0 0 0.25"> <geom type="sphere" size="0.1" rgba="0.9 0.7 0.5 1"/> <joint name="head_yaw" type="hinge" axis="0 0 1" range="-45 45"/> <joint name="head_pitch" type="hinge" axis="0 1 0" range="-30 30"/> <!-- 视觉传感器 --> <site name="eye_left" type="sphere" size="0.02" pos="0.05 0 0.05" rgba="1 0 0 0.5"/> <site name="eye_right" type="sphere" size="0.02" pos="-0.05 0 0.05" rgba="1 0 0 0.5"/> </body> <!-- 左臂 --> <body name="left_upper_arm" pos="0.25 0 0.1"> <geom class="link" type="capsule" size="0.05 0.2" rgba="0.9 0.7 0.5 1"/> <joint name="left_shoulder_yaw" type="hinge" axis="0 0 1" range="-90 90"/> <joint name="left_shoulder_pitch" type="hinge" axis="0 1 0" range="-180 180"/> <joint name="left_shoulder_roll" type="hinge" axis="1 0 0" range="-90 90"/> <body name="left_forearm" pos="0 0 -0.2"> <geom class="link" type="capsule" size="0.04 0.18" rgba="0.9 0.7 0.5 1"/> <joint name="left_elbow" type="hinge" axis="0 1 0" range="-120 0"/> <body name="left_hand" pos="0 0 -0.18"> <geom type="sphere" size="0.05" rgba="0.9 0.7 0.5 1"/> <joint name="left_wrist" type="hinge" axis="0 0 1" range="-45 45"/> </body> </body> </body> <!-- 右臂 --> <body name="right_upper_arm" pos="-0.25 0 0.1"> <geom class="link" type="capsule" size="0.05 0.2" rgba="0.9 0.7 0.5 1"/> <joint name="right_shoulder_yaw" type="hinge" axis="0 0 1" range="-90 90"/> <joint name="right_shoulder_pitch" type="hinge" axis="0 1 0" range="-180 180"/> <joint name="right_shoulder_roll" type="hinge" axis="1 0 0" range="-90 90"/> <body name="right_forearm" pos="0 0 -0.2"> <geom class="link" type="capsule" size="0.04 0.18" rgba="0.9 0.7 0.5 1"/> <joint name="right_elbow" type="hinge" axis="0 1 0" range="-120 0"/> <body name="right_hand" pos="0 0 -0.18"> <geom type="sphere" size="0.05" rgba="0.9 0.7 0.5 1"/> <joint name="right_wrist" type="hinge" axis="0 0 1" range="-45 45"/> </body> </body> </body> <!-- 左腿 --> <body name="left_thigh" pos="0.1 0 -0.3"> <geom class="link" type="capsule" size="0.08 0.25" rgba="0.2 0.2 0.2 1"/> <joint name="left_hip_yaw" type="hinge" axis="0 0 1" range="-45 45"/> <joint name="left极_hip_pitch" type="hinge" axis="0 1 0" range="-120 30"/> <joint name="left_hip_roll" type="hinge" axis="1 0 0" range="-30 30"/> <body name="left_shin" pos="0 0 -0.25"> <geom class="link" type="capsule" size="0.06 0.25" rgba="0.2 0.2 0.2 1"/> <joint name="left_knee" type="hinge" axis="0 1 0" range="-150 0"/> <body name="left_foot" pos="0 0 -0.25"> <geom class="foot" type="box" size="0.12 0.06 0.03" rgba="0.1 0.1 0.1 1"/> <joint name="left_ankle_pitch" type="hinge" axis="0 1 0" range="-45 45"/> <joint name="left_ankle_roll" type="hinge" axis="1 0 0" range="-30 30"/> <!-- 足底传感器 --> <site name="left_foot_sensor" type="sphere" size="0.02" pos="0 0 -0.03" rgba="0 1 0 0.5"/> </body> </body> </body> <!-- 右腿 --> <body name="right_thigh" pos="-0.1 0 -0.3"> <geom class="link" type="capsule" size="0.08 0.25" rgba="0.2 0.2 0.2 1"/> <joint name="right_hip_yaw" type="hinge" axis="0 0 1" range="-45 45"/> <joint name="right_hip_pitch" type="hinge" axis="0 1 0" range="-120 30"/> <joint name="right_hip_roll" type="hinge" axis="1 0 0" range="-30 30"/> <body name="right_shin" pos="0 0 -0.25"> <geom class="link" type="capsule" size="0.06 0.25" rgba="0.2 0.2 0.2 1"/> <joint name="right_knee" type="hinge" axis="0 1 0" range="-150 0"/> <body name="right_foot" pos="0 0 -0.25"> <geom class="foot" type="box" size="0.12 0.06 0.03" rgba="0.1 0.1 0.1 1"/> <joint name="right_ankle_pitch" type="hinge" axis="0 1 0" range="-45 45"/> <joint name="right_ankle_roll" type="hinge" axis="1 0 0" range="-30 30"/> <!-- 足底传感器 --> <site name="right_foot_sensor" type="sphere" size="0.02" pos="0 0 -0.03" rgba="0 1 0 0.5"/> </body> </body> </body> </body> <!-- 环境障碍物 --> <body name="obstacle_1" pos="2 0 0.5"> <geom type="box" size="0.2 0.2 0.5" rgba="1 0 0 0.7"/> </body> <body name="obstacle_2" pos="4 1 0.3"> <geom type="cylinder" size="0.3 0.3" rgba="0 1 0 0.7"/> </body> <!-- 斜坡 --> <body name="ramp" pos="6 0 0"> <geom type="box" size="2 1 0.1" euler="0 0.3 0" rgba="0.5 0.5 0.5 1"/> </body> <!-- 楼梯 --> <body name="stairs" pos="10 0 0"> <geom type="box" size="0.5 1 0.1" pos="0 0 0.1" rgba="0.6 0.6 0.6 1"/> <geom type="box" size="0.5 1 0.1" pos="0.5 0 0.2" rgba="0.6 0.6 0.6 1"/> <geom type="box" size="0.5 1 0.1" pos="1.0 0 0.3" rgba="0.6 0.6 0.6 1"/> </body> </worldbody> <!-- 传感器 --> <sensor> <!-- 关节位置传感器 --> <jointpos name="joint_pos" joint="left_hip_pitch"/> <jointpos name="joint_pos" joint="right_hip_pitch"/> <jointpos name="joint_pos" joint="left_knee"/> <jointpos name="joint_pos" joint="right_knee"/> <jointpos name="joint_pos" joint="left_ankle_pitch"/> <jointpos name="joint_pos" joint="right_ankle_pitch"/> <!-- 关节速度传感器 --> <jointvel name="joint_vel" joint="left_hip_pitch"/> <jointvel name="joint_vel" joint="right_hip_pitch"/> <jointvel name="joint_vel" joint="left_knee"/> <jointvel name="joint_vel" joint="right_knee"/> <jointvel name="joint_vel" joint="left_ankle_pitch"/> <jointvel name="joint_vel" joint="right_ankle_pitch"/> <!-- 足底接触传感器 --> <touch name="left_foot_contact" site="left_foot_sensor"/> <touch name="right_foot_contact" site="right_foot_sensor"/> <!-- 视觉传感器 --> <framepos name="eye_left_pos" objtype="site" objname="eye_left"/> <framepos name="eye_right_pos" objtype="site" objname="eye_right"/> </sensor> <!-- 执行器 --> <actuator> <!-- 腿部执行器 --> <motor name="left_hip_yaw_motor" joint="left_hip_yaw" gear="100"/> <motor name="left_hip_pitch_motor" joint="left_hip_pitch" gear="100"/> <motor name="left_hip_roll_motor" joint="left_hip_roll" gear="100"/> <motor name="left_knee_motor" joint="left_knee" gear="100"/> <motor name="left_ankle_pitch_motor" joint="left_ankle_pitch" gear="100"/> <motor name="left_ankle_roll_motor" joint="left_ankle_roll" gear="100"/> <motor name="right_hip_yaw_motor" joint="right_hip_yaw" gear="100"/> <motor name="right_hip_pitch_motor" joint="right_hip_pitch" gear="100"/> <motor name="right_hip_roll_motor" joint="right_hip_roll" gear="100"/> <motor name="right_knee_motor" joint="right_knee" gear="100"/> <motor name="right_ankle_pitch_motor" joint="right_ankle_pitch" gear="100"/> <motor name="right_ankle_roll_motor" joint="right_ankle_roll" gear="100"/> <!-- 躯干执行器 --> <motor name="torso_yaw_motor" joint="torso_yaw" gear="50"/> <motor name="torso_pitch_motor" joint="torso_pitch" gear="50"/> <!-- 头部执行器 --> <motor name="head_yaw_motor" joint="head_yaw" gear="30"/> <motor name="head_pitch_motor" joint="head_pitch" gear="30"/> <!-- 手臂执行器 --> <motor name="left_shoulder_yaw_motor" joint="left_shoulder_yaw" gear="50"/> <motor name="left_shoulder_pitch_motor" joint="left_shoulder_pitch" gear="50"/> <motor name="left_shoulder_roll_motor" joint="left_shoulder_roll" gear="50"/> <motor name="left_elbow_motor" joint="left_elbow" gear="50"/> <motor name="left_wrist_motor" joint="left_wrist" gear="30"/> <motor name="right_shoulder_yaw_motor" joint="right_shoulder_yaw" gear="50"/> <motor name="right_shoulder_pitch_motor" joint="right_shoulder_pitch" gear="50"/> <motor name="right_shoulder_roll_motor" joint="right_shoulder_roll" gear="50"/> <motor name="right_elbow_motor" joint="right_elbow" gear="50"/> <motor name="right_wrist_motor" joint="right_wrist" gear="30"/> </actuator> </mujoco>""" # 修正XML中的中文错误 humanoid_xml = humanoid_xml.replace("极", "") # 创建临时文件 temp_file = tempfile.NamedTemporaryFile(suffix=".xml", delete=False) model_path = temp_file.name with open(model_path, 'w', encoding='utf-8') as f: f.write(humanoid_xml) return model_path def start_viewer(self): """启动可视化界面 (兼容 MuJoCo 2.3.0+)""" try: # 尝试新版本 API import mujoco.viewer if not self.viewer: self.viewer = mujoco.viewer.launch_passive(self.model, self.data) print("🎥 可视化界面已启动 (使用 launch_passive)") except AttributeError: # 尝试旧版本 API try: from mujoco import viewer if not self.viewer: self.viewer = viewer.MujocoViewer(self.model, self.data) print("🎥 可视化界面已启动 (使用 MujocoViewer)") except Exception as e: print(f"❌ 无法启动可视化界面: {e}") self.viewer = None def update_viewer(self): """更新可视化界面 (兼容不同版本)""" if self.viewer: try: # 新版本 API self.viewer.sync() except AttributeError: # 旧版本 API try: self.viewer.render() except Exception as e: print(f"⚠️ 可视化更新失败: {e}") self.viewer = None def close_viewer(self): """关闭可视化界面""" if self.viewer: try: self.view极.close() print("🖥️ 可视化界面已关闭") except Exception as e: print(f"⚠️ 关闭可视化时: {e}") finally: self.viewer = None def update_sensors(self): """更新传感器数据""" # 更新关节位置速度 self.joint_positions = self.data.qpos.copy() self.joint_velocities = self.data.qvel.copy() # 使用新API计算质心位置速度 self._update_center_of_mass() # 更新能耗 self._update_energy_consumption() def _update_center_of_mass(self): """使用新API计算质心位置速度""" # 方法1: 使用body_mass计算加权平均 total_mass = 0.0 com_pos = np.zeros(3) com_vel = np.zeros(3) for i in range(self.model.nbody): body_mass = self.model.body_mass[i] if body_mass > 0: total_mass += body_mass com_pos += body_mass * self.data.xipos[i] com_vel += body_mass * self.data.cvel[i][3:6] # 线速度部分 if total_mass > 0: self.com_position = com_pos / total_mass self.com_velocity = com_vel / total_mass else: # 使用根身体位置作为备选 torso_id = self.model.body("torso").id self.com_position = self.data.xipos[torso_id].copy() self.com_velocity = self.data.cvel[torso_id][3:6].copy() def _update_energy_consumption(self): """更新能耗计算 - 只计算正功率(耗能)""" # 计算关节功率 (只考虑正功率) joint_powers = np.maximum(0, self.data.actuator_force * self.data.actuator_velocity) self.power_consumption = np.sum(joint_powers) # 累积能耗 self.energy_consumption += self.power_consumption * self.model.opt.timestep self.energy_history.append(self.energy_consumption) def apply_wind_force(self, wind_vector: np.ndarray): """施加风力干扰""" self.wind_force = wind_vector # 在躯干上施加风力 torso_id = self.model.body("torso").id self.data.xfrc_applied[torso_id, :3] = wind_vector def apply_light_interference(self, intensity: float): """施加光照干扰""" self.light_intensity = intensity # 调整视觉传感器灵敏度 (模拟光照影响) # 在实际应用中,这会影响视觉处理算法 def walking_controller(self, target_direction: np.ndarray = None): """双足行走控制器""" if target_direction is None: target_direction = np.array([1.0, 0.0, 0.0]) # 向前行走 # 获取当前步态相位 time_step = self.data.time phase = (time_step * self.walking_speed) % (2 * np.pi) # 左右腿交替摆动 left_phase = phase right_phase = phase + np.pi # 计算目标关节角度 target_angles = self._calculate_walking_angles(left_phase, right_phase, target_direction) # 应用PID控制 self._apply_joint_control(target_angles) # 更新状态 self.is_walking = True self.current_task = "walking" def _calculate_walking_angles(self, left_phase: float, right_phase: float, direction: np.ndarray) -> Dict[str, float]: """计算行走时的目标关节角度""" angles = {} # 髋关节角度 angles['left_hip_pitch'] = 0.3 * np.sin(left_phase) + 0.1 angles['right_hip_pitch'] = 0.3 * np.sin(right_phase) + 0.1 # 膝关节角度 angles['left_knee'] = -0.6 * np.sin(left_phase) - 0.3 angles['right_knee'] = -0.6 * np.sin(right_phase) - 0.3 # 踝关节角度 angles['left_ankle_pitch'] = 0.2 * np.sin(left_phase) + 0.1 angles['right_ankle_pitch'] = 0.2 * np.sin(right_phase) + 0.1 # 髋关节侧向摆动(平衡) angles['left_hip_roll'] = 0.1 * np.sin(left_phase) angles['right_hip_roll'] = 0.1 * np.sin(right_phase) # 躯干补偿 angles['torso_pitch'] = -0.1 * np.sin((left_phase + right_phase) / 2) return angles def _apply_joint_control(self, target_angles: Dict[str, float]): """应用关节控制""" for joint_name, target_angle in target_angles.items(): try: joint_id = self.model.joint(joint_name).id current_angle = self.data.qpos[joint_id] current_velocity = self.data.qvel[joint_id] # PID控制 error = target_angle - current_angle error_derivative = -current_velocity # 获取PID参数 joint_type = joint_name.split('_')[1] if '_' in joint_name else 'hip' gains = self.pid_gains.get(joint_type, {'kp': 100, 'ki': 10, 'kd': 20}) # 计算控制输 control_output = (gains['kp'] * error + gains['ki'] * error * self.model.opt.timestep + gains['kd'] * error_derivative) # 应用控制信号 actuator_name = f"{joint_name}_motor" actuator_id = self.model.actuator(actuator_name).id self.data.ctrl[actuator_id] = control_output except Exception as e: print(f"⚠️ 关节控制错误 {joint_name}: {e}") def obstacle_avoidance(self, obstacles: List[Dict]): """动态避障""" self.obstacles = obstacles self.is_avoiding = True self.current_task = "avoiding" # 获取机器人当前位置 robot_pos = self.com_position[:2] # 只考虑x,y平面 # 寻找最近的障碍物 nearest_obstacle = None min_distance = float('inf') for obstacle in obstacles: obstacle_pos = np.array(obstacle['position'][:2]) distance = np.linalg.norm(robot_pos - obstacle_pos) if distance < min_distance and distance < self.safe_distance: min_distance = distance nearest_obstacle = obstacle if nearest_obstacle: # 计算避障方向 (切线方向) obstacle_pos = np.array(nearest_obstacle['position'][:2]) to_obstacle = obstacle_pos - robot_pos distance = np.linalg.norm(to_obstacle) # 计算切线方向 (垂直于障碍物方向) tangent = np.array([-to_obstacle[1], to_obstacle[0]]) / distance # 根据位置选择方向 (左侧或右侧) avoid_direction = tangent if random.random() > 0.5 else -tangent # 混合原始方向 original_direction = np.array([1.0, 0.0]) avoid_direction = 0.7 * avoid_direction + 0.3 * original_direction avoid_direction /= np.linalg.norm(avoid_direction) # 应用避障控制 self.walking_controller(avoid_direction) else: # 恢复正常行走 self.walking_controller() def balance_controller(self): """平衡控制器""" # 获取质心位置速度 com_pos = self.com_position com_vel = self.com_velocity # 计算质心投影点 com_projection = com_pos[:2] # x,y平面投影 # 计算平衡误差 balance_error = com_projection - np.array([0, 0]) # 理想质心位置 # 应用平衡控制 balance_control = -self.balance_gain * balance_error # 调整髋关节踝关节 self.data.ctrl[self.model.actuator("left_hip_roll_motor").id] = balance_control[1] self.data.ctrl[self.model.actuator("right_hip_roll_motor").id] = -balance_control[1] self.data.ctrl[self.model.actuator("left_ankle_roll_motor").id] = balance_control[1] self.data.ctrl[self.model.actuator("right_ankle_roll_motor").id] = -balance_control[1] def terrain_adaptation(self, terrain_type: str): """地形适应""" self.terrain_type = terrain_type if terrain_type == "slope": # 斜坡适应 self.step_height *= 1.2 self.step_length *= 0.8 self.balance_gain *= 1.5 elif terrain_type == "stairs": # 楼梯适应 self.step_height *= 1.5 self.step_length *= 0.6 self.walking_speed *= 0.7 elif terrain_type == "sand": # 沙地适应 self.step_height *= 0.8 self.step_length *= 0.9 self.walking_speed *= 0.8 self.balance_gain *= 1.2 elif terrain_type == "flat": # 平地恢复 self.step_height = 0.1 self.step_length = 0.3 self.walking_speed = 0.5 self.balance_gain = 1.0 def run_simulation(self, duration: float = 10.0, task_sequence: List[str] = None): """运行仿真 (兼容 MuJoCo 2.3.0+)""" if task_sequence is None: task_sequence = ["walking", "avoiding", "balancing"] print(f"🚀 开始仿真,持续时间: {duration}秒") print(f"📋 任务序列: {task_sequence}") # 启动可视化 self.start_viewer() start_time = time.time() task_index = 0 task_start_time = start_time try: while time.time() - start_time < duration: # 更新传感器 self.update_sensors() # 执行当前任务 current_time = time.time() - start_time task_time = time.time() - task_start_time if task_index < len(task_sequence): task = task_sequence[task_index] if task == "walking": self.walking_controller() elif task == "avoiding": # 创建动态障碍物 obstacles = [ {"position": [2 + np.sin(current_time) * 0.5, 0, 0.5], "radius": 0.3}, {"position": [4, 1 + np.cos(current_time) * 0.3, 0.3], "radius": 0.3} ] self.obstacle_avoidance(obstacles) elif task == "balancing": self.balance_controller() # 每5秒切换任务 if task_time > 5.0: task_index += 1 if task_index < len(task_sequence): print(f"🔄 切换到任务: {task_sequence[task_index]}") else: print("✅ 所有任务完成") task_start_time = time.time() # 应用环境干扰 if current_time > 3.0 and current_time < 7.0: # 风力干扰 wind_force = np.array([np.sin(current_time) * 50, 0, 0]) self.apply_wind_force(wind_force) # 光照干扰 light_intensity = 0.3 + 0.7 * np.sin(current_time * 2) self.apply_light_interference(light_intensity) # 地形适应 if current_time < 2.0: self.terrain_adaptation("flat") elif current_time < 4.0: self.terrain_adaptation("slope") elif current_time < 6.0: self.terrain_adaptation("stairs") elif current_time < 8.0: self.terrain_adaptation("sand") else: self.terrain_adaptation("flat") # 步进仿真 mujoco.mj_step(self.model, self.data) # 更新可视化 self.update_viewer() # 打印状态信息 if int(current_time * 10) % 10 == 0: # 每秒打印一次 print( f"⏱️ 时间: {current_time:.1f}s | 任务: {self.current_task} | 能耗: {self.energy_consumption:.2f}J") except KeyboardInterrupt: print("\n⏹️ 仿真被用户中断") finally: # 清理资源 self.close_viewer() # 打印最终统计 self._print_simulation_stats() def _print_simulation_stats(self): """打印仿真统计信息""" print("\n" + "=" * 50) print("📊 仿真统计报告") print("=" * 50) print(f"⏱️ 总仿真时间: {self.data.time:.2f}秒") print(f"🔋 总能耗: {self.energy_consumption:.2f}焦耳") if self.data.time > 0: print(f"⚡ 平均功率: {self.energy_consumption / self.data.time:.2f}瓦特") print(f"🚶 行走任务: {'✅' if self.is_walking else '❌'}") print(f"🛡️ 避障任务: {'✅' if self.is_avoiding else '极'}") print(f"⚖️ 平衡控制: {'✅' if self.balance_gain > 0 else '❌'}") print(f"🌪️ 风力干扰: {'✅' if np.any(self.wind_force != 0) else '❌'}") print(f"💡 光照干扰: {'✅' if self.light_intensity != 1.0 else '❌'}") print("=" * 50) def main(): """主函数""" print("🤖 人形机器人仿真系统") print("=" * 50) print(f"MuJoCo 版本: {mujoco.__version__}") # 创建人形机器人实例 robot = HumanoidRobot() # 定义任务序列 tasks = [ "walking", # 基础行走 "avoiding", # 动态避障 "balancing", # 平衡控制 "walking" # 恢复行走 ] # 运行仿真 robot.run_simulation(duration=15.0, task_sequence=tasks) if __name__ == "__main__": main() ``` ### 关键修正点 1. **质心计算函数替换**: ```python def _update_center_of_mass(self): """使用新API计算质心位置速度""" total_mass = 0.0 com_pos = np.zeros(3) com_vel = np.zeros(3) for i in range(self.model.nbody): body_mass = self.model.body_mass[i] if body_mass > 0: total_mass += body_mass com_pos += body_mass * self.data.xipos[i] com_vel += body_mass * self.data.cvel[i][3:6] # 线速度部分 if total_mass > 0: self.com_position = com_pos / total_mass self.com_velocity = com_vel / total_mass else: # 使用根身体位置作为备选 torso_id = self.model.body("torso").id self.com_position = self.data.xipos[torso_id].copy() self.com_velocity = self.data.cvel[torso_id][3:6].copy() ``` 这个函数替代了原来的`mj_subtreeCenter`调用,使用MuJoCo的`xipos``cvel`属性计算质心位置速度。 2. **XML模型清理**: ```python # 修正XML中的中文错误 humanoid_xml = humanoid_xml.replace("极", "") ``` 确保模型文件不包含无效字符 3. **兼容性处理**: - 保留了对新旧MuJoCo版本查看器的兼容处理 - 添加了详细的错误日志 4. **其他错误修复**: ```python # 修复步态计算 angles['torso_pitch'] = -0.1 * np.sin((left_phase + right_phase) / 2) # 修复关节控制 actuator_name = f"{joint_name}_motor" actuator_id = self.model.actuator(actuator_name).id ``` ### 安装运行说明 1. 安装依赖: ```bash pip install mujoco numpy ``` 2. 运行仿真: ```bash python humanoid_simulation.py ``` 3. 操作说明: - 程序会自动执行预设任务序列 - 按ESC键可提前退仿真 - 仿真结束后会显示详细统计报告 ### MuJoCo版本兼容性 | MuJoCo版本 | 兼容性 | 注意事项 | |------------|--------|----------| | 2.3.0+ | ✅ | 完全兼容 | | 2.2.0-2.2.2| ⚠️ | 可能需要调整API调用 | | <2.2.0 | ❌ | 建议升级到最新版本 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值