/// <summary> /// UI操作下发数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tmrUI_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 //点击测试 sends = new JosystickSend(); sends.UpDown_Value_Y = trbVertical.Value; //垂直 sends.Forward_Back_Value_R = trbAround.Value; //前后 sends.Direc_Value_Z = trbRotate.Value; //旋转 sends.Side_Shift_Value_X = trbTranslation.Value; //平移 if (isAround) //前后 { sends.Forward_Back_Value_R = trbAround.Value; //前后 sends.Side_Shift_Value_X = 128; //平移 sends.Direc_Value_Z = 128; //旋转 } if (isRotate) //旋转 { sends.Direc_Value_Z = trbRotate.Value; //旋转 sends.Forward_Back_Value_R = 128; //前后 sends.Side_Shift_Value_X = 128; //平移 } if (isTranslation) //平移 { sends.Side_Shift_Value_X = trbTranslation.Value; //平移 sends.Forward_Back_Value_R = 128; //前后 sends.Direc_Value_Z = 128; //旋转 } byte[] sendDate = SendTransmitData(iLed, iSteering_Engine1, iSteering_Engine2, sends); NetSendData(sendDate);//下发UI操作数据 }
/// <summary> /// 下发数据组 /// </summary> /// <param name="iLed">LED</param> /// <param name="iSteering_Engine1">舵机1</param> /// <param name="iSteering_Engine2">舵机2</param> /// <param name="SendMotorData">手柄操作(手柄)/UI操作(测试)</param> /// <returns></returns> public byte[] SendTransmitData(int iLed, int iSteering_Engine1, int iSteering_Engine2, JosystickSend SendMotorData) { byte[] theData = new byte[27]; int inforData = GetInfoWord(); int index = 0; //报文头 theData[index++] = 0xfe; theData[index++] = 0xfe; theData[index++] = Convert.ToByte(inforData / 256); theData[index++] = Convert.ToByte(inforData % 256);; //LED theData[index++] = Convert.ToByte(iLed / 256); //高位 theData[index++] = Convert.ToByte(iLed % 256); //低位 theData[index++] = Convert.ToByte(iLed / 256); //高位 theData[index++] = Convert.ToByte(iLed % 256); //低位 //舵机1 theData[index++] = Convert.ToByte(iSteering_Engine1 / 256); //高位 theData[index++] = Convert.ToByte(iSteering_Engine1 % 256); //低位 //舵机2 theData[index++] = Convert.ToByte(iSteering_Engine2 / 256); //高位 theData[index++] = Convert.ToByte(iSteering_Engine2 % 256); //低位 //空值 for (int i = 0; i < 8; i++) { theData[index++] = 0x00; } //运动控制数据 theData[index++] = Convert.ToByte(SendMotorData.Forward_Back_Value_R); //电机前后运动控制值 theData[index++] = Convert.ToByte(SendMotorData.Side_Shift_Value_X); //电机平移运动控制值 theData[index++] = Convert.ToByte(SendMotorData.Direc_Value_Z); //电机转向运动控制值 theData[index++] = Convert.ToByte(SendMotorData.UpDown_Value_Y); //电机垂直运动控制值 byte sum = theData[0]; for (int i = 1; i <= index - 1; i++) { sum = Convert.ToByte(sum ^ theData[i]); } //校验和 theData[index++] = sum; //结尾 theData[index++] = 0xfd; theData[index++] = 0xfd; return(theData); }
/// <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);//下发手柄操作数据 }