Esempio n. 1
0
        static void Main(string[] args)
        {
            ZenClientHandle_t zenHandle = new ZenClientHandle_t();

            OpenZen.ZenInit(zenHandle);

            ExThread obj = new ExThread(zenHandle);

            // Creating thread
            // Using thread class
            Thread thr = new Thread(new ThreadStart(obj.sensorEventThread));

            thr.Start();

            // start sensor listing, new sensors will be reported as Events in our event thread
            OpenZen.ZenListSensorsAsync(zenHandle);

            while (!obj.mSearchDone)
            {
                Console.WriteLine("Searching for sensors ...");
                Thread.Sleep(1000);
            }

            if (obj.mFoundSensors.Count == 0)
            {
                Console.WriteLine("No sensor found on the system");
                OpenZen.ZenShutdown(zenHandle);
                thr.Abort();
                return;
            }

            ZenSensorInitError sensorInitError = ZenSensorInitError.ZenSensorInitError_Max;

            // try three connection attempts
            for (int i = 0; i < 3; i++)
            {
                ZenSensorHandle_t sensorHandle = new ZenSensorHandle_t();
                sensorInitError = OpenZen.ZenObtainSensor(zenHandle, obj.mFoundSensors[0], sensorHandle);
                Console.WriteLine("Connecting to sensor " + obj.mFoundSensors[0].identifier + " on IO interface " + obj.mFoundSensors[0].ioType);
                if (sensorInitError == ZenSensorInitError.ZenSensorInitError_None)
                {
                    Console.WriteLine("Succesfully connected to sensor");
                    break;
                }
            }

            if (sensorInitError != ZenSensorInitError.ZenSensorInitError_None)
            {
                Console.WriteLine("Could not connect to sensor");
                System.Environment.Exit(1);
            }

            // stream some data
            Thread.Sleep(5000);

            OpenZen.ZenShutdown(zenHandle);
            thr.Abort();
        }
Esempio n. 2
0
        public static void main()
        {
            // Start of Code
            ZenClientHandle_t zenHandle = new ZenClientHandle_t();

            OpenZen.ZenInit(zenHandle);

            ExThread obj = new ExThread(zenHandle);

            // Creating thread
            // Using thread class
            thr = new Thread(new ThreadStart(obj.sensorEventThread));
            thr.Start();

            // start sensor listing, new sensors will be reported as Events in our event thread
            OpenZen.ZenListSensorsAsync(zenHandle);

            while (!obj.mSearchDone)
            {
                Console.WriteLine("Searching for sensors ...");
                Thread.Sleep(1000);
            }

            ZenSensorInitError sensorInitError = ZenSensorInitError.ZenSensorInitError_Max;

            // try three connection attempts
            for (int i = 0; i < 10; i++)
            {
                ZenSensorHandle_t sensorHandle = new ZenSensorHandle_t();
                sensorInitError = OpenZen.ZenObtainSensor(zenHandle, obj.mFoundSensors[0], sensorHandle);
                if (sensorInitError == ZenSensorInitError.ZenSensorInitError_None)
                {
                    Console.WriteLine("Succesfully connected to sensor");
                    break;
                }
            }

            if (sensorInitError != ZenSensorInitError.ZenSensorInitError_None)
            {
                Console.WriteLine("Could not connect to sensor");
                System.Environment.Exit(1);
            }
        }
Esempio n. 3
0
            // Non-static method
            public void sensorEventThread()
            {
                try
                {
                    OpenZen.ZenInit(device_Handle);
                    ZenEvent zenEvent  = new ZenEvent();
                    Boolean  connected = false;

                    ZenSensorInitError sensorInitError = ZenSensorInitError.ZenSensorInitError_Max;
                    // try three connection attempts
                    for (int i = 0; i < 1; i++)
                    {
                        sensorInitError = OpenZen.ZenObtainSensor(device_Handle, mSensor, device_Sensor);

                        if (sensorInitError == ZenSensorInitError.ZenSensorInitError_None)
                        {
                            connected = true;
                            // Enable Disconnect Button
                            MainForm mainForm = FormProvider.getMain();
                            switch (device_No)
                            {
                            case 1:
                                if (mainForm.InvokeRequired)
                                {
                                    mainForm.BeginInvoke((Action) delegate() {
                                        mainForm.device_List1.Enabled       = false;
                                        mainForm.disconnect_Device1.Visible = true;
                                        mainForm.connect_Device1.Enabled    = false;
                                    });
                                }
                                else
                                {
                                    mainForm.device_List1.Enabled       = false;
                                    mainForm.disconnect_Device1.Visible = true;
                                    mainForm.connect_Device1.Enabled    = false;
                                }
                                break;

                            case 2:
                                if (mainForm.InvokeRequired)
                                {
                                    mainForm.BeginInvoke((Action) delegate() {
                                        mainForm.device_List2.Enabled       = false;
                                        mainForm.disconnect_Device2.Visible = true;
                                        mainForm.connect_Device2.Enabled    = false;
                                    });
                                }
                                else
                                {
                                    mainForm.device_List2.Enabled       = false;
                                    mainForm.disconnect_Device2.Visible = true;
                                    mainForm.connect_Device2.Enabled    = false;
                                }
                                break;

                            case 3:
                                if (mainForm.InvokeRequired)
                                {
                                    mainForm.BeginInvoke((Action) delegate() {
                                        mainForm.device_List3.Enabled       = false;
                                        mainForm.disconnect_Device3.Visible = true;
                                        mainForm.connect_Device3.Enabled    = false;
                                    });
                                }
                                else
                                {
                                    mainForm.device_List3.Enabled       = false;
                                    mainForm.disconnect_Device3.Visible = true;
                                    mainForm.connect_Device3.Enabled    = false;
                                }
                                break;

                            case 4:
                                if (mainForm.InvokeRequired)
                                {
                                    mainForm.BeginInvoke((Action) delegate() {
                                        mainForm.device_List4.Enabled       = false;
                                        mainForm.disconnect_Device4.Visible = true;
                                        mainForm.connect_Device4.Enabled    = false;
                                    });
                                }
                                else
                                {
                                    mainForm.device_List4.Enabled       = false;
                                    mainForm.disconnect_Device4.Visible = true;
                                    mainForm.connect_Device4.Enabled    = false;
                                }
                                break;
                            }
                            MessageBox.Show("Succesfully connected to sensor for Device #" + device_No.ToString());
                            break;
                        }
                        else
                        {
                            MessageBox.Show("Failed");
                        }
                    }

                    while (connected)
                    {
                        //Console.WriteLine(mZenHandle);
                        if (OpenZen.ZenWaitForNextEvent(device_Handle, zenEvent))
                        {
                            if (zenEvent.component.handle != 0)
                            {
                                switch (zenEvent.eventType)
                                {
                                case (int)ZenImuEvent.ZenImuEvent_Sample:
                                    count++;

                                    // read raw accelerometer
                                    OpenZenFloatArray raw_fa = OpenZenFloatArray.frompointer(zenEvent.data.imuData.aRaw);
                                    // read calibrated accelerometer
                                    OpenZenFloatArray fa = OpenZenFloatArray.frompointer(zenEvent.data.imuData.a);
                                    // read raw accelerometer
                                    OpenZenFloatArray raw_fg = OpenZenFloatArray.frompointer(zenEvent.data.imuData.gRaw);
                                    // read calibrated gyroscope
                                    OpenZenFloatArray fg = OpenZenFloatArray.frompointer(zenEvent.data.imuData.g);
                                    // read raw magnetometer
                                    OpenZenFloatArray raw_fb = OpenZenFloatArray.frompointer(zenEvent.data.imuData.bRaw);
                                    // read calibrated magnetometer
                                    OpenZenFloatArray fb = OpenZenFloatArray.frompointer(zenEvent.data.imuData.b);
                                    // read euler angle
                                    OpenZenFloatArray fr = OpenZenFloatArray.frompointer(zenEvent.data.imuData.r);
                                    // read quaternion
                                    OpenZenFloatArray fq = OpenZenFloatArray.frompointer(zenEvent.data.imuData.q);


                                    // Create new imuData object
                                    IMUData imuData = new IMUData(
                                        raw_fa.getitem(0), raw_fa.getitem(1), raw_fa.getitem(2),
                                        fa.getitem(0), fa.getitem(1), fa.getitem(2),
                                        raw_fg.getitem(0), raw_fg.getitem(1), raw_fg.getitem(2),
                                        fg.getitem(0), fg.getitem(1), fg.getitem(2),
                                        raw_fb.getitem(0), raw_fb.getitem(1), raw_fb.getitem(2),
                                        fb.getitem(0), fb.getitem(1), fb.getitem(2),
                                        fr.getitem(0), fr.getitem(1), fr.getitem(2),
                                        fq.getitem(0), fq.getitem(1), fq.getitem(2)
                                        );     // Push data in to the new imuData Object

                                    switch (device_No)
                                    {
                                    case (1):
                                        transferDevice1_Data(imuData);
                                        //Debug.WriteLine(count);
                                        break;

                                    case (2):
                                        transferDevice2_Data(imuData);
                                        break;

                                    case (3):
                                        transferDevice3_Data(imuData);
                                        break;

                                    case (4):
                                        transferDevice4_Data(imuData);
                                        Debug.WriteLine(count);
                                        break;
                                    }

                                    break;
                                }
                            }
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                }
            }