示例#1
0
        static HDCallbackCode AnchoredSpringForceHandler(IntPtr pUserData)
        {
            uint hHD = (uint)pUserData;

            double[] force = new double[3] {
                0.0, 0.0, 0.0
            };

            HDAPI.hdBeginFrame(hHD);

            double[] position = new double[3];
            HDAPI.hdGetDoublev(HDGetParameters.HD_CURRENT_POSITION, position);

            int currButtons = 0;
            int lastButtons = 0;

            HDAPI.hdGetIntegerv(HDGetParameters.HD_LAST_BUTTONS, ref lastButtons);
            HDAPI.hdGetIntegerv(HDGetParameters.HD_CURRENT_BUTTONS, ref currButtons);

            //按下 Button 1
            if ((currButtons & (int)HDButtonMasks.HD_DEVICE_BUTTON_1) != 0 &&
                (lastButtons & (int)HDButtonMasks.HD_DEVICE_BUTTON_1) == 0)
            {
                anchor      = position;
                renderForce = true;

                Console.WriteLine("gSpringStiffness:{0}", gSpringStiffness);
                //Console.WriteLine("Button Down:{0}  {1}  {2}", position[0], position[1], position[2]);
            }
            else if ((currButtons & (int)HDButtonMasks.HD_DEVICE_BUTTON_1) == 0 &&
                     (lastButtons & (int)HDButtonMasks.HD_DEVICE_BUTTON_1) != 0)
            {
                renderForce = false;
                //向设备发送零力,否则它将继续呈现最后发送的力
                HDAPI.hdSetDoublev(HDSetParameters.HD_CURRENT_FORCE, force);
            }

            if (renderForce)
            {
                //计算弹簧力为 F = k * (anchor - position),这将吸引设备位置朝向锚点位置
                Vector3D.Subtrace(ref force, anchor, position);
                Vector3D.ScaleInPlace(ref force, gSpringStiffness);

                HDAPI.hdSetDoublev(HDSetParameters.HD_CURRENT_FORCE, force);
            }

            HDAPI.hdEndFrame(hHD);

            return(HDCallbackCode.HD_CALLBACK_CONTINUE);
        }
示例#2
0
        static void Main(string[] args)
        {
            hHD   = HDAPI.hdInitDevice(null);
            error = HDAPI.hdGetError();

            if (error.CheckedError())
            {
                Console.WriteLine("Init Error.");
                Console.ReadKey();
                return;
            }

            //获取支持的校准样式,因为一些设备可能支持多种类型的校准
            HDAPI.hdGetIntegerv(HDGetParameters.HD_CALIBRATION_STYLE, ref supportedCalibrationStyles);
            Console.WriteLine("supportedCalibrationStyles:{0} {1}", supportedCalibrationStyles,
                              Enum.GetName(typeof(HDCalibrationStyles), supportedCalibrationStyles));

            //使用墨水池校准
            HDAPI.hdUpdateCalibration(HDCalibrationStyles.HD_CALIBRATION_INKWELL);
            //启动 Servo Loop
            HDAPI.hdStartScheduler();

#if 直接获取坐标位置输出
            double[] pPosition = new double[3];
            int      buttons   = 0;
            while (true)
            {
                if (HDAPI.hdCheckCalibration() == HDCalibrationCodes.HD_CALIBRATION_NEEDS_UPDATE)
                {
                    HDAPI.hdUpdateCalibration(HDCalibrationStyles.HD_CALIBRATION_INKWELL);
                }

                HDAPI.hdBeginFrame(hHD);
                HDAPI.hdGetDoublev(HDGetParameters.HD_CURRENT_POSITION, pPosition);
                HDAPI.hdGetIntegerv(HDGetParameters.HD_CURRENT_BUTTONS, ref buttons);
                HDAPI.hdEndFrame(hHD);

                Console.WriteLine("Button Status:Btn1:{0}  Btn2:{1}  Btn3:{2}  Btn4:{3}",
                                  buttons & (int)HDButtonMasks.HD_DEVICE_BUTTON_1,
                                  buttons & (int)HDButtonMasks.HD_DEVICE_BUTTON_2,
                                  buttons & (int)HDButtonMasks.HD_DEVICE_BUTTON_3,
                                  buttons & (int)HDButtonMasks.HD_DEVICE_BUTTON_4);
                Console.WriteLine("Position: X:{0}  Y:{1}   Z:{2}", pPosition[0], pPosition[1], pPosition[2]);

                Thread.Sleep(100);
            }
#endif

#if 使用调度器同步输出位置
            PositionSynchronous = GetSyncPos;
            Vector3D v3 = new Vector3D();
            v3.HHD = hHD;

            IntPtr pPosition = OHUtils.StructToIntPtr(v3);
            while (true)
            {
                HDAPI.hdScheduleSynchronous(PositionSynchronous, pPosition, HDSchedulerPriority.HD_DEFAULT_SCHEDULER_PRIORITY);
                v3 = OHUtils.IntPtrToStruct <Vector3D>(pPosition);
                Console.WriteLine("Position: X:{0}  Y:{1}   Z:{2}", v3.X, v3.Y, v3.Z);

                //Sleep一下,不然打印的数据太多,看不清,实际项目是不需要的
                //Thread.Sleep(100);
            }
#endif
            //停止 Servo Loop
            HDAPI.hdStopScheduler();
            //禁用设备
            HDAPI.hdDisableDevice(hHD);
            Console.ReadKey();
            return;
        }