Пример #1
0
        /** spin in this routine forever */
        public void RunForever()
        {
            /* config our talon, don't continue until it's successful */
            int initStatus = SetupConfig(); /* configuration */

            while (initStatus != 0)
            {
                Instrument.PrintConfigError();
                initStatus = SetupConfig(); /* (re)config*/
            }
            /* robot loop */
            while (true)
            {
                /* get joystick params */
                float leftY = -1f * _gamepad.GetAxis(1);
                bool  btnTopLeftShoulder = _gamepad.GetButton(5);
                bool  btnBtmLeftShoulder = _gamepad.GetButton(7);
                Deadband(ref leftY);

                /* keep robot enabled if gamepad is connected and in 'D' mode */
                if (_gamepad.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected)
                {
                    CTRE.Watchdog.Feed();
                }

                /* set the control mode based on button pressed */
                if (btnTopLeftShoulder)
                {
                    _mode = CTRE.TalonSrx.ControlMode.kPercentVbus;
                }
                if (btnBtmLeftShoulder)
                {
                    _mode = CTRE.TalonSrx.ControlMode.kMotionMagic;
                }

                /* calc the Talon output based on mode */
                if (_mode == CTRE.TalonSrx.ControlMode.kPercentVbus)
                {
                    float output = leftY; // [-1, +1] percent duty cycle
                    _talon.SetControlMode(_mode);
                    _talon.Set(output);
                }
                else if (_mode == CTRE.TalonSrx.ControlMode.kMotionMagic)
                {
                    float servoToRotation = leftY * 10;// [-10, +10] rotations
                    _talon.SetControlMode(_mode);
                    _talon.Set(servoToRotation);
                }
                /* instrumentation */
                Instrument.Process(_talon);

                /* wait a bit */
                System.Threading.Thread.Sleep(5);
            }
        }
Пример #2
0
        public static void Main()
        {
            //Gamepad for input
            CTRE.Gamepad _gamepad = new CTRE.Gamepad(CTRE.UsbHostDevice.GetInstance());

            //Create the DriverModule object by giving it the port you plugged it in to.
            CTRE.HERO.Module.DriverModule driver = new CTRE.HERO.Module.DriverModule(CTRE.HERO.IO.Port5);

            //these just act as shorter names for the driveLow and pullUp states
            bool driveLow = CTRE.HERO.Module.DriverModule.OutputState.driveLow;
            bool pullUp = CTRE.HERO.Module.DriverModule.OutputState.pullUp;

            while (true)
            {  
                //When the 'X' button is pressed, enable outputs
                if (_gamepad.GetButton(1) == true)
                {
                    driver.Set(1, driveLow);
                    driver.Set(2, driveLow);
                }
                else
                {
                    driver.Set(1, pullUp);
                    driver.Set(2, pullUp);
                }

                System.Threading.Thread.Sleep(10);
            }
        }
Пример #3
0
 void FillBtns(ref bool[] btns)
 {
     for (uint i = 1; i < btns.Length; ++i)
     {
         btns[i] = _gamepad.GetButton(i);
     }
 }
Пример #4
0
        public static void Main()
        {
            //Gamepad for input
            CTRE.Gamepad _gamepad = new CTRE.Gamepad(CTRE.UsbHostDevice.GetInstance());

            //Create the DriverModule object by giving it the port you plugged it in to.
            CTRE.HERO.Module.DriverModule driver = new CTRE.HERO.Module.DriverModule(CTRE.HERO.IO.Port5);

            //these just act as shorter names for the driveLow and pullUp states
            bool driveLow = CTRE.HERO.Module.DriverModule.OutputState.driveLow;
            bool pullUp   = CTRE.HERO.Module.DriverModule.OutputState.pullUp;

            while (true)
            {
                //When the 'X' button is pressed, enable outputs
                if (_gamepad.GetButton(1) == true)
                {
                    driver.Set(1, driveLow);
                    driver.Set(2, driveLow);
                }
                else
                {
                    driver.Set(1, pullUp);
                    driver.Set(2, pullUp);
                }

                System.Threading.Thread.Sleep(10);
            }
        }
Пример #5
0
        public static void Main()
        {
            /* enable compressor closed loop.  Compressor output will turn automatically
             *  if pressure switch signals low-pressure. */
            _pcm.StartCompressor();

            /* loop forever */
            while (true)
            {
                /* Check our gamepad inputs */
                bool bActivateSolenoid = _gamepad.GetButton(0);

                /* turn on channel 0 if button is held */
                _pcm.SetSolenoidOutput(0, bActivateSolenoid);
                /* turn on channel 1 if button is NOT held */
                _pcm.SetSolenoidOutput(1, !bActivateSolenoid);

                /* only enable actuators (PCM/Talons/etc.) if gamepad is present */
                if (_gamepad.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected)
                {
                    CTRE.Watchdog.Feed();
                }

                /* yield for a while */
                System.Threading.Thread.Sleep(10);
            }
        }
Пример #6
0
        uint [] _debLeftY = { 0, 0 }; // _debLeftY[0] is how many times leftY is zero, _debLeftY[1] is how many times leftY is not zeero.

        public void Run()
        {
            /* first choose the sensor */
            _talon.SetFeedbackDevice(CTRE.TalonSrx.FeedbackDevice.CtreMagEncoder_Relative);
            _talon.SetSensorDirection(false);
            //_talon.ConfigEncoderCodesPerRev(XXX), // if using CTRE.TalonSrx.FeedbackDevice.QuadEncoder
            //_talon.ConfigPotentiometerTurns(XXX), // if using CTRE.TalonSrx.FeedbackDevice.AnalogEncoder or CTRE.TalonSrx.FeedbackDevice.AnalogPot

            /* set closed loop gains in slot0 */
            _talon.SetP(0, 0.2f); /* tweak this first, a little bit of overshoot is okay */
            _talon.SetI(0, 0f);
            _talon.SetD(0, 0f);
            _talon.SetF(0, 0f); /* For position servo kF is rarely used. Leave zero */

            /* use slot0 for closed-looping */
            _talon.SelectProfileSlot(0);

            /* set the peak and nominal outputs, 12V means full */
            _talon.ConfigNominalOutputVoltage(+0.0f, -0.0f);
            _talon.ConfigPeakOutputVoltage(+3.0f, -3.0f);

            /* how much error is allowed?  This defaults to 0. */
            _talon.SetAllowableClosedLoopErr(0, 0);

            /* zero the sensor and throttle */
            ZeroSensorAndThrottle();

            /* loop forever */
            while (true)
            {
                Loop10Ms();

                //if (_gamepad.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected) // check if gamepad is plugged in OR....
                if (_gamepad.GetButton(kEnableButton)) // check if bottom left shoulder buttom is held down.
                {
                    /* then enable motor outputs*/
                    CTRE.Watchdog.Feed();
                }

                /* print signals to Output window */
                Instrument();

                /* 10ms loop */
                Thread.Sleep(10);
            }
        }
Пример #7
0
        public static void Main()
        {
            CTRE.Gamepad _gamepad = new CTRE.Gamepad(new CTRE.UsbHostDevice());
            //0 - left x
            //1 - left y
            //2 - right x
            //
            //5 - LB
            //6 - RB

            ledSpike.SetControlMode(CTRE.TalonSrx.ControlMode.kVoltage);
            ledSpike.SetCurrentLimit(1);

            while (true)
            {
                float y    = -_gamepad.GetAxis(1);
                float turn = _gamepad.GetAxis(2);

                Deadband(ref y);
                Deadband(ref turn);

                if (_gamepad.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected)
                {
                    if (_gamepad.GetButton(5))
                    {
                        ArcadeDrive(y, turn);
                        /* feed watchdog to keep Talon's enabled */
                        CTRE.Watchdog.Feed();
                    }
                    if (_gamepad.GetButton(6))
                    {
                        ledSpike.Set(3.5f);
                        CTRE.Watchdog.Feed();
                    }
                    else
                    {
                        ledSpike.Set(0);
                    }
                }

                /* run this task every 10ms */
                Thread.Sleep(10);
            }
        }
Пример #8
0
        /** get all buttons from a gamepad */
        public static void FillBtns(CTRE.Gamepad gamepad, ref bool[] btns, ref int pov)
        {
            gamepad.GetAllValues(ref _gamepadValues);

            pov = _gamepadValues.pov;
            for (uint i = 1; i < btns.Length; ++i)
            {
                btns[i] = gamepad.GetButton(i);
            }
        }
Пример #9
0
 public bool GetButton(Button button)
 {
     if (controller.GetButton((uint)button))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #10
0
 public int GetFirstButton(CTRE.Gamepad gamepad)
 {
     for (uint i = 0; i < 16; ++i)
     {
         if (gamepad.GetButton(i))
         {
             return((int)i);
         }
     }
     return(-1);
 }
Пример #11
0
        public static void Main()
        {
            //Gamepad for input
            CTRE.Gamepad _gamepad = new CTRE.Gamepad(CTRE.UsbHostDevice.GetInstance());

            //simple PWM for fine control of pulse width, period, timing...
            uint period   = 50000; //period between pulses
            uint duration = 1500;  //duration of pulse
            PWM  pwm_9    = new PWM(CTRE.HERO.IO.Port3.PWM_Pin9, period, duration, PWM.ScaleFactor.Microseconds, false);

            pwm_9.Start(); //starts the signal

            // ...and just a PWM SpeedController for motor controller (Victor SP, Talon SR, Victor 888, etc.)...
            CTRE.PWMSpeedController pwmSpeedController = new CTRE.PWMSpeedController(CTRE.HERO.IO.Port3.PWM_Pin4);

            while (true)
            {
                /* only enable motor control (PWM/CAN) if gamepad is connected.  Logitech gamepads may be disabled using the X/D switch */
                if (_gamepad.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected)
                {
                    CTRE.Watchdog.Feed();
                }

                /* let axis control the pwm speed controller */
                pwmSpeedController.Set(0.10f); /* 10% */

                /* let button1 control the explicit PWM pin duration*/
                if (_gamepad.GetButton(1) == true)
                {
                    pwm_9.Duration = 2000; /* 2.0ms */
                }
                else
                {
                    pwm_9.Duration = 1000; /* 1.0ms */
                }

                /* yield for a bit, this controls this task's frequency */
                System.Threading.Thread.Sleep(10);
            }
        }
Пример #12
0
        public void RunForever()
        {
            while (true)
            {
                if (_gamepad.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected)
                {
                    CTRE.Watchdog.Feed();
                }

                /* get buttons */
                for (uint i = 1; i < 20; ++i)
                {
                    _buttons[i].last = _buttons[i].now;
                    _buttons[i].now  = _gamepad.GetButton(i);
                }

                /* yield for a bit, and track timeouts */
                System.Threading.Thread.Sleep(10);

                /* pigeon related tasks */
                PidgyTask();
            }
        }
Пример #13
0
        public void RunForever()
        {
            /* enable XInput, if gamepad is in DInput it will disable robot.  This way you can
             * use X mode for drive, and D mode for disable (instead of vice versa as the
             * stock HERO implementation traditionally does). */
            CTRE.UsbHostDevice.EnableXInputDevices();

            while (true)
            {
                if (_gamepad.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected)
                {
                    CTRE.Watchdog.Feed();
                }

                /* get buttons */
                bool[] btns = new bool[_buttons.Length];
                for (uint i = 1; i < 20; ++i)
                {
                    btns[i] = _gamepad.GetButton(i);
                }

                /* get sticks */
                for (uint i = 0; i < _sticks.Length; ++i)
                {
                    _sticks[i] = _gamepad.GetAxis(i);
                }

                /* yield for a bit, and track timeouts */
                System.Threading.Thread.Sleep(10);
                if (_rumblingTimeMs < 5000)
                {
                    _rumblingTimeMs += 10;
                }

                /* update the Talon using the shoulder analog triggers */
                _tal.Set((_sticks[5] - _sticks[4]) * 0.60f);

                /* fire some solenoids based on buttons */
                _driver.Set(0, _buttons[1]);
                _driver.Set(1, _buttons[2]);
                _driver.Set(2, _buttons[3]);
                _driver.Set(3, _buttons[4]);

                /* rumble state machine */
                switch (_rumblinSt)
                {
                /* rumbling is disabled, require some off time to save battery */
                case 0:
                    _gamepad.SetLeftRumble(0);
                    _gamepad.SetRightRumble(0);

                    if (_rumblingTimeMs < 100)
                    {
                        /* waiting for off-time */
                    }
                    else if ((btns[1] && !_buttons[1]))     /* button off => on */
                    {
                        /* off time long enough, user pressed btn */
                        _rumblingTimeMs = 0;
                        _rumblinSt      = 1;
                        _gamepad.SetLeftRumble(0xFF);
                    }
                    else if ((btns[2] && !_buttons[2]))     /* button off => on */
                    {
                        /* off time long enough, user pressed btn */
                        _rumblingTimeMs = 0;
                        _rumblinSt      = 1;
                        _gamepad.SetRightRumble(0xFF);
                    }
                    break;

                /* already vibrating, track the time */
                case 1:
                    if (_rumblingTimeMs > 500)
                    {
                        /* vibrating too long, turn off now */
                        _rumblingTimeMs = 0;
                        _rumblinSt      = 0;
                        _gamepad.SetLeftRumble(0);
                        _gamepad.SetRightRumble(0);
                    }
                    else if ((btns[3] && !_buttons[3]))      /* button off => on */
                    {
                        /* immedietely turn off */
                        _rumblingTimeMs = 0;
                        _rumblinSt      = 0;
                        _gamepad.SetLeftRumble(0);
                        _gamepad.SetRightRumble(0);
                    }
                    else if ((btns[1] && !_buttons[1]))    /* button off => on */
                    {
                        _gamepad.SetLeftRumble(0xFF);
                    }
                    else if ((btns[2] && !_buttons[2]))     /* button off => on */
                    {
                        _gamepad.SetRightRumble(0xFF);
                    }
                    break;
                }

                /* this will likley be replaced with a strongly typed interface,
                 * control the LEDs on the center XBOX emblem. */
                if (btns[5] && !_buttons[5])
                {
                    _gamepad.SetLEDCode(6);
                }
                if (btns[6] && !_buttons[6])
                {
                    _gamepad.SetLEDCode(7);
                }
                if (btns[7] && !_buttons[7])
                {
                    _gamepad.SetLEDCode(8);
                }
                if (btns[8] && !_buttons[8])
                {
                    _gamepad.SetLEDCode(9);
                }

                /* build line to print */
                StringBuilder sb = new StringBuilder();
                foreach (float stick in _sticks)
                {
                    sb.Append(Format(stick));
                    sb.Append(",");
                }

                sb.Append("-");
                for (uint i = 1; i < _buttons.Length; ++i)
                {
                    if (_buttons[i])
                    {
                        sb.Append("b" + i + ",");
                    }
                }

                /* print useful info */
                sb.AppendLine();
                Debug.Print(sb.ToString());

                /* save button states for button-change states */
                _buttons = btns;
            }
        }
Пример #14
0
        //////////////////  Function where all the code is executed  ///////////////////
        public static void Main()
        {
            //  Invert the Motors to the correct direction
            InitializeMotors();

            //  Everything in the While Loop will continuously run until the robot is turned off
            while (true)
            {
                //  If the Controller is connected to the HERO Development Board, the user can drive the robot
                if (controller.GetConnectionStatus() == CTRE.UsbDeviceConnection.Connected)
                {
                    //  Get the Joystick Values and Deadband them
                    //  Since moving the Joysticks up on the y-axis is in the negative direction, the value of the
                    //  left Joystick Y Axis is multiplied by -1 so it's treated as being positive
                    deadbandedLeftJoystickYAxisValue  = -DeadbandJoystick(controller.GetAxis(LEFT_Y_AXIS_INDEX));
                    deadbandedLeftJoystickXAxisValue  = DeadbandJoystick(controller.GetAxis(LEFT_X_AXIS_INDEX));
                    deadbandedRightJoystickXAxisValue = DeadbandJoystick(controller.GetAxis(RIGHT_X_AXIS_INDEX));

                    //  Button used to switch between different Driving Modes
                    //  The Button is programmed as a toggle between the two Driving Modes
                    if (controller.GetButton(buttonSelect))
                    {
                        if (selectButtonPressed == false)
                        {
                            arcadeDriveSelection = !arcadeDriveSelection;
                            selectButtonPressed  = true;
                        }
                    }
                    else
                    {
                        selectButtonPressed = false;
                    }

                    if (arcadeDriveSelection == ARCADE_DRIVE)
                    {
                        //  If the Right X Axis Joystick Value does not equal zero, the robot will rotate.
                        //  Control with the Left Joystick will be disabled if the user decides to rotate the robot
                        //  This essentially allows the user to rotate the robot if he/she moves the Right X Axis Joystick Value out
                        //  of the Deadband
                        if (deadbandedRightJoystickXAxisValue != 0.0f)
                        {
                            RotateRobot(deadbandedRightJoystickXAxisValue);
                        }
                        //  If the Right X Axis Joystick Value is zero, control the direction and magnitude of the speed
                        //  of the robot with the Left Joystick
                        else
                        {
                            DriveKiwiBotWithJoystickValuesNoRotation(deadbandedLeftJoystickYAxisValue, deadbandedLeftJoystickXAxisValue);
                        }
                    }
                    else if (arcadeDriveSelection == DRIVING_WITH_STEERING)
                    {
                        DriveRobotWithSteeringControl(deadbandedLeftJoystickYAxisValue, deadbandedRightJoystickXAxisValue);
                    }
                }
                //  If the Controller is not connected to the HERO Development Board, the user cannot move the robot
                else
                {
                    StopAllMotors();
                }

                //  Update the Motor Powers calculated in the above code
                CTRE.Watchdog.Feed();

                //  Wait 10ms
                //  All the code in the While Loop will execute every 10ms
                System.Threading.Thread.Sleep(10);
            }
        }