亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Manufacturing Technology >> 制造工艺

自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作

所需组件与物料

组件数量
Arduino Nano R31
SG90 微型舵机3
HC-SR04 超声波测距模块1
PCBWay 定制 PCB1项目链接

所需应用与在线服务

项目简介

引言

《钢铁侠》中的“贾维斯”始终是程序员的梦想。本项目展示如何用 Python 实现语音助手核心,并配合 Arduino 控制实体机器人做动作,比纯软件助手更酷。机器人命名为“Jaundice”(黄疸),因整体喷漆成亮黄色。

核心部件清单

电子部分:Arduino Nano ×1、SG90 舵机 ×3、HC-SR04 超声波 ×1

机身结构:PVC 板(白色利于上色,我用蓝色)、舵机轮(做底座)

工具:美工刀、剪刀、热熔胶、喷漆

软件:Python 3、Arduino IDE

系统原理

AI 核心运行在电脑端(Python 语音识别、NLP、语音合成、浏览器自动化、串口通信),Arduino 通过 USB 连接电脑,仅负责执行预置动作函数。Python 发送单字节指令(如 'h' 触发挥手),Arduino 解析执行对应动作函数。

电路与 PCB 设计

自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作
自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作

为避免飞线烧毁元件,我用 EasyEDA 设计多功能 PCB(含 microSD、蓝牙模块座、5V 外部供电接口),通过 PCBWay 打样,3 天从中国寄达孟加拉,品质优秀。走线宽 0.3mm 可承载 1A 电流。PCB 文件可在项目页下载。

焊接与测试

自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作

焊接时注意通风防吸入烟雾。本项目未使用蓝牙与 TF 卡模块,留空焊盘。3 个舵机 + 1 个超声波均可从 USB 取电,无需额外电源。如需外部供电,建议 2S 锂聚合物(7.4V),电压过高会烧毁舵机。

机身制作

自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作

用 PVC 板制作盒状机身(容纳主板与舵机),头部预留两孔安装超声波模块做“眼睛”。手臂分别做成扳手与插头造型。尺寸随意,按喜好发挥。务必在合盖前装好所有电子件,先组装再喷漆(我错漆在 Arduino 上了)。

编程 1:Python 端(语音交互与指令分发)

自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作

安装 Python(勾选 Add to PATH),然后安装依赖库:

pip install speechrecognition pyaudio pyttsx3 pywhatkit pyserial

核心代码逻辑:

# 完整代码请访问 GitHub: https://github.com/ashraf-minhaj/JAUNDICE-AI-Assistant-Robot
import speech_recognition as sr
import pyttsx3, datetime, webbrowser, serial, pywhatkit, random

robot_name = 'jaundice'
hi_words = ['hi', 'hello', 'yo baby', 'salam']
bye_words = ['bye', 'tata', 'hasta la vista']

engine = pyttsx3.init()
listener = sr.Recognizer()
try:
    port = serial.Serial("COM15", 9600)
    print("Physical body connected.")
except:
    print("Unable to connect to physical body")

def listen():
    try:
        with sr.Microphone() as source:
            print("Talk>>")
            voice = listener.listen(source)
            command = listener.recognize_google(voice).lower()
            print(command)
            if command.split(' ')[0] == robot_name:
                print("[wake-up word found]")
                process(command)
    except: pass

def process(words):
    word_list = words.split(' ')[1:]
    if len(word_list)==1 and word_list[0]==robot_name:
        talk("How can I help?"); return
    if word_list[0] == 'play':
        talk("Okay boss, playing"); port.write(b'u')
        pywhatkit.playonyt(' '.join(word_list[1:])); port.write(b'l'); return
    if word_list[0] == 'search':
        port.write(b'u'); talk("Okay boss, searching"); port.write(b'l')
        pywhatkit.search(' '.join(word_list[1:])); return
    if word_list[0]=='get' and word_list[1]=='info':
        port.write(b'u'); talk("Okay, I am right on it"); port.write(b'u')
        talk(pywhatkit.info(' '.join(word_list[2:]))); return
    if word_list[0] == 'open':
        port.write(b'l'); talk("Opening, sir")
        webbrowser.open(f"https://{''.join(word_list[1:])}"); return
    if word_list[0] == 'uppercut': port.write(b'U')
    elif word_list[0] == 'smash': port.write(b's')
    elif word_list[0] == 'punch': port.write(b'p')
    for word in word_list:
        if word in hi_words: port.write(b'h'); talk(random.choice(hi_words))
        elif word in bye_words: talk(random.choice(bye_words))

def talk(sentence):
    engine.say(sentence); engine.runAndWait()

while True: listen()

编程 2:Arduino 端(动作库与串口解析)

/** JAUNDICE: AI Assistant robot with Arduino and Python
 * author: ashraf minhaj, License: GPL3+
 */
#include 
Servo head, l_hand, r_hand;
int trig = 4, echo = 5;
byte val = 0;

void setup() {
  head.attach(2); l_hand.attach(3); r_hand.attach(4);
  Serial.begin(9600);
}

void standby() { // 待机姿态
  head.write(90);
  int r_pos = 30, l_pos = map(r_pos, 0, 180, 180, 0);
  l_hand.write(l_pos); r_hand.write(r_pos);
}

void hi() { // 挥手
  head.write(90);
  for(int i=30; i<=170; i++){ r_hand.write(i); delay(5); }
  for(int i=170; i>=100; i--){ r_hand.write(i); delay(5); }
  for(int i=100; i<=170; i++){ r_hand.write(i); delay(5); }
  for(int i=170; i>=30; i--){ r_hand.write(i); delay(5); }
  standby();
}

void hands_up() { // 双手举起
  for(int i=30; i<=170; i++){
    int r_pos=i, l_pos=map(r_pos,0,180,180,0);
    l_hand.write(l_pos); r_hand.write(r_pos); delay(5);
  }
  delay(600);
  for(int i=170; i>=30; i--){
    int r_pos=i, l_pos=map(r_pos,0,180,180,0);
    l_hand.write(l_pos); r_hand.write(r_pos); delay(5);
  }
}

void weight_lift() { /* 举重动作 */ }
void look_left() { head.write(180); }
void confused() { /* 困惑动作 */ }
void double_punch() { /* 双拳出击 */ }
void r_upper_cut() { /* 右上勾拳 */ }
void smash() { /* 重锤砸地 */ }

void loop() {
  standby();
  while(Serial.available()>0){
    val = Serial.read();
    if(val=='h') hi();
    else if(val=='p') double_punch();
    else if(val=='u'){ hands_up(); delay(3000); }
    else if(val=='l'){ standby(); look_left(); delay(2000); }
    else if(val=='U'){ r_upper_cut(); delay(2000); }
    else if(val=='s'){ smash(); delay(2000); }
  }
}

整合与运行

Arduino 通过 USB 连接电脑,修改 Python 代码中的串口号(COM15),运行 Python 脚本即可唤醒机器人。完整代码见 GitHub 仓库。

原理图

自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作

制造工艺

  1. 树莓派与Python构建机器人:完整指南
  2. Arduino与树莓派构建互联网控制视频流机器人完整指南
  3. 基于 Arduino 与 Python 的可靠 RFID 考勤系统
  4. 舵机驱动避障机器人:基于 Arduino 与 HC-SR04 构建
  5. Arduino + Android 蓝牙控制 Roomba Create 2 扫地机器人完整指南
  6. Arduino + MPU6050 姿态控制舵机:SG90 与 MG996R 精准跟随实战
  7. u-blox LEA-6H GPS 模块:Arduino 与 Python 集成指南
  8. 在 Arduino Due 上实现语音识别与语音合成 - BitVoicer Server 方案
  9. 自主家庭助理机器人 O'nine - ROS + Alexa 语音控制 + 机械臂 + 麦克纳姆轮全向移动
  10. 通过专用 Android App 蓝牙控制 Arduino 机械臂
  11. Arduino 打造会说话的智能人形机器人「Mofiza」
  12. 理解 Python type() 与 isinstance():实战示例详解