×

带有Arduino和Python的AI助手机器人

消耗积分:0 | 格式:zip | 大小:0.05 MB | 2022-10-18

刘润生

分享资料个

描述

介绍:

我们都熟悉《钢铁侠》电影和漫威系列中的“贾维斯”AI助手机器人。自己动手做东西一直是程序员的梦想。今天我将展示一种使用 Python 编程制作这样一个助手的简单方法。而且,我还会给那个机器人做一个物理化身,这样每当我们和机器人说话的时候,它就可以做一些动作。这将比仅仅一个软件机器人更令人惊奇。因为如果它有身体,那就很酷了。所以今天我们将学习使用Arduino和Python编程来制作一个可以控制您的计算机并与您闲聊的AIrobot。让我们跳进去!

为什么我将机器人命名为“黄疸”?因为我把它涂成黄色,非常非常黄!

部分:

电子产品 -

Arduino Nano – 1x

微型伺服 Sg90 – 3x

超声波传感器 HCsr04– 1x

身体 -

PVC片材(最好是白色的,上色更好,我用的是蓝色的)

伺服轮(用于支架)

工具:

切刀

剪刀

热胶

喷漆

软件-

Python3

Arduino.ide

原则:

在进入实际建筑之前,很高兴知道我们将要做什么。

那么,主要代码或AI部分代码会在电脑上运行,为什么呢?因为它支持 python 并且比小 Arduino 具有更多的处理能力,而且 AI bot 将控制/自动化我的电脑的一些任务,它必须在我的电脑上运行。因此,Arduinoboard 使用 USB 电缆连接到我的计算机。

这个想法是运行一个 python 程序,它将执行 Speech totext 部分,处理文本并且还将执行 Text to Speech。意味着机器人会倾听、理解和回话。对于身体运动,我在 Arduino 板上保存了一些运动(封装在函数中)。每个动作的功能由python代码执行。

例如——如果机器人必须说“Hi/hello”,python 代码将发送一个字节“h”,Arduino 然后执行 hi() 函数。就如此容易。

现在您有了一个想法,让我们继续下一个过程。

电路和电子

schematic_try2_2020-11-15_05-56-45_3WXd4ccplp.png?auto=compress%2Cformat&w=740&h=555&fit=max

1 / 2

电路是让大多数制造商烦恼的事情。如果出现问题,您可能会烧掉一些昂贵的零件。为此,我设计了一个可用于制作大量项目的 PCB 板。它有微型 SD 卡插槽、蓝牙模块插槽、5v 外部电源,整个设备由 Arduino Nano 供电。

我使用 EasyEDA 设计了一个 PCB ,并使用PCBWay在线服务进行了打印。他们的服务很棒。我订购了PCB 即时报价,他们的系统自动为我完成了所有工作。三天之内,我从中国一路拿到了板子到孟加拉国。质量非常好,阻焊层完美,线条/痕迹,光洁度尽可能好。

无论如何,我使用了.300 mmtraces,因为它可以承受 1Amp 电流。

您可以从这里下载 PCB 文件。

焊接东西和测试电路:

pcb_Rp4t3zU6IH.jpg?auto=compress%2Cformat&w=740&h=555&fit=max

在这一步中,我焊接了所有东西。小心不要吸入烟雾,它可能会致癌。

我没有使用蓝牙模块或 TF 模块,所以我没有焊接那些。好消息是,我使用了 3 个伺服电机和一个声纳传感器,所有东西都可以通过我们连接的 USB 电缆轻松供电,以对 Arduino 进行编程。这很棒,因为我们不必考虑其他电源。

但是,如果您仍然想使用外部电池,那么请使用 lipo 2s (7.4V) 电池,不仅如此,伺服器也会烧毁。

制作身体:

back_rubhV9FPgK.PNG?auto=compress%2Cformat&w=740&h=555&fit=max

1 / 13

我用PVC板做身体,你也可以用纸板。首先,我为主要部分做了一个盒子,里面有主板和伺服电机。这就像制作盒子一样。我以同样的方式制作了头部,为传感器制作了两个孔(作为眼睛)。我已经添加了你需要的所有图片。我把一只手做成扳手,另一只手做成插头。我实际上使用了一个插头并使用热胶将其添加到一个手臂上。

方面?实际上没有,因为它是你的机器人,你可以使用一些简单的工具来制作它,让它随心所欲,让它成为任何形状和大小。

在关闭身体之前要非常小心地添加所有电子设备。在我放置传感器和所有电子设备后,我画了车身。不要那样做,我已经把我的 Arduino 染成了黄色。

所以,在车身上涂漆,然后放上所有的电子设备。

编码1(Python):

python_add_to_path_rRkSskGfeN.png?auto=compress%2Cformat&w=740&h=555&fit=max

从此站点下载Python,确保在安装时将 python 添加到您的路径中。

安装 Python 后,您需要从命令提示符/终端运行一些命令来安装用于语音识别、音频支持、文本到语音、浏览器自动化、串行通信目的的库。运行这些命令 -

 

pip installspeechrecognition
pip install pyaudio
pip install pyttsx3
pip install pywhatkit
pip install pyserial

 

然后从下面下载 pythoncode或从下面复制。前往coding2步骤。

 

 """ JAUNDICE: AI Assistant robot with Arduino and Python
author: ashraf minhaj
mail: ashraf_minhaj@yahoo.com
Last Edit: Nov 2020
License: Copyright (C) Ashraf Minhaj.
General Public License (GPL3+)
"""
import speech_recognition as sr   # voice recognition library
import random                     # to choose random words from list
import pyttsx3                    # offline Text to Speech
import datetime                   # to get date and time
import webbrowser                 # to open and perform web tasks
import serial                     # for serial communication
import pywhatkit                  # for more web automation
# Declare robot name (Wake-Up word)
robot_name = 'jaundice'
# random words list
hi_words = ['hi', 'hello', 'yo baby', 'salam']
bye_words = ['bye', 'tata', 'hasta la vista']
r_u_there = ['are you there', 'you there']
# initilize things
engine = pyttsx3.init()                    # init text to speech engine
#voices = engine.getProperty('voices')      #check for voices
#engine.setProperty('voice', voices[1].id)  # female voice
listener = sr.Recognizer()                 # initialize speech recognition API
# connect with NiNi motor driver board over serial communication
try:
   port = serial.Serial("COM15", 9600)
   print("Phycial body, connected.")
except:
   print("Unable to connect to my physical body")
def listen():
	""" listen to what user says"""
	try:
		with sr.Microphone() as source:                         # get input from mic
			print("Talk>>")
			voice = listener.listen(source)                     # listen from microphone
			command = listener.recognize_google(voice).lower()  # use google API
			# all words lowercase- so that we can process easily
			#command = command.lower()         
			print(command)
			# look for wake up word in the beginning
			if (command.split(' ')[0] == robot_name):
				# if wake up word found....
				print("[wake-up word found]")
				process(command)                 # call process funtion to take action
	except:
		pass
