基于ROS使用Arduino读取矩阵键盘的输入
1. 硬件
Arduino控制板:1个;
矩阵键盘:1个;
杜邦线:若干;
1.1 接线方式
接线方式为:
来张实际的照片(略麻烦):
2 程序
#include <Keypad.h>
#include <ros.h>
#include <std_msgs/Char.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
ros::NodeHandle nh;
std_msgs::Char data;
ros::Publisher chatter("chatter1",&data);
void setup(){
nh.initNode();
nh.advertise(chatter);
}
void loop(){
char customKey = customKeypad.getKey();
data.data = customKey;
if (customKey){
chatter.publish(&data);
}
nh.spinOnce();
}
2.1 程序下载和运行
首先:roscore
其次:rosrun rosserial_python serial_node.py /dev/ttyACM0
/dev/ttyACM0 这个是自己的端口号
最后:rostopic echo /chatter
备注:接收到的char数据为ASCII码,如果使用后期还需要进一步的转换。
来张图片: