The motor controller works fine when connected to the basic stamp, running the example code from the manual, but when I connect it to a logic level UART from a different “embedded linux” board I can’t get the controller to respond.
I have verified that the controller is wired correctly and that there is a signal coming from the UART.
Here is the C code that I’m using to test:
int main( int argc, char *argv[] )
{
int i;
char *portName = "/dev/ttyS2";
int portFd = -1;
struct termios attr;
char buffer[10];
int size;
if( argc <= 1 ) {
fprintf( stderr, "program requires parameter\n");
exit( 1 );
}
if(( portFd = open( portName, O_RDWR | O_EXCL )) < 0 )
{
fprintf( stderr, "Unable to open serial port '%s': %s\n", portName, strerror( errno ) );
exit( 2 );
}
if( tcgetattr( portFd, &attr ) < 0 )
{
fprintf( stderr, "Call to tcgetattr failed: %s\n", strerror( errno ) );
exit( 3 );
}
attr.c_iflag = 0;
attr.c_oflag = 0;
attr.c_cflag = CLOCAL | CREAD | CS8;
attr.c_lflag = 0;
attr.c_cc[ VTIME ] = 0; // timeout in tenths of a second
attr.c_cc[ VMIN ] = 1; // only wait for a single char
speed_t baudRate = B9600;
cfsetispeed( &attr, baudRate );
cfsetospeed( &attr, baudRate );
if( tcsetattr( portFd, TCSANOW, &attr ) < 0 )
{
fprintf( stderr, "Call to tcsetattr failed: %s\n", strerror( errno ) );
exit( 4 );
}
if(!strcmp(argv[1],"on")) {
buffer[0] = 0x80; // start byte
buffer[1] = 0x00; // motor controller device
buffer[2] = 0x01; // motor 0 in forward
buffer[3] = 0x7F; // full speed (127)
size = 4;
} else {
buffer[0] = 0x80; // statrt byte
buffer[1] = 0x00; // motor controller device
buffer[2] = 0x00; // motor 0 in reverse (brake)
buffer[3] = 0x00; // stop (0)
size = 4;
}
// write command to serial
if( write( portFd, &buffer, size ) != size )
{
fprintf( stderr, "write to serial port failed: %s\n", strerror( errno ) );
exit( 5 );
}
close( portFd );
return 0;
}
Does it matter that the serial, logic supply, and reset are all running at ~3.3V and may not exactly match? (ex. the logic supply is about 3.28V and the serial & reset are about 3.31V)
Which motor controller are you using? Not all of them support 3.3V operation. For those that do, the 3.31V vs. 3.28V is not an issue.
We cannot troubleshoot all of your code, but it looks like it could be okay based on a quick glance. We recommend that you simplify it to a single command to the motor controller. This should make it easier for you to narrow down the problem and make it more likely that someone will look at all of your code and find a problem.
Can you connect your serial output to some other device to verify that it’s sending what you expect?
It looks like your mistake is passing &buffer as an argument to write() instead of just buffer. So in effect you are writing the location of the array out to the serial port, instead of the values within the array.
Serial port code is hard to debug. I advise you to write the output to a file or to stdout if you continue to have problems like this, to make sure that you are generating the correct bytes.
Please let us know if it works with that modification. Thanks,
-Paul
The 3.3 V power supply should not be a problem. Once you get the oscilloscope, you might check the power supply to make sure that there aren’t any glitches on it.
I’m not sure why you’re so skeptical about the 3.3V operation, but you can easily do a test with your basic stamp if you want to be sure that your unit is not defective: divide your basic stamp signals in half with pairs of 1k resistors, and see if it works. Use your 3.3V supply for the logic supply, and the 2.5V signals should be high enough.