编写ros节点

基于rosserial_python,使arduino与ros进行通信,一些自己写的小例子

1602_i2c液晶显示模块(sub)

主要思路,1602作为接受端,接收来自ros的string数据,然后在自己显示出来

代码如下

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include<ros.h>
#include <std_msgs/String.h>

LiquidCrystal_I2C lcd(0x27,16,2);
ros::NodeHandle nh;


void lcd_cb( const std_msgs::String& lcd_msg)
{
  lcd.print(lcd_msg.data);
  digitalWrite(13, HIGH-digitalRead(13));  //toggle led 
  }
ros::Subscriber<std_msgs::String> sub_lcd("show_lcd",lcd_cb);
void setup() {
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  Serial.begin(57600);
  nh.initNode();
  nh.subscribe(sub_lcd);
  pinMode(13, OUTPUT);

}

void loop() {
  nh.spinOnce();
  delay(1);

}

触摸模块,模拟量(pub)

主要思路,触摸模块作为发送端向ros,提供数据(pub)

代码如下

#include<ros.h>
#include<std_msgs/Int32.h>
#define buttonPin A0
#define  fmq  7
ros::NodeHandle nh;
std_msgs::Int32 int_msg;
ros::Publisher pub_button("input_button",&int_msg);
int val;
 
void setup() {
  nh.initNode();
  nh.advertise(pub_button);
  pinMode(buttonPin,INPUT);   //设置引脚 A0 为输出模式
  pinMode(fmq,OUTPUT);
  Serial.begin(57600); //设置波特率为57600
}
 
void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(buttonPin);
  Serial.println(val); //串口输出
  int_msg.data=val;
  pub_button.publish(&int_msg);
  nh.spinOnce();
  
}

执行

roscore

rosrun rosserial_python serial_node.py /dev/ttyACM0

rostopic pub show_lcd std_msgs/String “hello”

rostopic echo input_button //读取pub数值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值