directionOfDetection.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view>
  3. <uni-nav-bar id="nav-bar" status-bar="true" @clickLeft="onBack()" title="校准设备方向" color="#000000" fixed="true"
  4. :border="false" @clickRight="onResetCalibration()">
  5. <view slot="left">
  6. <view class=" flex align-center margin-left">
  7. <image class="p-left-arrow" src="../../../static/p-left-arrow.png"></image>
  8. </view>
  9. </view>
  10. <view slot="right">
  11. <button class="cu-btn make-bg-bPurple text-white margin-top margin-bottom text-11px">重置</button>
  12. </view>
  13. </uni-nav-bar>
  14. <view class="flex justify-center align-center" style="height: 200rpx;">
  15. <view v-if="currentBLEData">
  16. <view>加速计 acc: x:{{currentBLEData.acc.ax.toFixed(4)}} y:{{currentBLEData.acc.ay.toFixed(4)}}
  17. z:{{currentBLEData.acc.az.toFixed(4)}}
  18. </view>
  19. <view v-if="outBLEData">校准后 acc: x:{{outBLEData.acc.ax.toFixed(4)}} y:{{outBLEData.acc.ay.toFixed(4)}}
  20. z:{{outBLEData.acc.az.toFixed(4)}}
  21. </view>
  22. <view>陀螺仪 gyro: x:{{currentBLEData.gyro.gx.toFixed(4)}} y:{{currentBLEData.gyro.gy.toFixed(4)}}
  23. z:{{currentBLEData.gyro.gz.toFixed(4)}}
  24. </view>
  25. <view v-if="outBLEData">校准后 gyro: y:{{outBLEData.gyro.gy.toFixed(4)}}
  26. </view>
  27. </view>
  28. </view>
  29. <!-- 任务栏 -->
  30. <scroll-view scroll-y="true" style="height: 500px; max-height: 600px;min-height: 230rpx;margin-top: 10rpx;"
  31. :scroll-into-view="taskIntoView" :scroll-top="taskScrollTop">
  32. <view class="my-column-cu-steps">
  33. <view class="cu-item" :class="!item.isUnlock?'':'make-text-bPurple'"
  34. v-for="(item,index) in currentTaskList" :key="index" :id='"task_"+index'>
  35. <image v-if="item.isUnlock" class="my-column-cu-image"
  36. src="../../../static/common/modal/unLock.png"></image>
  37. <image v-else-if="index>=1&&currentTaskList[index-1].isUnlock&&currentTaskList[index-1].isPassed"
  38. class="my-column-cu-image" src="../../../static/common/modal/canUnlock.png">
  39. </image>
  40. <image v-else class="my-column-cu-image" src="../../../static/common/modal/lock.png">
  41. </image>
  42. <view class="content" style="height: 130px;">
  43. <view class="text-15px text-bold text-cut text-left "
  44. style="margin-left: 48rpx; width: 360rpx;">
  45. {{index+1}}. {{item.name}}
  46. </view>
  47. <view class="text-13px text-grey text-left" style="margin: 20rpx 20rpx 0 86rpx;">
  48. {{item.explain}}
  49. </view>
  50. <button v-if="item.isAcc" :disabled="!(currentJumpTask!=null&&index==currentJumpTask.index)"
  51. class="cu-btn make-bg-bPurple text-white margin-top margin-bottom"
  52. @click="onCalibration(index,item)">确认</button>
  53. <button v-else :disabled="!(currentJumpTask!=null&&index==currentJumpTask.index)"
  54. class="cu-btn make-bg-bPurple text-white margin-top margin-bottom"
  55. @click="onCalibrationGyro(index,item)">开始</button>
  56. </view>
  57. <image class="my-column-cu-image" style="left: 130rpx;top: 40rpx; width: 50rpx;height: 50rpx;"
  58. :src="(currentJumpTask!=null&&index==currentJumpTask.index)?'/static/devicesOther/radio-b.png':'/static/devicesOther/radio-g.png'"
  59. mode="aspectFit"></image>
  60. </view>
  61. </view>
  62. </scroll-view>
  63. </view>
  64. </template>
  65. <script>
  66. import {
  67. mapState,
  68. mapMutations
  69. } from 'vuex'
  70. export default {
  71. data() {
  72. return {
  73. taskIntoView: '',
  74. taskScrollTop: 0,
  75. currentJumpTask: {
  76. index: 0
  77. },
  78. currentTaskList: [{
  79. name: 'y轴-设备垂直方向',
  80. isUnlock: false,
  81. isPassed: false,
  82. explain: '设备垂直桌面静止后,点击确认按钮',
  83. isAcc: true
  84. },
  85. {
  86. name: 'z轴-设备方向键方向',
  87. isUnlock: false,
  88. isPassed: false,
  89. explain: '设备方向键向上静止于桌面后,点击确认按钮',
  90. isAcc: true
  91. },
  92. {
  93. name: 'x轴-侧边按钮方向',
  94. isUnlock: false,
  95. isPassed: false,
  96. explain: '侧边按钮垂直向上静止后,点击确认按钮',
  97. isAcc: true
  98. },
  99. {
  100. name: 'y轴-左旋转',
  101. isUnlock: false,
  102. isPassed: false,
  103. explain: '垂直桌面方向进行绕Y轴左旋转,点击确认按钮',
  104. isAcc: false
  105. }
  106. ],
  107. currentBLEData: null,
  108. outBLEData: null,
  109. currentGyroXArray: [],
  110. currentGyroYArray: [],
  111. currentGyroZArray: [],
  112. bMatchGyro: false,
  113. currentInterval: null
  114. }
  115. },
  116. onLoad() {
  117. //测试socket
  118. console.log('test*******************************');
  119. // this.$testWS.connectSocket(() => {
  120. // this.$testWS.sendSocketMessage('unity');
  121. // });
  122. //监听蓝牙回调
  123. uni.$on('callbackCloseBLE', this.callbackCloseBLE);
  124. uni.$on('updateBLEDeviceData', this.callbackUpdateBLEData);
  125. // #ifndef APP-PLUS
  126. this.currentInterval = setInterval(() => {
  127. this.callbackUpdateBLEData({
  128. acc: {
  129. ax: Math.random() * 2,
  130. ay: Math.random() * 2,
  131. az: Math.random() * 2
  132. },
  133. gyro: {
  134. gx: Math.random(),
  135. gy: Math.random(),
  136. gz: Math.random()
  137. }
  138. })
  139. }, 100);
  140. // #endif
  141. // #ifdef APP-PLUS
  142. //打开加速计
  143. this.onWriteBLEConnectionValue({
  144. value: "3"
  145. });
  146. // #endif
  147. },
  148. onUnload() {
  149. //*****注释蓝牙操作******
  150. uni.$off('callbackCloseBLE', this.callbackCloseBLE);
  151. uni.$off('updateBLEDeviceData', this.callbackUpdateBLEData);
  152. // #ifdef APP-PLUS
  153. //打开加速计
  154. this.onWriteBLEConnectionValue({
  155. value: "4"
  156. });
  157. // #endif
  158. },
  159. computed: mapState(['convertAcc', 'convertGyro']),
  160. methods: {
  161. ...mapMutations(['onWriteBLEConnectionValue', 'onAccAndGyroConvertDataReset', 'onConvertDeviceData','onAccAndGyroConvertDataSave']),
  162. onBack() {
  163. uni.navigateBack({
  164. delta: 1
  165. })
  166. },
  167. /**
  168. * 蓝牙断开连接时候
  169. */
  170. callbackCloseBLE() {
  171. console.log('callbackCloseBLE');
  172. },
  173. //设备回调事件
  174. callbackUpdateBLEData(data) {
  175. //如果在监听状态时候隐藏页面,返回
  176. this.currentBLEData = data;
  177. //如果开始校准旋转方向
  178. if (this.bMatchGyro) {
  179. this.onUpdateGyroData();
  180. }
  181. //模拟数据转化
  182. this.onConvertDeviceData({
  183. data: this.currentBLEData,
  184. callback: (outData) => {
  185. this.outBLEData = {
  186. acc: outData.convertAcc,
  187. gyro: outData.convertGyro
  188. }
  189. }
  190. });
  191. },
  192. updateTaskScroll() {
  193. if (this.currentJumpTask != null) {
  194. let _index = this.currentJumpTask.index - 1 < 0 ? 0 : this.currentJumpTask.index - 1;
  195. this.$nextTick(() => {
  196. this.taskIntoView = "task_" + _index;
  197. });
  198. this.taskIntoView = '' //不清空再次跳到锚点位置会不起作用
  199. }
  200. },
  201. onResetCalibration() {
  202. this.currentJumpTask.index = 0;
  203. for (let i = 0; i < this.currentTaskList.length; i++) {
  204. this.currentTaskList[i].isUnlock = false;
  205. }
  206. this.onAccAndGyroConvertDataReset();
  207. if (this.currentInterval) {
  208. clearInterval(this.currentInterval)
  209. this.currentInterval = null;
  210. }
  211. },
  212. onCalibration(index, item) {
  213. console.log(index, JSON.stringify(item));
  214. item.isUnlock = true;
  215. this.currentJumpTask.index = index + 1;
  216. //这里处理校准设备方向处理,convertAcc 是一个 x,y,z 数组
  217. if (0 === index) {
  218. //校准y轴方向
  219. //获取匹配当前接近1的数据,三个轴同时匹配
  220. let {
  221. name,
  222. value
  223. } = this.onMatchAccData();
  224. this.$store.state.convertAcc[1].match = name;
  225. this.$store.state.convertAcc[1].direction = value > 0 ? 1 : -1;
  226. } else if (1 === index) {
  227. //校准z轴方向
  228. let {
  229. name,
  230. value
  231. } = this.onMatchAccData();
  232. this.$store.state.convertAcc[2].match = name;
  233. this.$store.state.convertAcc[2].direction = value > 0 ? 1 : -1;
  234. } else if (2 === index) {
  235. //校准x轴方向
  236. let {
  237. name,
  238. value
  239. } = this.onMatchAccData();
  240. this.$store.state.convertAcc[0].match = name;
  241. this.$store.state.convertAcc[0].direction = value > 0 ? 1 : -1;
  242. }
  243. this.updateTaskScroll();
  244. },
  245. onCalibrationGyro(index, item) {
  246. item.isUnlock = true;
  247. this.currentJumpTask.index = index + 1;
  248. if (3 === index) {
  249. //校准y轴旋转方向-左旋转
  250. this.onStartMatchGyro();
  251. uni.showToast({
  252. title: '校准中',
  253. icon: 'loading',
  254. mask: true,
  255. duration: 2000
  256. })
  257. }
  258. this.updateTaskScroll();
  259. },
  260. onStartMatchGyro() {
  261. this.currentGyroValue = 0;
  262. this.bMatchGyro = true;
  263. this.currentGyroXArray = [];
  264. this.currentGyroYArray = [];
  265. this.currentGyroZArray = [];
  266. },
  267. onMatchAccData() {
  268. let _data = {
  269. name: 'none',
  270. value: 0
  271. }
  272. // console.log(JSON.stringify(this.currentBLEData));
  273. //返回一个大于0.9的数据的轴
  274. if (Math.abs(this.currentBLEData.acc.ax) > 0.6) {
  275. _data.name = 'ax'
  276. _data.value = this.currentBLEData.acc.ax;
  277. return _data;
  278. }
  279. if (Math.abs(this.currentBLEData.acc.ay) > 0.6) {
  280. _data.name = 'ay'
  281. _data.value = this.currentBLEData.acc.ay;
  282. return _data;
  283. }
  284. if (Math.abs(this.currentBLEData.acc.az) > 0.6) {
  285. _data.name = 'az'
  286. _data.value = this.currentBLEData.acc.az;
  287. return _data;
  288. }
  289. // console.log(_data);
  290. return _data;
  291. },
  292. onUpdateGyroData() {
  293. let _temp = 50;
  294. if (Math.abs(this.currentBLEData.gyro.gx) < _temp &&
  295. Math.abs(this.currentBLEData.gyro.gy) < _temp &&
  296. Math.abs(this.currentBLEData.gyro.gz) < _temp)
  297. return;
  298. //这里不确定是哪个轴,
  299. this.currentGyroXArray.push(this.currentBLEData.gyro.gx);
  300. this.currentGyroYArray.push(this.currentBLEData.gyro.gy);
  301. this.currentGyroZArray.push(this.currentBLEData.gyro.gz);
  302. //取到10个数,停止。
  303. if (this.currentGyroXArray.length > 20) {
  304. this.bMatchGyro = false;
  305. //判断左旋时候,确定当前轴的值
  306. let _data = {
  307. name: 'none',
  308. value: 0
  309. }
  310. let currentGyroXValue = 0,
  311. currentGyroYValue = 0,
  312. currentGyroZValue = 0,
  313. currentMaxGyro = 0;
  314. for (let i = 0; i < this.currentGyroXArray.length; i++) {
  315. currentGyroXValue += this.currentGyroXArray[i];
  316. currentGyroYValue += this.currentGyroYArray[i];
  317. currentGyroZValue += this.currentGyroZArray[i];
  318. }
  319. if (Math.abs(currentGyroXValue) > Math.abs(currentGyroYValue)) {
  320. _data.name = 'gx';
  321. _data.value = currentGyroXValue;
  322. currentMaxGyro = currentGyroXValue;
  323. } else {
  324. _data.name = 'gy';
  325. _data.value = currentGyroYValue;
  326. currentMaxGyro = currentGyroYValue;
  327. }
  328. if (Math.abs(currentGyroZValue) > Math.abs(currentMaxGyro)) {
  329. _data.name = 'gz';
  330. _data.value = currentGyroZValue;
  331. currentMaxGyro = currentGyroZValue;
  332. }
  333. //定y为旋转轴,即 convertGyro 下标 1
  334. this.$store.state.convertGyro[1].match = _data.name;
  335. //以左旋转为校准轴,如果当前左旋值大于0.即设置direction -1;
  336. this.$store.state.convertGyro[1].direction = _data.value > 0 ? -1 : 1;
  337. console.log('convertGyro=',JSON.stringify(_data))
  338. uni.showToast({
  339. title: '校准完成'
  340. })
  341. this.onAccAndGyroConvertDataSave();
  342. }
  343. }
  344. }
  345. }
  346. </script>
  347. <style>
  348. </style>