[intel程序猿笔记]使用英特尔® EDISON 模块控制机器人 精华

11828浏览
查看: 11828|回复: 3

[intel程序猿笔记] 使用英特尔® EDISON 模块控制机器人

[复制链接]
介绍

未来的愿景正在逐步成为现实。 或许,机器人将会有一天接管整个世界,但是现在,我们仍然需要通过互联网对其进行远程控制。 在本文中,我们将创建一个 HTML 页面,以便帮助用户使用 MQTT 发送命令,进而控制机器人移动,并通过机器人身上的网络摄像头查看周围环境。  这种远程控制功能为我们在未来添加更多的特性奠定了基础。
使用英特尔® EDISON 模块控制机器人图2
图 1: 完全组装的 DFRobot Devastator Robot

在本文中,我们使用 DFRobot* Devastator Robot。 您可以在这里找到:Devastator Robot Kit (Built-in WiFi Vision and Sensors) -DFRobot
。 这是一款坦克样式的机器人,配备了两台电机、一个摄像头和若干个传感器。 本文以及相关代码也可轻松用于其他相应的微控制器机器人,甚至经过调整后可用于其他项目。

如欲了解关于 DFRobot Devastator Robot 的更多信息,请参考我的同事撰写的一篇文章: https://software.intel.com/zh-cn/articles/overview-of-intel-edison-based-robotics-platform
注: Devastator 的电池组为 6 块 AA 电池,电压仅为 9V,如果该项目加载所有电机和摄像头,便不足以支持。 如果要达到本文的要求,必须串联一个 2 AA 电池组,以便使总电压达到 12V。

设置摄像头
Devastator Robot 套件附带 USB 摄像头和云台套件,需要组装并连接到控制台。 如果您使用英特尔® Edison 模块和 USB 摄像头设计自己的 DIY 机器人,请注意摄像头必须兼容 UVC 和模块的 USB 驱动程序。 如欲查看 UVC 兼容设备列表,请查看下面的页面: http://www.ideasonboard.org/uvc/#devices
在 Devastator Robot 上,使用 OTG USB 适配器将 USB 网络摄像头插入 micro OTG USB 端口,并将另一根线缆插入电脑上的其他 micro USB 端口。
要确保 USB 网络摄像头正常工作,请在串行连接中键入: (使用 PuTTY)
ls -l /dev/video0

屏幕上应显示与下面类似的一行:
  1. crw-rw---- 1 root video 81, 0 May  6 22:36 /dev/video0
复制代码
否则,如果显示下面的一行,则说明未发现摄像头。
  1. ls: cannot access /dev/video0: No such file or directory
复制代码
接下来,在英特尔® Edison 模块上安装 mjpeg-streamer 库。
向 base-feeds.conf 添加以下行即可:
  1. echo "src/gz all <a href="http://repo.opkg.net/edison/repo/all" target="_blank">http://repo.opkg.net/edison/repo/all</a>
  2. src/gz edison <a href="http://repo.opkg.net/edison/repo/edison" target="_blank">http://repo.opkg.net/edison/repo/edison</a>
  3. src/gz core2-32 <a href="http://repo.opkg.net/edison/repo/core2-32" target="_blank">http://repo.opkg.net/edison/repo/core2-32</a>" >> /etc/opkg/base-feeds.conf
复制代码
更新存储卡索引:
  1. opkg update
复制代码
安装:
  1. opkg install mjpg-streamer
复制代码
要启动数据流:
  1. mjpg_streamer -i "input_uvc.so -n -f 10 -r 400x400" -o "output_http.so -p 8080 -w ./www"
复制代码

请注意,帧率 (-f 10) 和尺寸 (400x400) 受到一定的限制,以便降低功耗和计算需求,从而确保在不停止数据流的情况下获得最新的图像。
要查看同一 Wi-Fi* 网络中的数据流,请用英特尔® Edison 模块中的 IP 地址替换下面 URL 中的 ‘localhost’: http://localhost:8080/?action=stream
用英特尔® Edison 模块中的 IP 地址替换下面 URL 中的 ‘localhost’,还可以查看 feed 的静止图像: http://localhost:8080/?action=snapshot
如果要从 Wi-Fi 网络外部查看摄像头 feed,您需要正确配置路由器。 请查看以下详细的说明: https://ipcamnetwork.wordpress.com/2010/09/23/acessing-your-camera-from-the-internet/
此外,启动的时候还需要启动摄像头,以便准备好使用。 首先在/home/root/ 目录中创建一个脚本。
  1. vi cameraScript.sh
复制代码

将以下内容添加至脚本。
  1. #!/bin/sh
  2. mjpg_streamer -i "input_uvc.so -n -f 10 -r 400x400" -o "output_http.so -p 8080 -w ./www"
复制代码

确保该脚本可以执行。
  1. chmod +x /home/root/ cameraScript.sh
  2. chmod +x cameraScript.sh
复制代码

使其在系统启动时启动:
  1. update-rc.d cameraScript.sh defaults
复制代码

要确保正常工作,请重启英特尔® Edison 模块。 (该步骤假设其已经过配置并连接至 Wi-Fi)。
  1. reboot
复制代码

HTML 网页
要远程控制机器人,可以创建一个简单的 HTML 网页。该网页可以轻松地捆绑至网络摄像头的数据流并兼容多种不同的平台。 在电脑上的相同位置会有一个index.html文件和一个script.js 文件。 双击index.html 文件将会启动该网页。

使用英特尔® EDISON 模块控制机器人图1
图 2: HTML 页面截图(网络摄像头试图和 控制按钮)

index.html 文件代码如下所示。 由于机器人使用 MQTT,而且我们需要某些定制方法,因此我们包括了mqttws31.js和我们自己的script.js。 此外,我们还需要一个包含 web 数据流的对象,以及一些能够控制机器人的按钮。

  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>NodeJS Starter Application</title>
  5.     <meta charset="utf-8">
  6.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7.     <meta name="viewport" content="width=device-width, initial-scale=1">
  8.     <link rel="stylesheet" href="stylesheets/style.css">
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
  10. <script src="script.js"></script>
  11.   </head>
  12. <body>
  13.   <div>
  14.     <object type="text/html" data="http://192.168.1.104:8080/?action=stream" width="640px" height="480px" style="overflow:auto;border:5px ridge blue">
  15.     </object>
  16. </div>
  17.   <div>
  18.   <input type="button" value="Connect" id="connect"></input>
  19.   <input type="button" value="Forward" id="forwardButton"></input>
  20.   <input type="button" value="Reverse" id="reverseButton"></input>
  21.   <input type="button" value="Left" id="leftButton"></input>
  22.   <input type="button" value="Right" id="rightButton"></input>
  23.   <input type="button" value="Stop" id="stopButton"></input>
  24.   <input type="button" value="Disconnect" id="disconnect"></input>
  25. <div id="messages"></div>
  26.   </div>
  27. </body>
  28. </html>
复制代码
代码示例 1:index.html 文件

script.js将会突出显示当前的命令按钮,并处理 MQTT 连接和订阅逻辑。
注意script.js中的客户端实例行:
  1. var client = new Paho.MQTT.Client("broker.hivemq.com", 8000, "clientId");
复制代码

我们使用broker.hivemq.com作为我们的 MQTT 服务器。 这项服务是免费的,并且可以通过其服务建立您自己的代理。 clientId对于每个客户端来说必须是唯一的,因此请对其进行更改,比如添加某些随机字符和数字。机器人用于订阅和发布的主题是dfrobotControl,但是您可以根据自己的目的进行修改。
  1. function moveForward()
  2. {
  3. clearButtons();
  4. document.getElementById("forwardButton").style.color = "red";
  5. };
  6. function moveReverse()
  7. {
  8. clearButtons();
  9. document.getElementById("reverseButton").style.color = "red";
  10. };
  11. function moveLeft()
  12. {
  13. clearButtons();
  14. document.getElementById("leftButton").style.color = "red";
  15. };
  16. function moveRight()
  17. {
  18. clearButtons();
  19. document.getElementById("rightButton").style.color = "red";
  20. };
  21. function moveStop()
  22. {
  23. clearButtons();
  24. document.getElementById("stopButton").style.color = "red";
  25. };
  26. function clearButtons()
  27. {
  28. document.getElementById("forwardButton").style.color = "black";
  29. document.getElementById("reverseButton").style.color = "black";
  30. document.getElementById("leftButton").style.color = "black";
  31. document.getElementById("rightButton").style.color = "black";
  32. document.getElementById("stopButton").style.color = "black";
  33. };
  34. // Create a client instance
  35. var client = new Paho.MQTT.Client("broker.hivemq.com", 8000, "clientId");
  36. // called when the client loses its connection
  37. client.onConnectionLost = function (responseObject) {
  38.      alert("connection lost: " + responseObject.errorMessage);
  39. };
  40. //Connect Options
  41. var options = {
  42.      timeout: 3,
  43.      //Gets Called if the connection has sucessfully been established
  44.      onSuccess: function () {
  45.          alert("Connected");
  46.    //and subscribe to the topic
  47.    client.subscribe('dfrobotControl/#', {qos: 2});
  48.    alert('Subscribed');
  49.      },
  50.      //Gets Called if the connection could not be established
  51.      onFailure: function (message) {
  52.          alert("Connection failed: " + message.errorMessage);
  53.      }
  54. };
  55. //Publish the message to the topic
  56. var publish = function (payload, topic, qos) {
  57.      var message = new Paho.MQTT.Message(payload);
  58.      message.destinationName = topic;
  59.      message.qos = qos;
  60.      client.send(message);
  61. }
复制代码
代码示例 2:用于 HTML 页面的 script.js 文件

Arduino Sketch
Devastator 使用面向英特尔® Edison 模块的 Romeo 开发板,默认情况下通过 Arduino* IDE 进行编程。 在 Arduino sketch 中,我们需要订阅 MQTT 主题,以便接收并执行相关的命令。 网络摄像头已经准备就绪,但我们需要设置英特尔® Edison 模块的静态 IP 地址,并将其连接至 Wi-Fi* 网络。 完成这样的设置后,网络摄像头的 IP 便一直与网页的 IP 保持相同。
前端 LED 用作状态指示灯。 成功完成 Wi-Fi 连接后,机器人的右侧 LED 指示灯将会亮起。 当 MQTT 成功连接和订阅后,机器人的左侧 LED 指示灯将会亮起。  

  1. #include <SPI.h>
  2. #include <WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <DFRobot.h>
  5. #include <IIC1.h>
  6. // WiFi Login
  7. IPAddress ip(192, 168, 1, 104);   
  8. char ssid[] = "wifiname";      //  your network SSID (name)
  9. char pass[] = "wifipassword";             // your network password
  10. const char* server = "broker.mqttdashboard.com";
  11. // WiFi connection
  12. WiFiClient wifiClient;
  13. int status = WL_IDLE_STATUS;          // the Wifi radio's status
  14. #define leftLEDPin 7
  15. #define rightLEDPin 10
  16. DFrobotEdison MotorLeft;
  17. DFrobotEdison MotorRight;
复制代码
  1. void callback(char* topic, byte* payload, unsigned int length) {
  2. Serial.print("Message arrived [");
  3. Serial.print(topic);
  4. Serial.print("] ");
  5. String message= "";
  6. for (int i=0;i<length;i++) {
  7. Serial.print((char)payload);
  8. message= message + (char)payload;
  9. }
  10. Serial.println();
  11. String forward= "forward";
  12. if (message.equals("forward") == 1) {
  13. Serial.println("Forward!!!");
  14. motorForward();
  15. }
  16. if (message.equals("reverse") == 1) {
  17. motorBackward();
  18. Serial.println("reverse!!!");
  19. }
  20. if (message.equals("left") == 1) {
  21. motorLeft();
  22. Serial.println("left!!!");
  23. }
  24. if (message.equals("right") == 1) {
  25. motorRight();
  26. Serial.println("right!!!");
  27. }
  28. if (message.equals("stop") == 1) {
  29. motorStop();
  30. }
  31. }
  32. // PubSub Client.
  33. PubSubClient client(server, 1883, callback , wifiClient);
复制代码

连接到 MQTT 服务器时,务必为英特尔® Edison 模块选择唯一的clientID,并在需要的时候更改主题。 要实现这一点,可以更新client.connect("clientID "))和client.subscribe("dfrobotControl")行。
  1. void reconnectMQTT() {
  2.   // Loop until we're reconnected
  3.   digitalWrite(leftLEDPin, LOW);
  4.   motorStop();
  5.   while (!client.connected()) {
  6.     Serial.print("Attempting MQTT connection...");
  7.     // Attempt to connect
  8.     if (client.connect("clientID ")) {
  9.       Serial.println("connected");
  10.       // ... and resubscribe
  11.       client.subscribe("dfrobotControl");
  12.       digitalWrite(leftLEDPin, HIGH);
  13.     } else {
  14.       Serial.print("failed, rc=");
  15.       Serial.print(client.state());
  16.       Serial.println(" try again in 5 seconds");
  17.       // Wait 5 seconds before retrying
  18.       delay(5000);
  19.     }
  20.   }
  21. }
  22. void reconnectWiFi() {
  23.   // Loop until we're reconnected
  24.   digitalWrite(rightLEDPin, LOW);
  25.   digitalWrite(leftLEDPin, LOW);
  26.   motorStop();
  27.   status = WiFi.begin(ssid, pass);
  28.   while(!(status == WL_CONNECTED)){
  29.      Serial.println("WiFi Failed!");
  30.      status = WiFi.begin(ssid, pass);
  31.      delay(5000);
  32.   }
  33.   Serial.println("WiFi Connected!");
  34.    digitalWrite(rightLEDPin, HIGH);
  35. }
  36. void setup()
  37. {
  38.   Serial.begin(9600);
  39.   
  40.   Serial.println("Init the sensor");        
  41.   //Initilize Pin Mode
  42.   pinMode(rightLEDPin, OUTPUT);               
  43.   pinMode(leftLEDPin,OUTPUT);      
  44.   digitalWrite(rightLEDPin, LOW);            
  45.   digitalWrite(leftLEDPin, LOW);            
  46.   //Initilize Motor Drivers
  47.   MotorLeft.begin(M2);
  48.   MotorRight.begin(M1);
  49.   WiFi.config(ip);
  50.   status = WiFi.begin(ssid, pass);
  51.   while(!(status == WL_CONNECTED)){
  52.      Serial.println("WiFi Failed!");
  53.      status = WiFi.begin(ssid, pass);
  54.      delay(5000);
  55.   }
  56.   Serial.println("WiFi Connected!");
  57.    digitalWrite(rightLEDPin, HIGH);
  58. }
复制代码
  1. void loop()
  2. {
  3. client.loop();
  4. if (!client.connected()) {
  5.    reconnectMQTT();
  6. }
  7. if(WiFi.status()!= WL_CONNECTED){
  8.   reconnectWiFi();
  9. }
  10. delay(1000);
  11. }
复制代码
最后是控制机器人移动的方法。
  1. void motorBackward()
  2. {
  3.   motorStop();
  4.   MotorLeft.setDirection(ANTICLOCKWISE);
  5.   MotorRight.setDirection(ANTICLOCKWISE);
  6.   for(int i=0;i<150;i+=10)
  7.   {
  8.     MotorLeft.setSpeed(i);
  9.     MotorRight.setSpeed(i);
  10.     delay(20);
  11.   }
  12. }
  13. void motorForward()
  14. {
  15.   motorStop();
  16.   MotorLeft.setDirection(CLOCKWISE);
  17.   MotorRight.setDirection(CLOCKWISE);
  18.   for(int i=0;i<150;i+=10)
  19.   {
  20.     MotorLeft.setSpeed(i);
  21.     MotorRight.setSpeed(i);
  22.     delay(20);
  23.   }
  24. }
  25. inline void motorLeft()
  26. {
  27.   motorStop();
  28.   MotorLeft.setDirection(ANTICLOCKWISE);
  29.   MotorRight.setDirection(CLOCKWISE);
  30.   for(int i=0;i<150;i+=10)
  31.   {
  32.     MotorLeft.setSpeed(i);
  33.     MotorRight.setSpeed(i);
  34.     delay(20);
  35.   }
  36. }
  37. inline void motorRight()
  38. {
  39.   motorStop();
  40.   MotorLeft.setDirection(CLOCKWISE);
  41.   MotorRight.setDirection(ANTICLOCKWISE);
  42.   for(int i=0;i<150;i+=10)
  43.   {
  44.     MotorLeft.setSpeed(i);
  45.     MotorRight.setSpeed(i);
  46.     delay(20);
  47.   }
  48. }
  49. inline void motorStop()
  50. {
  51.   MotorLeft.setSpeed(0);
  52.   MotorRight.setSpeed(0);
  53.   delay(50);
  54. }
复制代码
代码示例 3: 控制机器人的 Arduino sketch
总结
现在,我们可以借助使用 MQTT 的简单网页来远程控制机器人,并通过网络摄像头查看其移动方向。 在此基础之上,我们可以在机器人中构建更多的特性,例如面积测绘或者摄像头的平移和倾斜。

更多机器人项目
作者介绍
Whitney Foster 是英特尔公司软件解决方案事业部的一名工程师,负责大规模物联网项目的支持工作。

声明
本文档不代表英特尔公司或其它机构向任何人明确或隐含地授予任何知识产权。
英特尔明确拒绝所有明确或隐含的担保,包括但不限于对于适销性、特定用途适用性和不侵犯任何权利的隐含担保,以及任何对于履约习惯、交易习惯或贸易惯例的担保。
本文包含尚处于开发阶段的产品、服务和/或流程的信息。 此处提供的信息可随时改变而毋需通知。 联系您的英特尔代表,了解最新的预测、时间表、规格和路线图。
本文件所描述的产品和服务可能包含使其与宣称的规格不符的设计缺陷或失误。 这些缺陷或失误已收录于勘误表中,可索取获得。
索取本文件中提的、包含订单号的文件的复印件,可拨打1-800-548-4725,或登陆www.intel.com/design/literature.htm
英特尔、Intel 标识、Intel RealSense 和英特尔实感是英特尔在美国和/或其他国家的商标。

* 其他的名称和品牌可能是其他所有者的资产。
**该示例源代码根据英特尔示例源代码许可协议发布。
英特尔公司 © 2016 年版权所有。

凌风清羽  中级技匠

发表于 2017-3-10 15:58:05

666666666666666666666666
回复

使用道具 举报

cccc00000000  见习技师

发表于 2017-4-3 16:51:22

这整套设备下来要多少成本啊?
回复

使用道具 举报

yokohama  学徒

发表于 2017-7-3 17:14:25

最后是控制机器人移动的方法,
把下面的编程复制到Arduino里面进行验证显示“exit status 1
'MotorLeft' was not declared in this scope”是什么意思呀?
我在按着教程一步一步做硬件是组装好了,但编程搞不懂,纯新手,望老手指点,谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail