speed control of motor

Hi all,
I am using a pololu high power motor controller 18 vs 15 with arduino. Both are interfaced through serial interface.Hall effect sensor has been introduced in a system,which measures speed and displays it on serial monitor. Arduino example code has been used setmotorspeed() function increases and decreases speed properly . whwn i apply load across it the speed of motor decreases. I want to control its speed in such a manner so that if load is applied the speed of motor remains constant. any suggestions plz

#include <NewSoftSerial.h>
#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);

// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0x83, BYTE);
}

// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0x86, BYTE); // motor reverse command
speed = -speed; // make speed positive
}
else
{
mySerial.print(0x85, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speed & 0x1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}

void setup()
{
// initialize software serial object with baud rate of 38.4 kbps
mySerial.begin(38400);

// the Simple Motor Controller must be running for at least 1 ms
// before we try to send serial data, so we delay here for 5 ms
delay(5);

// if the Simple Motor Controller has automatic baud detection
// enabled, we first need to send it the byte 0xAA (170 in decimal)
// so that it can learn the baud rate
mySerial.print(0xAA, BYTE); // send baud-indicator byte

// next we need to send the Exit Safe Start command, which
// clears the safe-start violation and lets the motor run
exitSafeStart(); // clear the safe-start violation and let the motor run
}

void loop()
{
setMotorSpeed(3200); // full-speed forward
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
delay(1000);
}

prince_sad2002:
I want to control its speed in such a manner so that if load is applied the speed of motor remains constant. any suggestions plz

A PID (Proportional, Integral, Derivative) controller is the most common method of controlling motor speed. There are plenty of resources available on the net on PID controllers in general (Google is a wonderful search engine), and I believe 1 or 2 libraries on the Playground that provide a readily available PID controller to use in your own sketches if you don't want to write one yourself. Implementation is actually fairly simple once you understand the concept behind it.

And of course PID speed control requires that you have a speed feedback measurement from the motor and the proper software to read the actual speed depending on the type of speed sensor your are using. So no matter what kind of control algorithm you use, you must first obtain the hardware needed to obtain and measure the actual motor speed.

Lefty

To get zero steady state error, you need a free integrator. In other words, if you want it to hold a speed under varying load, you need the I term in the PID.

Hi:)
Can PID control be implemented using the sensor readings for the purpose of following a wall??