Announcement

Collapse
No announcement yet.

Joystick control of a 180 servo's position

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

  • Joystick control of a 180 servo's position

    We are doing a small project where we need to be able to set a servo's position based on a joystick setting. (We are using the servo to control a potentiometer, which, in turn, controls the speed of a 100amp electric motor through another circuit. For example, when the joystick is fully forward, the servo goes to 0; fully back it goes to 180; in the middle it is at 90. Does anyone have a sample snippet of code (for Android Studio) that I can give the kids to get them started? I'm not a programmer. Thanks

  • #2
    Code:
    // Put before init in iterative mode or before waitforstart in linear mode
    Servo yourServoNameHere;
    //Put in init in iterative or before waitforstart in linear mode
    yourServoNameHere = hardwareMap.get(Servo.class, "myServo");
    // Put in loop in iterative or after waitforstart in linear
    // Add while (opModeActive) with braces around all of below if using linear mode
        // obtain joystick value,
            // multiply by negative one so positive values are pushing the stick away
            double joy = -gamepad1.left_stick_y;
            // joy range -1 to 1, servo range 0 to 1
            // divide by 2 to reduce joy range to -0.5 to 0.5
            // add 0.5 to shift range to 0 to 1
            double valAdjustedForServo = joy/2 + 0.5;
            yourServoNameHere.setPosition(valAdjustedForServo);
            telemetry.addData("servo ", valAdjustedForServo); 
     //add telemetry.update(); if using linear mode

    Comment


    • #3
      You will typically want to restrict the movement of your servos to a specific range. To do so, simply invoke scaleRange(minPos, maxPos) in your init() method. For example:
      Code:
      ...
      //Put in init in iterative or before waitforstart in linear mode
      yourServoNameHere = hardwareMap.get(Servo.class, "myServo");
      yourServoNameHere.scaleRange(0.37, 0.81);  // restrict the movement of the servo to range [0.37, 0.81]
      ...

      Comment


      • #4
        Thanks! This got them started.

        Comment

        Working...
        X