.Net Gadgeteer, REST, Cameras, Servos and Windows Phone 7
Today's Hardware Friday post lets us play with some of our favorite things, .Net Gadgeteer, Servos, REST, Cameras and Windows Phone 7...
The Gadgeteer.Interfaces.PWMOutput interface can support the use of a servo to move mechanical parts of a device as needed by applications. This example uses the Gadgeteer.Interfaces.PWMOutput interface and a servomechanism to turn a camera through an arc of about 140 degrees. The Pulse Width Modulation (PWM) input for this servo is obtained from a WCF REST Web service running on a remote IIS Server. The user can direct the camera to take pictures over its range of motion from any device that can send a Web request to the service. After the camera takes a picture, the device generates a POST request to upload the picture to the Web service so it is accessible remotely. The post: Controlling the Servo using a Windows Phone Application describes a client application for the device and Web service.
The core of this application is the code that polls the Web Service to get the PWM number that controls the servo. The number is a percentage of the delta between the top and bottom PWM settings, an unsigned integer that indicates a percentage of the range: 1% - 100%. The Web service returns the number to the application to be used in the SetPulse method of the Gadgeteer.Interfaces.PWMOutput interface.
Wiring the servo requires connecting the input wire to a pin on a P socket that supports pulse width modulation. The external power for the servo motor has to come from an external battery or transformer that supplies about 5 volts DC. The socket connections can be wired using the GHI Electronics Extender Module.
pulse = low + (delta * percent / 100);
Here is a schematic of the servo-control circuit from the GHI Electronics Extender module. Following the schematic, a photo shows the wiring. Only pin 9 and the ground on pin 10 are needed for the PWM circuit.
Implementing a REST Web service can be done with the WCF REST Web Service Template 4.0. For more information on using the template project, see REST Web Service to Record Data from a .NET Gadgeteer Sensor Device and Remote Control of .NET Gadgeteer Device via REST Web Service.
The Web service code that receives the picture from the camera module is shown in the following blocks. Implementing the Web service to manage state information was easy, but getting the bitmap data into a format that works on this service took some discussion. Marco Minerva is skilled in this area and provided some of this code in a discussion that you can follow on an MSDN WCF forum thread.
First the classes to store data and maintain state:
The WCF REST Web service template 4.0 works fine for the GET and for the POST methods.
The Web.config file for the Web service follows.
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </modules> </system.webServer> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> <standardEndpoints> <webHttpEndpoint> <!-- Configure the WCF REST service base address via the global.asax.cs file and the default endpoint via the attributes on the <standardEndpoint> element below --> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="4194304" transferMode="Buffered" /> </webHttpEndpoint> </standardEndpoints> </system.serviceModel> </configuration>
For more information about the WCF Web services, see the forum discussion:
In the post Using a Servo in a .NET Gadgeteer Camera Device we saw how to create a .NET Gadgeteer application that uses a Servo to change the direction of the Camera, takes a photo and uploads it to a Web Service. Now, we’ll realize a Windows Phone application to remotely control the camera and show the pictures.
Let’s create a standard Windows Phone Application, named CameraControl, targeting OS version 7.1 (Mango). We use too Silverlight for Windows Phone Toolkit, that can be downloaded from CodePlex site. The user interface is very simple and consists of an area where the last taken photo is shown and a slider with which we can control the direction of the camera:
The application is developed using the MVVM pattern. We have only one ViewModel, called MainViewModel. It uses the REST Web Service already described in the post Using a Servo in a .NET Gadgeteer Camera Device: in particular, the method to set the state of the servo and the one to get the last taken picture. Note that, to set the servo state, we need to send to the service a number that is a percentage of the delta between the top and bottom PWM settings. It’s an unsigned integer that indicates a percentage of the range: 1% – 100%. Communications are realized using RestSharp, a library that simplifies the process of making requests and processing REST responses in an asynchronous way.
And of course the source is available for download...
This conversation has been locked by the site admins. No new comments can be made.