Exemple #1
0
        /// <summary>
        /// Connect to the NXT
        /// </summary>
        /// <param name="ComPort">The COM Port to connect. ex. 4</param>
        public static void ConnectNXT(string ComPort)
        {
            try
            {
                comPort = byte.Parse(ComPort);

                Brick = new NxtBrick(NxtCommLinkType.Bluetooth, comPort);
                NxtMotor motorB = new NxtMotor();
                Brick.MotorB = motorB;
                NxtMotor motorC = new NxtMotor();
                Brick.MotorC = motorC;
                MotorPair = new NxtMotorSync(Brick.MotorB, Brick.MotorC);


                if (!isConnected)
                {
                    Brick.Connect();
                    isConnected = true;
                    Brick.PlaySoundfile("! Attention.rso");
                }
            }
            catch
            {
                isConnected = false;
                DisconnectNXT();
            }
        }
Exemple #2
0
        public static void RunForward()
        {
            NxtBrick brick = new NxtBrick(NxtCommLinkType.USB, 0);

            // Attach a motor to port B.
            brick.MotorB = new NxtMotor();

            // Connect to the NXT.
            brick.Connect();
            Console.WriteLine("NXT name: " + brick.Name);
            System.Diagnostics.Debug.WriteLine("NXT name: " + brick.Name);

            // Write out the firmware version of the NXT-brick.
            var conn = new NxtUsbConnection();
            NxtGetFirmwareVersionReply? reply = conn.GetFirmwareVersion();
            if (reply.HasValue)
                System.Diagnostics.Debug.WriteLine("NXT firmware version: " + reply.Value.firmwareVersion);

            // see: http://www.mindsqualls.net/MotorControl.aspx
            MotorControlProxy.StartMotorControl(brick.CommLink);
            System.Threading.Thread.Sleep(500);

            MotorControlProxy.CONTROLLED_MOTORCMD(brick.CommLink, MotorControlMotorPort.PortB,"111", "000360", '5');
            System.Diagnostics.Debug.WriteLine("Done");
            System.Threading.Thread.Sleep(5000);
            MotorControlProxy.StopMotorControl(brick.CommLink);
            brick.Disconnect();
        }
Exemple #3
0
        public void Disconnect()
        {
            if (_brick != null && _brick.IsConnected)
                _brick.Disconnect();

            _brick = null;
            _motorPair = null;
        }
 public RobotMovement(NxtBrick nxtBrick, NameValueCollection config)
 {
     _speed = sbyte.Parse(config["Speed"]);
     _nxtBrick = nxtBrick;
     var motorB = new NxtMotor();
     var motorC = new NxtMotor();
     _nxtBrick.MotorB = motorB;
     _nxtBrick.MotorC = motorC;
     _motorPair = new NxtMotorSync(motorB, motorC);
 }
Exemple #5
0
 public Nxt()
 {
     nxt = new NxtBrick(3);
     nxt.MotorC = new NxtMotor();
     try
     {
         this.nxt.Connect();
     }
     catch (Exception) { }
 }
        public RobotEngine()
        {
            _brick = new NxtBrick(NxtCommLinkType.Bluetooth, Config.SerialPortNo)
            {
                MotorA = new NxtMotor(),
                MotorB = new NxtMotor(),
                MotorC = new NxtMotor()
            };

            _motorSync = new NxtMotorSync(_brick.MotorA, _brick.MotorB);
        }
        public RobotSenses(NxtBrick nxtBrick, NameValueCollection config)
        {
            _nxtBrick = nxtBrick;
            _thresholdDistanceCm = byte.Parse(config["ThresholdDistanceCm"]);
            var distanceSensor = new NxtUltrasonicSensor
                                     {
                                         PollInterval = Int32.Parse(config["DistanceSensorPollingInterval"])
                                     };

            distanceSensor.OnPolled += distanceSensor_OnPolled;
            _nxtBrick.Sensor4 = distanceSensor;
        }
Exemple #8
0
        /// <summary>
        /// Disconnect the NXT
        /// </summary>
        public static void DisconnectNXT()
        {
            Idle();
            Brick.PlaySoundfile("! Attention.rso");
            Brick.CommLink.StopProgram();

            if (Brick != null && Brick.IsConnected)
                isConnected = false;
                Brick.Disconnect();

            Brick = null;
            MotorPair = null;            
        }
        static void Main(string[] args)
        {
            var config = ConfigurationManager.AppSettings;
            var comPort = byte.Parse(config["ComPort"]);

            var nxtBrick = new NxtBrick(NxtCommLinkType.Bluetooth, comPort);

            var robotMovement = new RobotMovement(nxtBrick, config);
            var robotSenses = new RobotSenses(nxtBrick, config);
            var robotBrain = new RobotBrain(nxtBrick, robotMovement, robotSenses);
            Console.WriteLine("Starting Robot...");
            robotBrain.Start();
            Console.WriteLine("Robot started, press any key to stop");
            Console.ReadKey();
            robotBrain.Stop();
        }
Exemple #10
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                myKinect = KinectSensor.KinectSensors[0];
                myKinect.SkeletonStream.Enable();
                myKinect.Start();
            }
            catch
            {
                MessageBox.Show("Kinect initialise failed", "camera view");
                Application.Exit();
            }

            leftInitialPosition  = new List <SkeletonPoint>();
            rightInitialPosition = new List <SkeletonPoint>();
            initialPositions     = new Dictionary <string, SkeletonPoint>();
            //cmds = new List<Motion>();
            cmds = new Dictionary <Motion, bool>();
            cmds[Motion.Back]      = false;
            cmds[Motion.Brake]     = false;
            cmds[Motion.Forward]   = false;
            cmds[Motion.TurnRight] = false;
            cmds[Motion.TurnLeft]  = false;

            nxtStatus = new Dictionary <Motion, bool>();
            nxtStatus[Motion.Brake]     = false;
            nxtStatus[Motion.Back]      = false;
            nxtStatus[Motion.Forward]   = false;
            nxtStatus[Motion.TurnLeft]  = false;
            nxtStatus[Motion.TurnRight] = false;

            nxt        = new NxtBrick(NxtCommLinkType.Bluetooth, 3);
            nxt.MotorB = new NxtMotor();
            nxt.MotorC = new NxtMotor();
            motorPair  = new NxtMotorSync(nxt.MotorB, nxt.MotorC);

            nxt.Connect();

            NXTLabel.Text = "NXT is connected.";

            myKinect.SkeletonFrameReady += new EventHandler <SkeletonFrameReadyEventArgs>(myKinect_SkeletonFrameReady);
        }
        /*
         * Connects to the NXTBrick with the given comport using the given connectiontype
         */
        public bool ConnectToDevice(NxtCommLinkType connectionType, byte comPort)
        {
            brick = new NxtBrick(connectionType, comPort);

            brick.MotorA = new NxtMotor();
            brick.MotorC = new NxtMotor();

            motorPair = new NxtMotorSync(brick.MotorA, brick.MotorC);

            try
            {
                brick.Connect();
                return true;
            }
            catch
            {
                return false;
            }
        }
Exemple #12
0
 public NxtRobot(byte port)
 {
     try
     {
         Position = new NXTRobotPosition();
         OnPositionChanged();
         _brick = new NxtBrick(NxtCommLinkType.Bluetooth, port)
         {
             MotorA = new NxtMotor(),
             MotorB = new NxtMotor(),
             MotorC = new NxtMotor()
         };
         _motorPair = new NxtMotorSync(_brick.MotorA, _brick.MotorB);
         _brick.Connect();
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         Disconnect();
     }
 }
Exemple #13
0
        static void Main()
        {
            Init();
            
            brick = new NxtBrick(NxtCommLinkType.USB, 0);
            //brick = new NxtBrick(NxtCommLinkType.Bluetooth, 40);

            var sound = new NxtSoundSensor();
            var touch = new NxtTouchSensor();
            var sonar = new NxtUltrasonicSensor();

            brick.MotorA = new NxtMotor();
            brick.MotorC = new NxtMotor();
            motorPair = new NxtMotorSync(brick.MotorA, brick.MotorC);

            brick.Sensor1 = sonar;
            brick.Sensor3 = touch;
            brick.Sensor4 = sound;

            sound.PollInterval = 50;
            sound.OnPolled += sound_OnPolled;

            sonar.PollInterval = 50;
            sonar.ThresholdDistanceCm = 25;
            sonar.OnPolled += sonar_OnPolled;
            sonar.OnWithinThresholdDistanceCm += OnWithinThreshold; 
            sonar.ContinuousMeasurementCommand();

            touch.PollInterval = 50;
            touch.OnPressed += OnCollision;

            brick.Connect();

            motorPair.Run(75, 0, 0); 

            Console.WriteLine("Press any key to stop.");
            Console.ReadKey();

            brick.Disconnect();
        }
Exemple #14
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                myKinect = KinectSensor.KinectSensors[0];
                myKinect.SkeletonStream.Enable();
                myKinect.Start();
            }
            catch
            {
                MessageBox.Show("Kinect initialise failed", "camera view");
                Application.Exit();
            }

            leftInitialPosition = new List<SkeletonPoint>();
            rightInitialPosition = new List<SkeletonPoint>();
            initialPositions = new Dictionary<string, SkeletonPoint>();
            //cmds = new List<Motion>();
            cmds = new Dictionary<Motion, bool>();
            cmds[Motion.Back] = false;
            cmds[Motion.Brake] = false;
            cmds[Motion.Forward] = false;
            cmds[Motion.TurnRight] = false;
            cmds[Motion.TurnLeft] = false;

            nxtStatus = new Dictionary<Motion, bool>();
            nxtStatus[Motion.Brake] = false;
            nxtStatus[Motion.Back] = false;
            nxtStatus[Motion.Forward] = false;
            nxtStatus[Motion.TurnLeft] = false;
            nxtStatus[Motion.TurnRight] = false;

            nxt = new NxtBrick(NxtCommLinkType.Bluetooth, 3);
            nxt.MotorB = new NxtMotor();
            nxt.MotorC = new NxtMotor();
            motorPair = new NxtMotorSync(nxt.MotorB, nxt.MotorC);

            nxt.Connect();

            NXTLabel.Text = "NXT is connected.";

            myKinect.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(myKinect_SkeletonFrameReady);
        }
        /**
         * Stops the car and disconnects from the device.
         */
        public bool Disconnect()
        {
            Yield();

            if (brick != null && brick.IsConnected)
            {
                brick.Disconnect();
            }

            brick = null;
            motorPair = null;

            return true;
        }
Exemple #16
0
        public void buttonBluetooth_Click(string comPort)
        {
            try
            {
                if (p4.isNXT)
                {
                    this.nxt = new NxtBrick(NxtCommLinkType.Bluetooth, (byte)Convert.ToInt32(comPort));
                    this.nxt.Connect();
                    this.nxt.PlayTone(8000, 500);
                    this.pictureStatusKinect.BackgroundImage = Properties.Resources.nxt_green;
                    this.nxt.MotorB = new NxtMotor();
                    this.nxt.MotorC = new NxtMotor();

                    if (nxt.Programs.Contains("KIFS.rxe"))
                    {
                        nxt.Program = "KIFS.rxe";
                        this.isCustom = true;
                    }

                    name = nxt.Name;
                }
                else
                {
                    this.ev3 = new Brick<Sensor, Sensor, Sensor, Sensor>("COM" + comPort);
                    this.ev3.Connection.Open();
                    this.ev3.PlayTone(5, (ushort) 8000, (ushort) 500);
                    this.pictureStatusKinect.BackgroundImage = Properties.Resources.nxt_green;

                    if (ev3.GetRunningProgram().Contains("kifen"))
                        this.isCustom = true;

                    name = "EV3";
                }
                this.timerUpdateNxt.Enabled = true;
            }
            catch (Exception enxt)
            {
                Console.Write(enxt.StackTrace);
            }
        }
 public RobotBrain(NxtBrick nxtBrick, IRobotMovement robotMovement, IRobotSenses robotSenses)
 {
     _nxtBrick = nxtBrick;
     _robotMovement = robotMovement;
     _robotSenses = robotSenses;
 }
Exemple #18
0
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (trackBar1.Value == 0)
            {
                if (ev3 != null && ev3.Connection.IsConnected)
                {
                    this.ev3.Connection.Close();
                    this.ev3 = null;
                }

                labelStatusNxt.Text = "Disconnected";
                labelStatusNxt.Visible = true;

                pictureNXT.BackgroundImage = Properties.Resources.nxt_red;

                pictureIconEV3.BackgroundImage = Properties.Resources.ev3_red;
                pictureIconNXT.BackgroundImage = Properties.Resources.nxt_green;
            }
            else
            {
                if (nxt != null && nxt.IsConnected)
                {
                    this.nxt.Disconnect();
                    this.nxt = null;
                }

                labelStatusNxt.Text = "Disconnected";
                labelStatusNxt.Visible = true;

                pictureNXT.BackgroundImage = Properties.Resources.ev3_red;

                pictureIconEV3.BackgroundImage = Properties.Resources.ev3_green;
                pictureIconNXT.BackgroundImage = Properties.Resources.nxt_red;
            }
        }
Exemple #19
0
        private void buttonBluetooth_Click(object sender, EventArgs e)
        {
            try
            {
                this.labelStatusNxt.Text = "Attempting to connect. . .";
                this.labelStatusNxt.Refresh();

                if (trackBar1.Value == 0)
                {
                    this.nxt = new NxtBrick(NxtCommLinkType.Bluetooth, (byte)Convert.ToInt32(textBoxComPort.Text));
                    this.nxt.Connect();
                    this.nxt.PlayTone(8000, 500);
                    this.pictureNXT.BackgroundImage = Properties.Resources.nxt_green;
                    this.nxt.MotorB = new NxtMotor();
                    this.nxt.MotorC = new NxtMotor();

                    if (nxt.Programs.Contains("KIFS.rxe"))
                    {
                        nxt.Program = "KIFS.rxe";
                        this.isCustom = true;
                    }

                    name = nxt.Name;
                }
                else
                {
                    this.ev3 = new Brick<Sensor, Sensor, Sensor, Sensor>("COM" + textBoxComPort.Text);
                    this.ev3.Connection.Open();
                    this.ev3.PlayTone(5, (ushort) 8000, (ushort) 500);
                    this.pictureNXT.BackgroundImage = Properties.Resources.nxt_green;

                    if (ev3.GetRunningProgram().Contains("kifen"))
                        this.isCustom = true;

                    name = "EV3";
                }

                this.labelStatusNxt.Text = "Connected to " + name;
                this.timerHideStatNxt.Enabled = true;
                this.timerUpdateNxt.Enabled = true;
            }
            catch (Exception enxt)
            {
                Console.Write(enxt.StackTrace);
                this.labelStatusNxt.Text = "Disconnected";
            }
        }
Exemple #20
0
        private NxtBrick ConnectBrick(int brickNo, NxtCommLinkType linkType, int comPort)
        {
            var brick = new NxtBrick(linkType, (byte)comPort);

            if (linkType == NxtCommLinkType.Bluetooth) {
                var attempts = 3;
                for (int attempt = 0; attempt < attempts; attempt++) {
                    Logger.Log("Connecting to brick {0} on port {1}. \t\tAttempt {2} of {3}...", brickNo, comPort,
                                      attempt + 1, attempts);
                    try {
                        brick.Connect();
                        Logger.Log("\tBrick {0} successfully connected (port: {1})", brickNo, comPort);
                        break;
                    }
                    catch (IOException) {
                        if (attempt + 1 == attempts)
                            throw;
                    }
                }
            }

            Logger.Log("Connected to brick {0}", brickNo);

            return brick;
        }
Exemple #21
0
 static void SetupUSBConnection()
 {
     brick = new NxtBrick(NxtCommLinkType.USB, 0);
     Console.WriteLine("\nUSB connection set up.");
 }
Exemple #22
0
 static void SetupBluetoothConnection()
 {
     Console.WriteLine("\n\nEnter your Bluetooth COM-Port (Default: 40)");
     byte comport = Convert.ToByte(Console.ReadLine());
     brick = new NxtBrick(NxtCommLinkType.Bluetooth, comport);
     Console.WriteLine("\nBluetooth connection set up at port " + comport + ".");
 }
Exemple #23
0
        public Form1()
        {
            InitializeComponent();

            var brick = new NxtBrick(NxtCommLinkType.Bluetooth, Config.SerialPortNo)
            {
                MotorA = new NxtMotor(),
                MotorB = new NxtMotor(),
                MotorC = new NxtMotor()
            };

            var motorSync = new NxtMotorSync(brick.MotorA, brick.MotorB);

            brick.Connect();

            const int units = 100;

            var realDistance = units / Config.UnitsInMeter;
            var tachoLimit = (ushort)(realDistance * Config.RunTacho);

            motorSync.Run(Config.RunPower, tachoLimit, 0);

            //brick.Disconnect();

            ////_brick = new NxtBrick(NxtCommLinkType.Bluetooth, 3)
            ////{
            ////    MotorA = new NxtMotor(),
            ////    MotorB = new NxtMotor()
            ////};

            ////_motorSync = new NxtMotorSync(_brick.MotorA, _brick.MotorB);

            ////do
            ////{
            ////    _brick.Connect();
            ////    Thread.Sleep(1000);
            ////} while (!_brick.IsConnected);

            ////for (int i = 0; i < 1; ++i)
            ////{
            ////    _motorSync.ResetMotorPosition(true);
            ////    _motorSync.Idle();
            ////    _motorSync.Run(50, 4040, 100);

            ////    Thread.Sleep(10*1000);
            ////}

            //McNxtBrick brick = new McNxtBrick(NxtCommLinkType.Bluetooth, 3);

            //// Attach motors to port B and C on the NXT.
            //brick.MotorA = new McNxtMotor();
            //brick.MotorB = new McNxtMotor();

            //// Syncronize the two motors.
            //McNxtMotorSync motorPair = new McNxtMotorSync(brick.MotorA, brick.MotorB);

            //// Connect to the NXT.
            //brick.Connect();

            //// If not already running, start the MotorControl program.
            //if (brick.IsMotorControlRunning())
            //    brick.StartMotorControl();

            //// Run the motor-pair at 75% power, for a 3600 degree run.
            //motorPair.Run(75, 3600, 100);

            ////for (int i = 0; i < 3; ++i)
            ////{
            ////    uint tacho = 2000;
            ////    brick.MotorA.Run(50, tacho);
            ////    brick.MotorB.Run(-50, tacho);

            ////    Thread.Sleep(10 * 1000);
            ////}

            //// Disconnect from the NXT.
            //brick.Disconnect();
        }
Exemple #24
0
        private void buttonBluetooth_Click(object sender, EventArgs e)
        {
            try
            {
                this.labelStatusNxt.Text = "Attempting to connect. . .";
                this.labelStatusNxt.Refresh();
                this.nxt = new NxtBrick(NxtCommLinkType.Bluetooth, (byte) Convert.ToInt32(textBoxComPort.Text));
                this.nxt.Connect();
                this.nxt.PlayTone(8000, 500);
                this.pictureNXT.BackgroundImage = Properties.Resources.nxt_green;
                this.nxt.MotorB = new NxtMotor();
                this.nxt.MotorC = new NxtMotor();

                if (nxt.Programs.Contains("KIFS.rxe"))
                {
                    nxt.Program = "KIFS.rxe";
                    this.isCustom = true;
                }

                name = nxt.Name;

                this.labelStatusNxt.Text = "Connected to " + nxt.Name;
                this.timerHideStatNxt.Enabled = true;
                //this.timerUpdateNxt.Enabled = true;
            }
            catch (Exception enxt)
            {
                Console.Write(enxt.StackTrace);
                this.labelStatusNxt.Text = "Disconnected";
            }
        }