Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. using System;
  2. using Ventuz.Kernel;
  3. using Phidget22;
  4. using Phidget22.Events;
  5.  
  6.  
  7. public class Script : ScriptBase, System.IDisposable
  8. {
  9.  
  10. // This member is used by the Validate() method to indicate
  11. // whether the Generate() method should return true or false
  12. // during its next execution.
  13. private bool changed;
  14.  
  15. Encoder encoderOne;
  16. DigitalInput digitalInput;
  17.  
  18. float _encoderValue;
  19.  
  20. // This Method is called if the component is loaded/created.
  21. public Script()
  22. {
  23. // Note: Accessing input or output properties from this method
  24. // will have no effect as they have not been allocated yet.
  25. }
  26.  
  27. // This Method is called if the component is unloaded/disposed
  28. public virtual void Dispose()
  29. {
  30. }
  31.  
  32. // This Method is called if an input property has changed its value
  33. public override void Validate()
  34. {
  35. // Remember: set changed to true if any of the output
  36. // properties has been changed, see Generate()
  37.  
  38. if(encoderOne != null && encoderOne.Attached){
  39. {
  40. d = _encoderValue;
  41. }
  42.  
  43. }
  44. }
  45.  
  46. // This Method is called every time before a frame is rendered.
  47. // Return value: if true, Ventuz will notify all nodes bound to this
  48. // script node that one of the script's outputs has a
  49. // new value and they therefore need to validate. For
  50. // performance reasons, only return true if output
  51. // values really have been changed.
  52. public override bool Generate()
  53. {
  54. if (changed)
  55. {
  56. changed = false;
  57. return true;
  58. }
  59.  
  60. return false;
  61. }
  62.  
  63.  
  64. // Phidgets Events
  65. void onAttach(object sender, AttachEventArgs e)
  66. {
  67. VLog.Info("Phidget with name:" + encoderOne.DeviceName + "is Attached!");
  68.  
  69.  
  70. }
  71.  
  72. void onVoltageRatioChange(object sender, Phidget22.Events.EncoderPositionChangeEventArgs e)
  73. {
  74.  
  75. VLog.Info("Velocity: " + e.PositionChange);
  76. _encoderValue = e.PositionChange;
  77.  
  78. }
  79.  
  80. void bridgeVoltageInput_error(object sender, Phidget22.Events.ErrorEventArgs e)
  81. {
  82. VLog.Info("DCMotor Error: " + e.Description);
  83.  
  84. }
  85.  
  86. void OnDetach(object sender, DetachEventArgs e)
  87. {
  88. VLog.Info("Phidget with name:" + encoderOne.DeviceName + "is not Attached!!");
  89.  
  90. }
  91.  
  92. // Digital Input
  93. void OnDigitalInputAttached(object sender, AttachEventArgs e)
  94. {
  95. VLog.Info("Phidget with name:" + digitalInput.DeviceName + "is Attached!");
  96.  
  97. }
  98.  
  99. void OnDigitalInputDettached(object sender, DetachEventArgs e)
  100. {
  101. VLog.Info("Phidget with name:" + digitalInput.DeviceName + "is not Attached!!");
  102.  
  103. }
  104.  
  105.  
  106. void digitalInput_error(object sender, Phidget22.Events.ErrorEventArgs e)
  107. {
  108. VLog.Info("DigitalInput Error: " + e.Description);
  109.  
  110. }
  111.  
  112. void OnDigitalInputProprtyChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
  113. {
  114. VLog.Info("digital Input Changed: " + e.State);
  115.  
  116. }
  117.  
  118. // This Method is called if the function/method initEncoder is invoked by the user or a bound event.
  119. // Return true, if this component has to be revalidated!
  120. public bool OninitEncoder(int arg)
  121. {
  122.  
  123. InitEncoder();
  124. return false;
  125. }
  126.  
  127. void InitEncoder()
  128. {
  129. if (encoderOne != null)
  130. return;
  131.  
  132. encoderOne = new Encoder();
  133.  
  134. // Encoder One
  135. encoderOne.Attach += onAttach;
  136. encoderOne.Detach += OnDetach;
  137. encoderOne.Error += bridgeVoltageInput_error;
  138.  
  139. encoderOne.PositionChange += onVoltageRatioChange;
  140.  
  141.  
  142.  
  143. encoderOne.Open();
  144. }
  145.  
  146. // This Method is called if the function/method initDigitalInput is invoked by the user or a bound event.
  147. // Return true, if this component has to be revalidated!
  148. public bool OninitDigitalInput(int arg)
  149. {
  150. initDigitalInput();
  151. return false;
  152. }
  153.  
  154. void initDigitalInput()
  155. {
  156. if (digitalInput != null)
  157. return;
  158.  
  159. digitalInput = new DigitalInput();
  160.  
  161.  
  162. digitalInput.Attach += OnDigitalInputAttached;
  163. digitalInput.Detach += OnDigitalInputDettached;
  164. digitalInput.Error += digitalInput_error;
  165. digitalInput.StateChange += OnDigitalInputProprtyChange;
  166.  
  167.  
  168. digitalInput.Open();
  169.  
  170. }
  171.  
  172. // This Method is called if the function/method DeinitEncoder is invoked by the user or a bound event.
  173. // Return true, if this component has to be revalidated!
  174. public bool OnDeinitEncoder(int arg)
  175. {
  176. if (encoderOne != null)
  177. encoderOne.Close();
  178.  
  179. encoderOne = null;
  180. return false;
  181. }
  182.  
  183. // This Method is called if the function/method deInitDigitalInput is invoked by the user or a bound event.
  184. // Return true, if this component has to be revalidated!
  185. public bool OndeInitDigitalInput(int arg)
  186. {
  187. if (digitalInput != null)
  188. digitalInput.Close();
  189.  
  190. digitalInput = null;
  191. return false;
  192. }
  193.  
  194.  
  195.  
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement