使用nodemcu和mpu6050给cemu做一个体感

说明

cemu就是这个 http://cemu.info/ (之所以做这个是因为我xbox手柄没体感 + 闲的)

nodemcu硬件选的esp12,体感用mpu6050

目前勉强每秒17帧数据吧,lua语言真的不会

项目地址 https://github.com/feilongfl/wifi-gyro

测试用硬件

我真的懒得焊板子…

绿色的是逻辑电平转换,我也不知道这是什么鬼,以前从来没用过这货,这次竟然不用读取不出来数据,不然我几根线就连好了(就是不想焊板子…)mpu6050插在IIC0上面

准备工作

wireshark

nodemcu代码及编译工具

cemu作者提供的PadTest(https://files.sshnuke.net/PadTest_1011.zip)

cemu传感器协议

这个主要可以参考项目 https://github.com/hjmmc/WebGyroForCemuhook ,里面注释描述了整个cemu传感器包

整个协议我也描述在 https://gist.github.com/feilongfl/2fe93501561d46ba59689b6a26b91ec5 这个wireshark插件里面了(虽然偷懒了一些只描述了陀螺仪和加速度计).

我实际用到的只有三种报文,分别是电脑的ping,mcu的pong和传输传感器数据的报文,三种报文使用相应的头部算法

头部

定义了包头,版本号,包长度,校验,id和类型,crc使用的是特征值为 0xCBF43926 的crc算法,nodemcu固件库中不存在,可使用https://github.com/nodemcu/nodemcu-firmware/pull/2771 (由于nodemcu成员希望绝大多数包使用lua语言创建,所以这个pr被合并的可能性不大)

电脑端ping报文

这个数据是电脑发送出来用于识别器件的,mcu收到后需要响应

这个包不需要制作出来,收到后解析(解析时候可以偷懒,毕竟mcu性能有限(我的lua太渣)),可得到电脑ip和端口

mcu的pong报文

这个报文就需要自己制作了,里面写一些状态参数,电池,id什么的

这个包基本是常量,直接硬拼出来就行了

传感器数据报文

先声明所有的unused指的是我没用到(偷懒)协议里面是有用的

陀螺仪单位是°,加速度单位是g,都是浮点型

lua优化记录

这个货开始的时候每秒2包都不到,经过一番艰难爬坑终于能有个十七八包左右(凑合应该能用了吧).

首先,绝对不要使用table.insert函数,这个函数巨慢无比可以使用t[#t+1] = xxx的写法,会快不少.

表的插入巨慢无比,因此在启动时先初始化包,以后每次仅修改有变化的位置

table序列化如果单纯使用lua去循环会奇慢无比,本来我制作了 https://github.com/nodemcu/nodemcu-firmware/pull/2774 来实现table序列化,但是被https://github.com/nwf指出修改了lua语言…好吧,这个库是lua的,nodemcu只是移植过来,不能乱改

不过https://github.com/nwf提供了lua的快速实现方法,利用unpack将table拆开送给string.char方法,不过这样的问题就是table里面只能存在数字,字符串是不可以的,也就是报文头部dsus和dsuc需要用ascii代替

mpu6050驱动在https://github.com/nodemcu/nodemcu-firmware/pull/2769不过同crc,他们更希望lua语言实现(这里在校准零点漂移需要注意喂狗,不然会重启)

float转化字符在https://github.com/nodemcu/nodemcu-firmware/pull/2773不过被指出可以用struct包实现(我没试,不过看文档好像可以)

启动的时候检查到lua文件则启动编译成.lc,按道理应该会快,不过我没感觉

尽量不要写死循环,用定时器代替,长时间循环注意喂狗

大量数据打印会浪费时间同时导致lua传输失败

目前还记得的就这些,想起来再补充 (最核心的就是lua太慢的时候就拿c来提速,逃:)

参考链接

https://github.com/hjmmc/WebGyroForCemuhook

https://nodemcu.readthedocs.io/en/master/

https://cemuhook.sshnuke.net/padudpserver.html

https://www.wireshark.org/

mpu6050 datasheet

fuchsia过了几天之后的重新编译

前两天挂了一块硬盘,本来数据是没丢的,但是为了快速清理空间,我随手把fuchsia删了,于是说重新下载编译。

按照之前的教程,输入fx set x64会出现错误:

⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx set x64                                                                             23:21:55
ERROR: Unable to parse PRODUCT.BOARD goal "x64"
ERROR: You must specify which product and which board you wish to build.
ERROR: Example:
ERROR:   fx set core.x64
ERROR:
ERROR: Run "fx list-products" to see the available products.
ERROR: Run "fx list-boards" to see the available boards.
ERROR: Run "fx set --help" to see full usage.
⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯                                                                                                  23:21:58
⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx set core.x64                                                                        23:22:00
+ /storage/wine/project/fuchsia/fuchsia/zircon/prebuilt/downloads/gn gen /storage/wine/project/fuchsia/fuchsia/out/build-zircon --root=/storage/wine/project/fuchsia/fuchsia/zircon                                                                                                        
Done. Made 12811 targets from 924 files in 5950ms
Done. Made 20355 targets from 1898 files in 90543ms

解决方法也说的很明白,直接换命令。

看一下貌似对于编译有了更为详细的设定

⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx list-products                                                                                                                                                                                                                      23:26:50
bringup
core
*
router
sdk_image
speaker
terminal
workstation
⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx list-boards                                                                                                                                                                                                                        23:27:01
arm64
cleo
eagle
*
hikey960
imx8mmevk
kirin970
msm8x53-som
mt8167s_ref
toulouse
vim2
x64

解决Android Studio错误“Gradle’s dependency cache may be corrupt”

由于换了硬盘,所以顺手升级了下Android Studio。不过今天编译的时候,出现了这个错误。

大概意思就是说gradle的缓存挂了,但是在搜索的时候很多网站都说是网络问题,要写个网址上去,其实这个问题的解决办法是把gradle的缓存删除即可(不知为什么ide自己不会删除坏包)

所以linux执行以下命令即可,其他系统类似

rm -rf .gradle/wrapper/dists

在manjaro上安装vivado2018.3

因为原来linux系统硬盘坏了,经历狗东售后之后,一直没安装FPGA的ide,最近要用下,所以开始安装。

直接从官网下载linux安装包,然后安装,会卡在生成设备列表,所以默默去看Arch wiki

简单来说就是,执行以下命令安装一堆东西

yay -Syyu -noconfirm
yay -S gtk2
yay -S libpng12 lib32-libpng12
yay -S fxload 

然后就能正常安装了~~

吐槽一句,说好的自己创建桌面快捷方式呢??目录呢??都没有,还要自己手动加上。。。

顺便后面还有个仿真的大坑等着我

fuchsia df 命令小bug

今天翻了翻fuchsia的代码,翻到df命令的时候发现不愧是google大佬,容量单位都到了EB。但是比较有趣的是他们两段代码貌似不是一个人写的,出现了低级错误:)

EB丢了,8成不是一个人写的…

这几天看了Google的c代码,发现goto命令也没有书(时间太久远了,不记得谁写的,说尽量不goto来着)里面说的那么恐怖,用得好的话代码还是很简洁的。

为fuchsia添加tree命令

节约时间,修改后的tree版本传送门,https://github.com/feilongfl/tree-for-fuchsia

高高兴兴的把tree下载下来发现根本不能用:)

http://mama.indstate.edu/users/ice/tree/ 下载最新(2019.3.4)版本1.8

放在//zircon/system/uapp/tree下,并在uapp下的BUILD.gn添加tree

在tree目录下建立编译文件,忽略各种warrning,然后改掉tree里面各种空括号函数()=>(void)

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("tree_config") {
  cflags = [
    "-Wno-discarded-qualifiers",
    "-Wno-implicit-fallthrough",
    "-Wno-sign-compare",
    "-Wno-unused-but-set-parameter",
    "-Wno-incompatible-pointer-types"
  ]
}

executable("tree") {
  configs += [ ":tree_config" ]

  sources = [
    "color.c",
    "hash.c",
    "json.c",
    "tree.c",
    "unix.c",
    "file.c",
    "html.c",
    "strverscmp.c",
    "tree.h",
    "xml.c",
  ]
}

开始编译…….顺利通过

高高兴兴在系统中打开tree命令,顺利的看到崩溃堆栈(手动滑稽),什么鬼…

今天(2019.3.4)太晚了,随手搞搞,先开个坑

开始查坑

首先早上(2019.3.5)起来先写了个helloworld试试水,毕竟这才是正路啊~~

#include <stdio.h>

int main(int argc, const char** argv) {
  printf("Hello world!\n");
  printf("get arg nums [%d]\n",argc);
  for (int i = 0;i < argc;i++) {
    printf("args [%d] => %s\n", argc, argv[i]);
  }
  return 0;
}
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("helloworld_config") {
  cflags = [
  ]
}

executable("helloworld") {
  configs += [ ":helloworld_config" ]

  sources = [
    "helloworld.c",
  ]
}

然后编译运行,之后竟然也崩溃了。。。不是吧,c语言我还是很有自信的…

于是开始看别的包,打开i2c的包,发现怎么还有个deps

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

executable("i2c") {
  sources = [
    "i2c.c",
  ]
  deps = [
    "$zx/system/fidl/fuchsia-hardware-i2c:c",
    "$zx/system/ulib/fdio",
    "$zx/system/ulib/zircon",
  ]
}

第一个怎么看都是i2c硬件相关,helloworld用不到,中间那个fdio让人联想到file device i/o,最后那个肯定是系统了,但是helloworld跟系统貌似没什么太大关系,于是说只添加fdio尝试一下,可以运行了

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("helloworld_config") {
  cflags = [
  ]
}

executable("helloworld") {
  configs += [ ":helloworld_config" ]

  sources = [
    "helloworld.c",
  ]

  deps = [
    "$zx/system/ulib/fdio",
  ]
}
所以说c语言helloworld我还是能不调试一次写成功的(笑)

这样的话问题就比较清晰了,之前的tree命令没有依赖项。使用输入输出必须添加fdio依赖

google对fdio的说明

FDIO是一个客户端库,它通过紫红色系统接口中的底层fuchsia.io协议提供类似POSIX的抽象。FDIO is a client library that provides a POSIX-like abstraction over the underlying fuchsia.io protocol in the Fuchsia System Interface.

解决tree

添加fdio依赖即可运行(我已经修正github仓库)

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("tree_config") {
  cflags = [
    "-Wno-discarded-qualifiers",
    "-Wno-implicit-fallthrough",
    "-Wno-sign-compare",
    "-Wno-unused-but-set-parameter",
    "-Wno-incompatible-pointer-types"
  ]
}

executable("tree") {
  configs += [ ":tree_config" ]

  sources = [
    "color.c",
    "hash.c",
    "json.c",
    "tree.c",
    "unix.c",
    "file.c",
    "html.c",
    "strverscmp.c",
    "tree.h",
    "xml.c",
  ]

  deps = [
    "$zx/system/ulib/fdio",
  ]
}

然后就可以查看fuchsia里面到底是什么目录结构了

想直接查看的朋友移步 https://gist.github.com/feilongfl/1a0d564b85d4de3747af9820abcc82fd