legoboost.js 4.2 KB

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