コード例 #1
0
ファイル: RadioDriver.cs プロジェクト: sun-yuliang/UAV-Swarm
        public int connect(string uri)
        {
            Regex reg   = new Regex("^radio://([0-9]+)(/(([0-9]+))(/((250K|1M|2M))?(/([A-F0-9]+))?)?)?$");
            Match match = reg.Match(uri);

            if (!match.Success)
            {
                return(1);
            }
            this.uri = uri;
            int channel = int.Parse(match.Groups[3].Value);

            if (channel == 0)
            {
                channel = 2;
            }
            Crazyradio.DATA_RATE datarate = Crazyradio.DATA_RATE.DR_2MPS;
            if (match.Groups[7].Value == "250K")
            {
                datarate = Crazyradio.DATA_RATE.DR_250KPS;
            }
            else if (match.Groups[7].Value == "1M")
            {
                datarate = Crazyradio.DATA_RATE.DR_1MPS;
            }
            else if (match.Groups[7].Value == "2M")
            {
                datarate = Crazyradio.DATA_RATE.DR_2MPS;
            }
            long addr    = Convert.ToInt64(match.Groups[9].Value, 16);
            long address = addr != 0 ? addr : 0xE7E7E7E7E7;

            if (_radio_manager == null)
            {
                _radio_manager = new _RadioManager(int.Parse(match.Groups[1].Value), channel, datarate, address);
            }
            else
            {
                return(2);
            }
            Crazyradio cradio = _radio_manager.enter();

            if (cradio.version >= 40)
            {
                cradio.set_arc(3);
            }

            _thread      = new Thread(run);
            _thread.Name = uri;
            _thread.Start();

            return(0);
        }
コード例 #2
0
ファイル: RadioDriver.cs プロジェクト: sun-yuliang/UAV-Swarm
        public int scan_interface(long address, out radio_interface[] result)
        {
            if (_radio_manager == null)
            {
                _radio_manager = new _RadioManager(0);
            }

            Crazyradio cradio = _radio_manager.enter();

            cradio.set_address(address);
            cradio.set_arc(1);

            cradio.set_data_rate(Crazyradio.DATA_RATE.DR_250KPS);
            int[] result1, result2, result3;
            int   cnt1 = _scan_radio_channels(cradio, out result1);

            cradio.set_data_rate(Crazyradio.DATA_RATE.DR_1MPS);
            int cnt2 = _scan_radio_channels(cradio, out result2);

            cradio.set_data_rate(Crazyradio.DATA_RATE.DR_2MPS);
            int cnt3 = _scan_radio_channels(cradio, out result3);

            int total = cnt1 + cnt2 + cnt3;

            radio_interface[] interfaces = new radio_interface[total];
            int i = 0;

            while (i < cnt1)
            {
                interfaces[i] = new radio_interface(Crazyradio.DATA_RATE.DR_250KPS, result1[i]);
                i++;
            }
            while (i < cnt1 + cnt2)
            {
                interfaces[i] = new radio_interface(Crazyradio.DATA_RATE.DR_1MPS, result2[i - cnt1]);
                i++;
            }
            while (i < total)
            {
                interfaces[i] = new radio_interface(Crazyradio.DATA_RATE.DR_2MPS, result3[i - cnt1 - cnt2]);
                i++;
            }
            result = interfaces;

            _radio_manager.close();
            _radio_manager = null;

            return(total);
        }
コード例 #3
0
ファイル: RadioDriver.cs プロジェクト: sun-yuliang/UAV-Swarm
        private _radio_ack _send_packet_safe(Crazyradio cr, byte[] packet)
        {
            packet[0] &= 0xF3;
            packet[0] |= (byte)(_curr_up << 3 | _curr_down << 2);
            _radio_ack resp = cr.send_packet(packet);

            if (resp != null && resp.ack && resp.data != null && resp.data.Length > 0 && ((resp.data[0] & 4) == (_curr_down << 2)))
            {
                _curr_down = 1 - _curr_down;
            }
            if (resp.ack)
            {
                _curr_up = 1 - _curr_up;
            }
            return(resp);
        }
コード例 #4
0
ファイル: RadioDriver.cs プロジェクト: sun-yuliang/UAV-Swarm
        public void run()
        {
            byte[] dataOut  = new byte[] { 0xFF };
            bool   wait     = false;
            int    emptyCtr = 0;

            Crazyradio cradio = _radio_manager.enter();

            for (int i = 0; i < 10; i++)
            {
                _radio_ack resp = cradio.send_packet(new byte[] { 0xFF, 0x05, 0x01 });
                if (resp.data != null && resp.data.SequenceEqual(new byte[] { 0xFF, 0x05, 0x01 }))
                {
                    _has_safelink = true;
                    break;
                }
            }
            this.needs_resending = !this._has_safelink;

            _send_packet_safe(cradio, new byte[] { 0xF3 });
            _send_packet_safe(cradio, new byte[] { 0x5D, 0x05 });

            while (true)
            {
                _radio_ack ackStatus;
                cradio = _radio_manager.enter();
                if (_has_safelink)
                {
                    ackStatus = _send_packet_safe(cradio, dataOut);
                }
                else
                {
                    ackStatus = cradio.send_packet(dataOut);
                }

                if (ackStatus == null)
                {
                    Console.WriteLine("Dongle reported ACK status == null");
                    continue;
                }

                if (!ackStatus.ack)
                {
                    _retry_before_disconnect--;
                    if (_retry_before_disconnect == 0)
                    {
                        Console.WriteLine("Too many packets lost");
                        continue;
                    }
                }
                _retry_before_disconnect = 100;

                byte[] data = ackStatus.data;
                if (data != null && data.Length > 0)
                {
                    in_queue.Enqueue(new CRTPPacket(data[0], data.Skip(1).ToArray()));
                    wait     = false;
                    emptyCtr = 0;
                }
                else
                {
                    emptyCtr += 1;
                    if (emptyCtr > 10)
                    {
                        emptyCtr = 10;
                        wait     = true;
                    }
                    else
                    {
                        wait = false;
                    }
                }

                CRTPPacket outPacket = null;
                if (out_queue.Count > 0)
                {
                    outPacket = out_queue.Dequeue();
                }
                else
                {
                    if (wait)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            Thread.Sleep(1);
                            if (out_queue.Count > 0)
                            {
                                outPacket = out_queue.Dequeue();
                                break;
                            }
                        }
                    }
                }

                if (outPacket != null)
                {
                    List <byte> tmp = new List <byte>(outPacket.data.Length + 1)
                    {
                        outPacket.header
                    };
                    tmp.AddRange(outPacket.data);
                    dataOut = tmp.ToArray();
                }
                else
                {
                    dataOut = new byte[] { 0xFF };
                }
            }
        }
コード例 #5
0
ファイル: RadioDriver.cs プロジェクト: sun-yuliang/UAV-Swarm
 private int _scan_radio_channels(Crazyradio cradio, out int[] output, int start = 0, int stop = 125)
 {
     return(cradio.scan_channels(start, stop, new byte[1] {
         0xFF
     }, out output));
 }
コード例 #6
0
ファイル: RadioDriver.cs プロジェクト: sun-yuliang/UAV-Swarm
 public _SharedRadio(int devid)
 {
     radio = new Crazyradio();
     radio.init(devid);
 }