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

majikayo~~

parent 5f2567e8
No related merge requests found
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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wuhyuo" xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Initialized="Window_Initialized" Closed="Window_Closed"> Title="MainWindow" Height="450" Width="800" Initialized="Window_Initialized" Closed="Window_Closed">
<Canvas HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransform="1 0 0 -1 0 0"> <Grid Margin="20">
<Rectangle x:Name="rect" Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" Fill="#FF6BE8B5" RenderTransformOrigin="0.5,0.5" > <Grid.RowDefinitions>
<Rectangle.RenderTransform> <RowDefinition />
<TransformGroup> <RowDefinition />
<RotateTransform x:Name="rectRotate" Angle="0"/> </Grid.RowDefinitions>
<TranslateTransform X="-25" Y="-25"/> <Grid.ColumnDefinitions>
</TransformGroup> <ColumnDefinition />
</Rectangle.RenderTransform> <ColumnDefinition />
</Rectangle> <ColumnDefinition />
</Canvas> <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> </Window>
...@@ -14,9 +14,8 @@ using System.Windows.Navigation; ...@@ -14,9 +14,8 @@ using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Threading; using System.Threading;
using System.IO.Ports; using System.IO.Ports;
using System.Diagnostics;
namespace wuhyuo namespace WpfApp1
{ {
/// <summary> /// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리 /// MainWindow.xaml에 대한 상호 작용 논리
...@@ -30,6 +29,17 @@ namespace wuhyuo ...@@ -30,6 +29,17 @@ namespace wuhyuo
private void Window_Initialized(object sender, EventArgs e) 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 = new Thread(InputThread);
_thrd.Start(); _thrd.Start();
} }
...@@ -42,32 +52,42 @@ namespace wuhyuo ...@@ -42,32 +52,42 @@ namespace wuhyuo
private void InputThread() private void InputThread()
{ {
SerialPort port = new SerialPort("COM8", 115200);
try try
{ {
using (var port = new SerialPort("COM6", 115200)) port.Open();
while (true)
{ {
port.Open(); try
while (true)
{ {
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(); renderer[0].AddValue(values[0]);
double[] values = input.Split(" \t\r\n".ToArray()).Where(x => x.Length > 0) renderer[1].AddValue(values[1]);
.Select(x => Convert.ToDouble(x)).ToArray(); renderer[2].AddValue(values[2]);
if (values.Length != 6) renderer[4].AddValue(values[3]);
continue; renderer[5].AddValue(values[4]);
renderer[6].AddValue(values[5]);
Dispatcher.Invoke(() =>
{ double acc = Math.Sqrt(values[0] * values[0] + values[1] * values[1] + values[2] * values[2]);
Debug.WriteLine(input); double gyro = Math.Sqrt(values[3] * values[3] + values[4] * values[4] + values[5] * values[5]);
Canvas.SetLeft(rect, values[0] * 10); renderer[3].AddValue(acc);
Canvas.SetRight(rect, values[1] * 10); renderer[7].AddValue(gyro);
rectRotate.Angle = values[5] * 180 / Math.PI; });
});
}
catch (FormatException) { }
catch (TimeoutException) { }
} }
catch (FormatException) { }
catch (TimeoutException) { }
catch (OverflowException) { }
} }
} }
catch (InvalidOperationException) catch (InvalidOperationException)
...@@ -78,8 +98,72 @@ namespace wuhyuo ...@@ -78,8 +98,72 @@ namespace wuhyuo
Close(); Close();
}); });
} }
finally
{
port.Close();
}
}
private static unsafe float uint2float(uint value)
{
return *(float*)(&value);
} }
private Thread _thrd; 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 @@ ...@@ -24,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
...@@ -33,6 +34,7 @@ ...@@ -33,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <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