Commit 44e7ca58 authored by 17임경현's avatar 17임경현

majikayo~~

parent 5f2567e8
This diff is collapsed.
This diff is collapsed.
#include "MPU9250.h"
MPU9250 IMU(Wire,0x68);
int status;
void setup()
{
Serial.begin(115200);
while(!Serial) {}
status = IMU.begin();
if (status < 0)
{
Serial.print("IMU Error: ");
Serial.println(status);
while(1) { }
}
}
void writefloat(float x)
{
byte* ptr = (byte*)&x;
Serial.print(ptr[3], HEX);
Serial.print(ptr[2], HEX);
Serial.print(ptr[1], HEX);
Serial.print(ptr[0], HEX);
Serial.print(" ");
}
void loop()
{
IMU.readSensor();
Serial.print("=");
writefloat(IMU.getAccelX_mss());
writefloat(IMU.getAccelY_mss());
writefloat(IMU.getAccelZ_mss());
writefloat(IMU.getGyroX_rads());
writefloat(IMU.getGyroY_rads());
writefloat(IMU.getGyroZ_rads());
Serial.println();
delay(20);
}
<Window x:Class="wuhyuo.MainWindow"
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wuhyuo"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Initialized="Window_Initialized" Closed="Window_Closed">
<Canvas HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransform="1 0 0 -1 0 0">
<Rectangle x:Name="rect" Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" Fill="#FF6BE8B5" RenderTransformOrigin="0.5,0.5" >
<Rectangle.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="rectRotate" Angle="0"/>
<TranslateTransform X="-25" Y="-25"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="0">
<Grid ClipToBounds="True">
<Label>AccelX</Label>
<Canvas x:Name="canvas1" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="1">
<Grid ClipToBounds="True">
<Label>AccelY</Label>
<Canvas x:Name="canvas2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="2">
<Grid ClipToBounds="True">
<Label>AccelZ</Label>
<Canvas x:Name="canvas3" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="3">
<Grid ClipToBounds="True">
<Label>Accel Total</Label>
<Canvas x:Name="canvas4" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="0">
<Grid ClipToBounds="True">
<Label>GryoX</Label>
<Canvas x:Name="canvas5" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="1">
<Grid ClipToBounds="True">
<Label>GyroY</Label>
<Canvas x:Name="canvas6" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="2">
<Grid ClipToBounds="True">
<Label>GyroZ</Label>
<Canvas x:Name="canvas7" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="3">
<Grid ClipToBounds="True">
<Label>Gyro Total</Label>
<Canvas x:Name="canvas8" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</Border>
</Grid>
</Window>
......@@ -14,9 +14,8 @@ using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.IO.Ports;
using System.Diagnostics;
namespace wuhyuo
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
......@@ -30,6 +29,17 @@ namespace wuhyuo
private void Window_Initialized(object sender, EventArgs e)
{
renderer = new GraphRenderer[] {
new GraphRenderer(canvas1, Colors.Red),
new GraphRenderer(canvas2, Colors.Orange),
new GraphRenderer(canvas3, Colors.DarkKhaki),
new GraphRenderer(canvas4, Colors.CornflowerBlue),
new GraphRenderer(canvas5, Colors.Green),
new GraphRenderer(canvas6, Colors.Blue),
new GraphRenderer(canvas7, Colors.Purple),
new GraphRenderer(canvas8, Colors.Chocolate),
};
_thrd = new Thread(InputThread);
_thrd.Start();
}
......@@ -42,32 +52,42 @@ namespace wuhyuo
private void InputThread()
{
SerialPort port = new SerialPort("COM8", 115200);
try
{
using (var port = new SerialPort("COM6", 115200))
port.Open();
while (true)
{
port.Open();
while (true)
try
{
try
string input = port.ReadLine();
if (input.FirstOrDefault() != '=')
continue;
double[] values = input.Substring(1).Split(" \t\r\n".ToArray()).Where(x => x.Length > 0)
.Select(x => (double)uint2float(Convert.ToUInt32(x, 16))).ToArray();
if (values.Length != 6)
continue;
Dispatcher.InvokeAsync(() =>
{
string input = port.ReadLine();
double[] values = input.Split(" \t\r\n".ToArray()).Where(x => x.Length > 0)
.Select(x => Convert.ToDouble(x)).ToArray();
if (values.Length != 6)
continue;
Dispatcher.Invoke(() =>
{
Debug.WriteLine(input);
Canvas.SetLeft(rect, values[0] * 10);
Canvas.SetRight(rect, values[1] * 10);
rectRotate.Angle = values[5] * 180 / Math.PI;
});
}
catch (FormatException) { }
catch (TimeoutException) { }
renderer[0].AddValue(values[0]);
renderer[1].AddValue(values[1]);
renderer[2].AddValue(values[2]);
renderer[4].AddValue(values[3]);
renderer[5].AddValue(values[4]);
renderer[6].AddValue(values[5]);
double acc = Math.Sqrt(values[0] * values[0] + values[1] * values[1] + values[2] * values[2]);
double gyro = Math.Sqrt(values[3] * values[3] + values[4] * values[4] + values[5] * values[5]);
renderer[3].AddValue(acc);
renderer[7].AddValue(gyro);
});
}
catch (FormatException) { }
catch (TimeoutException) { }
catch (OverflowException) { }
}
}
catch (InvalidOperationException)
......@@ -78,8 +98,72 @@ namespace wuhyuo
Close();
});
}
finally
{
port.Close();
}
}
private static unsafe float uint2float(uint value)
{
return *(float*)(&value);
}
private Thread _thrd;
private GraphRenderer[] renderer;
}
class GraphRenderer
{
public Canvas Canvas { get; }
public Color Color { get; set; }
public double Time { get; set; }
public double DeltaTime { get; set; }
public double Value { get; set; }
private TranslateTransform translate = new TranslateTransform();
private ScaleTransform scale = new ScaleTransform();
private List<Line> lines = new List<Line>();
public GraphRenderer(Canvas canvas, Color color)
{
Canvas = canvas;
Color = color;
Time = 0;
Value = 0;
DeltaTime = 1;
var transform = new TransformGroup();
transform.Children.Add(translate);
transform.Children.Add(scale);
Canvas.RenderTransform = transform;
}
public void AddValue(double newValue)
{
Line line = new Line();
line.X1 = Time;
line.Y1 = 10 * Value;
line.X2 = Time + DeltaTime;
line.Y2 = 10 * newValue;
line.StrokeThickness = 1;
line.Stroke = new SolidColorBrush(Color);
Canvas.Children.Add(line);
translate.X = -Time;
lines.Add(line);
if (lines.Count > 1000)
{
Canvas.Children.Remove(lines[0]);
lines.RemoveAt(0);
}
Value = newValue;
Time += DeltaTime;
}
}
}
......@@ -24,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
......@@ -33,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment