Announcement

Collapse
No announcement yet.

2 servos moving together

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 2 servos moving together

    I'm just trying to run two servos simultaneously. I'm trying to use the sample program and edit it for 2 servos. But with this, they work one after another. How can I have them work together? I'm using the double servo holder and would like to program them together for better support of a piece being lifted.

    Thanks,

    #pragma config(Hubs, S1, HTMotor, HTServo, none, none)
    #pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop)
    #pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
    #pragma config(Servo, srvo_S1_C2_1, servo1, servo1, tServoStandard)
    #pragma config(Servo, srvo_S1_C2_2, servo2, servo2, tServoStandard)
    #pragma config(Servo, srvo_S1_C2_3, servo3, servo3, tServoStandard)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

    /*--------------------------------------------------------------------------------------------------------*\
    |* *|
    |* - Servo Control by NXT Motor - *|
    |* ROBOTC on NXT + TETRIX *|
    |* *|
    |* This program simply sweeps the servo back and forth. Servo Change Rate can be changed by changing *|
    |* 'delta'. (0 = max servo speed, # > 0 = how many servo positions to move per update (every 20ms). *|
    |* *|
    |* ROBOT CONFIGURATION *|
    |* NOTES: *|
    |* 1) Be sure to attach a servo somewhere on the robot. *|
    |* *|
    |* MOTORS & SENSORS: *|
    |* [I/O Port] [Name] [Type] [Description] *|
    |* Port 1 none TETRIX Controller TETRIX *|
    |* Port 1 - Chan. 1 servo2 std. Servo The servo that you wish to test. *|
    \*---------------------------------------------------------------------------------------------------4246-*/
    task main()
    {
    int delta = 2; // Create int 'delta' to the be Servo Change Rate.

    while(true)
    {
    servoChangeRate[servo1] = delta; // Slow the Servo Change Rate down to only 'delta' positions per update.

    if(ServoValue[servo1] < 128) // If servo1 is closer to 0 (than 255):
    {
    while(ServoValue[servo1] < 255) // While the ServoValue of servo1 is less than 255:
    {
    servo[servo1] = 255; // Move servo1 to position to 255.
    }
    }

    wait1Msec(1000); // Wait 1 second.

    if(ServoValue[servo1] >= 128) // If servo1 is closer to 255 (than 0):
    {
    while(ServoValue[servo1] > 0) // While the ServoValue of servo1 is greater than 0:
    {
    servo[servo1] = 0; // Move servo1 to position to 0.
    }
    }

    wait1Msec(1000); // Wait 1 second.

    servoChangeRate[servo2] = delta; // Slow the Servo Change Rate down to only 'delta' positions per update.

    if(ServoValue[servo2] < 128) // If servo2 is closer to 0 (than 255):
    {
    while(ServoValue[servo2] < 255) // While the ServoValue of servo2 is less than 255:
    {
    servo[servo2] = 255; // Move servo2 to position to 255.
    }
    }

    wait1Msec(1000); // Wait 1 second.

    if(ServoValue[servo2] >= 128) // If servo2 is closer to 255 (than 0):
    {
    while(ServoValue[servo2] > 0) // While the ServoValue of servo2 is greater than 0:
    {
    servo[servo2] = 0; // Move servo2 to position to 0.
    }
    }

    wait1Msec(1000);
    }
    }
    Last edited by 4408; 01-30-2011, 08:38 PM.

  • #2
    I suggest not trying to solve this problem with programming. Instead using the Tetrix Servo Y Connector (W739082). This will allow you to connect both Servos into a single slot on the Servo Controller and both Servos will move at the same time with a single command.

    Comment


    • #3
      Originally posted by 4408 View Post
      I'm just trying to run two servos simultaneously. I'm trying to use the sample program and edit it for 2 servos. But with this, they work one after another. How can I have them work together? I'm using the double servo holder and would like to program them together for better support of a piece being lifted.
      Consider a couple things -- lay out your code so you can see it in blocks... that will help you see structures that occur over time

      Your "while" blocks only allow one servo at a time to operate. Perhaps you should consider only having two such blocks, with the two servo positions "anded" together ("&&") and both commands inside each block?


      Code:
         while((ServoValue[servo1] < 255) && (ServoValue[servo2] < 255)) // While the ServoValue of servos is less than 255:
         {
            servo[servo1] = 255; // Move servo1 to position to 255.
            servo[servo2] = 255; // Move servo2 to position to 255.
         }

      Comment


      • #4
        This is all you need. And format you code well.
        Code:
        #pragma config(Hubs, S1, HTMotor, HTServo, none, none)
        #pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop)
        #pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
        #pragma config(Servo, srvo_S1_C2_1, servo1, servo1, tServoStandard)
        #pragma config(Servo, srvo_S1_C2_2, servo2, servo2, tServoStandard)
        #pragma config(Servo, srvo_S1_C2_3, servo3, servo3, tServoStandard)
        //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
        
        task main() {
          int delta = 5; // Create int 'delta' to the be Servo Change Rate.
          servoChangeRate[servo1] = delta; // Slow the Servo Change Rate down to only 'delta' positions per update.
          servoChangeRate[servo2] = delta; // Slow the Servo Change Rate down to only 'delta' positions per update.
          servo[servo1] = 255;
          servo[servo2] = 255;
        }
        Qingyang Chen
        FTC Team 5485 - PRO, Horseheads, NY
        Sponsored by Corning Inc.
        Join our fan page: http://www.facebook.com/ftcpro

        Comment

        Working...
        X