def process(words):
	""" process what user says and take actions """
	print(words) # check if it received any command
	# break words in
	word_list = words.split(' ')[1:]   # split by space and ignore the wake-up word
	if (len(word_list)==1):
		if (word_list[0] == robot_name):
		    talk("How Can I help you?")
		    #.write(b'l')
		    return
	if word_list[0] == 'play':
		"""if command for playing things, play from youtube"""
		talk("Okay boss, playing")
		extension = ' '.join(word_list[1:])                    # search without the command word
		port.write(b'u')
		pywhatkit.playonyt(extension)   
		port.write(b'l')          
		return
	elif word_list[0] == 'search':
		"""if command for google search"""
		port.write(b'u')
		talk("Okay boss, searching")
		port.write(b'l')
		extension = ' '.join(word_list[1:])
		pywhatkit.search(extension)
		return
	if (word_list[0] == 'get') and (word_list[1] == 'info'):
		"""if command for getting info"""
		port.write(b'u')
		talk("Okay, I am right on it")
		port.write(b'u')
		extension = ' '.join(word_list[2:])                    # search without the command words
		inf = pywhatkit.info(extension)
		talk(inf)                                              # read from result             
		return
	elif word_list[0] == 'open':
		"""if command for opening URLs"""
		port.write(b'l')
		talk("Opening, sir")
		url = f"http://{''.join(word_list[1:])}"   # make the URL
		webbrowser.open(url)
		return
	elif 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')
   # now check for matches
	for word in word_list:
		if word in hi_words:
			""" if user says hi/hello greet him accordingly"""
			port.write(b'h')               # send command to wave hand
			talk(random.choice(hi_words))
		elif word in bye_words:
			""" if user says bye etc"""
			talk(random.choice(bye_words))
def talk(sentence):
	""" talk / respond to the user """
	engine.say(sentence)
	engine.runAndWait()
# run the app
while True:
   listen()  # runs listen one time

 

编码2(Arduino):

这部分很容易,无需安装。使用Arduino.ide对开发板进行编程。如果您以前从未使用过 Arduino,请从此处下载。

正如我之前提到的,Arduino 程序等待串行数据,如果它接收到任何数据,它会检查字节数据。如果数据与预定义的命令匹配,则它执行一条语句。如果发送'u',它会使两只手都向上,就像那样。

