5727| 4
|
手势识别带触摸传感器试用1:手势识别功能 |
本帖最后由 szjuliet 于 2019-9-8 09:43 编辑 手势识别带触摸传感器试用1:手势识别功能 手势识别带触摸传感器试用2:触摸功能 器材: 虚谷号教育版 虚谷号扩展板(购买链接:https://www.dfrobot.com.cn/goods-1953.html) 手势带触摸传感器(购买链接:https://www.dfrobot.com.cn/goods-1994.html) 视频: 传感器wiki:https://www.dfrobot.com.cn/goods-1994.html 一、开箱 感谢发货的小姐姐,知道我们干活辛苦,将不能吃不能玩的泡沫块换成了小零食,简直太贴心了,周末从早到晚整整监考两天,只能今晚加班干活,零食派上大用场了。 二、在UNO上测试
手势识别传感器为串口设备,而普通Arduino只有一个硬件串口,所以使用软串口来配合传感器使用 接线图: (图片来源:DF)
绿色信号线接11号引脚,蓝色信号线RXD接10号引脚,黑色信号线接GND,红色信号线接VCC (图片来源:DF)
将需要的库文件复制到Arduino的库目录下,在arduino IDE中打开示例文件simpleGuester,或直接复制下面的代码: [mw_shl_code=cpp,true]/* * file simpleGesture.ino * * @ https://github.com/DFRobot/DFRobot_Gesture_Touch * * example for DFRobot Gesture&Touch sensor * * sensor event will print on your serial monitor * * for esp32, rx_pin = D5, tx_pin = D6 * * Copyright [DFRobot](http://www.dfrobot.com), 2016 * Copyright GNU Lesser General Public License * * version V1.0 * date 2018-10-19 */ #include "DFRobot_Gesture_Touch.h" #ifdef __AVR__ #include "SoftwareSerial.h" SoftwareSerial mySerial(10, 11); // example for uno, use software serial #elif defined ESP_PLATFORM #include "HardwareSerial.h" HardwareSerial mySerial(1); #endif DFRobot_Gesture_Touch DFGT(&mySerial); // init sensor object, request write and read function void setup() { Serial.begin(115200); mySerial.begin(9600); delay(2000); while(mySerial.available()) // flush serial buffer mySerial.read(); Serial.println("gesture&touch sensor test"); DFGT.setGestureDistance(20); // suggest default value // DFGT.enableFunction(DFGT_FUN_ALL); // enable all functions // DFGT.disableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT); // disable function test // DFGT.disableFunction(DFGT_FUN_TOUCH1 | DFGT_FUN_TOUCH2); // disable function test // DFGT.enableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT); // enable function test // DFGT.setSleep(4); // set auto sleep time out, in sleep mode, put something } void loop() { int8_t rslt = DFGT.getAnEvent(); // get an event that data saved in serial buffer if(rslt != DF_ERR) { // DFGT.setSleep(DFGT_SLEEP_DISABLE); // disable auto sleep switch(rslt) { case DFGT_EVT_BACK: { Serial.println("get event back"); } break; case DFGT_EVT_FORWARD: { Serial.println("get event forward"); } break; case DFGT_EVT_RIGHT: { Serial.println("get event right"); } break; case DFGT_EVT_LEFT: { Serial.println("get event left"); } break; case DFGT_EVT_PULLUP: { Serial.println("get event pull up"); } break; case DFGT_EVT_PULLDOWN: { Serial.println("get event pull down"); } break; case DFGT_EVT_PULLREMOVE: { Serial.println("get event pull and remove"); } break; case DFGT_EVT_TOUCH1: { Serial.println("get event touch1"); } break; case DFGT_EVT_TOUCH2: { Serial.println("get event touch2"); } break; case DFGT_EVT_TOUCH3: { Serial.println("get event touch3"); } break; case DFGT_EVT_TOUCH4: { Serial.println("get event touch4"); } break; case DFGT_EVT_TOUCH5: { Serial.println("get event touch5"); } break; } } }[/mw_shl_code] 程序上传后进行测试,很快就在串口监视器显示结果(截图写文字时把上下写反了): 刚开始对手势不太熟练,测试结果总是觉得不对,试过几次发现是需要技巧的: 首先传感器按下面的方向放置,手势的方向如图所示;其次手势的动作要迅速,太慢不行。 另外还有一点非常重要,进行上下动作时手不要发生水平的位移;进行水平移动时,手不要发生垂直方向的位移。否则传感器就容易判断出错:到底是前后左右还是上下。 调整好传感器并以迅速的手势挥动,识别的结果基本就全部与预期动作一致了。 一共支持7种动作:向右、向左、向后、向前、下压pull down、上拉pull up及上拉下压后手松开pull and remove 三、在虚谷号上测试:
将需要的库文件复制到虚谷号vvboard-->Arduino-->02.lib目录下(可以使用U盘方式直接复制,或使用主机模式在浏览器中下载到目录),在arduino IDE中打开示例文件simpleGuester,或直接复制前面的代码。 将代码上传到虚谷号,成功后打开串口监视器,稍等片刻就会输出识别结果: 在虚谷号上识别的结果感觉不如UNO上的好,反映要慢一些。
四、程序代码 [mw_shl_code=cpp,true]/* * file simpleGesture.ino * * @ https://github.com/DFRobot/DFRobot_Gesture_Touch * * example for DFRobot Gesture&Touch sensor * * sensor event will print on your serial monitor * * for esp32, rx_pin = D5, tx_pin = D6 * * Copyright [DFRobot](http://www.dfrobot.com), 2016 * Copyright GNU Lesser General Public License * * version V1.0 * date 2018-10-19 */ #include "DFRobot_Gesture_Touch.h" #include <Adafruit_NeoPixel.h> #include "U8glib.h" U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI char* gesture; #ifdef __AVR__ #include "SoftwareSerial.h" SoftwareSerial mySerial(10, 11); // example for uno, use software serial #elif defined ESP_PLATFORM #include "HardwareSerial.h" HardwareSerial mySerial(1); #endif Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); DFRobot_Gesture_Touch DFGT(&mySerial); // init sensor object, request write and read function void draw(char* gesture) { // graphic commands to redraw the complete screen should be placed here u8g.setFont(u8g_font_unifont); u8g.setPrintPos(0, 20); // call procedure from base class, http://arduino.cc/en/Serial/Print u8g.print(gesture); } void setup() { Serial.begin(115200); mySerial.begin(9600); delay(500); while(mySerial.available()) // flush serial buffer mySerial.read(); Serial.println("gesture&touch sensor test"); strip.begin(); strip.show(); // Initialize all pixels to 'off' DFGT.setGestureDistance(20); // suggest default value // DFGT.enableFunction(DFGT_FUN_ALL); // enable all functions // DFGT.disableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT); // disable function test // DFGT.enableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT); // enable function test // DFGT.setSleep(4); // set auto sleep time out, in sleep mode, something approach will weak it up } void loop() { u8g.firstPage(); do { draw(gesture); } while( u8g.nextPage() ); // rebuild the picture after some delay delay(500); int8_t rslt = DFGT.getAnEvent(); // get an event that data saved in serial buffer if(rslt != DF_ERR) { // DFGT.setSleep(DFGT_SLEEP_DISABLE); // disable auto sleep switch(rslt) { case DFGT_EVT_BACK: { Serial.println("get event back"); gesture = "back"; draw(gesture); } break; case DFGT_EVT_FORWARD: { Serial.println("get event forward"); gesture = "forward"; draw(gesture); } break; case DFGT_EVT_RIGHT: { Serial.println("get event right"); gesture = "right"; draw(gesture); } break; case DFGT_EVT_LEFT: { Serial.println("get event left"); gesture = "left"; draw(gesture); } break; case DFGT_EVT_PULLUP: { Serial.println("get event pull up"); gesture = "pull up"; draw(gesture); } break; case DFGT_EVT_PULLDOWN: { Serial.println("get event pull down"); gesture = "pull down"; draw(gesture); } break; case DFGT_EVT_PULLREMOVE: { Serial.println("get event pull and remove"); gesture = "pull and remove"; draw(gesture); } break; case DFGT_EVT_TOUCH1: { Serial.println("get event touch1"); gesture = "touch1"; draw(gesture); } break; case DFGT_EVT_TOUCH2: { Serial.println("get event touch2"); gesture = "touch2"; draw(gesture); } break; case DFGT_EVT_TOUCH3: { Serial.println("get event touch3"); gesture = "touch3"; draw(gesture); } break; case DFGT_EVT_TOUCH4: { Serial.println("get event touch4"); gesture = "touch4"; draw(gesture); } break; case DFGT_EVT_TOUCH5: { Serial.println("get event touch5"); gesture = "touch5"; draw(gesture); } break; } } }[/mw_shl_code] 试用感受: 要让手势识别传感器按自己的设想动作效果并不容易,如果用在项目中可能会有误操作,需要使用者多次练习:手势动作要迅速;进行上下动作时手不要发生水平的位移;进行水平移动时,手不要发生垂直方向的位移。 刚开始测试时,各个手势都还能识别,但是试用到后来,向左的手势无论如何也识别不出来。过了一天再重新上电又能够识别了。稳定性还是有待改进。 总结:手势识别准确率不高,现实应用意义不大。接下来进行触摸功能的测试。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed