说明:
安卓手机开启USB调试模式,具体如何开启,百度自己手机即可
安装ADB工具
安装python2.7
步骤:
1、安装adb工具
下载地址:https://download.csdn.net/download/qq_29769263/10420731
下载后解压到便于记忆的地方即可,以解压到桌面为例:
如图,我们记住他的地址,
C:Users.GungnirDesktopplatform-tools-latest-windowsplatform-tools
下面我们验证一下是否能够正常工作。
将手机通过数据线连接电脑,按住Windows + R键,输入cmd,点击确定,进入命令行界面
输入cd加刚才我们记住的地址,进入我们解压后的地址,记得在cd和地址之间加一个空格。
下面我们输入
adb devices
如图,如果出现寻找到device,说明已经连接成功了,如果出现下图,
List of devices attached
说明我们连接没有成功,那就要查看你是否开启了usb调试或adb打开是否正确。
adb工具的命令不止这两个,下面举两个例子,也是我们一会即将用到的。
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png C:Users.GungnirDesktopscreen.png
第一个指令的意思是截图,此截图将会保存到sdcard目录下,名字为screen.png,第二条指令的意思是将sdcard目录下名称为screen.png的文件pull到C:Users.GungnirDesktop目录下,名称为screen.png,C:Users.GungnirDesktop这一个地址根据你们电脑实际位置而定。
当然,这样还是很麻烦,因为我们每次使用adb指令时,都需要cd到adb工具所在的目录,这在我们使用开发过程中都很不方便,下面介绍一下他的配置方法。
右击 我的电脑 -> 属性 ,选择系统设置
进入 高级 页面,选择 环境变量
在下方 系统变量 处选择 Path ,点击 编辑
点击 新建 后,在左侧列表中输入我们adb工具解压的目录
C:Users.GungnirDesktopplatform-tools-latest-windowsplatform-tools
然后一路 确定 即可。
此时,我们再次进入命令行界面后,即可直接输入adb命令,而无需cd到adb目录。当然,如果电脑是win10 的话,也可以通过Windows power shell执行adb命令。
2、python2.7安装
安装完adb工具后,我们下载python2.7及所需要的库
下载地址:python2.7下载地址
在下方的列表中,选择 Python 2.7.15 - 2018-05-01 下的 Download Windows x86-64 MSI installer 选项,下载完成后直接安装即可。
python本身提供了一个IDLE,可以直接在其界面下使用
当然,我们也可以在命令行界面下运行,不过,我们同样需要在 Path下添加python2.7的位置,如果选择默认安装的话,python2.7的默认目录为
C:Python27
可以直接将这个地址添加到 Path 目录下,这样,我们就可以直接在命令行界面 或者 Windows power shell下进入python的环境
除了安装python2.7外,我们还需要装一些我们在运行过程中所需要的一些库,如果python2.7选择在默认目录下安装的话,进入目录
C:Python27Scripts
然后我们再次打开命令行界面,cd进该文件夹后,输入 pip,如果出现该界面而不是找不到pip命令,就说明电脑上已经存在pip包管理工具了,如果不存在的话,就需要下载一个
然后依次在命令行界面输入下面命令,安装相应的库
pip install backports.functools-lru-cache==1.4
pip install cycler==0.10.0
pip install matplotlib==2.1.1
pip install numpy==1.13.3
pip install olefile==0.44
pip install opencv-python==3.4.0.12
pip install Pillow==4.3.0
pip install pyparsing==2.2.0
pip install python-dateutil==2.6.1
pip install pytz==2017.3
pip install six==1.11.0
pip install tensorflow==1.4.0
pip install pandas==0.22.0
pip install scipy==1.0.0
pip install scikit_learn==0.19.1
pip install pyserial
安装成功后就可以进入下面的步骤。
3、下载实验
下载地址:https://download.csdn.net/download/qq_29769263/10421958
下载解压后,打开 wechat_jump.py 文件,可以用python自带的IDLE运行,也可以用其他编译软件运行,在运行之前
ser = serial.Serial('com5',9600)
需要将 com5 更改为连接到电脑的串口号,同时将串口(CH340)与电脑连接好后方可运行。
同时,还需要注意
def send(distance):
distance = int(distance)
ser.write('[')
ser.write(str(distance))
ser.write(']')
这一段代码规定了我们向串口发送数据的格式,即以 字符 ‘ [ ’ 开头,以 字符 ’ ] ’ 结尾,我们在STM32编写代码时要根据这个确定我们串口接收后的程序。
如以下:
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1); //读取接收到的数据
if(USART_RX_STA == 1)
{
Distance[rectim] = Res - 48; //发送的数据为char类型,需要 - 48
rectim ++; //用于记录位数
if(Res == 0x5D) //如果接收到了 ‘ ] ’,接收完成
{
if(rectim == 2) //接收到了两位,最后一位置 0xFF,用于后续检测位数
{
Distance[1] = 0xFF;
}
else
if(rectim == 1) //如果接收到了个位数
{
Distance[1] = 0xFF;
Distance[2] = 0xFF;
}
USART_RX_STA = 0; //接收完毕,清标志位
rectim = 0;
RecOK = 1; //接收成功,置标志位
}
}
if(Res == 0x5B) //如果接收到了 ‘ [ ’,置标志位,开始传输
USART_RX_STA = 1;
}
}
if(RecOK)
{
if(Distance[1] == 0xFF)
Dist = Distance[0];
else if(Distance[2] == 0xFF)
Dist = Distance[0] * 10 + Distance[1] ;
else
Dist = Distance[0] * 100 + Distance[1] * 10 + Distance[2];
RecOK = 0;
}
这只是一个例子,串口将数据存储到Distance数组,再将数组数据转为距离即可,具体编写可按实际情况来,通常情况下,传输数据为两点之间的距离,只需要再将一个舵机与STM32连接,即可通过STM32控制舵机按压屏幕时间来控制跳跃时间和距离。
当然,我们可以通过adb指令直接按压屏幕,这样就无需我们使用STM32通过舵机按压,
def jump(distance):
press_time = distance * 1.35
press_time = int(press_time)
cmd = 'adb shell input swipe 320 410 320 410 ' + str(press_time)
print(cmd)
os.system(cmd)
这一段代码即为通过adb指令实现按压屏幕,实验时,需要将
def on_click(event):
global update
global src_x, src_y
dst_x, dst_y = event.xdata, event.ydata
distance = (dst_x - src_x)**2 + (dst_y - src_y)**2
distance = (distance ** 0.5) / scale
print('distance = ', distance)
send(distance)
update = True
on_click 函数中的
send(distance)
更改为
jump(distance)
同时,为了获得更好的效果,可以更改按压时间与距离的比例系数
press_time = distance * 1.35