Page 1 of 4 1234 LastLast
Results 1 to 10 of 35

Thread: Controlling AX or RX servos using arbotix

  1. #1
    Join Date
    Dec 2007
    Location
    Whidbey Island, WA
    Posts
    1,718
    Images
    456
    Rep Power
    95

    Controlling AX or RX servos using arbotix

    Ok, so I tried to come up with a program that would take data from the serial port and use it to control AX-12 servos. The idea is that you send two numbers out the serial port, one being the servo the other being the position and the arbotix would move the servo that the desired position. Figured this would be useful to more than just me.

    Code:
    #include <ax12.h>
    #include <BioloidController.h>
    #include "poses.h"  // exported from PyPose
    
    BioloidController bioloid = BioloidController(1000000);
    
    void setup()
    {  
        // need to start the serial port
        Serial.begin(38400);
    
        // recommended pause
        delay(100);
    
        // stand up slowly    
        bioloid.loadPose(Start);
        bioloid.readPose();
        bioloid.interpolateSetup(500);
        
        while(bioloid.interpolating > 0)
        {
            bioloid.interpolateStep();
            delay(3);
        }
    }
    
    void loop()
    {
        // check for new messages over the serial port
        //looking for 2 numbers set with a comma..."1,512"
        //not sure int or x,y is even remotely close to workable.
        //also not sure if I can input numbers greater than 9 this way.
        int x,y = Serial.read()
    //possibly better as int x = serial.read()
    //                         int y = serial.read()
        ;
        if( x,y >= 0 )
    //if (x >= 0 ) and (y >=0 )
        {   
            // toggle LED so we know johnny5 is alive
            digitalWrite(0,HIGH-digitalRead(0));
        
            // only process if we aren't currently doing a sequence
            if(bioloid.playing == 0)
            {               
              //sets servo position using 2 numbers one for servo, other for position.
                Bioloid.SetPosition(x,y)
        
        if(bioloid.playing > 0)
        {
            // continue sequence
            bioloid.play();  
        }
      
    }
    }
    DB

  2. #2
    Join Date
    May 2008
    Location
    TN
    Posts
    853
    Images
    99
    Rep Power
    71

    Re: Controlling AX or RX servos using arbotix

    Nice work!
    "If A is a success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
    -
    Einstein

    Don't be a HelpVampire

  3. #3
    Join Date
    Dec 2007
    Location
    Whidbey Island, WA
    Posts
    1,718
    Images
    456
    Rep Power
    95

    Re: Controlling AX or RX servos using arbotix

    Quote Originally Posted by jes1510 View Post
    Nice work!
    would be if it worked.

  4. #4
    Join Date
    Sep 2010
    Location
    ಠ_ಠ
    Posts
    2,283
    Images
    27
    Rep Power
    275

    Re: Controlling AX or RX servos using arbotix

    I hope I am not being an ass or overwhelming and/or confusing anyone, as I really am trying to be helpful. I just have never used an arbotix and am not so good at the whole "helping people to figure things out for themselves" thing.

    A few things to remember about C/C++ in general: 1) all names are case-sensitive, 2) a semicolon is required at the end of each line of code, 3) braces make all the difference in execution order/occurrence.

    About the code specifically, does 'Serial.read()' return a single byte (8-bit char), 2 bytes (16-bit short), or 4 bytes (32-bit integer)? Also, in what format will you be sending data over the serial connection (Binary, Binary Coded Decimal, or ASCII)?

    For instance, if the 'Serial.read()' returns only a single byte and you want to use plain 8-bit ASCII characters as if you were typing on the keyboard, then you would use three bytes for the servo ID (up to 255 IDs allowed by dynamixels) and four bytes for the servo position (AX-12+ position values range from 0 to 1023).
    This is certainly not the most efficient code and is ignoring the use of a ',' as delimiting the two values, but I was going mostly for human readable:
    Code:
        int x, x0, x1, x2;
        int y, y0, y1, y2, y3;
    
        x2 = (Serial.read()&0x000F)*100; // ignore the top nibble to convert to BCD and then put in hundreds
        x1 = (Serial.read()&0x000F)*10; // ignore the top nibble to convert to BCD and then put in tens
        x0 = (Serial.read()&0x000F); // ignore the top nibble to convert to BCD and then put in ones
        x = x2 + x1 + x0; // sum the three decimal digits
    
        y3 = (Serial.read()&0x000F)*1000; // ignore the top nibble to convert to BCD and then put in thousands
        y2 = (Serial.read()&0x000F)*100; // ignore the top nibble to convert to BCD and then put in hundreds
        y1 = (Serial.read()&0x000F)*10; // ignore the top nibble to convert to BCD and then put in tens
        y0 = (Serial.read()&0x000F); // ignore the top nibble to convert to BCD and then put in ones
        y = y3 + y2 + y1 + y0; // sum the four decimal digits
    This code is assuming we are sending data with the Most Significant digit first, so a servo ID of "213" would be sent as three ASCII characters: '2', then '1', then '3'. If we are assuming that there will never be anything other than a single ASCII coded decimal digit sent at one time, then we can just ignore the top nibble (most significant 4-bits) as the decimal number '2' is represented in ASCII coding by 0x32 (0011 0010 in binary) (50 in decimal). A nice thing about ASCII is that the representations of the decimal digits '0' through '9' are simply the Binary Coded Decimal value in the lower nibble with 3 in the upper nibble. '2' is 0x32, '5' is 0x35, etc.
    Confused yet?

    Added a few basic fixes to your code, but it will still need work depending on what you want it to do.
    Code:
    #include <ax12.h>
    #include <BioloidController.h>
    #include "poses.h"  // exported from PyPose
    
    BioloidController bioloid = BioloidController(1000000);
    
    void setup()
    {  
        // need to start the serial port
        Serial.begin(38400);
    
        // recommended pause
        delay(100);
    
        // stand up slowly    
        bioloid.loadPose(Start);
        bioloid.readPose();
        bioloid.interpolateSetup(500);
        
        while(bioloid.interpolating > 0)
        {
            bioloid.interpolateStep();
            delay(3);
        }
    }
    
    void loop()
    {
        // check for new messages over the serial port
        //looking for 2 numbers set with a comma..."1,512"
        //not sure int or x,y is even remotely close to workable.
        //also not sure if I can input numbers greater than 9 this way.
    
    //  int x,y = Serial.read()
        // If it is C/C++, then the line above declares two integer variables named 'x' and 'y', but calls the
        // function Serial.read() only once and assigns the resulting value to the variable 'y'
    
    // For whatever format you decide to send the Serial data stream in, the code would go here:
        int x = Serial.read();
        int y = Serial.read();
    
    //  C/C++?  Could add parenthesis around each condition for readability
        if ( x >= 0 && y >=0 )
        {   
            // toggle LED so we know johnny5 is alive
            digitalWrite(0,HIGH-digitalRead(0));
        
            // only process if we aren't currently doing a sequence
            if(bioloid.playing == 0)
            {               
              //sets servo position using 2 numbers one for servo, other for position.
                bioloid.SetPosition(x,y);
            }
    
        // Might want to change this to 'else' or 'else if' (bioloid.playing should have only two 
        // states/values '1' or '0'), but not sure it is really necessary to change it.  
            if(bioloid.playing > 0)
            {
                // continue sequence
                bioloid.play();  
            }
        }
    }

  5. #5
    Join Date
    Dec 2007
    Location
    Whidbey Island, WA
    Posts
    1,718
    Images
    456
    Rep Power
    95

    Re: Controlling AX or RX servos using arbotix

    Hey Tician,

    First off thanks for your help with this.

    I am trying to write a program that would allow a user to pass the arbotix two numbers through the serial port. For example 19,512 The arbotix would respond by moving servo 19 to position 512. We could later change it so that the arbotix would respond to different inputs for example 19,D300 would move servo 19 to 300 degrees...We could also use 19,R1.57 so that the arbotix would move servo 19 to 1.57 radians. That way anyone could write a program that could control servos on an arbotix relatively easily.

    Second to the larger trossen community.

    The idea here isn't to help someone help themself. The idea here is to get working code that any of us can use. I am trying to make the arbotix which is already dead simple to use more simple to use with other software. So...if you have a solution that would work in this situation, then please post it. I'm sorry if I sound snippy, I don't mean to and am greatful for any help that anyone can bring to the table.

    DB

  6. #6
    Join Date
    May 2008
    Posts
    2,228
    Images
    155
    Rep Power
    125

    Re: Controlling AX or RX servos using arbotix

    DB -- I have some older code that does a similar structure (except it used commands like "GET IR 1") -- same idea though, it'll probably only take me about ~15 minutes to fix it up for this task -- but I just haven't had time yet....

    (Hopefully tonight I'll have a bit of time to do this, and I also wanted to update PyMech to use the new commander protocol, so that people with ArbotiX/NUKE based mechs have an out-of-the box control method, since I know at least Cire would be happy to not have to dig in and update the code...)

    -Fergs
    Last edited by lnxfergy; 10-29-2010 at 11:51 AM.

  7. #7
    Join Date
    Dec 2007
    Location
    Whidbey Island, WA
    Posts
    1,718
    Images
    456
    Rep Power
    95

    Re: Controlling AX or RX servos using arbotix

    Thanks Fergs, Seams like we could all use it.

  8. #8
    Join Date
    Dec 2007
    Location
    Whidbey Island, WA
    Posts
    1,718
    Images
    456
    Rep Power
    95

    Re: Controlling AX or RX servos using arbotix

    Hey Guys,

    So...I had the fortune of having a really decent programmer (my cousin in law) come to my house for Thanks Giving. He helped me write the following for controlling servos using the arbotix. The structure is "p" servo number ","position "cr" and then it sets the servo position using that data. Tests resulted in nothing working, but I'm not sure I sent the data right. Here is our code. Any help would be appreciated.

    DB

    Code:
    #include <ax12.h>
    #include <BioloidController.h>
    #include "iPose01.h"  // exported from PyPose
    
    BioloidController bioloid = BioloidController(1000000);
    
    void setup()
    {  
        // need to start the serial port
        Serial.begin(38400);
    
        // recommended pause
        delay(100);
    
        // stand up slowly    
        bioloid.loadPose(up);
        bioloid.readPose();
        bioloid.interpolateSetup(500);
        
        while(bioloid.interpolating > 0)
        {
            bioloid.interpolateStep();
            delay(3);
        }
    }
    
    void loop() {
        char cmd[8];
        int startByte = 0;
        
        // if there are bytes waiting on the serial port
        if(Serial.available() > 0) {
          // read the incoming byte
          startByte = Serial.read();   
        } // end Serial.available()
        if(startByte == 'p') {
          // get the command from the serial port
          if(Serial.available() >= 8) {
            for(i=0;i<8;i++){
              cmd[i] = Serial.read();
            }
          }
          
          // convert command to two numbers
          int servoNum = 0;
          int servoPos = 0;
          
          servoNum = cmd[0]*10 + cmd[1];
          servoPos = cmd[3]*1000 + cmd[4]*100 + cmd[5]*10 + cmd[6];
          
          //sets servo position using 2 numbers one for servo, other for position.
          Bioloid.SetPosition(servoNum,servoPos);
          
          // debug
          Serial.print("cmd array:");
          for(i=0;i<8;i++){
            Serial.print(cmd[i]);
          }
          Serial.println("----");
          Serial.println("servoNum:"+servoNum);
          Serial.println("servoPos:"+servoPos);
          
        }
          
    } // end loop()

  9. #9
    Join Date
    May 2008
    Posts
    2,228
    Images
    155
    Rep Power
    125

    Re: Controlling AX or RX servos using arbotix

    From what I can see right off the bat DB, you probably want to change how you're looping. Currently, you are reading the first byte (start byte) but then checking to see if there are 8 more bytes available -- if not, you lose that start byte, rather than waiting for more bytes to arrive, I would do the following:

    Code:
    #include <ax12.h>
    #include <BioloidController.h>
    #include "iPose01.h"  // exported from PyPose
    
    BioloidController bioloid = BioloidController(1000000);
    
    void setup()
    {  
        // need to start the serial port
        Serial.begin(38400);
    
        // recommended pause
        delay(100);
    
        // stand up slowly    
        bioloid.loadPose(up);
        bioloid.readPose();
        bioloid.interpolateSetup(500);
        
        while(bioloid.interpolating > 0)
        {
            bioloid.interpolateStep();
            delay(3);
        }
    }
    
    void loop() {
        char cmd[8];
        int startByte = 0;
        
        // if there are bytes waiting on the serial port
        if(Serial.available() > 0) {
          // read the incoming byte
          startByte = Serial.read();   
        } // end Serial.available()
        if(startByte == 'p') {
          // get the command from the serial port
    
    // START FERGY EDITS HERE
          while(Serial.available < 8); // wait for data to arrive, do nothing      
          // get rid of this... if(Serial.available() >= 8) {
          for(i=0;i<8;i++){
            cmd[i] = Serial.read();
          }
          //} also get rid of this....
    // END THE FERGY EDITS
          
          // convert command to two numbers
          int servoNum = 0;
          int servoPos = 0;
          
          servoNum = cmd[0]*10 + cmd[1];
          servoPos = cmd[3]*1000 + cmd[4]*100 + cmd[5]*10 + cmd[6];
          
          //sets servo position using 2 numbers one for servo, other for position.
          Bioloid.SetPosition(servoNum,servoPos);
          
          // debug
          Serial.print("cmd array:");
          for(i=0;i<8;i++){
            Serial.print(cmd[i]);
          }
          Serial.println("----");
          Serial.println("servoNum:"+servoNum);
          Serial.println("servoPos:"+servoPos);
          
        }
          
    } // end loop()
    There may be other issues -- but that's the one that jumps out to me real fast. See if that at least gets it doing something, then we could look further.

    -Fergs

  10. #10
    Join Date
    Dec 2007
    Location
    Whidbey Island, WA
    Posts
    1,718
    Images
    456
    Rep Power
    95

    Re: Controlling AX or RX servos using arbotix

    So now I'm getting this
    Code:
    error: redefinition of 'BioloidController bioloid' In function 'void setup()':
     In function 'void loop()':
    error. Just got back from a party. Unfortunately I won't be able to test this out until I get a new xbee...(just had a minor xbee mishap. Chris wants to deny helping me with my code because of what your edit...so it must be really good.

    DB

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Discussion Parallel Leg Mechanism Design
    By DresnerRobotics in forum Humanoids, Walkers & Crawlers
    Replies: 87
    Last Post: 06-24-2012, 10:13 AM
  2. ArbotiX ROS Package 0.3.0
    By lnxfergy in forum ROS - Robot Operating System
    Replies: 233
    Last Post: 02-06-2011, 11:36 AM
  3. Question(s) Newby Arbotix hassles
    By Slugman in forum Robot Computers
    Replies: 29
    Last Post: 11-15-2010, 08:52 AM
  4. Controlling Servos with a servo controller
    By bodhibuilder in forum Arbotix, Microcontrollers, Arduino
    Replies: 25
    Last Post: 09-29-2008, 11:07 AM
  5. Question(s) Controlling servos using Midi...help
    By darkback2 in forum Software and Programming
    Replies: 38
    Last Post: 07-29-2008, 03:17 AM

Posting Permissions

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