create repository
This commit is contained in:
154
DroneClient/FormMain.cs
Normal file
154
DroneClient/FormMain.cs
Normal file
@ -0,0 +1,154 @@
|
||||
using System.Text;
|
||||
using System.Numerics;
|
||||
using System.Windows.Forms;
|
||||
using static DroneSimulator.NetClient;
|
||||
using DroneClient;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
public partial class Form_Main : Form
|
||||
{
|
||||
private NetClient netClient = new NetClient();
|
||||
|
||||
public Form_Main()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void ConnectionCallback(object o)
|
||||
{
|
||||
ConnectData data = (ConnectData)o;
|
||||
|
||||
if (!data.Connect)
|
||||
{
|
||||
Invoke((MethodInvoker)delegate
|
||||
{
|
||||
button_Connect.Text = "Connect";
|
||||
button_Connect.BackColor = Color.Transparent;
|
||||
MessageBox.Show("Connection closed");
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] send = Drone.getBytes(sendDrone);
|
||||
|
||||
data.Server.Send(send);
|
||||
}
|
||||
|
||||
Drone.DataIn sendDrone;
|
||||
|
||||
Drone.DataOut recvDrone;
|
||||
|
||||
private void ReceiveCallback(object o)
|
||||
{
|
||||
ReceiveData data = (ReceiveData)o;
|
||||
|
||||
recvDrone = (Drone.DataOut)Drone.fromBytes(data.Buffer, typeof(Drone.DataOut));
|
||||
|
||||
byte[] send = Drone.getBytes(sendDrone);
|
||||
|
||||
try { data.Server.Send(send); }
|
||||
catch { }
|
||||
}
|
||||
|
||||
private void button_Connect_Click(object sender, EventArgs e)
|
||||
{
|
||||
var done = netClient.Connect(textBox_Server_Addr.Text, (int)numericUpDown_Server_Port.Value, ConnectionCallback, ReceiveCallback);
|
||||
|
||||
switch (done)
|
||||
{
|
||||
case NetClient.ClientState.Error:
|
||||
{
|
||||
MessageBox.Show("Error connecting to server");
|
||||
break;
|
||||
}
|
||||
case NetClient.ClientState.Connected:
|
||||
{
|
||||
button_Connect.Text = "Disconnect";
|
||||
button_Connect.BackColor = Color.LimeGreen;
|
||||
break;
|
||||
}
|
||||
case NetClient.ClientState.Stop:
|
||||
{
|
||||
button_Connect.Text = "Connect";
|
||||
button_Connect.BackColor = Color.Transparent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (done != NetClient.ClientState.Connected) return;
|
||||
}
|
||||
|
||||
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
netClient?.Close();
|
||||
netClient = null;
|
||||
}
|
||||
|
||||
private void timer_Test_Tick(object sender, EventArgs e)
|
||||
{
|
||||
label_Acc_X.Text = recvDrone.AccX.ToString();
|
||||
label_Acc_Y.Text = recvDrone.AccY.ToString();
|
||||
label_Acc_Z.Text = recvDrone.AccZ.ToString();
|
||||
|
||||
label_Gyr_X.Text = recvDrone.GyrX.ToString();
|
||||
label_Gyr_Y.Text = recvDrone.GyrY.ToString();
|
||||
label_Gyr_Z.Text = recvDrone.GyrZ.ToString();
|
||||
|
||||
label_Pos_X.Text = recvDrone.PosX.ToString();
|
||||
label_Pos_Y.Text = recvDrone.PosY.ToString();
|
||||
label_Pos_L.Text = recvDrone.LaserRange.ToString();
|
||||
}
|
||||
|
||||
private void trackBar_Power_Scroll(object sender, EventArgs e)
|
||||
{
|
||||
float pow = (float)trackBar_Power.Value / 100;
|
||||
|
||||
label_Pow.Text = pow.ToString();
|
||||
|
||||
sendDrone.MotorUL = sendDrone.MotorUR = sendDrone.MotorDL = sendDrone.MotorDR = pow;
|
||||
}
|
||||
|
||||
private void button_UU_MouseDown(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (sender == button_UU)
|
||||
{
|
||||
sendDrone.MotorUL -= 0.1f; sendDrone.MotorUR -= 0.1f;
|
||||
sendDrone.MotorDL += 0.1f; sendDrone.MotorDR += 0.1f;
|
||||
}
|
||||
if (sender == button_DD)
|
||||
{
|
||||
sendDrone.MotorUL += 0.1f; sendDrone.MotorUR += 0.1f;
|
||||
sendDrone.MotorDL -= 0.1f; sendDrone.MotorDR -= 0.1f;
|
||||
}
|
||||
if (sender == button_LL)
|
||||
{
|
||||
sendDrone.MotorUL -= 0.1f; sendDrone.MotorUR += 0.1f;
|
||||
sendDrone.MotorDL -= 0.1f; sendDrone.MotorDR += 0.1f;
|
||||
}
|
||||
if (sender == button_RR)
|
||||
{
|
||||
sendDrone.MotorUL += 0.1f; sendDrone.MotorUR -= 0.1f;
|
||||
sendDrone.MotorDL += 0.1f; sendDrone.MotorDR -= 0.1f;
|
||||
}
|
||||
|
||||
if (sender == button_ML)
|
||||
{
|
||||
sendDrone.MotorUL -= 0.1f; sendDrone.MotorUR += 0.1f;
|
||||
sendDrone.MotorDL += 0.1f; sendDrone.MotorDR -= 0.1f;
|
||||
}
|
||||
|
||||
if (sender == button_MR)
|
||||
{
|
||||
sendDrone.MotorUL += 0.1f; sendDrone.MotorUR -= 0.1f;
|
||||
sendDrone.MotorDL -= 0.1f; sendDrone.MotorDR += 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
private void button_UU_MouseUp(object sender, MouseEventArgs e)
|
||||
{
|
||||
trackBar_Power_Scroll(null, null);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user