legoboost.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // The MIT License (MIT)
  2. // Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  3. this.initialize = function () {
  4. }
  5. this.createVisual = function () {
  6. let motorNode = function (motorName, position, rotation) {
  7. let rot = rotation ? rotation : [0, 0, 0];
  8. return {
  9. "extends": "proxy/aframe/abox.vwf",
  10. "properties": {
  11. "position": position,
  12. "rotation": rot,
  13. "height": 0.05,
  14. "width": 0.05,
  15. "depth": 0.05
  16. },
  17. "children": {
  18. "material": {
  19. "extends": "proxy/aframe/aMaterialComponent.vwf",
  20. "type": "component",
  21. "properties": {
  22. "color": "white",
  23. "transparent": true,
  24. "opacity": 0.5
  25. }
  26. },
  27. "vis": {
  28. "extends": "proxy/aframe/abox.vwf",
  29. "properties": {
  30. "height": 0.4,
  31. "width": 0.4,
  32. "depth": 0.05
  33. },
  34. "children": {
  35. "material": {
  36. "extends": "proxy/aframe/aMaterialComponent.vwf",
  37. "type": "component",
  38. "properties": {
  39. "color": "orange"
  40. }
  41. },
  42. "label":{
  43. "extends": "proxy/aframe/atext.vwf",
  44. "properties": {
  45. "displayName": motorName,
  46. "color": "black",
  47. "value": motorName,
  48. "side": "double",
  49. "position": [-0.02,0,0.07],
  50. "rotation": [0,0,0],
  51. "scale":[0.5,0.5,0.5]
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. let visNode = {
  60. "extends": "proxy/aframe/abox.vwf",
  61. "properties": {
  62. "position": [0, 0, 0],
  63. "rotation": [0, 0, 0],
  64. "height": 0.5,
  65. "width": 0.5,
  66. "depth": 0.5
  67. },
  68. "children": {
  69. "material": {
  70. "extends": "proxy/aframe/aMaterialComponent.vwf",
  71. "type": "component",
  72. "properties": {
  73. "color": "green"
  74. }
  75. },
  76. "motorA": motorNode('A', [0.3, 0, 0], [0, 90, 0]),
  77. "motorB": motorNode('B', [-0.3, 0, 0], [0, -90, 0]),
  78. "motorC": motorNode('C', [0, 0, 0.3], [0, 0, 0]),
  79. "motorD": motorNode('D', [0, 0, -0.3], [0, 180, 0])
  80. }
  81. }
  82. this.children.create("visualNode", visNode);
  83. }
  84. this.setPitch = function (value) {
  85. this.pitch = value;
  86. let rot = this.visualNode.rotation;
  87. this.visualNode.rotation = [this.pitch, rot[1], rot[2]];
  88. }
  89. this.getPitch = function () {
  90. return this.pitch;
  91. }
  92. this.gotDeviceInfo = function (info, key) {
  93. //got device info
  94. console.log(info);
  95. if (key == 'pitch' || key == 'roll') {
  96. this.pitch = info.tilt.pitch;
  97. this.roll = info.tilt.roll;
  98. }
  99. if (key == 'led') {
  100. this.rawLed = this.led = info.led;
  101. this.setVisualLed(this.led);
  102. }
  103. if (key == 'A' || key == 'B' || key == 'C') {
  104. this.setVisualMotorRotation('motor' + key, info.ports[key].angle - this['motor' + key]);
  105. this['rawMotor' + key] = this['motor' + key] = info.ports[key].angle;
  106. }
  107. return info
  108. }
  109. this.trackLego = function () {
  110. if (this.tracking) {
  111. this.getDeviceInfo('pitch');
  112. }
  113. this.future(0.2).trackLego();
  114. }
  115. this.setRoll = function (value) {
  116. this.roll = value;
  117. let rot = this.visualNode.rotation;
  118. this.visualNode.rotation = [rot[0], rot[1], this.roll];
  119. }
  120. this.getRoll = function () {
  121. return this.roll;
  122. }
  123. this.setLed = function (value, sync) {
  124. //set led after lego boost action - in sat_led
  125. }
  126. this.setDelay = function (value, sync) {
  127. //set delay after lego boost action - in sat_led
  128. }
  129. this.sat_setLed = function (value) {
  130. this.getDeviceInfo('led');
  131. }
  132. this.setVisualLed = function (value) {
  133. this.visualNode.material.color = value;
  134. }
  135. this.setVisualMotorRotation = function (port, angle, dutyCycle) {
  136. //let rot = this.visualNode[motor].vis.rotation;
  137. //TODO: remap dutyCycle (10,100, 1, 0.1)
  138. this.visualNode[port].vis.rotateBy([angle, 0, 0], 0.1);
  139. }
  140. this.sat_setMotorAngle = function (port, angle, dutyCycle) {
  141. this.getDeviceInfo(port);
  142. }
  143. this.setMotorAngle = function (port, angle, dutyCycle, sync) {
  144. }
  145. this.sat_setDelay = function (value) {
  146. }