PDA

View Full Version : How would i write this in pic basic pro (Servo controller)



Jhdgkss
- 3rd February 2006, 21:49
Hi there i am trying to create a program to control two servo inputs to control one servo.

The jist of it is. There are two servo inputs

Servo input 1
Servo input 2

And one output

Servo output 1

when input 1 is centerd the output 1 = Input 2
and when input one is not centered i.e its more or less then 1.5ms pulse then Output 1 = Input 2


I have enclosed a flow diagram which i think will help.

Please i need some help as it is kritical in my r/C project

btaylor
- 10th February 2006, 01:17
I think your verbal description is wrong. As written, your output is always driven by input 2.

How are you making the transition from pot position to pulsewidth? Analog or digital input?
How are you driving the output? Let's assume PULSOUT and further assume a 20 MHz clock for a 2 uSec PULSOUT step size.

Assuming two pots, their ends each connected to +5V and ground, with the wiper coming to analog inputs, then the input will be a word between 0 and 1023.

The output needs to be a word between 500 and 1000 centred on 750 to allow for the servo range of 1000 uSec to 2000 uSec with 2 uSec steps.

Psuedocode follows

Servo var portb.0 ' or whatever
Position1 var word
Position2 var word
Output var word

Start:
GetPot1Position:
read analog value where 512 = pot central
store in word variable Position1
GetPot2Position:
read analog value and store in word Position2

Width1 = (Position1 - 12)/2 'now in the range 0 to 506 with 250 = centre
Width2 = (Position2 - 12)/2 ' don't worry about the small offset

if (width1 < 248) OR (width1 > 252) then ' allow for non exact centering
output = width1 + 500
else
output = width2 + 500
endif
LOW servo ' VITAL you set servo to 0 beforehand - try not doing it!
PULSOUT pin, Output
pause 20
goto start

Jhdgkss
- 10th February 2006, 08:17
hi there thanks for the reply.

Input 1 is an analouge input and input 2 is a pwm signal from a futarba reciver.

the clock frequeny is 4mhz with this make a big differance to the code and what would it look like?

Jhdgkss
- 10th February 2006, 08:21
hi there here is the code i have at the moment

'servo control prog

'set variables

ch1 var byte
ch2 var byte
s1 var byte
s1 = 150

start:

pulsin portb,1,7000,Ch1
pot portb,2,200,Ch2

if ch1 < s1 then output1
if ch1 > s1 then output1
if ch1 = s1 then output2


output1:

pulsout portb.3,ch1
pause 18
goto start

output2:

pulsout portb.3,ch2
pause 18
goto start

end

i have compiled it and it seams to work. not tried it out on any hardware yet .