agrobot_base/AgroBase/AgroBase/Forms/frmWT901C.cs

370 lines
16 KiB
C#

using AgroBase.Models;
using AgroBase.Services;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading.Tasks;
namespace AgroBase.Forms
{
public partial class frmWT901C : Form
{
double OrientacaoMagnetica = 0;
double AnguloX = 0;
double AnguloY = 0;
double AnguloZ = 0;
private Visualizador3DService visualizador3D;
AsyncTaskTimerModel tmrLeitura;
//WT901C485 WT901C = Variaveis.WT901C;
public frmWT901C()
{
InitializeComponent();
// Ativa o double buffering
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
pnlInclinacao.GetType().GetMethod("SetStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(pnlInclinacao, new object[] { ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true });
picBussola.Paint += PicBussola_Paint;
VersaoArquivoModel arquivo = VersionamentoService.ArquivoModelo3D("obj");
visualizador3D = new Visualizador3DService();
visualizador3D.InitializeGLControl(pnl3D, arquivo.CaminhoCompleto);
}
private void frmWT901C_Load(object sender, EventArgs e)
{
PopularDadosGrid();
tmrLeitura = new AsyncTaskTimerModel("tmrLeitura", tmrLeitura_Tick, 500, this);
tmrLeitura.Start();
}
private void frmWT901C_FormClosing(object sender, FormClosingEventArgs e)
{
tmrLeitura.Dispose();
}
private async Task tmrLeitura_Tick()
{
AnguloX = Variaveis.OperacaoEmAndamento.Sensoriamento.InclinacaoX;
AnguloY = Variaveis.OperacaoEmAndamento.Sensoriamento.InclinacaoY;
AnguloZ = Variaveis.OperacaoEmAndamento.Sensoriamento.InclinacaoZ;
OrientacaoMagnetica = Variaveis.OperacaoEmAndamento.Sensoriamento.AnguloCarroBussola;
AtualizarDadosTela();
}
private void PopularDadosGrid()
{
gridDados.Rows.Clear();
gridDados.Columns.Clear();
gridDados.Columns.Add("clDado", "Dado");
gridDados.Columns.Add("clValor", "Valor");
gridDados.Columns.Add("clUnidade", "Unidade");
gridDados.Rows.Add("Versão", "", "");
gridDados.Rows.Add("Modelo", "", "");
gridDados.Rows.Add("Data", "", "");
gridDados.Rows.Add("Hora", "", "");
gridDados.Rows.Add("Acelerôemtro X", "", "g");
gridDados.Rows.Add("Acelerôemtro Y", "", "g");
gridDados.Rows.Add("Acelerôemtro Z", "", "g");
gridDados.Rows.Add("Acelerôemtro M", "", "g");
gridDados.Rows.Add("Giroscópio X", "", "º/s");
gridDados.Rows.Add("Giroscópio Y", "", "º/s");
gridDados.Rows.Add("Giroscópio Z", "", "º/s");
gridDados.Rows.Add("Giroscópio M", "", "º/s");
gridDados.Rows.Add("Angulo X", "", "º/s");
gridDados.Rows.Add("Angulo Y", "", "º/s");
gridDados.Rows.Add("Angulo Z", "", "º/s");
gridDados.Rows.Add("Magnetômetro X", "", "uT");
gridDados.Rows.Add("Magnetômetro Y", "", "uT");
gridDados.Rows.Add("Magnetômetro Z", "", "uT");
gridDados.Rows.Add("Magnetômetro M", "", "uT");
gridDados.Rows.Add("Temperatura", "", "ºC");
gridDados.Rows.Add("Pressão", "", "Pa");
gridDados.Rows.Add("Altitude", "", "m");
gridDados.Rows.Add("Tensão", "", "V");
gridDados.Rows.Add("D0", "", "");
gridDados.Rows.Add("D1", "", "");
gridDados.Rows.Add("D2", "", "");
gridDados.Rows.Add("D3", "", "");
gridDados.Rows.Add("q0", "", "");
gridDados.Rows.Add("q1", "", "");
gridDados.Rows.Add("q2", "", "");
gridDados.Rows.Add("q3", "", "");
gridDados.Rows.Add("Longitude", "", "º");
gridDados.Rows.Add("Latitude", "", "º");
gridDados.Rows.Add("GPS Altitude", "", "m");
gridDados.Rows.Add("GPS Angulo", "", "º");
gridDados.Rows.Add("GPS Velocidade da Terra", "", "km/h");
gridDados.Rows.Add("Número Satélites", "", "");
gridDados.Rows.Add("Precisão", "", "");
gridDados.Rows.Add("Precisão Horizontal", "", "");
gridDados.Rows.Add("Precisão Vertical", "", "");
}
#region 3D
/*
private void InitializeGLControl()
{
// Inicialize o GLControl com os parâmetros corretos
glControl = new GLControl(new OpenTK.Graphics.GraphicsMode(32, 24, 0, 8));
glControl.Dock = DockStyle.Fill;
glControl.Paint += new PaintEventHandler(GLControl_Paint);
glControl.Resize += new EventHandler(GLControl_Resize);
glControl.TopLevel = false;
glControl.Visible = true;
glControl.FormBorderStyle = FormBorderStyle.None;
pnl3D.Controls.Add(glControl);
GL.Enable(EnableCap.DepthTest);
}
private void GLControl_Paint(object sender, PaintEventArgs e)
{
Render();
}
private void GLControl_Resize(object sender, EventArgs e)
{
GL.Viewport(0, 0, glControl.Width, glControl.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, glControl.Width / (float)glControl.Height, 1.0f, 64.0f);
GL.LoadMatrix(ref perspective);
}
private void Render()
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
// Ajuste a matriz de visualização para garantir que a câmera esteja fora do cubo
Matrix4 lookAt = Matrix4.LookAt(new Vector3(4, 3, 3), Vector3.Zero, Vector3.UnitY);
GL.LoadMatrix(ref lookAt);
GL.Rotate(AnguloY, 1.0f, 0.0f, 0.0f);
GL.Rotate(AnguloZ, 0.0f, 1.0f, 0.0f);
GL.Rotate(AnguloX, 0.0f, 0.0f, 1.0f);
GL.Begin(PrimitiveType.Quads);
// Frente (Vermelho)
GL.Color3(1.0f, 0.0f, 0.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
// Trás (Verde)
GL.Color3(0.0f, 1.0f, 0.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
// Esquerda (Azul)
GL.Color3(0.0f, 0.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
// Direita (Amarelo)
GL.Color3(1.0f, 1.0f, 0.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
// Topo (Ciano)
GL.Color3(0.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
// Fundo (Magenta)
GL.Color3(1.0f, 0.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.End();
glControl.SwapBuffers();
}
*/
#endregion
private void AtualizarDadosTela()
{
//pnlOrientacao.Invalidate();
//picBussola.Image = FuncoesGlobais.RotacionarImagem(Resources.bussola, OrientacaoMagnetica, false);
picBussola.Invalidate();
//glControl.Invalidate();
pnlInclinacao.Invalidate();
visualizador3D.AtualizarAngulos(AnguloX, AnguloY, AnguloZ);
txtX.Text = AnguloX.ToString("0.00");
txtY.Text = AnguloY.ToString("0.00");
txtZ.Text = AnguloZ.ToString("0.00");
gridDados.Rows[0].Cells[1].Value = WT901CService.DadosLeitura.Versao;
gridDados.Rows[1].Cells[1].Value = WT901CService.DadosLeitura.Modelo;
gridDados.Rows[2].Cells[1].Value = WT901CService.DadosLeitura.DataHora.ToString("dd/MM/yyyy");
gridDados.Rows[3].Cells[1].Value = WT901CService.DadosLeitura.DataHora.ToString("HH:mm:ss");
gridDados.Rows[4].Cells[1].Value = WT901CService.DadosLeitura.AccX;
gridDados.Rows[5].Cells[1].Value = WT901CService.DadosLeitura.AccY;
gridDados.Rows[6].Cells[1].Value = WT901CService.DadosLeitura.AccZ;
gridDados.Rows[7].Cells[1].Value = WT901CService.DadosLeitura.AccM;
gridDados.Rows[8].Cells[1].Value = WT901CService.DadosLeitura.AsX;
gridDados.Rows[9].Cells[1].Value = WT901CService.DadosLeitura.AsY;
gridDados.Rows[10].Cells[1].Value = WT901CService.DadosLeitura.AsZ;
gridDados.Rows[11].Cells[1].Value = WT901CService.DadosLeitura.AsM;
gridDados.Rows[12].Cells[1].Value = WT901CService.DadosLeitura.AngleX;
gridDados.Rows[13].Cells[1].Value = WT901CService.DadosLeitura.AngleY;
gridDados.Rows[14].Cells[1].Value = WT901CService.DadosLeitura.AngleZ;
gridDados.Rows[15].Cells[1].Value = WT901CService.DadosLeitura.HX;
gridDados.Rows[16].Cells[1].Value = WT901CService.DadosLeitura.HY;
gridDados.Rows[17].Cells[1].Value = WT901CService.DadosLeitura.HZ;
gridDados.Rows[18].Cells[1].Value = WT901CService.DadosLeitura.HM;
gridDados.Rows[19].Cells[1].Value = WT901CService.DadosLeitura.Temperatura;
gridDados.Rows[20].Cells[1].Value = WT901CService.DadosLeitura.Pressao;
gridDados.Rows[21].Cells[1].Value = WT901CService.DadosLeitura.Altitude;
gridDados.Rows[22].Cells[1].Value = "";
gridDados.Rows[23].Cells[1].Value = WT901CService.DadosLeitura.D0;
gridDados.Rows[24].Cells[1].Value = WT901CService.DadosLeitura.D1;
gridDados.Rows[25].Cells[1].Value = WT901CService.DadosLeitura.D2;
gridDados.Rows[26].Cells[1].Value = WT901CService.DadosLeitura.D3;
gridDados.Rows[27].Cells[1].Value = WT901CService.DadosLeitura.Q0;
gridDados.Rows[28].Cells[1].Value = WT901CService.DadosLeitura.Q1;
gridDados.Rows[29].Cells[1].Value = WT901CService.DadosLeitura.Q2;
gridDados.Rows[30].Cells[1].Value = WT901CService.DadosLeitura.Q3;
gridDados.Rows[31].Cells[1].Value = WT901CService.DadosLeitura.Longitude;
gridDados.Rows[32].Cells[1].Value = WT901CService.DadosLeitura.Latitude;
gridDados.Rows[33].Cells[1].Value = WT901CService.DadosLeitura.GPS_Altitude;
gridDados.Rows[34].Cells[1].Value = WT901CService.DadosLeitura.GPS_Orientacao;
gridDados.Rows[35].Cells[1].Value = WT901CService.DadosLeitura.GPS_Velcodiade;
gridDados.Rows[36].Cells[1].Value = WT901CService.DadosLeitura.GPS_NumeroSatelites;
gridDados.Rows[37].Cells[1].Value = WT901CService.DadosLeitura.PDOP;
gridDados.Rows[38].Cells[1].Value = WT901CService.DadosLeitura.HDOP;
gridDados.Rows[39].Cells[1].Value = WT901CService.DadosLeitura.VDOP;
}
private void pnlInclinacao_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int centerX = pnlInclinacao.Width / 2;
int centerY = pnlInclinacao.Height / 2;
int radius = 10;
// Desenhar fundo com bordas arredondadas
GraphicsPath path = new GraphicsPath();
int cornerRadius = 20;
path.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90);
path.AddArc(pnlInclinacao.Width - cornerRadius, 0, cornerRadius, cornerRadius, 270, 90);
path.AddArc(pnlInclinacao.Width - cornerRadius, pnlInclinacao.Height - cornerRadius, cornerRadius, cornerRadius, 0, 90);
path.AddArc(0, pnlInclinacao.Height - cornerRadius, cornerRadius, cornerRadius, 90, 90);
path.CloseAllFigures();
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillPath(new SolidBrush(Color.LightBlue), path);
g.DrawPath(new Pen(Color.DarkBlue, 2), path);
// Desenhar linhas de referência
Pen pen = new Pen(Color.Gray, 1);
g.DrawLine(pen, centerX, 0, centerX, pnlInclinacao.Height);
g.DrawLine(pen, 0, centerY, pnlInclinacao.Width, centerY);
// Desenhar linhas adicionais para ângulos
for (int i = -90; i <= 90; i += 15)
{
int posX = centerX + (int)(i * (pnlInclinacao.Width / 2 - radius) / 90);
int posY = centerY - (int)(i * (pnlInclinacao.Height / 2 - radius) / 90);
g.DrawLine(pen, centerX, centerY, posX, centerY);
g.DrawLine(pen, centerX, centerY, centerX, posY);
}
// Desenhar régua na linha central
for (int i = -90; i <= 90; i += 15)
{
int posX = centerX + (int)(i * (pnlInclinacao.Width / 2 - radius) / 90);
int posY = centerY - (int)(i * (pnlInclinacao.Height / 2 - radius) / 90);
// Marcas horizontais
g.DrawLine(pen, posX, centerY - 5, posX, centerY + 5);
g.DrawString(i.ToString(), new Font("Arial", 8), Brushes.Black, posX - 10, centerY + 10);
// Marcas verticais
g.DrawLine(pen, centerX - 5, posY, centerX + 5, posY);
g.DrawString(i.ToString(), new Font("Arial", 8), Brushes.Black, centerX + 10, posY - 5);
}
// Calcular a posição do círculo com base nos ângulos
int circlePosX = centerX + (int)(AnguloX * (pnlInclinacao.Width / 2 - radius) / 90);
int circlePosY = centerY - (int)(AnguloY * (pnlInclinacao.Height / 2 - radius) / 90);
// Desenhar o círculo
Brush brush = new SolidBrush(Color.Red);
g.FillEllipse(brush, circlePosX - radius, circlePosY - radius, radius * 2, radius * 2);
}
private void PicBussola_Paint(object sender, PaintEventArgs e)
{
PictureBox pictureBoxCompass = (PictureBox)sender;
float orientation = (float)OrientacaoMagnetica - 90; // Exemplo de orientação em graus (você pode substituir pela sua variável de orientação)
// Converter a orientação para radianos
float angle = orientation * (float)Math.PI / 180.0f;
// Calcular o raio do círculo (metade do menor lado do PictureBox)
float radius = Math.Min(pictureBoxCompass.Width, pictureBoxCompass.Height) / 2.0f - 6; // Ajuste o valor para posicionar o triângulo corretamente
// Calcular a posição do centro do PictureBox
float centerX = pictureBoxCompass.Width / 2.0f;
float centerY = pictureBoxCompass.Height / 2.0f;
// Calcular a posição do ponto superior do triângulo
float tipX = centerX + radius * (float)Math.Cos(angle);
float tipY = centerY + radius * (float)Math.Sin(angle);
// Calcular os pontos do triângulo
PointF[] points = new PointF[]
{
new PointF(tipX, tipY), // Ponto superior do triângulo
new PointF(centerX + (radius - 5) * (float)Math.Cos(angle + Math.PI / 10), centerY + (radius - 5) * (float)Math.Sin(angle + Math.PI / 10)), // Ponto inferior esquerdo
new PointF(centerX + (radius - 5) * (float)Math.Cos(angle - Math.PI / 10), centerY + (radius - 5) * (float)Math.Sin(angle - Math.PI / 10)) // Ponto inferior direito
};
// Definir o pincel para desenhar o triângulo
using (Brush redBrush = new SolidBrush(Color.Red))
{
// Desenhar o triângulo preenchido
e.Graphics.FillPolygon(redBrush, points);
}
}
}
}