0
点赞
收藏
分享

微信扫一扫

ros读取arduino的按键状态

婉殇成长笔记 2022-03-11 阅读 46
#include <ros.h>
#include <std_msgs/Bool.h>
ros::NodeHandle nh;
std_msgs::Bool pushed_msg;
ros::Publisher pub_button("pushed", &pushed_msg);
//初始化
const int button_pin = 7;
const int wheel_pin = 13;
//7为按键端口,13接轮子高电平
bool last_reading;
long last_debounce_time=0;
long debounce_delay=50;
bool published = true;
//防止电平波动,等待按钮按下50毫秒以后按钮确定为按下状态
void setup()
{
  nh.initNode();
  nh.advertise(pub_button);
  
  //initialize an LED output pin 
  //and a input pin for our push button
  pinMode(wheel_pin, OUTPUT);
  pinMode(button_pin, INPUT);
  
  //Enable the pullup resistor on the button
  digitalWrite(button_pin, HIGH);
  
  //The button is a normally button
  last_reading = ! digitalRead(button_pin);
 
}

void loop()
{
  
  bool reading = ! digitalRead(button_pin);
  
  if (last_reading!= reading){
      last_debounce_time = millis();
      published = false;
  }
  
  //if the button value has not changed for the debounce delay, we know its stable
  if ( !published && (millis() - last_debounce_time)  > debounce_delay) {
    digitalWrite(wheel_pin, reading);
    pushed_msg.data = reading;
    pub_button.publish(&pushed_msg);
    published = true;
  }

  last_reading = reading;
  
  nh.spinOnce();
}

roscore

rostopic pushed 查看

举报

相关推荐

ros+arduino=robot

0 条评论