Advertisement
Guest User

Untitled

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