Friday, March 23, 2012

Writing Web Services


Web Services, unlike other ASP.NET files, have .asmx extensions. They are essentially all code with little or no user interface.
Each Web unlike Service can be discovered from a remote server via a .DISCO file, which is on the server containing the Web Service. This .DISCO has all the information necessary to enable the remote server to find out about available Web Services and how to use them.
Web Services make available a lot of information about themselves. They use the service description language (SDL) to do so. Although SDL is what .NET is using now, it is moving to Service Control Language (SCL) very soon. This is a more expressive version of the SDL. It will probably replace SDL by the final release of .NET.
Now I would like to show you examples of what I have been talking about. The first thing I would like you to take a look at is the contents of a discovery file as follows:

<?xml version="1.0" ?>

<dynamicDiscovery

  xmlns="urn:schemas-dynamicdiscovery:disco.2000-03-17">

<exclude path="_vti_cnf" />

</dynamicDiscovery>
The next thing you should look at, just so you can familiarize yourself with it, is an SDL file. The following is a simple SDL file from a Web Service named HelloWorld.


<?xml version="1.0"?>

<serviceDescription>

  <soap >

    <service>

      <addresses>

        <address uri="http://localhost//HelloWorld.asmx"/>

      </addresses>

      <requestResponse name="SayHelloWorld" soapAction= "http://tempuri.org/

graphics/ccc.gifSayHelloWorld">

        <request ref="s0:SayHelloWorld"/>

        <response ref="s0:SayHelloWorldResult"/>

      </requestResponse>

    </service>

  </soap>

</serviceDescription>
Finally, I would like to show you the SOAP payload that a simple Web Service might return. In the following example the SOAP envelope contains a credit card number and a credit card PIN.

<?xml version="1.0"?>

    <Envelope>

        <Body>

           <GetCCInfo>

            <CCNum>12345</CCNum>

        <CCPin>1234</CCPin>

       </GetCCInfo>

    </Body>

</Envelope>
Now, I am going to walk through the creation of a simple Web Service. Start by running Visual Studio.NET. Go to the file menu, select New Project, and set up for a C# Web Service project.
VisualStudio.NET creates the project and all its support files. If you open the project's default .asmx (which is usually named WebServicex.asmx) file and then right-click on it to view the code, you can see the boilerplate code for the Web Service. In this code you can find a HelloWorld() method. The HelloWorld() method gives you an idea of how to create methods for Web Services.
The HelloWorld() method is actually remarked out when the code is first created. If you unremarked it out you could make the following observations. First, there is a Web method attribute above it. This is all that is necessary to make any method in a Web Service implement SOAP to communicate across the Internet. The rest of the method looks pretty standard. It is declared as public. It might or might not have a return type and it might or might not have parameters.
I am going to create a Web Service for this discussion that will have a method that multiplies two numbers. When I create the project I will open up the multiply.asmx and find the HelloWorld() method. Next I will remove the HelloWorld() method and create my own method called multiply.
The multiply method takes two parameters and they are both integers. I named the first parameter nFirst and the second nSecond. The multiply method returns an integer, and this integer will be the result of multiplying the first and second parameters. You can see this Web method (marked with a WebMethod attribute) in the following code:

[WebMethod]

public int Multiply(int nFirst, int nSecond)

{

    return( nFirst * nSecond );

}
One of the problems with writing Web Services is that there is no user interface—this means you can't run them and see anything. Visual Studio.NET takes care of this for you, and gives you a way to actually invoke Web Services. By right-clicking on the default .asmx file you can run the Web Service in a browser. Before doing this, though, you must build it. What happens when the browser first runs is that it queries the Web Service and finds out what the parameters are. So when it examines my multiply Web Service it finds that the Web Service takes two parameters, both of them integers, and returns an integer. Visual Studio .NET actually creates a user interface for you in HTML so that you can type in the parameters and then invoke your Web Service.
When you invoke the service what you get back is XML. This XML is the result of invoking the Web Service. For instance, if my first integer were 5 and my second integer were 6, the resulting answer would be 30. 

Wednesday, March 21, 2012

WEB SERVICES


What Is a Web Service?
What would you use a Web Service for? Well, that is a fair enough question. Web Services are used to program the Web. Web Services represents a way to abstract a Universal Resource Identifier (URI) and access it across the Web on another server. If this doesn't seem clear, keep reading and you will understand it as I explain and talk about Web Services.
The Simple Object Application Protocol (SOAP) is what Web Services use to communicate across the Internet.
What Is SOAP?
SOAP is used to send messages across the wire. One computer can use SOAP to send a block of data—say, for instance, a billing record—to another computer. When using SOAP, both computers understand the protocol, and can send and receive the data.
SOAP (Simple Object Access Protocol) starts with simple, and that's one of the key things it delivers. If you've ever worked with DCOM to communicate to remote servers, you know that DCOM is anything but simple—as a matter of fact, it's pretty ugly.
SOAP is based on XML. The data that is carried in a SOAP package is always represented by XML. If you plan to do much with SOAP, you may want to take a look at the XML specification or do some reading and understand the basics of XML.
SOAP works with any operating system because it doesn't rely in any way on operating systems. It's a protocol on its own merit that dictates no hardware, operating system, or language specifications.
SOAP is also built on standards, and therefore can easily be implemented by anyone. First, it relies on HTTP to send data across the wire. Second, it relies on XML to represent the data. And third, it relies on the SOAP specification so that every implementer will be in compliance and able to communicate with all other implementers. You can get the SOAP specification at http://msdn.microsoft.com/workshop/xml/general/soapspec.asp.
SOAP Packages
SOAP data is sent in a well-organized manner as it goes across the Internet. This section shows you the hierarchy of SOAP packages.
Because all SOAP packages are sent within an HTTP message, the first part of a SOAP message is the HTTP header. Information in the section usually contains the domain name, the keyword POST, the Content-Type specification, and other optional information.
After the HTTP header comes the SOAP envelope. This encompasses the entire SOAP message. It's kind of like what's inside the box that arrives at your doorstep. It's all the important stuff you need (the actual data). The address label on the front of the box (the HTTP header) can almost always be discarded.
The SOAP envelope starts off with a SOAP header. This is different than the HTTP header. It doesn't contain information about the routing of the data (such as the domain name and the size of the content), but contains information related to the data, such as a transaction ID.
Why Is SOAP Important for Web Services?
Using SOAP for Web Services makes Web Services easy to use and powerful. SOAP enables Web Services to communicate across the Internet without having to worry about what operating system they are talking to and what language the other site is using.
If you have every programmed with VB5 or VB6 you will remember how easy it was to use an object that was registered or installed on a system. It was a matter of declaring the object and "newing" it as the following code shows:

MyObject Obj = new MyObject

Obj.DoSomething()
As you can see, after I created my object it was a simple matter to make method calls on that object. Wouldn't it be nice to have a programming model this simple for calling methods across the Web? Of course—and that's what Web Services offer. The same type of simple object programming model that VB offered is now available using Web Services. You simply create a service object and call methods on it, as follows:

MyService Ser = new MyService();

Ser.DoSomething();
As I said at the beginning of this chapter, Web Services might change how you develop and build Web applications. After seeing this new simplified—yet powerful—programming model, you are going tohave to agree that Web Services open things up for you unlike any other technology that has been introduced in recent years.
Why Are Web Services Useful?
Imagine you have some sort of an e-commerce Web site, and you have a partner that provides you with content. Or you might provide your partner with some sort of content or you have some other interaction. Web Services give you an easy way to integrate with partners. I will give some examples of this shortly.
Web Services are very easy to program. They are also based on standards. The fundamental standard on which they are based is HTTP; all the SOAP information uses this protocol as its basis. Then, the data is contained within a SOAP envelope, which is also a standard that has been submitted to the WC3. Within the SOAP envelope is XML. And XML is the standard that is becoming very popular and widely adopted for Internet data representation.
Another reason you would want to implement Web Services is because they can be easily upgraded on one server without affecting the applications that call them. If server A contains a Web Service, I can easily change it as long as I don't change the calling and return parameters. And I can do this without any knowledge or effect on server B, which calls methods in my Web Service on server A.
Real-World Scenarios for Using Web Services
I would like to talk for a minute about when you would use a Web Service. Although I can think of many examples, I'll cover just a few here. I'm sure that with a little effort you can come up with your own.
Suppose I have a Web site on which I want to serve up news content to users. The news content comes from a server located somewhere else on the Internet. A Web Service provides the easiest programming model for a Web application to retrieve and consume from across the Web.
Although I have carried out tasks before that pull in data and content from other Web servers, it hasn't always been easy. It has taken me several days to do this, then several more days to debug the material I received, and making changes was sometimes difficult. In the end, this process might have taken me anywhere from four to five days of programming. With a Web Service I can consume content from a remote server in about two hours.
Credit card authorization of course comes to mind. A Web Service makes the ideal method of doing credit card authorization. From one server you can easily hit another server and authorize a credit card with just several lines of programming code.
Centralization of user information is another option that Web Services provide. You might have a user who has a set of information that is associated with that user. A Web Service can enable multiple Web Services to retrieve and use information about a given user. Of course this assumes that there is proper security clearance and the proper authorization credentials.
Web Services enable you to create a fantastic ad server. Imagine being able to provide user information, such as a user's preferences, and an ad server serves up a targeted ad toward that particular user. Say, for instance, that I am on a Web site and somehow I have let it be known that I like golf. As a matter of fact, I may have indicated on the Web site that I am looking for a new set of clubs. Rather than just rotate through a bunch of ads, a Web application can call a Web Service and let the Web Service know that the user likes or has an interest in golf, and the Web Service serves up a targeted ad with golf clubs or golf accessories.

Monday, March 12, 2012

DC Motor interfacing with L293D H-Bridge


L293D is a dual H-Bridge motor driver, So with one IC we can interface two DC motors which can be controlled in both clockwise and counter clockwise direction and if you have motor with fix direction of motion the you can make use of all the four I/Os to connect up to four DC motors. L293D has output current of 600mA and peak output current of 1.2A per channel. Moreover for protection of circuit from back EMF ouput diodes are included within the IC. The output supply (VCC2) has a wide range from 4.5V to 36V, which has made L293D a best choice for DC motor driver.

A simple schematic for interfacing a DC motor using L293D is shown below:
As you can see in the circuit, three pins are needed for interfacing a DC motor (A, B, Enable). If you want the o/p to be enabled completely then you can connect Enable to VCC and only 2 pins needed from controller to make the motor work.


As per the truth mentioned in the image above its fairly simple to program the microcontroller. Its also clear from the truth table of BJT circuit and L293D the programming will be same for both of them, just keeping in mind the allowed combinations of A and B. We will discuss about programming in C as well as assembly for running motor with the help of a microcontroller.

►Assembly programming
CODE:
L293D_A   equ P2.0          ;L293D A - Positive of Motor
L293D_B   equ P2.1          ;L293D B - Negative of Motor
L293D_E   equ P2.2          ;L293D E - Enable pin of IC

         org 0H
Main:
         acall rotate_f     ;Rotate motor forward
         acall delay        ;Let the motor rotate
         acall break        ;Stop the motor
         acall delay        ;Wait for some time
         acall rotate_b     ;Rotate motor backward
         acall delay        ;Let the motor rotate
         acall break        ;Stop the motor
         acall delay        ;Wait for some time
         sjmp  Main         ;Do this in loop
               
rotate_f:
         setb  L293D_A      ;Make Positive of motor 1
         clr   L293D_B      ;Make negative of motor 0
         setb  L293D_E      ;Enable to run the motor
         ret                ;Return from routine
       
rotate_b:
         clr   L293D_A      ;Make positive of motor 0
         setb  L293D_B      ;Make negative of motor 1
         setb  L293D_E      ;Enable to run the motor
         ret                ;Return from routine
       
break:
         clr   L293D_A      ;Make Positive of motor 0
         clr   L293D_B      ;Make negative of motor 0
         clr   L293D_E      ;Disable the o/p
         ret                ;Return from routine
       
delay:                      ;Some Delay
         mov   r7,#20H
back:    mov   r6,#FFH
back1:   mov   r5,#FFH
here:    djnz  r5, here
         djnz  r6, back1
         djnz  r7, back
         ret
 




►C programming
CODE:
#include <AT89X51.H>#define L293D_A P2_0         //Positive of motor
#define L293D_B P2_1         //Negative of motor
#define L293D_E P2_2         //Enable of L293D

// Function Prototypes
void rotate_f(void);         //Forward run funtion
void rotate_b(void);         //Backward run function
void breaks(void);           //Motor stop function
void delay(void);            //Some delay

void main(){                 //Our main function
    while(1){                //Infinite loop
                rotate_f();          //Run forward
                delay();             //Some delay
                breaks();            //Stop
                delay();             //Some delay
                rotate_b();          //Run Backwards
                delay();             //Some delay
                breaks();            //Stop
                delay();             //Some delay
        }                        //Do this infinitely
}

void rotate_f(){
        L293D_A = 1;             //Make positive of motor 1
        L293D_B = 0;             //Make negative of motor 0
        L293D_E = 1;             //Enable L293D
}

void rotate_b(){
        L293D_A = 0;             //Make positive of motor 0
        L293D_B = 1;             //Make negative of motor 1
        L293D_E = 1;             //Enable L293D
}

void breaks(){
        L293D_A = 0;             //Make positive of motor 0
        L293D_B = 0;             //Make negative of motor 0
        L293D_E = 0;             //Disable L293D
}

void delay(){                //Some delay...
    unsigned char i,j,k;
    for(i=0;i<0x20;i++)
        for(j=0;j<255;j++)
                for(k=0;k<255;k++);
}

Sunday, February 26, 2012

Beginners Robotics : Motors


This article covers the 3 main types of motors which are used in robotics namely the DC motor , Stepper motor and the Servo motor. The article also gives various circuits which can be used to drive the motors . This article is a must for all those starting out or are confused about motors .
Nearly all the robots we make have motors be it servo , stepper or some other unless you are using stuff like pneumatics , synthetic muscle etc . Any way coming back to the point there are mainly 3 types of motors

DC Motors

DC motors are widely used in robotics because of their small size and high energy output. They are excellent for powering the drive wheels of a mobile robot as well as powering other mechanical assemblies.

Ratings and Specifications

Several characteristics are important in selecting a DC motor. The first two are its input ratings that specify the electrical characteristics of the motor.
Operating Voltage.
If batteries are the source of power for the motor, low operating voltages are desirable because fewer cells are needed to obtain the specified voltage. However, the electronics to drive motors are typically more efficient at higher voltages. Typical DC motors may operate on as few as 1.5 Volts or up to 100 Volts or more. Robotics often use motors that operate on 6, 12, or 24 volts because most robots are battery powered, and batteries are typically available with these values.
Operating Current.
The ideal motor would produce a great deal of power while requiring a minimum of current. However, the current rating (in conjunction with the voltage rating) is usually a good indication of the power output capacity of a motor. The power input (current times voltage) is a good indicator of the mechanical power output. Also, a given motor draws more current as it delivers more output torque. Thus current ratings are often given when the motor is stalled. At this point it is drawing the maximum amount of current and applying maximum torque. A low voltage (e.g., 12 Volt or less) DC motor may draw from 100 mA to several amperes at stall, depending on its design.
Speed.
Usually this is specified as the speed in rotations per minute (RPM) of the motor when it is unloaded, or running freely, at its specified operating voltage. Typical DC motors run at speeds from one to twenty thousand RPM. Motor speed can be measured easily by mounting a disk or LEGO pulley wheel with one hole on the motor, and using a slotted optical switch and oscilloscope to measure the time between the switch openings.
Torque.
The torque of a motor is the rotary force produced on its output shaft. When a motor is stalled it is producing the maximum amount of torque that it can produce. Hence the torque rating is usually taken when the motor has stalled and is called the stall torque. The motor torque is measured in ounce-inches (in the English system) or Newton-meters (metric). The torque of small electric motors is often given in milli-Newton-meters (mN-m) or 1/1000 of a N-m. A rating of one ounce-inch means that the motor is exerting a tangential force of one ounce at a radius of one inch from the center of its shaft. Torque ratings may vary from less than one ounce-inch to several dozen ounce-inches for large motors.
Power.
The power of a motor is the product of its speed and torque. The power output is greatest at about half way between the unloaded speed (maximum speed, no torque) and the stalled state (maximum torque, no speed). The output power in watts is about (torque) x (rpm) / 9.57.
DC Motor Control

HBridge
To control a DC motor we have to first convert the Digital 01 output into one which can drive the motor for this we use the HBridge . Given below is a simple HBridge circuit
                       

When both the points A & B are "HIGH" Q1 and Q2 are in saturation. Hence the bases of Q3 to Q6 are grounded. Hence Q3,Q5 are OFF and Q4,Q6 are ON . The voltages at both the motor terminals is the same and hence the motor is OFF. Similarly when both A and B are "LOW" the motor is OFF. When A is HIGH and B is LOW, Q1 saturates ,Q2 is OFF. The bases of Q3 and Q4 are grounded and that of Q4 and Q5 are HIGH. Hence Q4 and Q5 conduct making the right terminal of the motor more positive than the left and the motor is ON. When A is LOW and B is HIGH ,the left terminal of the motor is more positive than the right and the motor rotates in the reverse direction. You could have used only the SL/SK100s ,but BC148 used have a very low hFE ~70 and they would enter the active region for 3V(2.9V was what I got from the computer for a HIGH) . You can ditch the BC148 if you have a SL/SK100 with a decent value of hFE ( like 150).The diodes protect the transistors from surge produced due to the sudden reversal of the motor. The approx. cost of the circuit without the motor is around Rs.40.

After the Hbridge is you wish to control the power of the motor this can be easily done with a PWM based control
Pulse Width Modulation
Pulse width modulation is a technique for reducing the amount of power delivered to a DC motor. Instead of reducing the voltage operating the motor (which would reduce its power), the motor's power supply is rapidly switched on and off. The percentage of time that the power is on determines the percentage of full operating power that is accomplished. This type of motor speed control is easier to implement with digital circuitry. It is typically used in mechanical systems that will not need to be operated at full power all of the time.
 

Figure illustrates this concept, showing pulse width modulation signals to operate a motor at 75%, 50%, and 25% of the full power potential.
Stepper Motors
The shaft of a stepper motor moves between discrete rotary positions typically separated by a few degrees. Because of this precise position controllability, stepper motors are excellent for applications that require high positioning accuracy. Stepper motors are used in X-Y scanners, plotters, and machine tools, floppy and hard disk drive head positioning, computer printer head positioning, and numerous other applications.
                         
Stepper motors have several electromagnetic coils that must be powered sequentially to make the motor turn, or step, from one position, to the next. By reversing the order that the coils are powered, a stepper motor can be made to reverse direction. The rate at which the coils are respectively energized determines the velocity of the motor up to a physical limit. Typical stepper motors have two or four coils. For more information on stepper motors you can read the stepper motor tutorial on the website . Any way here is a very simple stepper controller


Servo Motors
Servo motors incorporate several components into one device package:
* a small DC motor;
* a gear reduction drive for torque increase;
* an electronic shaft position sensing and control circuit.
The output shaft of a servo motor does not rotate freely, but rather is commanded to move to a particular angular position. The electronic sensing and control circuitry -- the servo feedback control loop -- drives the motor to move the shaft to the commanded position. If the position is outside the range of movement of the shaft, or if the resisting torque on the shaft is too great, the motor will continue trying to attain the commanded position.

