基於ROS使用Arduino讀取矩陣鍵盤的輸入
阿新 • • 發佈:2018-11-01
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碼,如果使用後期還需要進一步的轉換。
來張圖片: