/// <summary> /// 定时器的回调方法 /// </summary> /// <param name="state"></param> //private void OnTimerCallback(object state) public JoystickHandle OnTimerCallback() { JoyHandle = new JoystickHandle(); JoystickAPI.JOYINFOEX infoEx = new JoystickAPI.JOYINFOEX(); infoEx.dwSize = Marshal.SizeOf(typeof(JoystickAPI.JOYINFOEX)); infoEx.dwFlags = (int)JoystickAPI.JOY_RETURNBUTTONS; int result = JoystickAPI.joyGetPosEx(this.Id, ref infoEx); if (result == JoystickAPI.JOYERR_NOERROR) { JoyHandle.Xpos = infoEx.dwXpos; JoyHandle.Ypos = infoEx.dwYpos; JoyHandle.Zpos = infoEx.dwZpos; JoyHandle.Rpos = infoEx.dwRpos; } return(JoyHandle); }
/// <summary> /// 手柄操作定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tmrHandle_Tick(object sender, EventArgs e) { //LED及舵角操作 int iLed = this.trbLED.Value; //LED int iSteering_Engine1 = trbSteering_Engine1.Value + 500; //舵机1 int iSteering_Engine2 = trbSteering_Engine2.Value + 500; //舵机2 //手柄操作 JoystickHandle handle = joy.OnTimerCallback(); vscY.Value = Convert.ToInt32(handle.Ypos / 256); //上浮/下潜 hscX.Value = Convert.ToInt32(handle.Xpos / 256); //左移/右移 vscR.Value = Convert.ToInt32(handle.Rpos / 256); //前进后退 hscZ.Value = Convert.ToInt32(handle.Zpos / 256); //左转/右转 sends = new JosystickSend(); sends.UpDown_Value_Y = vscY.Value;//垂直 //左摇杆X轴数据处理 if (((handle.Rpos > 32757) && (handle.Rpos < 32777)) && ((handle.Zpos > 32757) && (handle.Zpos < 32777))) //判断右摇杆是否处于中间状态 { sends.Side_Shift_Value_X = hscX.Value; //平移赋值 sends.Forward_Back_Value_R = 128; //R轴赋中值 sends.Direc_Value_Z = 128; //Z轴赋中值 } else //右摇杆操作或者左摇杆与右摇杆同时操作 { if ((handle.Zpos >= 32767) && (handle.Rpos <= 32767)) //第一象限 { if (handle.Rpos <= (65535 - handle.Zpos)) //靠近R周 { sends.Forward_Back_Value_R = vscR.Value; //向前走 sends.Direc_Value_Z = 128; //Z轴赋中值 } else//靠近Z轴 { sends.Direc_Value_Z = hscZ.Value; //旋转 sends.Forward_Back_Value_R = 128; //R轴赋中值 } } else if ((handle.Zpos < 32767) && (handle.Rpos <= 32767)) //第二象限 { if (handle.Rpos <= handle.Zpos) //靠近R轴 { sends.Forward_Back_Value_R = vscR.Value; sends.Direc_Value_Z = 128;//Z轴赋中值 } else //靠近Z轴 { sends.Direc_Value_Z = hscZ.Value; //旋转 sends.Forward_Back_Value_R = 128; //R轴赋中值 } } else if ((handle.Zpos < 32767) && (handle.Rpos > 32767)) //第三象限 { if (handle.Rpos < 65535 - handle.Zpos) //靠近Z周 { sends.Direc_Value_Z = hscZ.Value; sends.Forward_Back_Value_R = 128;//R轴赋中值 } else //靠近R轴 { sends.Forward_Back_Value_R = vscR.Value; sends.Direc_Value_Z = 128; } } else if ((handle.Zpos >= 32767) && (handle.Rpos > 32767)) //第四象限 { if (handle.Rpos >= handle.Zpos) //靠近R轴 { sends.Forward_Back_Value_R = vscR.Value; sends.Direc_Value_Z = 128;//Z轴赋中值 } else //靠近Z轴 { sends.Direc_Value_Z = hscZ.Value; sends.Forward_Back_Value_R = 128;//R轴赋中值 } } sends.Side_Shift_Value_X = 128;//平移中值 } byte[] sendDate = SendTransmitData(iLed, iSteering_Engine1, iSteering_Engine2, sends); NetSendData(sendDate);//下发手柄操作数据 }