Programming a Simple Servo Motor in C# / WPF

CPSC581.PhidgetSimpleServo History

Show minor edits - Show changes to markup

Changed lines 3-4 from:
to:

Download Source code for this example

Changed line 25 from:
  1. Declare your phidget
to:

Declare your phidget

Changed line 32 from:
  1. Initialize your phidget byhaving it look for the Attach event and then opening it
to:

Initialize your phidget byhaving it look for the Attach event and then opening it

Changed line 44 from:
  1. In the Attach event, add the various event handlers, set parameters and activate UI controls as needed
to:

In the Attach event, add the various event handlers, set parameters and activate UI controls as needed

Changed line 65 from:
  1. In the Detach event, remove the various event handlers and deactivate UI controls as needed
to:

In the Detach event, remove the various event handlers and deactivate UI controls as needed

Changed line 81 from:
  1. Handle errors, although usually there is no need to do anything
to:

Handle errors, although usually there is no need to do anything

Changed line 93 from:
  1. ''Handle Phidget-specific callbacks
to:

'Handle Phidget-specific callbacks

Changed line 105 from:
  1. Handle callbacks for UI elements that control phidgets
to:

Handle callbacks for UI elements that control phidgets

Changed lines 117-118 from:
  1. Utility function for showing feedback, in this case just printing in the title bar
to:

Utility function for showing feedback, in this case just printing in the title bar

Added line 3:
Changed lines 7-8 from:
  • Have a Phidget Servo ready (i.e., the phidget servo board with connected servo motor, and a USB cable
to:
  • Have a Phidget Servo ready (i.e., the phidget servo board with connected servo motor, and a USB cable)
Changed lines 19-21 from:

Common next steps but illustrated with an example of a servo.

  1. ''Declare your phidget
to:

Create your GUI

- a slider used to control the position of the servo motor - a small texblock next to it (the textblock will contain the position of the servo)

Common order of activities, illustrated with an example of a servo.

  1. Declare your phidget

(:source lang=csharp tabwidth=2 :) public partial class MainWindow : Window { Phidgets.Servo servo = new Phidgets.Servo(); // The basic servo object that controls the actual hardware

  1. Initialize your phidget byhaving it look for the Attach event and then opening it

(:source lang=csharp tabwidth=2 :) public MainWindow() { InitializeComponent(); this.slider1.IsEnabled = false; // We don't want the slider operable until the servo is plugged in servo.Attach += new AttachEventHandler(servo_Attach); // Invoked when the servo is actually plugged in (Attached) servo.open(); }

  1. In the Attach event, add the various event handlers, set parameters and activate UI controls as needed

(:source lang=csharp tabwidth=2 :) // The servo is attached. Add callbacks, enable the slider, and set it all to position 0 void servo_Attach(object sender, AttachEventArgs e) { this.Dispatcher.Invoke(new Action(delegate() { // If you want to look for a particular servo, do something like this. Note the 283 is a device-specific serial number // if (servo.SerialNumber != 283) return; printMessage("servo " + servo.SerialNumber + " is attached"); servo.Detach += new DetachEventHandler(servo_Detach); // generic phdiget callbacks servo.Error += new ErrorEventHandler(servo_Error); servo.PositionChange += new PositionChangeEventHandler(servo_PositionChange); // servo-specific callbacks servo.servos[0].Position = 0; slider1.Value = 0; this.slider1.IsEnabled = true; })); }

  1. In the Detach event, remove the various event handlers and deactivate UI controls as needed

(:source lang=csharp tabwidth=2 :) // The servo is detached. Disable the slider and remove callbacks void servo_Detach(object sender, DetachEventArgs e) { this.Dispatcher.Invoke(new Action(delegate() { printMessage("servo Detached"); this.slider1.IsEnabled = false; servo.Detach -= new DetachEventHandler(servo_Detach); servo.Error -= new ErrorEventHandler(servo_Error); servo.PositionChange -= new PositionChangeEventHandler(servo_PositionChange); })); }

  1. Handle errors, although usually there is no need to do anything

(:source lang=csharp tabwidth=2 :) // Some random error was generated. void servo_Error(object sender, ErrorEventArgs e) { this.Dispatcher.Invoke(new Action(delegate() { printMessage("servo Error: " + e.Description); })); }

  1. ''Handle Phidget-specific callbacks

(:source lang=csharp tabwidth=2 :) // The servo motor position has been changed. Show its new position void servo_PositionChange(object sender, PositionChangeEventArgs e) { this.Dispatcher.Invoke(new Action(delegate() { this.textBlock1.Text = e.Position.ToString(); ; })); }

  1. Handle callbacks for UI elements that control phidgets

(:source lang=csharp tabwidth=2 :) // Change the servo position so it corresponds to the slider value private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { this.Dispatcher.Invoke(new Action(delegate() { servo.servos[0].Position = slider1.Value; })); }

  1. Utility function for showing feedback, in this case just printing in the title bar

(:source lang=csharp tabwidth=2 :) // Put some feedback in the title bar private void printMessage(string msg) { this.Title = msg; }

Changed lines 9-10 from:
  1. Start visual studio in the usual way and create a C# / WPF project.
  2. Add a reference to the Phidgets dll library by:
to:
  1. Start visual studio in the usual way and create a C# / WPF project.
  2. Add a reference to the Phidgets dll library by:
Changed lines 14-16 from:
  1. Add the following Using statements to the top of your code using Phidgets; using Phidgets.Events;
to:
  1. Add Using statements to the top of your code
    • using Phidgets;
    • using Phidgets.Events;

Common next steps but illustrated with an example of a servo.

  1. ''Declare your phidget
Added lines 1-16:

(:title Programming a Simple Servo Motor in C# / WPF :)

Before you begin

  • Make sure the Phidgets software is correctly installed
  • Start the Phidgets server via the control panel. If a phidget is plugged in, can you see it?
  • Have a Phidget Servo ready (i.e., the phidget servo board with connected servo motor, and a USB cable

Common steps to all Phidget programming.

  1. Start visual studio in the usual way and create a C# / WPF project.
  2. Add a reference to the Phidgets dll library by:
    • choose Add References from the context menu on the References icon in your solution explorer
    • select the Browse tab from the Add Reference dialog box
    • browse to C:\\Program Files\Phidgets and select Phidgets21.Net.dll
  3. Add the following Using statements to the top of your code using Phidgets; using Phidgets.Events;