Friday, March 27, 2009

Internet Controled Car v2 Parallel Port



My first version of the Internet Controled Car used a BASIC stamp to handle serial communication. This was a mundane task for a complex and expensive little microchip

This new version uses a parallel port printer cable which is controlled by a C program on the server. Using a parallel port also fixed an issue where the direction commands would get "stuck" if you hit two keys at once as the BASIC stamp code didn't have a buffer. I also mounted the components to a piece of wood and some standoffs so it wasn't a mess of wires all over the place.


The Parallel Port C Program

The parallel port program, written in C, will take a parameter to set the data lines, that is pins 2 through 9, to a logic high or logic low.

Example: ppp 1l 2l 3h 5h 8l



Here is the C source code for Linux.

#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/io.h>

//#include <asm/io.h>
// Didn't work, replaced with sys/io.h


char *binprint( unsigned char x, char *buf )
{
int i;
for( i=0; i<8; i++ )
buf[7-i]=(x&(1<<i))?'1':'0';
buf[8]=0;
return buf;
}

int main( int argc, char *argv[] )
{
char c;
unsigned char val;
char buf[9];
int x;
if( argc<2 )
{
printf("Example usage: ppp 1l 2l 3h 5h 8l\n");
return 2;
}
if( ioperm(888,1,1) )
{
printf("Couldn't find parallel port (888)\n");
return 1;
}
val = inb(888);
printf("old = %s\n",binprint(val,buf));
for( x=1; x<argc; x++ )
if( argv[x][1]!='h' )
val &= ~(1<<(argv[x][0]-'1'));
else
val |= 1<<(argv[x][0]-'1');
printf("new = %s\n",binprint(val,buf));
outb(val,888);
return 0;
}

To compile save the above code, in a text editor, as ppp.c
Then in the command line execute,
gcc ppp.c -o ppp

And then drop the compiled executable into $/bin/ so it can be executed from anywhere as a linux command.

Flash controller code:
The flash controller is pretty similar to netcar v1, it detects what arrowkey is being pressed and sends the information to the PHP script. I did change the variables though so use the new actionscript,

Download: car_remote_pp.as

PHP
The PHP program, combined with the C program, is basiaclly replacing what the BASIC stamp did, turning pins high and low to control the relays.

The PHP program gets the command from the flash aplication and, depending on what that command is, executes the C program with the paramaters to turn the proper data pins logic high or low.

In my hardware configuration I mapped pin 2 for forward, so to make the car go forward you would execute,
ppp 1h
2l
Pin 3 is mapped to backward so I make 2 a logic low because you can't be going forward and backward at the same time.

To make PHP execute a program you put the command in single quotes, for example 'ppp 1h 2l'

Let this PHP file sit in the same directory as your flash app.

Hardware
The rest of the hardware, for me, is the same as Net Car v1 sans the Basic Stamp. You will have to find a parallel port cable to splice open and use a multimeter with a continuity check to find what wires correspond to the data pins and, of course, ground. If you can find a ribbon cable parallel port those are very easy to solder into a chip socket and can be plugged into and removed from a breadboard or another chip socket if you decide to make a circuit board.

The hardware interface to the car remote circuit will be a little bit different depending on how the remote works. I used some double pole, double throw relays, turned on by transistors, to make the connections on the circuit board, which replaced what would normally be the connections made by the controller sticks. Now that I think about it I could have just used the transistors. You may have to put a diode on the base to make sure no higher voltage can back-feed into the lower voltage TTL circuitry, because most car remotes operate on 9v. Please correct me if I am wrong!

Edit: A couple of people recommended in the comments to use optoisolators instead of relays. They draw less power off the parallel port and are simpler, and quieter, than having a transistor turn on a relay. Actually now that I think of it using relay's is probably the most ridiculous idea I have ever had! I kinda like the clicking sound though :-)

The next step is to put a mini wireless webcam on it and stream the video through a webcam service. Or you can just have a webcam look at the car in a room and drive it around with an overview.

Let me know if you complete this project successfully, take a video and post it as a video response to my youtube video.

9 comments:

Drew Perttula said...

Ha ha! My http://bigasterisk.com/projects/parallel/parcon.c code (from 2002) strikes again! Looks like your stack is running ok, although nowadays I would do the php and C parts in a single python program, myself.

For parallel port comm in python, you can use the ubuntu package 'python-parallel'. Then "import parallel", "port=parallel.Parallel(port=0)", "port.setData(value)", etc.

BTW, I just did a very similar project where the parallel port pins close the switches on some remote-control lights (http://www.amazon.com/gp/product/B000HAVVKG). Instead of relays, I used optoisolators (http://www.futurlec.com/LED/4N26.shtml) which put less load on the parport and they're quicker and quieter of course. Those remotes run on 12V, as it turns out.

cheers-
drewp

BlackCow said...

Oh wow that's the same code! My friend gave me some source, I assumed he wrote it. I'll be sure to credit you, props man. :-)

I am working on some software for an eprom burner I was thinking about using python for the serial communications, thanks for the links.

tsaavik said...

I'd like to second the recommendation for using optical-isolators instead of relays, it would simplify your circuit immensely and provide protection for your PC in the case of an accidental over current draw.

I've been using 4 NTE3081's in my project an online web controlled r/c car at (http://www.hellspark.com/vcar) for the last 10-11 years.

Drew Perttula said...

I'm not positive the code originated with me, but probably at least some of it did because I went to the trouble to post it. Don't worry about the credit; it's not even code I would recommend to people now :)

Thanks for posting your project and the rest of the code!

Pippo said...

hello, can use to windows script? is very important, Thanks

BlackCow said...

Hello Pippo, I am not familiar with windows servers. Assuming you are running Apache on your windows machine everything should be the same except for the actual parallel port controller program. I don't really program on windows but I imagine a google search for something along the lines of "controlling parallel port windows C" should give you some helpful results.

You may also want to take Drew Perttula's advice and use python as it is cross platform and very simple (also very different)! I have been playing around with python; I would defiantly recommend it to someone who is a bit new to programming.

And of course you could always give Linux a try :-)

Pippo said...

ok thanks BlackCow, will use Linux ;). I can not download car_remote_pp.as you reload it.

BlackCow said...

Ah, I fixed the link to "car_remote_pp.as".

Good luck Pippo.

AxeFestis said...

Keep up the good work man!