Пример #1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            joy.Update();
            if (serialPort1.IsOpen) {

                //
                int yaw = (int)(joy.LeftStick.Position.X * 32768);
                int throttle = (int)(joy.LeftStick.Position.Y * 32768);
                bool ls = joy.LeftStick.Clicked;
                bool lb=joy.LeftShoulder;
                float lt = joy.LeftTrigger;

                //
                bool up = joy.DPad.Up;
                bool down = joy.DPad.Down;
                bool right = joy.DPad.Right;
                bool left = joy.DPad.Left;

                //
                int roll = (int)(joy.RightStick.Position.X * 32768);
                int pitch = (int)(joy.RightStick.Position.Y * 32768);
                bool rs = joy.RightStick.Clicked;
                bool rb = joy.RightShoulder;
                float rt = joy.RightTrigger;

                //
                bool start = joy.Start;
                bool back = joy.Back;

                //                
                bool a=joy.A;
                bool b=joy.B;
                bool x = joy.X;
                bool y = joy.Y;

                //




                string saida = "joy(" + throttle + "," + roll +"," + pitch + "," + yaw + ")\r\n";
                try
                {
                    serialPort1.Write(saida);
                    string lido = serialPort1.ReadTo("ok");
                    Console.WriteLine(lido);
                }
                catch (Exception ex) { 
                    
                }
            }
        }
Пример #2
0
        private void backgroundWorker_joystick_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                Thread.Sleep(10);
                try
                {
                    Gamepad_State_SlimDX joy = new Gamepad_State_SlimDX(SlimDX.XInput.UserIndex.One);
                    joy.Update();

                    joystick js = new joystick();
                    js.pitch    = (Int16)(joy.LeftStick.Position.Y * 250);
                    js.roll     = (Int16)(joy.LeftStick.Position.X * 250);
                    js.throttle = (Int16)(joy.RightStick.Position.Y * 10);
                    js.yaw      = (Int16)(joy.RightStick.Position.X * 250);

                    if (joy.B)
                    {
                        js.emergency = 1;
                    }
                    else
                    {
                        js.emergency = 0;
                    }

                    if (joy.A)
                    {
                        js.takeoff = 1;
                    }
                    else
                    {
                        js.takeoff = 0;
                    }

                    string json = JsonConvert.SerializeObject(js);

                    udp_send_recv(3000, json, 5);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
Пример #3
0
        public void OnRenderFrame(object s, FrameEventArgs e)
        {
            Gamepad_State_SlimDX joy = new Gamepad_State_SlimDX(SlimDX.XInput.UserIndex.One);

            joy.Update();

            node.Pitch(joy.LeftStick.Position.Y);
            node.Roll(joy.LeftStick.Position.X);
            node.Yaw(joy.RightStick.Position.X);


            node.Position += new Vector3(0, joy.RightStick.Position.Y, 0);

            if (node.Position.y < 25)
            {
                node.Position = new Vector3(node.Position.x, 25, node.Position.z);
            }

            HandleKeyboardInput(e);
        }
Пример #4
0
        static void Main(string[] args)
        {
            try
            {
                Gamepad_State_SlimDX joy  = new Gamepad_State_SlimDX(SlimDX.XInput.UserIndex.One);
                SerialPort           port = new SerialPort();
                port.PortName = "COM4";
                port.BaudRate = 115200;
                port.Open();

                while (true)
                {
                    joy.Update();


                    string cmd = "pilot(";
                    cmd += range_get(joy.LeftStick.Position.X, 0, 254);
                    cmd += ",";
                    cmd += range_get(joy.LeftStick.Position.Y, 0, 254);
                    cmd += ",";
                    cmd += range_get(joy.RightStick.Position.X, 0, 254);
                    cmd += ",";
                    cmd += range_get(joy.RightStick.Position.Y, 0, 254);
                    cmd += ",";

                    if (joy.A)
                    {
                        cmd += "1";
                    }
                    else
                    {
                        cmd += "0";
                    }

                    cmd += ",";

                    if (joy.B)
                    {
                        cmd += "1";
                    }
                    else
                    {
                        cmd += "0";
                    }

                    cmd += ")";

                    //Console.Write(cmd);

                    DateTime t_start = DateTime.Now;
                    port.DiscardOutBuffer();
                    port.DiscardInBuffer();
                    port.ReadTimeout = 30;
                    port.WriteLine(cmd + "\r");
                    try
                    {
                        string txt = port.ReadTo("ok");
                        Console.Write(txt.Replace("\r", "").Replace("\n", ""));
                        TimeSpan diff = DateTime.Now - t_start;
                        Console.WriteLine(" dt=" + diff.TotalMilliseconds);
                    }
                    catch (Exception) {
                        TimeSpan diff = DateTime.Now - t_start;
                        Console.WriteLine(" dt=" + diff.TotalMilliseconds);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    Gamepad_State_SlimDX joy       = new Gamepad_State_SlimDX(SlimDX.XInput.UserIndex.One);
                    UdpClient            udpClient = new UdpClient();

                    udpClient.Connect("192.168.1.1", 3000);

                    Int16 pitch_now    = 0;
                    Int16 roll_now     = 0;
                    Int16 yaw_now      = 0;
                    Int16 throttle_now = 0;
                    Int16 emergency    = 0;
                    Int16 takeoff      = 0;

                    int    tx_count         = 0;
                    int    tx_timeout_count = 0;
                    double timeouts_perct   = 0;

                    while (true)
                    {
                        joy.Update();

                        pitch_now    = (Int16)(joy.LeftStick.Position.Y * 128);
                        roll_now     = (Int16)(joy.LeftStick.Position.X * 128);
                        yaw_now      = (Int16)(joy.RightStick.Position.X * 128);
                        throttle_now = (Int16)(joy.RightStick.Position.Y * 128);

                        emergency = 0;
                        if (joy.B || joy.RightTrigger > 0.5)
                        {
                            emergency = 1;
                        }

                        takeoff = 0;
                        if (joy.A)
                        {
                            takeoff = 1;
                        }

                        //
                        string cmd = pitch_now + "|" + roll_now + "|" + throttle_now + "|" + yaw_now + "|" + takeoff + "|" + emergency + "|";

                        //
                        //Console.WriteLine(cmd);

                        //
                        Byte[] sendBytes = Encoding.ASCII.GetBytes(cmd);
                        udpClient.Send(sendBytes, sendBytes.Length);
                        tx_count++;
                        DateTime start = DateTime.Now;

                        while (true)
                        {
                            TimeSpan timeItTook = DateTime.Now - start;
                            if (timeItTook.Milliseconds > 17) //1s/60Hz=16.66ms
                            {
                                tx_timeout_count++;
                                timeouts_perct = ((double)tx_timeout_count / (double)tx_count) * 100;
                                Console.WriteLine("pkt=" + tx_count + " rtt timeouts=" + tx_timeout_count + " (" + timeouts_perct + "%)");
                                break;
                            }
                            if (udpClient.Available > 0)
                            {
                                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                                Byte[]     receiveBytes     = udpClient.Receive(ref RemoteIpEndPoint);
                                Console.WriteLine("pkt=" + tx_count + " rtt=" + timeItTook.TotalMilliseconds + " timeouts=" + tx_timeout_count + " (" + timeouts_perct + "%)");
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }