Let's Make Robots!

Continuous servo with picbasic

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

wait =(20-(L_servo_value+R_servo_value/100)) should be

wait =(20-((L_servo_value+R_servo_value)/100)) i think

Yes you are right, but as i said this code is not tested it was merely meant as an "easy" way to make both servos spin i the same direction without the code being to confusing.But you are right and i have now changed it.

Thanks for pointing it out.

//Mixmar

I think all errors are corrected now. The reason for me not posting this as a tip is because you need to insert a image as header on tips and i did not have one. I'm only glad to get tips witch help me improve so keep it up. :)

I second what birdmun said about servo_stop. This variable or constant is not defined.

Also, may offer some advice on how you wrote your tip? Please take it as constructive critism. I am glad you wrote your post, and I certainly encourage you to keep it up!

  1. Your title is misleading. Although the code may be PIC Basic, the real purpose of the tip is about controlling the speed of continuous rotation servos, isn't it? A descriptive title will really improve your tip.
  2. You should consider entering this as a tip/walkthrough, once you have tested your code.
  3. Improving your capitalization and grammar will make your tip more readable to a wider audience. 

 

Is servo_stop supposed to be servo_neutral? And, wouldn't (R_servo_value - servo_stop) = bot_speed ?

Yes you are correct. I came up with this snippet and wrote it down a little to quick. But the second part of your question would work as a way to get the speed, while the purpose of this code is to set the speed. Thanks for the quick response.

L_servo_value = servo_neutral - (R_servo_value - servo_neutral)

R_servo_value - servo_neutral will always be equal to bot_speed, because:

R_servo_value = servo_neutral + bot_speed which means that solving for bot_speed = R_servo_value - servo_neutral

If you wish to control the speed of each motor/servo, you will require a second variable, one for each motor/servo.

You are once again correct.

I have now changed it to an even simpler piece of code.

I have a piece of code that u can set the turn % and direction witch i use myself.I am trying to clean up the code ,so i could add that part later.But it is true what u are saying about turning.In the piece of code that i currently are using on my robot, i have made loops that slow the robot down in a smooth way aswell as speeding up equally smooth.I'm currently working on making the turns a bit smoother.

Baby steps, Baby steps.....