If you have 2 servos that you use to propell your robot, chances are that one of them are inverted from the other one.
This is a bit of code i use so i only have to set the speed at one place(bot_speed) and this does the rest
This code is not tested yet, but it should work pretty much as it is.
My servos range from 100 to 200 so this example is based on that.100 spins the left wheel forward and 200 spins the right wheel forward.
var bot_speed = 0 //This is the variable witch you use to control the speed.
var newbot_speed = bot_speed
var L_servo_value as byte
var R_servo_value as byte
L_servo var PORTB.1 //This is only aliasing the pin b.1 as L_servo and b.2 as R_servo.
R_servo var PORTB.2
con servo_neutral = 150 //You might need to change this to your servos center position.
con max_speed = 50 //Since my max position on the servo is 200 and 150 is neutral.
con min_speed = 0
var wait //Used to store the delay time in, so the pulses are 20ms(50hz).
move:
IF newbot_speed <> bot_speed THEN // if the variable bot_speed has changed
bot_speed=newbot_speed
IF bot_speed < min_speed THEN // this if statement is to make sure i dont have a invalid value passed to the servos
bot_speed = min_speed
ELSEIF bot_speed > max_speed THEN
bot_speed = max_speed
ENDIF
R_servo_value = servo_neutral + bot_speed //this sets the speed of the movement
L_servo_value = servo_neutral - bot_speed
ENDIF
low L_servo
pulsout L_servo,L_servo_value
low R_servo
pulsout R_servo,R_servo_value
wait =(20-((L_servo_value+R_servo_value)/100))
delay wait
return
This is only used to make the bot go straight.now i can just give a valid value (0-50) to the variable bot_speed when i want to change the speed and gosub move when i only want it to move at the set speed.If the speed is unchanged it just keeps on moving
If you have any questions or improvements please post them.
I have tried to make the code as understandable as possible with names etc..
I hope it can be of use to someone.
// Mixmar