Results 1 to 4 of 4

Thread: AX-12 controled by Serial communication

  1. Question AX-12 controled by Serial communication

    Hey,

    i´m trying to get my AX-12-a Servo´s running by an Serial input via pc.
    So i want to write "move 1 542" and my AX-12 with ID 1 has to move to position 542.
    I want to use the Dynamixel library.

    I´m using an Arduino Mega 2560.

    I found this code on the Internet.

    Code:
    #define STRING_SIZE 16
    #define MAX_ARGS 5
    
    
    void setup()
    {
     Serial.begin(9600);
     Serial.flush();
    }
    
    
    void loop()
    {
     if( Serial.available() > 0 ) {  // command length is 6 bytes
       delay(10);
       char* cmdbuf = (char*)malloc(sizeof(char) * STRING_SIZE);
       char c;
       int i = 0;
       while( Serial.available() && c != '\n' ) {  // buffer up a line
         c = Serial.read();
         cmdbuf[i++] = c;
       }
    
    
       i = 0;
       while( cmdbuf[++i] != ' ' ) ; // find first space
       cmdbuf[i] = 0;          // null terminate command
       char* cmd = cmdbuf;     
       int cmdlen = i;         // length of cmd
       int args[5] = {0}, a;         // five args max, 'a' is arg counter
       char* s;
       char* argbuf = cmdbuf+cmdlen+1;
    
    
       while( (s = strtok(argbuf, " ")) != NULL && a <= MAX_ARGS ) {
         argbuf = NULL;
         args[a++] = (byte)strtol(s, NULL, 0); // parse hex or decimal arg
       }
    
    
       int argcnt = a;         // number of args read
       Serial.flush();
       
       delay(1000);
       
       //wrote to test input and output
       //  Serial.write(cmd);  //works in this conditions
       // Serial.write(' ');
       // Serial.write(s);
       //if you wrote something in de command line like "test 512" you will recieve
       // test 512
      
        }
        
        
    }
    Now i want to get the Number to write in to the Position of my servo.

    It has to look like

    Code:
    int inputPosition = arg2;  //arg2 from the Input Buffer or so
    
    Dynamixel.write(1, inputPosition);
    How can i get the argument as an integer from the char* -array?
    If someone has any idea that can help me ím plaesed to say it to me.

    sorry for my bad english.

    Thx Patrick

  2. #2

    Re: AX-12 controled by Serial communication

    The atoi() function converts from a char* pointing to a text representation of a number (say, "1234") and an integer (say, 1234).

    Also, the strtol() function does that, with slightly different usage. You are already using strtol() in your program above, though, putting the integers on that line into args[0], args[1] , ... args[n-1] (for max n of 5.)

  3. Re: AX-12 controled by Serial communication

    Thx jwatte,

    i tried to do this, but i´m not able to get in the right order. I´ve got an other problem. It is not possible to write only the argument without the command. Every time i tried to Serial.write(arg[0]); it doesn´t worked.
    Could you help me out with this?

    thx
    Patrick

  4. #4

    Re: AX-12 controled by Serial communication

    Do you want to write the argument in binary format, or text format?
    The arg[0] is a binary (normal) integer. The write() function may treat this a "a single character" and throw away the high byte.
    If you want to write it as text, you may have to convert it to a string, using something like sprintf().
    If you want to write it in binary, you have to explicitly write it in a byte order -- high byte, then low byte, or vice versa.
    Big endian example:
    Code:
    Serial.write(args[0] >> 8); Serial.write(args[0] & 0xff);
    Text example:
    Code:
    char buf[12];
    sprintf(buf, "%d", args[0]);
    Serial.write(buf, strlen(buf));
    And apologies if this is slightly off, it's been a long time since I've used the Serial class so I may recall the order of arguments or what-not wrong.

    By the way, when you say "it does not work," it would be useful to describe exactly what goes wrong. Does it fail compilation? Or does it compile, but do the wrong thing when running? If so, have you verified what it is actually doing when running? A logic analyzer or oscilloscope on the output to verify the output data would be very useful.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Robot controled by a Kinect
    By Gort in forum Project Showcase
    Replies: 1
    Last Post: 04-16-2012, 09:37 AM
  2. Question(s) RAW Serial Communication
    By ilo_oli in forum Software and Programming
    Replies: 5
    Last Post: 11-01-2011, 05:38 PM
  3. SSC-32 communication
    By Bevels in forum Software and Programming
    Replies: 14
    Last Post: 07-18-2009, 11:23 PM
  4. Question(s) RoboRealm and Axon serial communication issues
    By Resilient in forum Software and Programming
    Replies: 2
    Last Post: 05-15-2009, 04:06 AM
  5. C++ Serial Communication
    By Resilient in forum Software and Programming
    Replies: 14
    Last Post: 02-03-2009, 03:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •