Visca.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //implementation following this protocol : https://www.epiphan.com/userguides/LUMiO12x/Content/UserGuides/PTZ/3-operation/VISCAcommands.htm#InquiryCommands
  2. // var parameterPath = local.parameters;
  3. // var valuesPath = local.values;
  4. var ZoomPos = script.addFloatParameter("Zoom Position", "", 0., 0., 1.);
  5. var ZoomPosLastUpdate = script.addFloatParameter("Zoom update", "", 0.);
  6. // var state =
  7. var inquiry = ['CAM_ZoomPosInq', 'CAM_FocusModeInq', 'CAM_FocusPosInq'];
  8. var camID = 1;
  9. var waitTaskComplete = 0 ;
  10. var currentInquiry = '';
  11. var ZoomAuto = false ;
  12. var ZoomPos_target ;
  13. var ZoomTime_target ;
  14. //////////////////////// HELPERS FUNCTIONS ////////////////////////////
  15. function printArray(name, array){
  16. var printHeader = ' '+name+' : ';
  17. if (array.length){for(var i=0; i<array.length; i++){printHeader+=array[i] + ' ';}}
  18. script.log(printHeader);
  19. }
  20. //////////////////////// VISCA FUNCTIONS ////////////////////////////////
  21. function setZoomPos(ID, position){
  22. var header = [1, 4, 71];
  23. var posCmd = [];
  24. var pos = Math.round(Math.floor(16384 * position));
  25. // script.log(pos);
  26. posCmd[3]=pos%16;
  27. pos = Math.round(Math.floor(pos/16));
  28. posCmd[2]=pos%16;
  29. pos=Math.round(Math.floor(pos/16));
  30. posCmd[1]=pos%16;
  31. posCmd[0]=Math.round(Math.floor(pos/16));
  32. // local.sendBytes(idHex, 1, 4, 71, posCommand, 255);
  33. sendHeaderCommand(camID, header, posCmd);
  34. }
  35. function setZoomSpeed(ID, speed){
  36. var header = [1, 4, 7];
  37. var speedCmd = speed < 0 ? 48 : 32;
  38. speedCmd += Math.abs(speed)-1;
  39. speedCmd = speed == 0 ? 0 : speedCmd ;
  40. sendHeaderCommand(camID, header, speedCmd);
  41. }
  42. function sendInquiry(id, inquiryName){
  43. var command ;
  44. if (currentInquiry == ''){ //no inquiry running;
  45. currentInquiry = inquiryName ;
  46. if(inquiryName == 'CAM_ZoomPosInq'){ command = [9, 4, 71];}
  47. sendCommand(camID, command);
  48. }
  49. else{script.log("inquiry running");}
  50. }
  51. function sendCommand(id, command){
  52. var idHex = (parseInt("0x80") + id);
  53. // printArray('command', command);
  54. local.sendBytes(idHex, command, 255);
  55. }
  56. function sendHeaderCommand(id, header, command){
  57. if (!waitTaskComplete){
  58. var idHex = (parseInt("0x80") + id);
  59. // printArray('header', header);
  60. // printArray('command', command);
  61. local.sendBytes(idHex, header, command, 255);
  62. waitTaskComplete = 1;
  63. }
  64. else { script.log('task not complete');}
  65. }
  66. //////////////////////// ZOOM AUTOMATION ////////////////////////////////
  67. function setZoomPositionInTime(ID, position, time){
  68. //update zoom position and time targets
  69. ZoomPos_target = position ;
  70. ZoomTime_target = util.getTime()+time;
  71. //activate callback
  72. ZoomAuto = true ;
  73. //deduce appropriate speed
  74. // var speedCmd = speed < 0 ? 48 : 32;
  75. // speedCmd = speed == 0 ? 0 : speedCmd ;
  76. // speedCmd += Math.abs(speed);
  77. // sendHeaderCommand(camID, header, speedCmd);
  78. }
  79. function updateZoomPosition() {
  80. if (ZoomAuto){
  81. //update position
  82. sendInquiry(1, 'CAM_ZoomPosInq');
  83. //update speed if needed
  84. var lastUpdated = util.getTime()-ZoomPosLastUpdate.get();
  85. // script.log(lastUpdated);
  86. if(lastUpdated<0.5){
  87. //compute remaining time
  88. var remainTime = ZoomTime_target - util.getTime();
  89. if(remainTime > 0){
  90. var remainLength = (ZoomPos_target - ZoomPos.get()) * 100;
  91. var rawSpeed = remainLength / remainTime ;
  92. var absSpeed = Math.abs(rawSpeed);
  93. var realSpeed = -0.00221 * Math.pow(absSpeed,2) + 0.30898 * absSpeed ;
  94. var direction = rawSpeed > 0 ? 1 : -1;
  95. realSpeed *= direction ;
  96. realSpeed = Math.floor(realSpeed);
  97. script.log(remainLength + '% in ' + remainTime + ' s : ' + realSpeed + ' / ' +rawSpeed );
  98. setZoomSpeed(1, realSpeed);
  99. }
  100. else {
  101. ZoomAuto = false;
  102. script.log('automation timeout');
  103. //go to position
  104. setZoomPos(1, ZoomPos_target);
  105. sendInquiry(1, 'CAM_ZoomPosInq');
  106. }
  107. }
  108. }
  109. }
  110. function computeSpeed(){
  111. // 0,151*PUISSANCE((A18+1);2) +3,0001717*(A18+1)+0,007557
  112. }
  113. /////////////////////// SCRIPT FUNCTIONS ///////////////////////////////
  114. function init() {
  115. //loadFixtures();
  116. script.log("VISCA module loaded");
  117. script.setUpdateRate(2);
  118. }
  119. function update(deltaTime){
  120. updateZoomPosition();
  121. computeSpeed();
  122. // script.log('update');
  123. }
  124. var buffer=[];
  125. var bufferIndex = 0 ;
  126. function dataReceived(data)
  127. {
  128. //If mode is "Lines", you can expect data to be a single line String
  129. // script.log("Data received : " +data);
  130. //If mode is anything else, you can expect data to be an array of bytes
  131. // script.log(" Bytes received : "+data.length);
  132. // for(var i=0; i < data.length; i++)
  133. // {
  134. // script.log(" > " + parseInt((""+data[i]),16));
  135. // }
  136. //fill buffer with bytes until you find a 255
  137. for (var i = 0 ; i<data.length ; i++){
  138. if (data[i]!=255){
  139. buffer[bufferIndex]=data[i];
  140. bufferIndex += 1;
  141. }
  142. else{
  143. if(buffer[0]==144){ //0x90, you've got a message
  144. if (buffer[1]==65){script.log("received command");} //0x41
  145. else if (buffer[1]==81){script.log("complete command");waitTaskComplete=0;} //0x51
  146. else if (buffer[1]==96){script.log("syntax error");} //0x60
  147. else if (buffer[1]==97){//0x61
  148. var msg = "error :";
  149. if(buffer[2]==1){msg=msg+"message length";}
  150. else if(buffer[2]==2){msg=msg+"syntax";}
  151. else if(buffer[2]==3){msg=msg+"command buffer full";}
  152. else if(buffer[2]==4){msg=msg+"command cancelled";}
  153. else if(buffer[2]==5){msg=msg+"no socket";}
  154. else if(buffer[2]==65){msg=msg+"command not executable";}
  155. else{
  156. msg=msg+"unknow error";
  157. for(var j=2; j < buffer.length; j++)
  158. {
  159. script.log(" > " +data[i]);
  160. }
  161. }
  162. script.log(msg);
  163. }
  164. //inquiry answers
  165. else if (buffer[1]==80){//0x50
  166. if(currentInquiry == 'CAM_ZoomPosInq'){
  167. var position = buffer[2]*4096 + buffer[3]*256 + buffer[4]*16 + buffer[5];
  168. // printArray("zoom position :", buffer);
  169. // script.log(position);
  170. ZoomPos.set(position/16384.);
  171. ZoomPosLastUpdate.set(util.getTime());
  172. }
  173. //free inquiry slot
  174. currentInquiry = '';
  175. }
  176. else{printArray("unknown answer :", buffer);}
  177. }
  178. bufferIndex = 0;
  179. buffer=[];
  180. }
  181. }
  182. //then process buffer
  183. }