PDA

View Full Version : Servo Control



zmet68
- 12th February 2005, 15:39
Can someone show me how to sweep two servo’s back and forth. I would like to be able to control the speed and distance individually. I am using pbp with a 16F84A IC. I have no idea where to start, any help would be appreciated.

Thanks Brett

mister_e
- 12th February 2005, 16:03
to make your back and forth stuff, if your motor don't need much current, it will be easier to use a H-Bridge driver IC like NJM2670 or else.

By using 2 i/o, you'll be able to do it easy...

will look something like this


TRISA = 255 ' set PORTA as input
TRISB=0 ' set PORTB as output

Motor1_a var PORTB.0
Motor1_b var PORTB.1

PushButtonForward var PORTA.0 ' Push button connected
PushButtonReverse var PORTA.1 ' between vcc and PIC pin
PushButtonStop Var PORTA.2

PORTB=0

Start:
If PushButtonForward then
Motor1_a = 1
Motor1_b = 0
endif

If PushButtonReverse then
Motor1_a = 0
Motor1_b = 1
endif

If PushButtonStop then
Motor1_a = 0
Motor1_b = 0
endif

goto start



to measure the distance you'll have to use rotary encoder, opto devices or something who will provide you some pulses when motor/wheel is turning. By counting those pulse in background (on RA4 pin using internal Timer/Counter module) you'll have some numbers to do your maths

For the speed...it's up to you... some will use PWM, some will vary the voltage accross the motor and probably there's many other way to do it... food for brain cells ;o]

Bruce
- 12th February 2005, 16:58
The lower section of this page (link below) explains how to use the PULSOUT command to control hobby servo motors.

http://www.rentron.com/Micro-Bot/IR_Nav.htm

zmet68
- 12th February 2005, 18:31
Can this be done without pushing a button? sorry i'm verry new to this. I'm doing pretty good with the hole blinking LED thing.

Thanks for all the help

mister_e
- 12th February 2005, 18:44
it can be done as you wish. You're the boss, you decide what you want to do and how.

Bruce
- 12th February 2005, 21:54
Try this;


@ DEVICE HS_OSC,WDT_OFF,PROTECT_OFF
DEFINE OSC 20 ' We're using a 20MHz crystal

TRISB=%00000000 ' Set PortB to all outputs
PORTB=%00000000 ' Clear PortB
Position VAR WORD ' Servo position data
Y VAR BYTE ' Servo position update speed
Left_Servo var PortB.4 ' PortB.4 = left servo output
Right_Servo var PortB.5 ' PortB.5 = right servo output

Y = 20
Servo:
FOR Position = 1100 to 400 step-Y
PULSOUT Left_Servo,Position
PAUSE 15
NEXT Position
FOR Position = 1100 to 400 step-Y
PULSOUT Right_Servo,Position
PAUSE 15
NEXT Position
FOR Position = 400 to 1100 step Y
PULSOUT Left_Servo,Position
PAUSE 15
NEXT Position
FOR Position = 400 to 1100 step Y
PULSOUT Right_Servo,Position
PAUSE 15
NEXT Position
Y = Y-1
IF Y <=4 THEN Y=20
GOTO Servo
Adjust the values 1100 to 400 for your particular servos.

The resolution of PULSOUT at 20MHz is 2uS. 1100 * 2uS = 2.2mS. 400 * 2uS = 800uS. Most hobby type servos require between 1 to 2mS pulses, but it can vary depending on the servo brand.

Just experiment until you get the movement you need without slamming the servo into its stops when moving full left or full right. Increase the value of Y to speed up movement. Decrease it to slow down.

Have fun.....;o]

DWV
- 13th February 2005, 09:19
zmet68
Go to my website and download the "Pic Primer" it will tell you everything you need to know. I wrote it around the 16F628 and the servo section is designed for the 12F675, but you should be able to adapt it to any pic.

http://www.dragonslair.ca/workstation.html

Dave

zmet68
- 13th February 2005, 18:38
WOW, Thanks for the Pic Primer. You have all been very helpful.Being that I’m new to pbp and I have no prior programming skills all the help I can get the better.

Thanks