From iLab Cookbook

Toolkits: ExampleUsingASliderToControlAServo

<< Back to the SharedPhidgets page


What you will learn in this tutorial:


Download source: SharedPhidgetsExample_SliderServo.zip
The zip file contains the program code of this example.

Overview

The Phidget Servo
The Phidget Servo
  1. Attach the devices
  2. Create a new Windows C# application in VisualStudio.NET
  3. Use the ConnectionManager for connecting to the server
  4. Create a servo object
  5. Create a interfacekit object
  6. Receiving sensor inputs to control the servo
  7. Finished! Compile the application!


Step-by-step in detail

1) To any of the computers with a running "SharedPhidgets Connector" software, attach a servo and an interface kit. Use the Controller GUI tool to discover the serial number of these devices. To one of the sensor inputs of the interfacekit attach a sensor, e.g. the slider sensor.

2) Open VisualStudio.NET, click New Project, select a new C# Windows application, enter the name of the project (SharedPhidgetsExample_SliderServo) and confirm the dialog.

3) Now we click on the SharedPhidgets toolbox on the left side of the VisualStudio window, select the ConnectionManager object and click on your Windows form.

Then change the SharedDictionaryURL property of the Connection Manager to the address of the server:

4) Drag a SharedPhidget Servo component on your form

...click on the FilterSerialNumber property in the Properties explorer of Visual Studio...

...and specify the serial number of the servo. If you have only one servo at your infrastructure, you don't need to add the serial number because the component will then connect to the only component it founds.

5) Create a interfacekit object on your form:

...click on the FilterSerialNumber property in the Properties explorer of Visual Studio...

...and specify the serial number of the interfacekit. If you have only one interfacekit at your infrastructure, you don't need to add the serial number because the component will then connect to the only component it founds.

6) Create an event handler for the SensorChange event of the interfacekit: select the interfacekit1 object, select the event view in the properties panel and double click in the SensorChange field.

Add the following code to the callback method for the SensorChange event (Note: you have to change the port number of your sensor; just change the integer in the line e.Index = X to your sensor port of the interfacekit where the slider is connected):

private void interfaceKit1_SensorChange(object sender, GroupLab.SharedPhidgets.IndexSensorChangeEventArgs e)
{
  if(e.Index == 2)
  {
    if(this.servo1.Attached)
    {
      this.servo1.Motors[0].Position = (float)( (180.0 / 1000.0) * (float)e.Value );
    }
  }
}

6) Compile your application:

When the application is started, you can now use the interfacekit slider (or any other sensor you have connected) to control the servo.


That's it,

you can now modify this application and enhance the functionality. Try to listen for other sensor changes of the interfacekit, and display these events for example with a connected TextLC display.


Retrieved from http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/ExampleUsingASliderToControlAServo
Page last modified on February 01, 2006, at 06:59 PM