从下面的副本下载代码。

 

 /** JAUNDICE: AI Assistant robot with Arduino and Python **
*  
*  author: ashraf minhaj
*  mail: ashraf_minhaj@yahoo.com
*  Last Edit: Nov 2020
*  
*  License: Copyright (C) Ashraf Minhaj.
*  General Public License (GPL3+)
*/
#include
Servo head;
Servo l_hand;
Servo r_hand;
// define sonar sensor's pins
int trig = 4;
int echo = 5;
// received data
byte val = "";
void setup() {
 // put your setup code here, to run once:
 head.attach(2);
 l_hand.attach(3);
 r_hand.attach(4);
 Serial.begin(9600); // for communicating via serial port with Python
}
void standby(){
 // all motors to these positions
 head.write(90);
 int r_pos = 30;
 int l_pos = map(r_pos, 0, 180, 180, 0);
 l_hand.write(l_pos);
 r_hand.write(r_pos);
}
void hi(){
 // all motors to these positions
 head.write(90);
 int i = 0;
 for(i=30; i<= 170; i++){
   r_hand.write(i);
   delay(5);
 }
 for(i=170; i>= 100; i--){
   r_hand.write(i);
   delay(5);
 }
 for(i=100; i<= 170; i++){
   r_hand.write(i);
   delay(5);
 }
 for(i=170; i>= 30; i--){
   r_hand.write(i);
   delay(5);
 }
 standby();
}
void hands_up(){
 // do this on every command (nothing much just move hands a bit)
 //head.write(150);
 //delay(300);
 //head.write(90);
 int i = 0;
 for(i=30; i<= 170; i++){
   int r_pos = i;
   int l_pos = map(r_pos, 0, 180, 180, 0);
   l_hand.write(l_pos);
   r_hand.write(r_pos);
   delay(5);
 }
 delay(600);
 for(i=170; i>= 30; i--){
   int r_pos = i;
   int l_pos = map(r_pos, 0, 180, 180, 0);
   l_hand.write(l_pos);
   r_hand.write(r_pos);
   delay(5);
 }
}
void weight_lift(){
 // lift weight using both hands
 int i = 0;
 for(i=30; i<= 170; i++){
   int r_pos = i;
   int l_pos = map(r_pos, 0, 180, 180, 0);
   l_hand.write(l_pos);
   r_hand.write(r_pos);
   delay(5);
 }
 for(int count=0; count<=4; count++){
   for(i=170; i>= 60; i--){
     int r_pos = i;
     int l_pos = map(r_pos, 0, 180, 180, 0);
     l_hand.write(l_pos);
     r_hand.write(r_pos);
     delay(5);
     }
   for(i=60; i<= 170; i++){
     int r_pos = i;
     int l_pos = map(r_pos, 0, 180, 180, 0);
     l_hand.write(l_pos);
     r_hand.write(r_pos);
     delay(5);
     }
   }
 for(i=170; i>= 30; i--){
   int r_pos = i;
   int l_pos = map(r_pos, 0, 180, 180, 0);
   l_hand.write(l_pos);
   r_hand.write(r_pos);
   delay(5);
 }
}
void excited(){
 return;
}
void look_left(){
 // rotate hed to left
 head.write(180);
}
void confused(){
 for(int count=0; count<=1; count++){
   head.write(30);
   r_hand.write(170);
   delay(700);
   r_hand.write(30);
   head.write(120);
   l_hand.write(30);
   delay(700);
   l_hand.write(160);
   }
 standby();
}
void double_punch(){
 // do a punch
 int i = 0;
 for(i=30; i>= 0; i--){
     int r_pos = i;
     int l_pos = map(r_pos, 0, 180, 180, 0);
     l_hand.write(l_pos);
     r_hand.write(r_pos);
     delay(5);
     }
 delay(2000);
 int r_pos = 80;
 int l_pos = map(r_pos, 0, 180, 180, 0);
 l_hand.write(l_pos);
 r_hand.write(r_pos);
 delay(500);
 standby();
}
void r_upper_cut(){
 // make right upper-cut
 int i = 0;
 for(i=30; i<= 170; i++){
   int r_pos = i;
   int l_pos = map(r_pos, 0, 180, 180, 0);
   l_hand.write(l_pos);
   r_hand.write(r_pos);
   delay(5);
 }
 for(int count=0; count<=4; count++){
   int i = 0;
   for(i=170; i>= 60; i--){
     r_hand.write(i);
     delay(1);
     }
   for(i=60; i<= 170; i++){
     r_hand.write(i);
     delay(1);
     }
   }
  standby();
  delay(100);
}
void smash(){
 // smash things
 int i = 0;
 for(i=30; i<= 170; i++){
   int r_pos = i;
   int l_pos = map(r_pos, 0, 180, 180, 0);
   l_hand.write(l_pos);
   r_hand.write(r_pos);
   delay(5);
 }
 delay(2000);
 for(i=170; i>= 0; i--){
   int r_pos = i;
   int l_pos = map(r_pos, 0, 180, 180, 0);
   l_hand.write(l_pos);
   r_hand.write(r_pos);
   delay(1);
 }
 delay(300);
 int r_pos = 180;
 int l_pos = map(r_pos, 0, 180, 180, 0);
 l_hand.write(l_pos);
 r_hand.write(r_pos);
 delay(1000);
 standby();
}
void eye_detect(){
 // do something if eye sensor detect motion
 return;
}
void loop() {
 // put your main code here, to run repeatedly:
 standby();
 while(Serial.available() > 0)  //look for serial data available or not
 {
   val = Serial.read();        //read the serial value
   if(val == 'h'){
     // do hi
      hi();
   }
   if(val == 'p'){
     // do hi
      double_punch();
   }
   if(val == 'u'){
     hands_up();
     delay(3000);
   }
   if(val == 'l'){
     standby();
     look_left();
     delay(2000);
   }
   if(val == 'U'){
     // uppercut
     r_upper_cut();
     delay(2000);
   }
   if(val == 's'){
     smash();
     delay(2000);
   }
 }
}

 

上传代码。

将所有内容放在一起并完成:

完成所有这些步骤后,我使用 USB 电缆将我的 Arduino 连接到 pc,然后运行 ​​python 程序。当您使用它时,请确保在 python 代码中添加正确的 Arduino 端口。如果你按照我提到的那样做所有事情,机器人应该像魅力一样工作。

谢谢你!

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !