Shop OBEX P1 Docs P2 Docs Learn Events
Problem with Propeller Servo Controller USB — Parallax Forums

Problem with Propeller Servo Controller USB

LarryPotterLarryPotter Posts: 8
edited 2010-06-12 19:56 in Robotics
I purchased a Propeller Servo Controller USB·to control Parallax's Motor Mount and Wheel Kit with Position Controller.· I am using the [size=-1]Flashlite 386Ex[/size]·by JK Microsystems for the controller.· I am using the C programming language.· I have not been successful in sending commands to the Propeller Servo Controller.· I am using a serial connection.· The servo controller does work fine when I send commands with the Basic Stamp, so I know that the problem is not with the hardware.··I wrote to Parallax and asked if they had·any C programming examples for writing to this controller, and they directed me to this·forum.· Incidentally,·this is a robot project, and I am using [size=-1]eRTOS[/size]·to allow multitasking.·

Post Edited By Moderator (Bean) : 6/10/2010 3:12:56 PM GMT

Comments

  • BeanBean Posts: 8,129
    edited 2010-06-10 15:09
    Are you sure the serial output is the correct polarity (True or inverted) ?

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.
    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There are two rules in life:
    · 1) Never divulge all information
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-06-10 17:42
    If you have the USB version of the servo-controller, how are you talking to it without a USB port on the Flashlite?

    Also, I believe the BS2 talks to it with "RS232-TTL" signals -- and yes, they may be inverted from what the Flashlite is trying.
  • Mike GMike G Posts: 2,702
    edited 2010-06-10 18:04
    If you're not using the PSC's USB port, you'll need to supply proper power and ground to the electronics (not the same as servo power). 5V to the center pin (+) on of the the "serial in" port on the PSC and ground (-). Make sure properly ground your circuit. The signal line is a single wire so how are you connecting your serial communication? Through a tri-state or maybe a PNP/NPN circuit?

    As for C code... that's dependent on your hardware and OS. You might want to contact Flashlite and/or eRTOS support?

    Post Edited (Mike G) : 6/10/2010 8:48:30 PM GMT
  • Mike GMike G Posts: 2,702
    edited 2010-06-10 22:06
    Here's a simple "Get the PSC Version" in C running on windows. The PC is connected to the PSC through the USB port. Hope this helps.

    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //Setup the serial port
    void serialConfig(HANDLE h, long baud);
    
    int main(int argc, char **argv)
    {
        HANDLE serial_port;                /* Handle to the serial port */
    
        long baud_rate = 2400;            /* Specified baud rate */
        //char port_name[noparse][[/noparse]] = "COM6";        /* Name of the serial port */
    
        // Get PSC version
        //&#8220;!SCVER?&#8221; <CR>
        char pscVer[noparse][[/noparse]] = {"!SCVER?\r"};
    
        unsigned long bytes_written;    /* Bytes written to serial port */
    
    
        
        // Open up a handle to the serial port
        //serial_port = CreateFileA(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
    
        serial_port = CreateFile(TEXT("COM6"), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    
        // Make sure port was opened
        if (serial_port == INVALID_HANDLE_VALUE)
        {
            fprintf(stderr, "Error opening port\n");
            CloseHandle(serial_port);
            exit(0);
        }
    
        // Set up the serial port
        serialConfig(serial_port, baud_rate);
    
        // Send the PSC version command
        WriteFile(serial_port, pscVer, sizeof(pscVer), &bytes_written, NULL);
        printf("\nTransmitted !SCVER? [noparse][[/noparse]%lu bytes]\n", sizeof(pscVer));
    
    
        DWORD dwBytesRead = 0;
        char buffer[noparse][[/noparse]13];
        buffer[noparse][[/noparse]12] = '\0';
    
        // Read the serial port
        ReadFile(serial_port, buffer, 12, &dwBytesRead, NULL);
        printf("\nVersion %s \n", &buffer[noparse][[/noparse]9]);
    
        // Close the handle
        CloseHandle(serial_port);
    
        return 0;
    }
    
    void serialConfig(HANDLE h, long baud)
    {
        DCB properties;            /* Properties of serial port */
    
        /* Get the properties */
        GetCommState(h, &properties);
    
        /* Set the baud rate */
        switch(baud)
        {
        case 1200:
            properties.BaudRate = CBR_1200;
            break;
        case 2400:
            properties.BaudRate = CBR_2400;
            break;
        case 4800:
            properties.BaudRate = CBR_4800;
            break;
        case 9600:
            properties.BaudRate = CBR_9600;
            break;
        case 14400:
            properties.BaudRate = CBR_14400;
            break;
        case 19200:
            properties.BaudRate = CBR_19200;
            break;
        case 38400:
            properties.BaudRate = CBR_38400;
            break;
        default:
            fprintf(stderr, "Invalid baud rate: %ld", baud);
            exit(0);
            break;
        }
        
        /* Set the other properties */
        properties.Parity = NOPARITY;
        properties.ByteSize = 8;
        properties.StopBits = ONESTOPBIT;
    
        SetCommState(h, &properties);
    
        return;
    }
    
    

    Post Edited (Mike G) : 6/10/2010 10:17:25 PM GMT
  • LarryPotterLarryPotter Posts: 8
    edited 2010-06-12 15:03
    Thank you Bean, allanlane5, and Mike G. After writing my post, I had realized that the serial port on the 386Ex was inverted. So I used a 74LS04 on the transmit line to invert it. As far as the voltages, I do have 5 volts and grond supplied to the serial input of the servo controller. For my C code, I filled a byte array (insigned char) with the decimal equivalents of the speed command. Using a printf command to view what was being sent to the controller, all looks good. However, still no success. Istill need to look at the code Mike G posted.

    So, again, thanks loads guys for the feedback. I will let you know on the forum of any success.
  • LarryPotterLarryPotter Posts: 8
    edited 2010-06-12 19:56
    I finally got the 386Ex to talk to the Parallax Servo Controller.· The problem was the voltage levels.· I used an Acroname Serial Interface Connector I had on hand and now it is working fine.· If anyone is interested, following is the C code I used to drive the controller.

    /**************************************************/
    /*·················· << Position Command >>··································*/
    /*····················································································· */
    /*··················· Written by L. Wendell···································· */
    /*··············································································· ····· */
    /**************************************************/

    #include <rtos.h>
    #include <sio.h>
    #include <stdio.h>
    #include <driver.h>
    #include <string.h>
    #include <stdlib.h>

    //void SendCommand(char [noparse]/noparse);··· //Send command function
    void SerXmit(BYTE [noparse]/noparse);········· //Serial xmit function
    void Com1Init(void);··········· //Serial port initiate func
    void MotorTest(void);·········· //Test Motors using PSCU

    /**************** Main Thread ************************/
    void main(void)
    {
    ·kdebug = 1;
    ·rt_init( 200 );
    ·Com1Init();·· //Initiate camera com port (Com 1 386EX)
    ·rt_sleep(1000);
    ·rt_newthread(MotorTest, 1, 2048, 0, "MotorTest");
    ·do{
    ··rt_sleep(1000);
    ·}while(1);
    }//End of maiin thread

    /************** Com1Init function *********************/
    void Com1Init(void){····· //ititiate 386Ex Com 1 for camera

    ·sio_init(1,0x3F8,4,512,512,NULL,0);···· //These are RTOS functions...
    ·sio_setup(1,2400,8,0,1,0);
    }· //End of cam_sio function

    /**************** SerXmit function ********************/
    void SerXmit(BYTE string[noparse]/noparse){····· //Sends a string to camera.
    ·int i = 0;
    ·do{
    ··sio_writebyte(1,string[noparse][[/noparse]i]);
    ··//printf("Sent: %i, %c, \n", string[noparse][[/noparse]i], string[noparse][[/noparse]i]);
    ··printf("Sent: %i \n", string[noparse][[/noparse]i]);
    ··i++;
    ··if (string[noparse][[/noparse]i] == NULL) break;
    ·}while(1);
    } //End of SerXmit function

    /************** MotorTest Thread ********************/
    void MotorTest(){················· //Pos values of sonar_rot = CW rotation
    unsigned int ch = 1, ra = 7, pw; //, lonyb, hinyb;
    BYTE buffer[noparse][[/noparse]9], lonyb, hinyb;
    ·do{
    ··rt_yield();
    ··pw = 1100;
    ··lonyb = pw & 255;
    ··hinyb =(pw & 65280 ) >> 8;
    ··printf ("%i, %i \n", lonyb, hinyb);
    ··buffer[noparse][[/noparse]0] = 33;··· //"!"
    ··buffer[noparse][[/noparse]1] = 83;··· //"S"
    ··buffer[noparse][[/noparse]2] = 67;··· //"C"
    ··buffer[noparse][[/noparse]3] = ch;··· //"Channel "0"
    ··buffer[noparse][[/noparse]4] = ra;··· //rate "7"
    ··buffer[noparse][[/noparse]5] = lonyb; //Low Byte pulse width
    ··buffer[noparse][[/noparse]6] = hinyb; //High Byte pulse width
    ··buffer[noparse][[/noparse]7] = 13;··· //"\r"
    ··buffer[noparse][[/noparse]8] = NULL;
    ··SerXmit(buffer);
    ··rt_sleep(1500);

    ··pw = 400;
    ··lonyb = pw & 255;
    ··hinyb =(pw & 65280 ) >> 8;
    ····· printf ("%i, %i \n", lonyb, hinyb);
    ··buffer[noparse][[/noparse]0] = 33;··· //"!"
    ··buffer[noparse][[/noparse]1] = 83;··· //"S"
    ··buffer[noparse][[/noparse]2] = 67;··· //"C"
    ··buffer[noparse][[/noparse]3] = ch;··· //"Channel "0"
    ··buffer[noparse][[/noparse]4] = ra;··· //rate "7"
    ··buffer[noparse][[/noparse]5] = lonyb; //Low Byte pulse width
    ··buffer[noparse][[/noparse]6] = hinyb; //High Byte pulse width
    ··buffer[noparse][[/noparse]7] = 13;··· //"\r"
    ····· buffer[noparse][[/noparse]8] = NULL;
    ··SerXmit(buffer);
    ··rt_sleep(1500);

    ·}while(1);
    } //End of MotorTest thread

    ·
Sign In or Register to comment.