PDA

View Full Version : Moving a Robotic Car Using Keyboard



shyam4uall
10-20-2008, 10:01 PM
I want to control the movement using the arrow keys.I have been able to control it to an extent,the problem arises when I want both the movement like FORWARD & LEFT and all those combinations.

The programming involves parallel port and I am not able to send two data or rather the transmitter (of a R/C car) is not able to handle both data at the same time.

Hope i am able to clear my doubts.Now how should I proceed?

I am using Matlab for this but if anyone can give a code in any other language that too will help me.

sanj_k
10-21-2008, 06:35 PM
add a delay command in the code while pressing two keys simultaneously.
For eg: suppose you want the car to move forward and left, the code should be..... forward....[delay]...left..... that should fix it up..!!

anilmaddala
10-27-2008, 01:14 AM
Hello Shyam,
I did some thing like this a while ago, but using C.
Instead of using arrow keys, i used keypad numbers 1-9 to control the direction. ex 7- to go forward left, 3- to go back right.
By doing this you can solve your prob.

docel
10-28-2008, 01:17 AM
.....you cant type 2 letters at the same time, can you??
for example: if I hold down a and simultaneously press s, then the console shows - aaaaaaaaaaaaaaasssssssssssss .

The keys register and implement the first value till it is changed with a new value. ie., the port lines are 'latched' till a new value is input.
So, the output will be a serial 2 state out- one after the other- rather than the combo.

Dont know how your code runs, nor the motor arrangement, but all you should do is implement a 4bit binary number at the port output for every key.

shyam4uall
10-28-2008, 11:40 AM
@docel

I am also sending four bits through the parallel port.

UP ARROW 1 0 0 0
DOWN ARROW 0 1 0 0
LEFT ARROW 0 0 1 0
RIGHT ARROW 0 0 0 1

My car is working normally when I press the keys individually but whenever i press two keys (left+up) then the last pressed key is working.Also if i press a key and release it the parallel port continues to send the signal.

My motor arrangement is like one motor at the back for forward/backward and one motor at the front for left/right movement.

What i want to have is that whenever i press two keys UP+LEFT then the two motor should work one should move forward(the back motor) and the other in left(front motor).

docel
10-29-2008, 03:39 AM
Like I said earlier, the parallel port is a latching output- it will retain the last state till updated or changed.
I think you didnt understand......!
Nor do I : why do you want both fwd + left simultaneously???
Forward is 0101
left is 0001
The binary can be only one of these states and will execute if you press fwd first and then left, but not both . In that case it will latch the first pressed key and execute a forward and continue forward unless you release the key and press something else.

Why not increase the keys like anilmaddala suggested??

ashish_agarwal123456
10-30-2008, 11:31 AM
i dont know how to do it but there must be a way to do it...... in computer games even 4-5 keys works together. so for sure this can be done...... may be someone with experience in computer game designing may help!!!

docel
11-01-2008, 12:35 AM
Sure you can!!!
You just have to punch some lengthy math functions, thats all!
....and that may take quite some time for the OP to master.........but not worth it for the present task at hand.
So: at the moment, can do with 9 keys.............

mk
07-09-2009, 12:12 AM
I think you can use something like this.

When you keep a key pressed, it gets typed continously until you press another key.

Consider this:

aaaaaagggggggggggjjjjjjjjjjjjjj

I pressed the a key (KeyDown) then pressed g, and then j. j gets while all three- a,g,j are pressed.

What I mean is that for a and g, the Key Up event has not yet occured. i.e. you have there Key Down event, but not the Key Up event.

Now make the program something like this at you PC side.

Scan Inside a loop for the KeyCode with KeyDown Event, store its status in temperory variable and execute its action. Next time you if another keyCode is retrieved for the KeyDown event, then check if any temperory variable has status 1 ( corosponding that some other key is also pressed. Now take action sequentionally and continously (using delay_ms(50) )for both the keys that are pressed.

example- Suppose for UP arrow key I use variable U, for Down, D, for Left L, and for Right R.

Now when UP key is pressed, KeyDown event occurs, and you set U=1.
Suppose Next time you press the Left arrow key. So now you set L=1. But also you also see that U is also 1. i.e. Up and Left keys are simultaneously pressed. So now you send over your Parallel port


1000
delay_ms(10)
0010
delay_ms(10)

So, effectively you'l see both your motors running simultaneously.


Now suppose that keeping UP pressed, you now remove the LEFT key, So, now you get the Key Up event. So, now set L=0 And you send command only for the Forward.





Loop
{

KeyDown Event (KeyCode)
{

switch (KeyCode)
{
case UP: U=1;
case Down: D=1;
case Left: L=1;
case Right: R=1;
}
}

KeyUP Event (KeyCode)
{

switch (KeyCode)
{
case UP: U=0;
case Down: D=0;
case Left: L=0;
case Right: R=0;
}
}


if(U==1 And L==1)
{
send code: forward
delay
left
}
else if (U==1 And R==1)
{
send code: forward
delay
right
}
else if (U==1 and L==0 and R==0)
{
send code:forward
}

else........ <whatever>




}