예제 #1
0
        public void RunMotors()
        {
            RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;

            if (rdi == null || rdi.NXT == null)
            {
                return;
            }

            sbyte turn  = 0;
            ulong tacho = 0;

            int motorcnt = 0;

            foreach (Motor mtr in runningMotors)
            {
                if (mtr == null || mtr.power == 0)
                {
                    rdi.NXT.SetOutputState((NXTOutputPort)motorcnt, 0, NXTOutputMode.NONE, NXTOutputRegulationMode.REGULATION_MODE_IDLE, 0, NXTOutputRunState.MOTOR_RUN_STATE_IDLE, tacho);
                }
                else
                {
                    rdi.NXT.SetOutputState(mtr.port, (sbyte)(mtr.power * mtr.direction), NXTOutputMode.MOTORON, NXTOutputRegulationMode.REGULATION_MODE_MOTOR_SPEED, turn, NXTOutputRunState.MOTOR_RUN_STATE_RUNNING, tacho);
                }
                motorcnt++;
            }
        }
예제 #2
0
        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;

            rdi.Disconnect();

            lstDevices.Enabled     = true;
            btnConnect.Enabled     = true;
            btnFindDevices.Enabled = true;
        }
예제 #3
0
        private void btnVersion_Click(object sender, EventArgs e)
        {
            RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;

            try
            {
                rdi.NXT.GetVersion();
            }
            catch (Exception ex)
            {
            }
        }
예제 #4
0
        private void btnBeep_Click(object sender, EventArgs e)
        {
            RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;

            try
            {
                rdi.NXT.PlayTone(400, 100, false);
            }
            catch (Exception ex)
            {
            }
        }
예제 #5
0
        private void btnDistance_Click(object sender, EventArgs e)
        {
            RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;

            try
            {
                rdi.NXT.SetInputMode(NXTInputPort.Port4, NXTSensorType.LOWSPEED_9V, NXTSensorMode.RAWMODE);
            }
            catch (Exception ex)
            {
            }

            timer1.Enabled = !timer1.Enabled;
        }
예제 #6
0
 public void ConnectToDevice()
 {
     try
     {
         btnConnect.Enabled     = false;
         btnFindDevices.Enabled = false;
         lstDevices.Enabled     = false;
         RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;
         rdi.Connect();
     }
     catch (Exception e)
     {
         btnConnect.Enabled     = true;
         btnFindDevices.Enabled = true;
         lstDevices.Enabled     = true;
     }
 }
예제 #7
0
        /// <summary>
        /// Locates all Bluetooth Devices in range and grabs their COM port, and saves to list.
        /// </summary>
        public void FindDevices()
        {
            BluetoothDeviceInfo[] bdi;
            try
            {
                //Get a list of Discovered Devices
                btClient = new BluetoothClient();
                bdi      = (BluetoothDeviceInfo[])btClient.DiscoverDevices();
            }
            catch (Exception e)
            {
                this.Invoke(addDeviceDelegate);
                return;
            }
            //Compare discovered Bluetooth Devices with COM ports, and create new RoboticsDeviceInfo
            foreach (BluetoothDeviceInfo di in bdi)
            {
                //Add device to nxtDevices list
                RoboticsDeviceInfo rdi = new RoboticsDeviceInfo(di);

                lock (nxtDevices)
                {
                    bool bAdd = true;
                    foreach (RoboticsDeviceInfo nxtdi in nxtDevices)
                    {
                        if (nxtdi.DeviceName.Equals(rdi.DeviceName))
                        {
                            bAdd = false;
                            break;
                        }
                    }
                    if (bAdd)
                    {
                        nxtDevices.Add(rdi);
                    }
                }
            }

            //Update our ComboBox and the FindDevices button state
            this.Invoke(addDeviceDelegate);
        }
예제 #8
0
        protected override bool ProcessKeyPreview(ref Message msg)//, Keys keyData)
        {
            RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;

            byte[] keys = new byte[255];
            GetKeyboardState(keys);

            bool passed = false;

            //Check double keys first
            foreach (Command cmd in Variables.config.commands)
            {
                if (cmd.keys1Count < 2)
                {
                    continue;
                }

                passed = true;
                foreach (int iKey in cmd.keys1)
                {
                    if ((keys[iKey] & 128) == 0)
                    {
                        passed = false;
                        break;
                    }
                }

                if (passed)
                {
                    int motorcnt = 0;
                    foreach (Motor mtr in cmd.motors)
                    {
                        if (mtr.power > 0)
                        {
                            runningMotors[motorcnt] = mtr;
                        }
                        else
                        {
                            runningMotors[motorcnt] = null;
                        }
                        motorcnt++;
                    }
                    szCurrentCommand = cmd.name;
                    break;
                }
            }

            //Check single keys
            //if (!passed)
            {
                foreach (Command cmd in Variables.config.commands)
                {
                    if (cmd.keys1Count >= 2)
                    {
                        continue;
                    }

                    passed = true;
                    foreach (int iKey in cmd.keys1)
                    {
                        if ((keys[iKey] & 128) == 0)
                        {
                            passed = false;
                            break;
                        }
                    }

                    if (passed)
                    {
                        szCurrentCommand = cmd.name;
                        int motorcnt = 0;
                        foreach (Motor mtr in cmd.motors)
                        {
                            if (runningMotors[motorcnt] == null && mtr.power > 0)
                            {
                                runningMotors[motorcnt] = mtr;
                            }

                            motorcnt++;
                        }
                        break;
                    }
                }
            }


            /*
             * int UP = keys[(int)Keys.Up];
             * int LEFT = keys[(int)Keys.Left];
             * int RIGHT = keys[(int)Keys.Right];
             * int DOWN = keys[(int)Keys.Down];
             *
             * sbyte power = 100;
             * sbyte turn = 100;
             * ulong tacho = 200;
             * //Port A- - - Positive= forward
             * //Port B- - - Positive= Reverse
             * //Port C- - - Positive= Forward
             * //String cmdname = "";
             *
             * if ((UP & 128) > 0 && (LEFT & 128) > 0)
             * {
             *  szCurrentCommand = "[UPLEFT]";
             * }
             * else if ((UP & 128) > 0 && (RIGHT & 128) > 0)
             * {
             *  szCurrentCommand = "[UPRIGHT]";
             * }
             * else if ((DOWN & 128) > 0 && (LEFT & 128) > 0)
             * {
             *  szCurrentCommand = "[DOWNLEFT]";
             * }
             * else if ((DOWN & 128) > 0 && (RIGHT & 128) > 0)
             * {
             *  szCurrentCommand = "[DOWNRIGHT]";
             * }
             * else if ((UP & 128) > 0)
             * {
             *  szCurrentCommand = "[UP]";
             * }
             * else if ((DOWN & 128) > 0)
             * {
             *  szCurrentCommand = "[DOWN]";
             * }
             * else if ((LEFT & 128) > 0)
             * {
             *  szCurrentCommand = "[LEFT]";
             * }
             * else if ((RIGHT & 128) > 0)
             * {
             *  szCurrentCommand = "[RIGHT]";
             * }
             * else
             */
            if (!passed)
            {
                for (int i = 0; i < runningMotors.Length; i++)
                {
                    runningMotors[i] = null;
                }
                szCurrentCommand = "";
                RunMotors();
                //    timer1.Enabled = false;
            }

            //if ((UP & 128) > 0)
            //{

            this.rtxtLog.Focus();
            //this.rtxtLog.Text = "Message = " + msg.Msg + "\nUP = " + keys[(int)Keys.Up] + "\nDOWN = " + keys[(int)Keys.Down] + "\nLEFT = " + keys[(int)Keys.Left] + "\nRight = " + keys[(int)Keys.Right];
            //   return true;
            //   return false;
            //}

            if (szCurrentCommand.Length > 0)
            {
                //this.rtxtLog.Text = "Key = " + szCurrentCommand + "\n";

                timer1.Enabled = true;
                return(false);
            }


            return(base.ProcessKeyPreview(ref msg));
        }
예제 #9
0
        private void btnUpdatePort1_Click(object sender, EventArgs e)
        {
            RoboticsDeviceInfo rdi = (RoboticsDeviceInfo)lstDevices.SelectedItem;

            rdi.GetUltrasonicValue(NXTInputPort.Port1);
        }