Keyboard Controll of Servos

kapoarch

New Member
Hi,
I am building my first sentry gun, this is a college project for my capstone class and im having trouble getting control of my servos for pan and tilt in a nice fluid motion. Im using an 18m2 chip and have the servos wired in to legs 8 and 11. Now im using a keyboard with a ps/2 slot as my input i have the arrow keys all mapped out, (up=$38,down=$32,left=$34,right=$36,enter=$3F) im jus not sure why im not getting the control i want. here is a sample of what i have came up with

servo b.2,1000
dirsb=%01111111
let b0=75
pinsb=%00000000

start:
kbin #b0
select case b0
case $38
servopos b.2, 125
end select
goto start

with this code the keyboard does give a signal and trys moving the servo but, every other key does also, and the servo jus barley jerks up in a very slow pase. Now i used just a simple push button controlled program to test my servos and with the push of the button i can get it to go to positon and hold but not when i try and use the keyboard. If anyone can help i would greatly appreciate it, i have 2 weeks to complete and im stuck!! I need a little brain power!!!
Mechatronic Student
 

AllyCat

Senior Member
Hi,

I suggest you read the User Manual 2 for the servo command (again).

The second parameter (pulse) should be between 75 and 225 (not 1000 or 20). I think boriz was suggesting that you should use a pause 20 (at least) before writing to the servo again.

Cheers, Alan.
 

AllyCat

Senior Member
Hi,

You could probably have tried both quicker than asking. But definitely somewhere within the program loop!

Cheers, Alan.
 
Code:
servo 4,75
start:
 kbin #b0
 select case b0
 case $38
 servopos b.2, 125
end select
[COLOR="#FF0000"]Pause 20[/COLOR] 
 goto start
Using 125 means that the servo will be center or close to centered. Try 75 and that will be one end of the servo travel, 225 will be the other end
 

AllyCat

Senior Member
Hi,

What do you actually expect your code to do? To me, it looks that at best it will just drive the servo to the "centre" position (and stay there) when the <up arrow> key is pressed. If you want the servo to "tilt up" whilst the appropriate key is pressed, then you'll need something like:

kbin #b0
select case b0
case $38
b1 = b1 + 1 ; (b1 will have been initialised to 125 and needs to be limited to say 225)
servopos b.2, b1
end select

If you're finding that the keyboard keys are not working correctly, then you need to put a debug or sertxd(#b0," ") instruction after the kbin to see what's actually being received.

Cheers, Alan.
 

kapoarch

New Member
i put your code in and it still does the samething..... im at a loss of words at this point, i dont understand why when i use push button command with a simple push button switch it wil go to position and hold but want do it with the keyboard
 

Technical

Technical Support
Staff member
Take the # out of kbin #b0, its not required as # doesn't work with non-ascii characters like arrow up/down, so everything you are pressing is giving a "?" character.
 

AllyCat

Senior Member
If you're finding that the keyboard keys are not working correctly, then you need to put a debug or sertxd(#b0," ") instruction after the kbin to see what's actually being received.
Hi,

So what key code(s) is/are being received?

Cheers, Alan.
 

kapoarch

New Member
so for up arrow key it is gving code 117,$75,%01110101,'u' in debug window here is a recent sample of code i jus tried and still no results......
servo 4,75
let b1=125
start:
kbin b0
debug b0
select case b0
case $75
b1=b1+10
servopos b.2, b1
end select
Pause 20
goto start
 

AllyCat

Senior Member
Hi,

Well, $75 seems to be correct according to the table in User Manual 2. Are the other keys (which you say have the same effect) producing different values?

I'm not clear myself whether the keyboard has any auto-repeat function. You may need to add a debug for b1 to see what's happening on each pass around the loop (and don't forget that b1 should hold/limit at about 225). I presume the '4' in the servo command is the same as the b.2 pin?

Cheers, Alan.
 

nick12ab

Senior Member
Have you checked to see if the kbin command interferes with the servo? What happens if you remove the command and just execute servopos repeatedly?
 

Technical

Technical Support
Staff member
The main issue is probably that the servo pulses are not running during the timing sensitive kbin command. Depending on the project you may solve that by simply increasing the pause time.

You may have better luck with two PICAXE joined together - one doing the keyboard work and switching an output on/off, with a second moving the servo accoridng to that high/low signal on an input.
 

westaust55

Moderator
At post 15, AllyCat has flagged a valid point.

SERVO 4, 75 = acts upon pin B.4
SERVOPOS B.2, b1= acts upon a different pin.

If you are wired to B.4 then nothing within the program looping part will cause the servo motor to move.
Post the code which works with pushbuttons so we can check for differences other than the kbin part.
 

AllyCat

Senior Member
Hi,

I believe that the PS2 keyboard interface is basically serial "RS232" (I don't know the baud rate offhand). So if Technical's analysis is correct then you might be able to use the hardware serial input (HSERIN command), which happens to be on pin b.2 so you'd have to swap pins around. Note that the arrow keys send two character bytes so it might be better to start with some normal ASCII "letter" keys.

However, this would certainly add another level of complexity to the project. But, for testing you might run the two interfaces in parallel (linked pins) to compare your own keyboard code against the "normal" embedded kbin result until they correspond. Aplologies if this suggestion is a step too far. :eek:

Cheers, Alan.
 

westaust55

Moderator
The PS/2 port is special serial comms bus more akin to the i2c with control and selection involving both clock and data lines. The data rate far too high for a PICAXE to handle with interpreted BASIC commands and needs the speed of the underlying PIC chip's machine code to monitor/read the PS/2 bus/port as done with the inbuilt PICAXE kbin command.
 
Last edited:

boriz

Senior Member
Do you actually need a keyboard? Why not just 4 direction buttons and maybe another for 'fire' ?
 

AllyCat

Senior Member
The PS/2 port is special serial comms bus more akin to the i2c with contrl and selection involving both clack and data lines. The data rate far too high for a PICAXE to handle with interpreted BASIC commands and needs the speed of the underlying PIC chip's machine code to monitor/read the PS/2 bus/port as done with the inbuilt PICAXE kbin command.
Hi,

Apologies for "muddying the water", Westaust55 is absolutely correct, maybe I was confused because "dual mode" (serial-PS/2) mice used to be made (employing a simple pin-linked PS/2 socket to 9-pin sub.D plug)? But now they'd be dual mode PS/2-USB so of no use here. For reference I found the protocol here.

So, another idea for the experts: Is there any chance of using the "Hardware" PWM outputs to emulate the Servo pulses, or is this going to have a similar timing conflict?

Otherwise, it may be necessary to use simple pushbuttons as boriz suggests, or dual processors, or perhaps modify the keyboard strategy to use single-byte commands (i.e. not the arrow keys) and to press (but not hold) each key to "Start moving up" (or down, etc.) with a separate "Stop moving" key (and ideally, optionally pressing another direction key) when the correct value/direction has been achieved.

Cheers, Alan.
 

erco

Senior Member
Repeating, a 20M2 has no trouble reading a keyboard and driving servos. Video & simple demo code provided for 2 servos. Unless there is something different in the one-year older 18M2, this code should work there too. You will want to add some limits within the program to prevent driving your servos beyond their mech limits and/or wrapping pulsouts below 0 or above 255.

Cheers,

erco


Code:
#picaxe 20m2
#no_data
sertxd("servokeyboard",13,10) 

b5=125		'initial center position
b4=125		'initial center position
servo 5,b5		'servo on pin b.5 at pulsout value b5
servo 4, b4		'servo on pin b.4 at pulsout value b4

pause 1000
 
 start:
 servopos 5,b5
 servopos 4,b4
 kbin [20,start],#b0	'read keyboard
 
 'sertxd("b0=",#b0,13,10)  'useful in debug, but causes jitter
 select case b0
 case 52			'left arrow
 inc b5
 case 54			'right arrow
 dec b5
 case 56			'up arrow
 inc b4
 case 50			'down arrow
 dec b4
 endselect
 
 goto start
 

erco

Senior Member
There does appear to be an auto-repeat built into the keyboard, as evidenced by watching the servo move in the video. Useful for fine control for the OP's aiming mechanism!
 

erco

Senior Member
@kapoarch: Did you get this work to with your 18M2? It's helpful to the group to report back with your findings.
 
Top