Integers are how the servo is controlled, so no there is no other way using EasyC. SetMotor(speed) will be looking for an unsigned char variable rather than a floating point value. Adam's answer still holds
Using MPLAB and C directly there are methods to increase the accuracy of the controlling integers, but discrete integers are always how the motors are controlled. That's because they are digital controls rather than analog.
Giving "speed" a starting value close to what you know it should be will help. That may avoid having speed go too high or too low, but without a check that'll be a risk.
The way you wrote your code example seems to indicate you want to drive for a specific distance at a constant velocity. Is my interpretation correct?
In your code example "for" loops aren't used in that way.
You'd use a "while" loop to do what you want.
Also, rps in your calculation needs an extra factor for the encoder equivalent of one revolution of your motor before it becomes revolutions per second.
rps isn't in terms of the the motor command values of 127 to 255, it's in terms of some velocity, so (rps < 127) doesn't do what you want in keeping the speed value from adding up to more than 255 or less than 127 eventually. It might coincidentally work out that 127 is a valid "distance per second," but that doesn't sound like what you want.
Code:
// Initialize stuff
StartTimer(1)
enco = 0;
// Drive the motor
while (enco < distance)
{
enco = GetEncoder(1);
make speed adjustments
}
You're going to see overcompensation, because you aren't accounting for how often the value set by the SetMotor() command is actually sent to the motor.
It is not sent to the motor whenever SetMotor is called. SetMotor only stores the value for later pickup by the master processor which doesn't check very often. For instance,
Code:
for (speed=127; speed <255; speed ++)
{
SetMotor (speed);
}
will step through the values 127 to 255 so fast that the master processor will probably only pickup the SetMotor value once and so see only one of the many values SetMotor sets.
We're competing tomorrow, so I won't be replying for awhile.
Someone else will probably help out if you post more questions.