Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: hazzer123 on April 30, 2007, 01:50:57 PM

Title: Calibrating Servo control
Post by: hazzer123 on April 30, 2007, 01:50:57 PM
I have just created a servo motor program for a PIC and am just wondering, what is the best way to calibrate the pulse widths to an angle. Is it simply by measuring them with a protractor and noting down the pulse width required? All servos are slightly different arent they?

Also, is the angle directly proportional to the pulsewidth?

Thanks
Title: Re: Calibrating Servo control
Post by: nanob0t on April 30, 2007, 02:47:36 PM
Also, is the angle directly proportional to the pulsewidth?

Yes, the angle is proportional to pulsewidth.   If a 1.5ms pulse is neutral on the servo, anything less would bring it closer to 0 degrees, while anything higher would bring it closer to 180 degrees.  If you had a 180 degree servo.

In terms of finding this information, look at the data sheet or the information of that line of servos.  It should have a short clip on what is neutral, and what is max/min angles, etc.  From there, it's algebra to figure it out.

I can help you with the math if you don't know how to do it.
Title: Re: Calibrating Servo control
Post by: nanob0t on April 30, 2007, 02:53:20 PM
A question I have that I might as well spit out since this is servo topic. 

Can I control a servo using a PWM on a microcontroller?  Is this possible?

I need to know if it's possible, if it needs bunches of math in order to solve it, I can do that easy.  I just need to know if I have to buy a servo controller or if I can control my servos with my own microcontroller.
 
I am starting to make a robot by myself.  I'm building aside from the funding I get from companies, so I don't have a really great budget.  I am designing a tiny robot.  If I can get a chassic built and a tiny robot built that is very inexpensive, I plan to be able to change it into wonderous things a mass produce them.  Then I can have millions of tiny robots cleaning this house or something.  In all the projects I have done, the companies have always made me use DC motors, instead I'd like to use continuous rotation servos, but I don't want a huge servo controller on the thing.

Thanks
Title: Re: Calibrating Servo control
Post by: JonHylands on April 30, 2007, 02:58:40 PM
If you're using two servos per robot, then you can easily control them using hardware PWM depending on which microcontroller you are using. The AVR microcontrollers (specifically the ATmega line) are quite capable of this.

- Jon
Title: Re: Calibrating Servo control
Post by: nanob0t on April 30, 2007, 03:19:53 PM
If you're using two servos per robot, then you can easily control them using hardware PWM depending on which microcontroller you are using. The AVR microcontrollers (specifically the ATmega line) are quite capable of this.

- Jon



What about a PIC with a few installed PWMs?

I'm getting a couple sampled that have PWMs, because I want to get started soon   :)
Title: Re: Calibrating Servo control
Post by: hazzer123 on April 30, 2007, 03:30:21 PM
Heres the code i have made. Its working, but the delays are slightly out im thinking...

Good thing is there is no PWM needed.

Code: [Select]
;----------------------------------------
;Dual servo control using PIC16F628A
;send two bytes to the PIC - 1st = servo number, second = position
;by Hazzer123
;--------------------------------------


; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
;
        movlw 0x19              ; 0x19=9600 bps (0x0C=19200 bps)
        movwf SPBRG
        movlw b'00100100'       ; brgh = high (2)
        movwf TXSTA             ; enable Async Transmission, set brgh

        bcf STATUS,RP0          ; RAM PAGE 0

        movlw b'10010000'       ; enable Async Reception
        movwf RCSTA
;
; ------------------------------------
; PROVIDE A SETTLING TIME FOR START UP
; ------------------------------------
;
        clrf dataL
settle  decfsz dataL,F
        goto settle

        movf RCREG,W
        movf RCREG,W
        movf RCREG,W            ; flush receive buffer;

movwf 0x80 ;initial pulse width of roughly 1.5ms
movwf servo_pos0
movwf servo_pos1

loop23 btfsc PIR1, RCIF ;Has something been received?
call receive


; -------------------------------------------
; SEND PULSE
; -------------------------------------------

PULSE BSF PORTA,0 ;Begin pulse on servo 0
call delay_long ;delay 1 ms
movf servo_pos0,w ;delay by more time to make up required pulse width
call delay_tiny
BCF PORTA,0 ;end pulse
BSF PORTA,1 ;repeat on servo 1
call delay_long
movf servo_pos1,w
call delay_tiny
BCF PORTA,1
call delay_vlong ;~30ms delay between pulses
goto loop23


; -------------------------------------------
; RECEIVE CHARACTER FROM RS232
; -------------------------------------------

receive call delay_vlong ;delay to ensure both bytes are received
movf RCREG,W ;read first byte - servo number
addlw 0x21 ;find the address of the position variable for the servo in question
movwf FSR
movf RCREG,W ;copy the second byte received, position, to the appropriate servo position variable
movwf INDF
return
;-------------------------------------------
; 4uS delay
; -------------------------------------------

delay_tiny movwf move_pos

loop nop
decfsz move_pos,f
goto loop
return

;-------------------------------------------
; 1ms delay
; ------------------------------------------

;998 cycles
delay_long movlw 0xC7
movwf d1
movlw 0x01
movwf d2
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_0

;2 cycles
goto $+1
return


;-------------------------------------------
; 30ms delay
; ------------------------------------------
;29998 cycles
delay_vlong movlw 0x6F
movwf d1
movlw 0x18
movwf d2
Delay_1
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_1

;2 cycles
goto $+1
return
end


Thanks for your replies
Title: Re: Calibrating Servo control
Post by: hazzer123 on April 30, 2007, 03:58:30 PM
I was just thinking. There is a website somewhere that says that the HS311 has only 90 degrees of travel.  When i used my pic to out put 1.5ms it centred, and when i changed it to 1ms, it shifted by 45 degrees. This means within the pulse range of 1 - 2ms, there is a position range of 90 degrees.

Is this what the websites that spec the HS311 with 90 degrees rotation actually mean?
Title: Re: Calibrating Servo control
Post by: nanob0t on April 30, 2007, 04:04:55 PM
Yes.  It is a 90 degree servo, so 1ms pulse would be 0 while 2ms pulse would be 90 degrees.  To achieve different angles, it's just a ratio.
Title: Re: Calibrating Servo control
Post by: hazzer123 on April 30, 2007, 04:07:51 PM
Yes, but why say its a 90 degree servo, when it can actually acheive more than that (just more than 180).

I have just modified my code accordingly to create 0.5 - 2.5 ms pulses in 256 increments and its working great now.
Title: Re: Calibrating Servo control
Post by: ed1380 on April 30, 2007, 04:26:02 PM
IIRC the hs311 is 180degrees. the site is just misinformed
Title: Re: Calibrating Servo control
Post by: Admin on April 30, 2007, 04:39:43 PM
Quote
Is it simply by measuring them with a protractor and noting down the pulse width required? All servos are slightly different arent they?
yeap thats what I do

Quote
Also, is the angle directly proportional to the pulsewidth?
not always. if you apply a force to your servo, it will be off by a few degrees. on my fish robot i require high precision under stress, but my servos are always off a few degrees . . .