一、第一种安装方式(不支持自定义消息)

第一步打开官网

http://wiki.ros.org/rosserial_arduino/Tutorials/Arduino%20IDE%20Setup

第二步按第一种方式安装

第三步生成arduino ros库

第四步编译arduino等等等

二、第一种安装方式的问题

一切正常了吧,但如果使用自定义数据类型,会发现source /opt/ros/indigo/setup.bash会找不到在catkin_ws中的自定义类型。

但如果使用source catkin_ws/devel/setup.bash,你会发现无法找到ros_rosserial这个包。

所以需要再进行一次第二种安装(也许第一种还有其他改进方法)

三、接着进行第二种安装方式

之后可以开始自定义消息类型了

四、自定义消息类型

参见官网

http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv

在自己包里创建自定义数据类型后,catkin_make

重复一中的第三步,生成arduino ros库,之后就会在arduino的roslibaray中找到自定义消息类型的.h文件了。

如图mycat包中cat自定义消息类型

四、如果只用第二种方法

用第二种方法安装ros_rosserial,在生成arduino ros库后,arduino无法编译ros内容

五、备注

也许是我出错了

我第一个电脑中默认source catkin_ws/devel/setup.bash

但后来只用第二种方法时忘了source究竟是不是指向了catkin_ws/devel/setup.bash

第一种方法是否还可以挽救下??使用rospack profile。

六、arduino代码

事实上在rosrun rosserial_python serial_node.py /dev/ttyUSB0后

在arduino端生成一个节点,含有发布器或订阅器。

代码里的数据类型就是ros里的消息类型,必须先导入消息的头文件。

七、发布器(publisher)(多线程)

*
* rosserial Publisher Example
* Prints "hello world!"
*/
#include <NewPing.h>
#include <ros.h>
#include <std_msgs/Float32.h> ros::NodeHandle nh;
const int TrigPin2 = 4;
const int EchoPin2 = 5;
const int TrigPin1 = 2;
const int EchoPin1 = 3;
const int TrigPin3= 8;
const int EchoPin3 = 9;
std_msgs::Float32 str_msg1;
std_msgs::Float32 str_msg2;
std_msgs::Float32 str_msg3;
ros::Publisher chatter1("chatter1", &str_msg1);
ros::Publisher chatter2("chatter2", &str_msg2);
ros::Publisher chatter3("chatter3", &str_msg3);
float distance1;
float distance2;
float distance3;
NewPing sonar1(TrigPin1, EchoPin1, 200);
NewPing sonar2(TrigPin2, EchoPin2, 200);
NewPing sonar3(TrigPin3, EchoPin3, 200);
void setup()
{
// 初始化串口通信及连接SR04的引脚
Serial.begin(9600); Serial.println("Ultrasonic sensor:"); nh.initNode();
nh.advertise(chatter1);
nh.advertise(chatter2);
nh.advertise(chatter3);
} void loop()
{
distance1 = sonar1.ping_cm();
str_msg1.data =distance1;
chatter1.publish( &str_msg1 ); // 检测脉冲宽度,并计算出距离
distance2 = sonar2.ping_cm();
str_msg2.data =distance2;
chatter2.publish( &str_msg2 ); distance3 = sonar3.ping_cm();
str_msg3.data =distance3;
chatter3.publish( &str_msg3); nh.spinOnce();
delay(15);
}

八、订阅器

实例一无参实例

/*
* rosserial Subscriber Example
* Blinks an LED on callback
*/ #include <Servo.h>
#include <ros.h>
#include <std_msgs/Empty.h>
Servo myservo;
ros::NodeHandle nh;
int pos; void messageCb( const std_msgs::Empty& toggle_msg){
digitalWrite(13, HIGH-digitalRead(13)); // blink the led
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
} ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );
///toggle_led is pubisher in ros void setup()
{ myservo.attach(9);
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
} void loop()
{
nh.spinOnce();
delay(1);
} 在nh节点激活后
void messageCb()函数会被调用执行。

实例二有参实例

*
* rosserial Servo Control Example
*
* This sketch demonstrates the control of hobby R/C servos
* using ROS and the arduiono
*
* For the full tutorial write up, visit
* www.ros.org/wiki/rosserial_arduino_demos
*
* For more information on the Arduino Servo Library
* Checkout :
* http://www.arduino.cc/en/Reference/Servo
*/ #if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif #include <Servo.h>
#include <ros.h>
#include <std_msgs/UInt16.h> ros::NodeHandle nh; Servo servo; void servo_cb( const std_msgs::UInt16& cmd_msg){
servo.write(cmd_msg.data); //set servo angle, should be from 0-180
digitalWrite(13, HIGH-digitalRead(13)); //toggle led
} ros::Subscriber<std_msgs::UInt16> sub("servo", servo_cb); void setup(){
pinMode(13, OUTPUT); nh.initNode();
nh.subscribe(sub);
servo.attach(9); //attach it to pin 9
} void loop(){
nh.spinOnce();
delay(1);
}

九、自定义消息类型

(原ros_rosserial自带adc.h自定义消息类型)(自定义消息还可以,但自定义数组一直搞不定)

发布器

#include <ros.h>
#include <mycat/cat.h> ros::NodeHandle nh; mycat::cat cat_msg;
ros::Publisher p("cat", &cat_msg); void setup()
{ nh.initNode(); nh.advertise(p);
} //We average the analog reading to elminate some of the noise void loop()
{
cat_msg.num = 1;
cat_msg.num1 = 2;
cat_msg.num2 = 3;
cat_msg.num3 = 4;
p.publish(&cat_msg); nh.spinOnce();
} 订阅器 #include <Servo.h>
#include <ros.h>
#include <mycat/cat.h> ros::NodeHandle nh; Servo servo; void servo_cb( const mycat::cat& cmd_msg){
servo.write(cmd_msg.num); //set servo angle, should be from 0-180
delay(1000);
servo.write(cmd_msg.num1); //set servo angle, should be from 0-180
delay(1000);
servo.write(cmd_msg.num2); //set servo angle, should be from 0-180
delay(1000);
servo.write(cmd_msg.num3); //set servo angle, should be from 0-180
delay(1000); } ros::Subscriber<mycat::cat> sub("cat", servo_cb); void setup(){ nh.initNode();
nh.subscribe(sub);
servo.attach(9); //attach it to pin 9
} void loop(){
nh.spinOnce();
delay(100);
}

ROS之arduino交互的更多相关文章

  1. ROS机器人语音交互(一)

    语音交互早期已经广泛应用在手机端,电脑端,随着技术的成熟,接口逐渐开放,ROS上老外搞的开源语音识别只支持英文,识别率还低. 国内语音识别技术已经相当成熟稳定.感谢ros小课堂的讲解,解决了自己的疑惑 ...

  2. Ros使用Arduino 3用rosserial创建一个subscriber

    在前面的一节中,我们已经使用arduino创建了一个publisher节点,接下来将会用arduino来创建一个subscriber,接收电脑传去的信息并做出相应的反应. 1启动Arduino 将ar ...

  3. Ros使用Arduino 2 使用rosserial创建一个publisher

    1 启动arduino 将arduino开发板连接到电脑的usb口,在arduino IDE中进行设置. 选择Tools->Board,选择你所使用的arduino开发板的类型,所使用的ardu ...

  4. Ros使用Arduino 1安装Arduino IDE

    安装Arsuino IDE sudo apt-get install arduino 设置库文件路径 在使用ROS的库文件时,必须在代码的开头包括: #include <ros.h> 接下 ...

  5. ROS Learning-016 Arduino-For-ROS-001 搭建 Arduino 和 ROS 之间相连接的开发环境

    Arduino For ROS-001 - 搭建 ROS 和 Arduino 相连接的开发环境 我的Ubuntu系统:Ubuntu 14.04.10 TLS 32位 Arduino的版本:Arduin ...

  6. 【Arduino】、Arduino+ESP8266上传至oneNet云

    一.硬件简介 1. Arudino 是一种开源的电子平台,该平台最初主要基于AVR单片机的微控制器和相应的开发软件,包含硬件(各种型号的Arduino板)和软件(Arduino IDE). 2. ES ...

  7. ROS Learning-018 Arduino-For-ROS-003 (总结篇) 模板程序 即 如何运行

    Arduino For ROS-003 - (总结篇) 模板程序 即 如何运行 我的Ubuntu系统:Ubuntu 14.04.10 TLS 32位 Arduino的版本:Arduino 1.6.11 ...

  8. ROS Learning-017 Arduino-For-ROS-002 第一个程序: Hello World

    Arduino For ROS-002 - 第一个程序: Hello World 我的Ubuntu系统:Ubuntu 14.04.10 TLS 32位 Arduino的版本:Arduino 1.6.1 ...

  9. ROS和Gazebo进行机器人仿真(一)

    Gazebo是一种多机器人仿真器,可用于室内外机器人仿真.Gazebo在ROS中有良好的接口,包含ROS和Gazebo的所有控制. 若要实现ROS到Gazebo的通信,我们必须安装ROS-Gazebo ...

随机推荐

  1. IDEA设置默认(指定)的注释作者信息

    有时候我们想在IDEA里面创建的时候就默认设置一个指定的作者信息 填入作者信息 然后点击ok /** * * @author yvioo */ 然后我们新建文件的时候就会自动带上这个了,模板可以根据自 ...

  2. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  3. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  4. 版本不兼容Jar包冲突该如何是好?

    一.引言 "老婆"和"妈妈"同时掉进水里,先救谁? 常言道:编码五分钟,解冲突两小时.作为Java开发来说,第一眼见到ClassNotFoundExceptio ...

  5. Log4j 2.17.0 再曝漏洞,但不要惊慌!

    最新消息!根据Log4j官网发布,2.17.0版本还存在漏洞! 上图来自Log4j2官网:https://logging.apache.org/log4j/2.x/ 漏洞编号:CVE-2021-448 ...

  6. Java编程基础

    JDK与JRE有什么区别 JDK:Java开发工具包(Java Development Kit),提供了Java的开发环境和运行环境. JRE:Java运行环境(Java Runtime Enviro ...

  7. isEmpty 和 isBlank

    <org.apache.commons.lang3.StringUtils> isEmpty系列 StringUtils.isEmpty() ========> StringUtil ...

  8. 第七个知识点:随机性如何辅助计算和什么是BPP类问题

    第七个知识点:随机性如何辅助计算和什么是BPP类问题 原文地址:http://bristolcrypto.blogspot.com/2014/11/52-things-number-7-how-doe ...

  9. 13 个 C# 10 特性

    原文链接:https://blog.okyrylchuk.dev 原文作者:Oleg Kyrylchuk 译: 等天黑 常量的内插字符串 C# 10 允许使用在常量字符串初始化中使用插值, 如下 co ...

  10. Solon 1.6.11 发布。类似 Spring 的生态体系

    关于官网 千呼万唤始出来: https://solon.noear.org .整了一个月多了,总体样子有了...还得不断接着整! 关于 Solon Solon 是一个轻量级应用开发框架.支持 Web. ...