Servo Motor Control
A servo motor has three wires: power, ground, and control. The power and ground wires are simply connected to a power supply. Most servo motors operate from five volts.


The servo controller receives position commands through a serial connection which can be provided by using one I/O pin of another microcontroller, or a PCs serial port! The communication protocol, that is used for this controller, is the same with the protocol of all the famous servo controllers of Scott Edwards Electronics Inc., this makes this new controller 100% compatible with all the programs that have been written for the "SSC" controllers...! However, if you want to write your own software, it is as easy as sending positioning data to the serial port as follows:
Byte1 = Sync (255)
Byte2 = Servo #(0-15)
Byte3 = Position (0-254)
So sending a 255,4,150 would move servo 4 to position 150, sending 255,12,35 would move servo 12 to position 35.
The standards of the serial communication should be the following: 9600 baud, 8 data bits, 1 stop bit and no parity.
The control signal consists of a series of pulses that indicate the desired position of the shaft. Each pulse represents one position command. The length of a pulse in time corresponds to the angular position. Typical pulse times range from 0.7 to 2.0 milliseconds for the full range of travel of a servo shaft. Most servo shafts have a 180 degree range of rotation. The control pulse must repeat every 20 milliseconds. This pulse signal will cause the shaft to locate itself at the midway position +/-90 degrees. The shaft rotation on a servo motor is limited to approximately 180 degrees (+/-90 degrees from center position). A 1-ms pulse will rotate the shaft all the way to the left, while a 2-ms pulse will turn the shaft all the way to the right. By varying the pulse width between 1 and 2 ms, the servo motor shaft can be rotated to any degree position within its range.

Friday, February 24, 2012

Robot Basics


A robotic hand, developed by NASA, is made up of metal segments moved by tiny motors. The hand is one of the most difficult structures to replicate in robotics.



Robot Basics

The vast majority of robots do have several qualities in common. First of all, almost all robots have a movable body. Some only have motorized wheels, and others have dozens of movable segments, typically made of metal or plastic. Like the bones in your body, the individual segments are connected together with joints.
Robots spin wheels and pivot jointed segments with some sort of actuator. Some robots use electric motors and solenoids as actuators; some use a hydraulic system; and some use a pneumatic system (a system driven by compressed gases). Robots may use all these actuator types.
A robot needs a power source to drive these actuators. Most robots either have a battery or they plug into the wall. Hydraulic robots also need a pump to pressurize the hydraulic fluid, and pneumatic robots need an air compressor or compressed air tanks.
The actuators are all wired to an electrical circuit. The circuit powers electrical motors and solenoids directly, and it activates the hydraulic system by manipulating electrical valves. The valves determine the pressurized fluid's path through the machine. To move a hydraulic leg, for example, the robot's controller would open the valve leading from the fluid pump to a piston cylinder attached to that leg. The pressurized fluid would extend the piston, swiveling the leg forward. Typically, in order to move their segments in two directions, robots use pistons that can push both ways.
NASA's Urbie climbing stairs
Photo courtesy NASA JPL
The robot's computer controls everything attached to the circuit. To move the robot, the computer switches on all the necessary motors and valves. Most robots arereprogrammable -- to change the robot's behavior, you simply write a new program to its computer.
Not all robots have sensory systems, and few have the ability to see, hear, smell or taste. The most common robotic sense is the sense of movement -- the robot's ability to monitor its own motion. A standard design uses slotted wheels attached to the robot's joints. An LED on one side of the wheel shines a beam of light through the slots to a light sensor on the other side of the wheel. When the robot moves a particular joint, the slotted wheel turns. The slots break the light beam as the wheel spins. The light sensor reads the pattern of the flashing light and transmits the data to the computer. The computer can tell exactly how far the joint has swiveled based on this pattern. This is the same basic system used in computer mice.
These are the basic nuts and bolts of robotics. Roboticists can combine these elements in an infinite number of ways to create robots of unlimited complexity.