这篇文章来源于deviceplus.com英语网站的翻译稿。
在利用rohm传感器评估套件实现ucla airmouse – 第1部分中,我们完成了项目的硬件。对于发射器部分,我们将airmouse按钮以及uno的gpio引脚和rf模块之间的接口组装在一块面包板上。由于rohm传感器开发板(sensorshield)在原型设计和diy项目方面的应用非常方便,因此我们选择将其与加速度计模块接合在一起使用。正如您将在本教程中看到的那样,rohm开发板内置arduino与其外设之间的i2c通信功能,因此,用户通过一些简单代码就可以接收加速度计的数据,无需编写任何底层i2c函数来发送和接收来自设备地址的数据。对于接收器部分,我们为teensy微控制器组装了类似的分线板,以便与rf模块对接。
本教程介绍和描述的代码将帮您将两个模块连接在一起,以完成该项目。我们将向您展示两个模块之间发送数据的基本代码以及处理加速度计数据以在计算机显示器上移动光标的基本代码。同时,我们希望您能创造一些更酷的补充和改进!
airmouse快速概览
我们都用过电脑鼠标,但是它们只能在桌面之类的平面上工作。我们已经制作了一个“airmouse”——一款能够在3d空间中运行的计算机鼠标。用户通过倾斜鼠标就可以让屏幕上的光标移动,从而可以进行大范围自定义动作。我们已经在第1部分中介绍了构建airmouse的基础知识。airmouse主要由两部分组成:戴在手上的鼠标发射器和连接用户计算机的接收器。发射器收集鼠标的方向和按钮状态信息;而接收器则负责转换这些信息,从而在计算机屏幕上执行相应操作。airmouse由arduino uno和nrf24l01射频模块结合rohm传感器开发板的加速度计构建而成。
第1部分的硬件:
发射器:
● 1 x arduino uno微控制器
1 x rohm传感器评估套件
1 x rohm加速度计 (kx022-1020)
1 x nrf24l01+ rf模块
排母
滑动开关
1 x 可焊接试验电路板,足够大,能焊接微控制器和所有电子器件
2 x 按键
1 x 1s lipo电池
1 x 1kΩ电阻r
1 x 3.3kΩ电阻
接收器:
1 x teensy 3.2微控制器
1 x nrf24l01+ rf模块
排母
1 x 可焊接试验电路板,足够大,能焊接微控制器和所有电子器件
1 x 红、黄、绿led灯
1 x 150Ω电阻
按照第1部分连接硬件之后,您就可以利用以下程序运行发送器和接收器。
发射器:
#include #include rf24.h #define byte uint8_t#include #include kx022 accelerometer(kx022_device_address_1e); rf24 radio(9,10); uint64_t pipes[2] = {0xf0f0f0f0f0, 0xf0f1f1f1f1}; //reading, writing void initradio(){ radio.setpalevel(rf24_pa_high); //payload size default 32... radio.setchannel(10); //you can change the channel setting radio.setcrclength(rf24_crc_16); //2-byte crc radio.setdatarate(rf24_1mbps); //1mbps data rate radio.openreadingpipe(0, pipes[0]); radio.openwritingpipe(pipes[1]);} #define buttonpinr 2 //change these accordingly#define buttonpinl 3 void setup() { // put your setup code here, to run once: serial.begin(9600); while (!serial); pinmode(buttonpinr, input); pinmode(buttonpinl, input); radio.begin(); initradio(); radio.stoplistening(); wire.begin(); accelerometer.init();} long lastdebouncetimer = 0; // the last time the output pin was toggledlong lastdebouncetimel = 0;long debouncedelay = 50;int buttonstater = low; // the current reading from the input pinint buttonstatel = low;int lastreadingr = low;int lastreadingl = low; char readbuttonr(){ int reading = digitalread(buttonpinr);//get what state the button is char out = 'a';//the value to return if nothing special happened if (reading != lastreadingr) {//we're reading a new state for button // reset the debouncing timer lastdebouncetimer = millis(); } if ((millis() - lastdebouncetimer) > debouncedelay) {//we finally have a stable value if (reading != buttonstater)//compared to our previous state, we have a flip { out = 'r';//prepare to toggle the mini } buttonstater = reading;//make the buttonstate the same } lastreadingr = reading;//make the last state the current state return out;} char readbuttonl(){ int reading = digitalread(buttonpinl); char out = 'a'; if (reading != lastreadingl) { // reset the debouncing timer lastdebouncetimel = millis(); } if ((millis() - lastdebouncetimel) > debouncedelay) { if (reading != buttonstatel) { out = 'l'; } buttonstatel = reading; } lastreadingl = reading; return out;} struct data{ boolean ispushedr = false; boolean ispushedl = false; int8_t acceleration[3] = {0, 0, 0};}; data packet;boolean rstate = false;//these states are used to represent the current state of the buttonsboolean lstate = false; void loop() { if(readbuttonr() == 'r'){ //toggle button state when button state change is detected rstate = !rstate; } if(readbuttonl() == 'l'){ //toggle button state when button state change is detected lstate=!lstate; } packet.ispushedr = rstate; packet.ispushedl = lstate; uint8_t rc; float acc[3]; rc = accelerometer.get_val(acc); if (rc == 0) { //we cast to drop the decimal, we don't need that high precision packet.acceleration[0] = (int8_t)(acc[0]*100); //x //serial.print(packet.acceleration[0]); serial.print( ); packet.acceleration[1] = (int8_t)(acc[1]*100); //y //serial.print(packet.acceleration[1]); serial.print( ); packet.acceleration[2] = (int8_t)(acc[2]*100); //z //serial.println(packet.acceleration[2]); } radio.write((char*) &packet, sizeof(packet));}
接收器:
#include #include rf24.h rf24 radio(9,10); uint64_t pipes[2] = {0xf0f1f1f1f1, 0xf0f0f0f0f0}; //reading, writing void initradio(){ radio.setpalevel(rf24_pa_high); //payload size default 32... radio.setchannel(10); radio.setcrclength(rf24_crc_16); //2-byte crc radio.setdatarate(rf24_1mbps); //1mbps data rate radio.openreadingpipe(0, pipes[0]); //reading pipe radio.openwritingpipe(pipes[1]); radio.startlistening(); }#define r_pin 6 //red led#define g_pin 7 //green led#define y_pin 8 //yellow led void setup() { serial.begin(9600); while(!serial); //wait until serial is initialized...(we found that not including this line of code caused errors on the //teensy because it started executing code without ensuring that serial communication with the laptop was //properly initialized... radio.begin(); initradio(); mouse.screensize(1920, 1080); // configure screen size randomseed(analogread(0)); pinmode(r_pin, output); pinmode(g_pin, output); pinmode(y_pin, output);} #define calix 6 //calibration for x#define caliy -1 //calibration for y #define scalingfactor 0.05#define threshold 1 double movevector[2] = {0, 0}; void tilttovector(const int8_t* acceleration){ movevector[0] = 0; movevector[1] = 0; if(abs(acceleration[0] - calix) > threshold){ //calculate move movevector[1] = (double)(acceleration[0] * scalingfactor); } if(abs(acceleration[1] - caliy) > threshold) { movevector[0] = (double)(acceleration[1] * scalingfactor); }} struct data{ boolean ispushedr = false; boolean ispushedl = false; int8_t acceleration[3] = {0, 0, 0};}; data packet; void loop() { bool stillwaiting = true; //serial.println(about to read); while(stillwaiting){ if(radio.available(0)){ //you've got mail!!! radio.read((char*) &packet, sizeof(packet)); stillwaiting = false; } } mouse.move(movevector[0], movevector[1]); mouse.move(movevector[0], movevector[1]); //call it twice within the loop for smoothness :) //prints for debugging purposes serial.println(finished writing the pins); if (packet.ispushedr) { serial.println(the right button has been clicked!!! (did you mean to right click?!?!)); } if (packet.ispushedl) { serial.println(the left button has been clicked!!! (did you mean to left click?!?!)); //mouse.click(); } serial.print(x: ); serial.println(packet.acceleration[0]); serial.print(y: ); serial.println(packet.acceleration[1]); serial.print(z: ); serial.println(packet.acceleration[2]); tilttovector(packet.acceleration); //re-calculate move vector coordinates // mouse.move(movevector[0], movevector[1]); }
将代码上传到teensy与将代码上传到arduino uno略有不同。对于uno,您只需按照通常的编译和上传步骤执行即可:
依次选择tools > board > arduino/genuino uno
选择端口(port)
点击arduino ide中的“upload”(上传)按钮
而对于teensy,请按照以下步骤上传接收器代码:
依次选择tools > board > teensy 3.2 / 3.1
依次选择tools > usb type > keyboard + mouse + joystick (我们将teensy当作一个usb设备使用)
点击arduino ide中的“upload”(上传)按钮
为了本教程的目的,我们不会详细涉及所使用的不同通信协议,也不会详细介绍rf模块的通信软件。要了解有关这些主题的更多信息,请查看我们的 通信协议 和 nrf24l01+ 模块 教程 相反,我们将简要介绍软件中主控制电路的工作原理。
在airmouse中,发射器负责收集数据,但大部分数据处理由接收器模块进行。系统的这种设计方式使得arduino——比teensy更弱的处理器——只需要收集数据即可,因此能够在决策和计算上花费更少的资源,并能够以更快的周期运行。通过这种实现方法,两台设备之间唯一发送的数据就是原始加速计数据和按钮数据。teensy接收这些原始数据并进行处理,从而在计算机屏幕上执行相应操作。
为了检测airmouse的方位,系统必须能够解析原始加速计数据。要做到这一点,首先必须确定每个坐标的“零值”。零值的定义如下:airmouse保持平坦(平行于地面)时每个轴的加速度计输出。确定零值后,软件就能够将加速度计数据转换为方向和数量,通过分析每个轴的加速度(由于重力)并将其与零值进行比较以便在屏幕上移动光标。
现在,我们来看一下接收器模块与电脑之间的交互。teensy被指定为usb人机界面设备(这里是指usb鼠标)。解析方位数据后,软件会计算光标移动的速度和方向。此外,该软件还将点击左键解析为左键单击,将点击右键解析为右键单击,调用适当的方法在计算机屏幕上显示左键单击或右键单击功能。以下才是最酷的部分:您完全可以只通过软件就能够修改或添加鼠标的屏幕功能!目前,鼠标只具有最基本的功能和特性,但是您只需对软件进行简单改动,就可以轻松添加诸如滚动、将光标移至屏幕上的某个点等功能!(请点击此处,了解teensy usb鼠标的参考指南) 以下是您可以实现的一些很酷的硬件和软件想法:
将不同模式的按键点击用作不同的控制功能(比如,双击右键关闭窗口)
添加滚动功能!(同时点击两个按键变为“滚动模式”)
添加其他鼠标按键以执行滚动操作或为不同功能获取更多按键模式
我们希望您能够喜欢这个airmouse项目,并且非常期待您可以对设计和功能进行修改和改进!
作为ucla ieee高级项目(advanced projects)计划的一部分,最初的airmouse由rahul iyer、aaron和andrew wilhelm研发。
360N4S骁龙英伦灰于12月6日于360、京东、天猫、苏宁、1号店开放购买
负荷开关与断路器区别
大米重金属检测仪详细参数
用户到处都在寻找Google Pixel 3动态壁纸
深圳移动如何解决最具挑战的深圳与香港边境的信号干扰问题?
利用ROHM传感器评估套件实现UCLA AirMouse–第2部分
重磅!传中方将采购美国芯片2000亿美元!
TI工程师用乐高打造星球大战BB-8呆萌机器人
图腾柱TCM的工作过程分析
印度要求智能手机产品兼容自主导航系统
Board从入门到精通系列(七)
云是AI的容器 云原生AI成过去式AI原生的云才是未来
电烙铁的类型和线路板快速制板方法
同一天开发布会:小米5X与魅族pro7标准版相差1381元,你该如何选择?
配置高,空间大,耗油低-比亚迪2017款S7,居家旅行的首选之车!
Microsoft Surface Go与Surface Pro的区别是什么
盘点传感器制造那些不得不知的21种表面处理工艺
螺旋波纹管(FEP波纹管的应用)详解
百度首席科学家吴恩达:人工智能的寒冬不会再来了
首个能根据单一图像生成较高分辨率3D人脸模型的系统