private void ServoBoard_Attach(object sender, AttachEventArgs e) { for (int i = 0; i < _servoBoard.servos.Count; i++) { if (_servoCount == 0) { SetServoProperties(i, _servos[i].ServoType, _servos[i].ServoMinPulseWidth, _servos[i].ServoMaxPulseWidth, _servos[i].ServoMaxVelocity, _servos[i].ServoDegrees); _servos[i].MinAcceleration = _servoBoard.servos[i].AccelerationMin; _servos[i].MaxAcceleration = _servoBoard.servos[i].AccelerationMax; _servos[i].Acceleration = _servos[i].MaxAcceleration; _servos[i].MinVelocity = _servoBoard.servos[i].VelocityMin; _servos[i].MaxVelocity = _servoBoard.servos[i].VelocityMax; _servos[i].VelocityLimit = _servos[i].MaxVelocity; RegisterBindings(i); } _servoBoard.servos[i].VelocityLimit = _servos[i].VelocityLimit; _servoBoard.servos[i].Acceleration = _servos[i].Acceleration < _servoBoard.servos[i].AccelerationMax ? _servos[i].Acceleration : _servoBoard.servos[i].AccelerationMax;; _servos[i].Calibration.OutputLimitMax = _servoBoard.servos[i].PositionMax; _servos[i].Calibration.OutputLimitMin = _servoBoard.servos[i].PositionMin; } if (_servoCount == 0) { _servoCount = _servoBoard.servos.Count; ConfigManager.LogManager.LogDebug("Detaching phidget servo board after config. (SerialNumber=\"" + _serialNumber + "\")"); _servoBoard.close(); _servoBoard = null; } }
//When the application is terminating, close the Phidget. private void Form1_FormClosed(object sender, FormClosedEventArgs e) { advServo.Attach -= new AttachEventHandler(advServo_Attach); advServo.Detach -= new DetachEventHandler(advServo_Detach); advServo.Error -= new ErrorEventHandler(advServo_Error); advServo.CurrentChange -= new CurrentChangeEventHandler(advServo_CurrentChange); advServo.PositionChange -= new PositionChangeEventHandler(advServo_PositionChange); advServo.VelocityChange -= new VelocityChangeEventHandler(advServo_VelocityChange); //run any events in the message queue - otherwise close will hang if there are any outstanding events Application.DoEvents(); advServo.close(); advServo = null; }
static void Main(string[] args) { //Declare an Advanced Servo object AdvancedServo advServo = new AdvancedServo(); //Hook the basic event handlers advServo.Attach += new AttachEventHandler(advServo_Attach); advServo.Detach += new DetachEventHandler(advServo_Detach); advServo.Error += new ErrorEventHandler(advServo_Error); //hook the phidget specific event handlers //I decided to leave out the current change event handler for readability. advServo.PositionChange += new PositionChangeEventHandler (advServo_PositionChange); advServo.VelocityChange += new VelocityChangeEventHandler (advServo_VelocityChange); //open the Servo object for device connections advServo.open(); //Get the program to wait for an Advanced Servo to be attached Console.WriteLine("Waiting for Advanced Servo to be attached..."); advServo.waitForAttachment(); //Set the initial position for the servo motor. I set it to 15 here just //since I am going to cycle through the positive values to show a basic //full movement if (advServo.Attached) { advServo.servos[0].Acceleration = 1000.00; //max 4590, min 0 advServo.servos[0].VelocityLimit = 1000.00; //max 1500, min 0 advServo.servos[0].Position = 30.00; //range is -23 to 232 } //Wait for the motor to finish getting to position and let user continue Console.WriteLine("Press any key to continue..."); Console.Read(); //Move the motor from position value 30 to 200, we sleep for 500 //milliseconds to give the motor enough time to move to the set position if (advServo.Attached) { advServo.servos[0].Position = 200; //range is -23 to 232 Thread.Sleep(500); } //Wait for the events to fire and display, user input will continue the //program Console.WriteLine("Press any key to end..."); Console.Read(); //user input was read so we can terminate the program now, close the //AdvancedServo object. Left out the current change event handler for //readability. advServo.PositionChange -= new PositionChangeEventHandler (advServo_PositionChange); advServo.VelocityChange -= new VelocityChangeEventHandler (advServo_VelocityChange); //user input was read so we can terminate the program now, close the //AdvancedServo object advServo.close(); //set the object to null to get it out of memory advServo = null; Console.WriteLine("ok"); }