legoboost.js 4.0 KB

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