converting double to continuous range

9 views
Skip to first unread message

pl22...@ahschool.com

unread,
Feb 12, 2016, 11:55:13 AM2/12/16
to Strongback
In auto, you cannot  get any feedback from the joystick, so if I want to use tank drive with specific speeds, I cannot convert from double to continouous range. How do i do so?

Randall Hauch

unread,
Feb 12, 2016, 12:05:33 PM2/12/16
to stron...@googlegroups.com
ContinuousRange is a simple functional interface, with a "read()" method that returns the double. You can make an instance with:

    ContinuousRange throttle = ()->0.5;

or use whatever constant (or function) you want.

You can also make a new ContinuousRange that in auto-mode returns the value(s) you want (or does whatever logic you want), and in teleop returns the throttle. For example:


public class Robot extends IterativeRobot {


    private DriveSystem drive;

    private FlightStick joystick;

    private ContinuousRange driveSpeed;

    private ContinuousRange turnSpeed;

    private ContinuousRange throttle;


    @Override

    public void robotInit() {

        ...

        // Define the interface components ...

        joystick = Hardware.HumanInterfaceDevices.logitechAttack3D(0);

        throttle = joystick.getThrottle().map(t -> (t + 1.0) / 2.0);

        driveSpeed = joystick.getPitch().scale(this::throttle);

        turnSpeed = joystick.getRoll().scale(this::throttle).invert();


        ...

    }

    

    /**

     * Function that returns the current throttle in both autonomous and teleop.

     * @return the current throttle

     */

    private double throttle() {

        return throttle.read();

    }


    @Override

    public void autonomousInit() {

        // Set the maximum throttle in autonomous to be 100% ...

        this.throttle = ()->1.0;

    }


    ...


    @Override

    public void teleopInit() {

        // Set the throttle obtain the value from the joystick ...

        throttle = joystick.getThrottle().map(t -> (t + 1.0) / 2.0);

        ...

    }


    @Override

    public void teleopPeriodic() {

        drive.arcade(driveSpeed.read(), turnSpeed.read());

    }



    ...

}


Here, we use the `throttle()` method on this class as a lambda that returns a double, and that signature is compatible with the ContinuousRange.read() method. That means we can have the "scale(...)" method used for the driveSpeed and turnSpeed fields use that instead. In autonomousInit(), we set our throttle field (that points to a function that returns the actual throttle value) to the lambda `()->1.0` (which of course could do anything). Then in teleopInit(), we set the throttle field to the  `joystick.getThrottle().map(t -> (t + 1.0) / 2.0)`.

Hopefully that makes sense.

Randall Hauch

unread,
Feb 14, 2016, 10:38:32 AM2/14/16
to Strongback
Actually, I'll amend my previous reply. You actually shouldn't need to do anything special, because you won't be using the joystick inputs at all during autonomous mode. Instead, during autonomous you can directly call DriveSystem methods with whatever speeds you want. For example, many teams implement autonomous with a single Command (or CommandGroup), and they can directly call the various drive methods on DriveSystem.
Reply all
Reply to author
Forward
0 new messages