| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Dijkstra.NET.Graph;
- using Dijkstra.NET.Graph.Simple;
- using Dijkstra.NET.ShortestPath;
- using GSG.NET.Concurrent;
- using GSG.NET.Logging;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace OHVLoggerTest
- {
- public partial class Form1 : Form
- {
- Logger frontWheelLogger = Logger.GetLogger( "PhysicalCheckup_Front" );
- Logger rearWheelLogger = Logger.GetLogger( "PhysicalCheckup_Rear" );
- Logger logger = Logger.GetLogger();
- ThreadCancel threadCancel = new ThreadCancel();
- List<point> Points = null;
- List<segment> Segments = null;
- Graph<int, string> graph = new Graph<int, string>();
- ZmqManager zmq = null;
- NetMq mq = null;
- public Form1()
- {
- InitializeComponent();
- this.Load += Form1_Load;
- this.FormClosing += Form1_FormClosing;
- }
- private void Form1_FormClosing( object sender, FormClosingEventArgs e )
- {
- this.threadCancel.Cancel();
- this.threadCancel.StopWaitAll();
- Redis.Instance.Dispose();
- }
- private void Form1_Load( object sender, EventArgs e )
- {
- Redis.Instance.Init();
- this.button1_Click( this, null );
- this.threadCancel.AddGo( Thread_Dowork );
- this.threadCancel.AddGo( Thread_DriveRun );
- this.zmq = new ZmqManager();
- //this.mq = new NetMq();
- }
- void Thread_Dowork()
- {
- while ( !this.threadCancel.Canceled )
- {
- try
- {
- LockUtils.Wait( 1000 );
- if ( Redis.Instance.GetMoveStart() )
- {
- Redis.Instance.ResetMoveStart();
- this.RunDrive();
- }
- }
- catch ( Exception e)
- {
- }
- }
- }
- bool isDriveRun = false;
- void Thread_DriveRun()
- {
- while ( !this.threadCancel.Canceled )
- {
- try
- {
- LockUtils.Wait( 2000 );
- }
- catch ( Exception )
- {
- }
- }
- }
- void AddNode()
- {
- this.Points.ForEach( p =>
- {
- this.graph.AddNode( p.ID );
- logger.D( $"AddNode : {p.ID}" );
- } );
- this.graph.Connect( 20, 19, 20, null );
- this.graph.Connect( 1, 21, 10, null );
- this.graph.Connect( 2, 22, 10, null);
- this.graph.Connect( 3, 2, 10, null );
- this.graph.Connect( 4, 3, 10, null );
- this.graph.Connect( 5, 23, 10, null );
- this.graph.Connect( 6, 5, 10, null );
- this.graph.Connect( 7, 6, 20, null );
- this.graph.Connect( 8, 24, 10, null );
- this.graph.Connect( 9, 8, 10, null );
- this.graph.Connect( 10, 25, 10, null );
- this.graph.Connect( 10, 4, 20, null );
- this.graph.Connect( 11, 26, 10, null );
- this.graph.Connect( 12, 11, 10, null );
- this.graph.Connect( 13, 12, 10, null );
- this.graph.Connect( 14, 13, 10, null );
- this.graph.Connect( 15, 14, 10, null );
- this.graph.Connect( 16, 15, 10, null );
- this.graph.Connect( 17, 27, 10, null );
- this.graph.Connect( 18, 17, 10, null );
- this.graph.Connect( 19, 18, 10, null );
- this.graph.Connect( 20, 19, 10, null );
- this.graph.Connect( 21, 20, 10, null );
- this.graph.Connect( 22, 1, 10, null );
- this.graph.Connect( 23, 4, 10, null );
- this.graph.Connect( 24, 7, 10, null );
- this.graph.Connect( 25, 9, 10, null );
- this.graph.Connect( 26, 10, 10, null );
- this.graph.Connect( 27, 16, 10, null );
- this.graph.Connect( 27, 21, 20, null );
- //this.Segments.ForEach( s =>
- //{
- // if ( s.Middle == null )
- // {
- // this.graph.Connect( (uint)s.Start.pointID, (uint)s.End.pointID, 10, $"Segment ID {s.ID}" );
- // logger.D( $"Connect : {s.Start.pointID} -> {s.End.pointID} / SegmentID [{s.ID}]" );
- // }
- // else
- // {
- // this.graph.Connect( (uint)s.Start.pointID, (uint)s.Middle.pointID, 10, $"Segment ID {s.ID}" );
- // logger.D( $"Connect : {s.Start.pointID} -> {s.Middle.pointID} / SegmentID [{s.ID}]" );
- // this.graph.Connect( (uint)s.Middle.pointID, (uint)s.End.pointID, 10, $"Segment ID {s.ID}" );
- // logger.D( $"Connect : {s.Middle.pointID} -> {s.End.pointID} / SegmentID [{s.ID}]" );
- // }
- //} );
- }
- void RunDrive( )
- {
- var redis = Redis.Instance;
- var currentPoint = redis.CurrentPointID();
- var targetPoint = redis.GetMovePoint();
- List<int> routeIDList = new List<int>();
- routeIDList.Add( currentPoint );
- var cuttentSegment = this.Segments.Where( x => x.Start.pointID == currentPoint ).FirstOrDefault();
- uint f = (uint)currentPoint;
- uint t = (uint)targetPoint;
- ShortestPathResult result = this.graph.Dijkstra( f, t );
- var route = result.GetPath();
- //segment preS = null;
- //int noPoint = currentPoint;
- //foreach ( var s in this.Segments )
- //{
- // if ( preS == null )
- // {
- // if ( s.Start.pointID == currentPoint )
- // preS = s;
- // }
- // foreach ( var ss in this.Segments )
- // {
- // if ( ss.Start.pointID == noPoint )
- // {
- // noPoint = ss.End.pointID;
- // routeIDList.Add( ss.Start.pointID );
- // break;
- // }
- // else if ( ss.Middle != null )
- // {
- // if ( ss.Middle.pointID == noPoint )
- // {
- // noPoint = ss.End.pointID;
- // routeIDList.Add( ss.Start.pointID );
- // break;
- // }
- // }
- // else { }
- // }
- // if ( s.End.pointID == 3 )
- // break;
- //}
- var ll = route.ToList();
- redis.SetDriveMove();
- foreach ( var point in ll )
- {
- LockUtils.Wait( 1000 );
- redis.SetCurrentPointID( (int)point );
- }
- redis.ResetDriveMove();
- }
- private void button1_Click( object sender, EventArgs e )
- {
- if ( !System.IO.File.Exists( Application.StartupPath + @"\Json\ohv_testmap7.json" ) )
- return;
- using ( StreamReader r = new StreamReader( Application.StartupPath + @"\Json\ohv_testmap7.json" ) )
- {
- string json = r.ReadToEnd();
- var o = JObject.Parse( json );
- this.Points = o["points"].ToObject<List<point>>();
- this.Segments = o["segments"].ToObject<List<segment>>();
- }
- AddNode();
- }
- private void button2_Click( object sender, EventArgs e )
- {
- //RunDrive( 0 );
- }
- }
- public class point
- {
- public int ID { get; set; }
- public int x { get; set; }
- public int y { get; set; }
- public int barcode { get; set; }
- }
- public class segment
- {
- public int ID { get; set; }
- public string route_type { get; set; }
- public double speed { get; set; }
- public int steering { get; set; }
- [JsonProperty( "start" )]
- public segmentPart Start { get; set; }
- [JsonProperty( "middle" )]
- public segmentPart Middle { get; set; }
- [JsonProperty( "end" )]
- public segmentPart End { get; set; }
- }
- public class segmentPart
- {
- public int pointID { get; set; }
- public int skew { get; set; }
- public int slope { get; set; }
- }
- }
|