Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Ventuz.Kernel;
- using Phidget22;
- using Phidget22.Events;
- public class Script : ScriptBase, System.IDisposable
- {
- // This member is used by the Validate() method to indicate
- // whether the Generate() method should return true or false
- // during its next execution.
- private bool changed;
- Encoder encoderOne;
- DigitalInput digitalInput;
- float _encoderValue;
- // This Method is called if the component is loaded/created.
- public Script()
- {
- // Note: Accessing input or output properties from this method
- // will have no effect as they have not been allocated yet.
- }
- // This Method is called if the component is unloaded/disposed
- public virtual void Dispose()
- {
- }
- // This Method is called if an input property has changed its value
- public override void Validate()
- {
- // Remember: set changed to true if any of the output
- // properties has been changed, see Generate()
- if(encoderOne != null && encoderOne.Attached){
- {
- d = _encoderValue;
- }
- }
- }
- // This Method is called every time before a frame is rendered.
- // Return value: if true, Ventuz will notify all nodes bound to this
- // script node that one of the script's outputs has a
- // new value and they therefore need to validate. For
- // performance reasons, only return true if output
- // values really have been changed.
- public override bool Generate()
- {
- if (changed)
- {
- changed = false;
- return true;
- }
- return false;
- }
- // Phidgets Events
- void onAttach(object sender, AttachEventArgs e)
- {
- VLog.Info("Phidget with name:" + encoderOne.DeviceName + "is Attached!");
- }
- void onVoltageRatioChange(object sender, Phidget22.Events.EncoderPositionChangeEventArgs e)
- {
- VLog.Info("Velocity: " + e.PositionChange);
- _encoderValue = e.PositionChange;
- }
- void bridgeVoltageInput_error(object sender, Phidget22.Events.ErrorEventArgs e)
- {
- VLog.Info("DCMotor Error: " + e.Description);
- }
- void OnDetach(object sender, DetachEventArgs e)
- {
- VLog.Info("Phidget with name:" + encoderOne.DeviceName + "is not Attached!!");
- }
- // Digital Input
- void OnDigitalInputAttached(object sender, AttachEventArgs e)
- {
- VLog.Info("Phidget with name:" + digitalInput.DeviceName + "is Attached!");
- }
- void OnDigitalInputDettached(object sender, DetachEventArgs e)
- {
- VLog.Info("Phidget with name:" + digitalInput.DeviceName + "is not Attached!!");
- }
- void digitalInput_error(object sender, Phidget22.Events.ErrorEventArgs e)
- {
- VLog.Info("DigitalInput Error: " + e.Description);
- }
- void OnDigitalInputProprtyChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
- {
- VLog.Info("digital Input Changed: " + e.State);
- }
- // This Method is called if the function/method initEncoder is invoked by the user or a bound event.
- // Return true, if this component has to be revalidated!
- public bool OninitEncoder(int arg)
- {
- InitEncoder();
- return false;
- }
- void InitEncoder()
- {
- if (encoderOne != null)
- return;
- encoderOne = new Encoder();
- // Encoder One
- encoderOne.Attach += onAttach;
- encoderOne.Detach += OnDetach;
- encoderOne.Error += bridgeVoltageInput_error;
- encoderOne.PositionChange += onVoltageRatioChange;
- encoderOne.Open();
- }
- // This Method is called if the function/method initDigitalInput is invoked by the user or a bound event.
- // Return true, if this component has to be revalidated!
- public bool OninitDigitalInput(int arg)
- {
- initDigitalInput();
- return false;
- }
- void initDigitalInput()
- {
- if (digitalInput != null)
- return;
- digitalInput = new DigitalInput();
- digitalInput.Attach += OnDigitalInputAttached;
- digitalInput.Detach += OnDigitalInputDettached;
- digitalInput.Error += digitalInput_error;
- digitalInput.StateChange += OnDigitalInputProprtyChange;
- digitalInput.Open();
- }
- // This Method is called if the function/method DeinitEncoder is invoked by the user or a bound event.
- // Return true, if this component has to be revalidated!
- public bool OnDeinitEncoder(int arg)
- {
- if (encoderOne != null)
- encoderOne.Close();
- encoderOne = null;
- return false;
- }
- // This Method is called if the function/method deInitDigitalInput is invoked by the user or a bound event.
- // Return true, if this component has to be revalidated!
- public bool OndeInitDigitalInput(int arg)
- {
- if (digitalInput != null)
- digitalInput.Close();
- digitalInput = null;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement