Shop OBEX P1 Docs P2 Docs Learn Events
write() C function to Propeller Servo Controller — Parallax Forums

write() C function to Propeller Servo Controller

QuadrtrFlyrQuadrtrFlyr Posts: 73
edited 2013-01-03 10:02 in Propeller 1
Hello folks,

I have once again stumbled upon a problem which I have been trying to solve for a good amount of time but to no avail.. I am currently trying to control the Parallax Propeller Servo Controller via Linux over USB. I have done all of the nitty gritty work of opening up a virtual serial port and being able to read from the device but I am having major problems writing commands to the controller.

It all basically comes down to one function which in C is write(). The Servo Controller requires 8 bytes of information to set a position of a servo so I need the following format
Syntax: “!SC” <channel> <ramp speed> <lowbyte> <highbyte> <CR>
Reply: None

So using the write C command which is in the following format:
#include <fcntl.h>
int write( int handle, void *buffer, int nbyte );

I have successfully passed "CHARACTERS" to the servo controlller. (ex. !SCVER?) However when I try to send a mix of characters AND integers I come across problems, the servo controller does not respond to my commands.

Here is my command
n = write(tty_fd,("!SC",&i,&i,&lowbyte,&highbyte,"\r"),8);

I do not think this is in the correct format.... Could anyone lend a helping hand in helping me solve this problem? I tried googling for hours but with little luck. The mix of characters and integers is what seems to be the problem. I would send all 8 bytes of characters instead of mixing integers but since decimals 0-31 are not typical characters, writing these values is not easy. I also tried splitting up the write command into first sending the characters !SC and then the integers separately and then the return carriage but that did not work either....

Many Thanks!
Robert

Here is the entire code:
#include <string.h>#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <wiringSerial.h>
 
int main(int argc,char** argv)
{
        struct termios tio;
        struct termios stdio;
        struct termios old_stdio;
        int tty_fd;
 
        unsigned char c='D';
        tcgetattr(STDOUT_FILENO,&old_stdio);
 
        memset(&stdio,0,sizeof(stdio));
        stdio.c_iflag=0;
        stdio.c_oflag=0;
        stdio.c_cflag=0;
        stdio.c_lflag=0;
        stdio.c_cc[VMIN]=1;
        stdio.c_cc[VTIME]=0;
        tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
        tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
        fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);       // make the reads non-blocking
 
        memset(&tio,0,sizeof(tio));
        tio.c_iflag=0;
        tio.c_oflag=0;
        tio.c_cflag=CS8|CREAD|CLOCAL;           // 8n1, see termios.h for more information
        tio.c_lflag=0;
        tio.c_cc[VMIN]=1;
        tio.c_cc[VTIME]=5;
        


        tty_fd=open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK);      
        cfsetospeed(&tio,B2400);            // 115200 baud
        cfsetispeed(&tio,B2400);            // 115200 baud


        tcsetattr(tty_fd,TCSANOW,&tio);


        int count = 0;
        int n=0;
        unsigned char buffer[8]; 
        //FIND LOWBYTE AND HIGHBYTE BASED ON INPUTED SPEED
        int speed = 800;
        printf("speed: %d\n\r", speed);
        int highbyte = speed >> 8;
        printf("highbyte: %d\n\r", highbyte);
        highbyte = highbyte << 8;
        int lowbyte = speed-highbyte;
        printf("lowbyte: %d\n\r", lowbyte);

        n = write(tty_fd,("!SC",&i,&i,&lowbyte,&highbyte,"\r"),8);
        if (n < 0)
            fputs("write() of 8 bytes failed!\n\r", stderr);
        else
            printf ("Write succeed n = %i\n\r", n );
 
        close(tty_fd);
        tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
 
        return EXIT_SUCCESS;
}

Comments

  • QuadrtrFlyrQuadrtrFlyr Posts: 73
    edited 2013-01-03 10:02
    Solution to problem can be found here to those interested!

    http://www.raspberrypi.org/phpBB3/viewtopic.php?f=33&t=28060&p=249714#p249714
  • Can you post the code you used on the Prop end of things?

    Can you describe the nitty gritty stuff you had to do to make this work?
  • TorTor Posts: 2,010
    edited 2015-07-21 08:05
    Didn't read the pi link, but the right approach would be to create an array of bytes, and fill it up with what you need. Something equivalent to
    char buffer[8];
     memcpy (buffer, "!SC", 3);
     buffer[3] = (char) i; buffer[4] = (char) i; // Where did you set &#39;i&#39; btw? I&#39;m just looking at the write line
     buffer[5] = (char) lowbyte;
     buffer[6] = (char) highbyte;
     buffer[7] = &#39;
&#39;;
     write (tty_fd, buffer, 8 );
    
    And btw you should work with chars or unsigned chars, not ints, when you want to write byte-sized data.
  • Can you post the code you used on the Prop end of things?


    I couldn't find all the files that go with the Propeller Servo Controller online but I had them on my hard drive. I added a post to the Robotics Forum with links to the files I found online and with the missing files attached to the post.
    http://forums.parallax.com/discussion/161702/resources-for-propeller-servo-controller-usb
Sign In or Register to comment.