-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationState.cs
634 lines (499 loc) · 17.7 KB
/
ApplicationState.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
using Ballcuber.Hardware;
using Ballcuber.Modele;
using RevengeCube;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Ballcuber
{
public abstract class ApplicationState : ICloneable
{
public virtual object Clone()
{
return Clone(this);
}
public static object Clone(object obj)
{
if (obj == null) return null;
var newObj = Activator.CreateInstance(obj.GetType());
foreach (var prop in newObj.GetType().GetProperties().Where((x) => x.CanWrite && x.CanRead))
{
var propValue = prop.GetValue(obj);
if (propValue == null) continue;
if (typeof(ICloneable).IsAssignableFrom(prop.PropertyType))
{
prop.SetValue(newObj, ((ICloneable)propValue).Clone());
}
else if (prop.PropertyType.IsValueType)
{
prop.SetValue(newObj, propValue);
}
else if (typeof(IList).IsAssignableFrom(prop.PropertyType))
{
var lst = (IList)Activator.CreateInstance(prop.PropertyType);
lst.Clear();
foreach (var elt in (IList)propValue)
{
lst.Add(Clone(elt));
}
prop.SetValue(newObj, lst);
}
else
{
throw new Exception("La propriété " + prop.Name + " de la classe " + obj.GetType().Name + " n'est pas clonable. Toutes les propriétés de l'objet état doivent être clonable afin d'en avoir une copie");
}
}
return newObj;
}
// par défaut il y a égalité si toutes les propriétés sont égales
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj.GetType() != this.GetType()) return false;
foreach (var prop in obj.GetType().GetProperties().Where((x) => x.CanRead))
{
var objValue = prop.GetValue(obj);
var value = prop.GetValue(this);
if (objValue == null && value == null)
{
// ok equals
}
else if (objValue == null && value != null || objValue != null && value == null || !objValue.Equals(value))
{
return false;
}
}
return true;
}
// avoid warning
public override int GetHashCode()
{
return base.GetHashCode();
}
}
public class GlobalState : ApplicationState, IDisposable
{
private static GlobalState _state = new GlobalState();
private static object _lock = new object();
public static GlobalState GetState()
{
Monitor.Enter(_lock);
return _state;
}
public void Dispose()
{
Monitor.Exit(_lock);
}
public GlobalState CloneState()
{
return (GlobalState)Clone();
}
private ColorCube _initialCube = new ColorCube();
[XmlIgnore()]
public ColorCube InitialCube
{
get
{
return _initialCube;
}
set
{
_initialCube = value;
Solution = null;
}
}
public bool Simulated
{
get
{
return HardwareConfig1.Simulated || HardwareConfig2.Simulated;
}
}
public HardwareConfig HardwareConfig(HardwareConfigBoard index)
{
return index == HardwareConfigBoard.Board1 ? HardwareConfig1 : HardwareConfig2;
}
public HardwareConfig HardwareConfig1 { get; set; } = new HardwareConfig(HardwareConfigBoard.Board1);
public HardwareConfig HardwareConfig2 { get; set; } = new HardwareConfig(HardwareConfigBoard.Board2);
public HardwareConfigGlobal HardwareConfigGlobal { get; set; } = new HardwareConfigGlobal();
public VideoParameters VideoParameters { get; set; } = new VideoParameters();
private Solution _solution;
public MotorList Motors { get; set; } = new MotorList();
[XmlIgnore()]
public Solution Solution
{
get
{
return _solution;
}
set
{
_solution = value;
//ResolutionSession = null;
}
}
public ResolutionSessionStatus ResolutionSessionStatus
{
get
{
return ResolutionSession.GetStatus();
}
}
[XmlIgnore()]
public List<Alarm> Alarms { get; set; } = new List<Alarm>();
[XmlIgnore()]
public bool SolutionInCalculation { get; set; } = false;
// public ResolutionSession ResolutionSession { get; set; }
[XmlIgnore()]
public Scanner Scanner { get; set; } = new Scanner();
public void SaveConfiguration()
{
XmlSerializer xs = new XmlSerializer(typeof(GlobalState));
using (StreamWriter wr = new StreamWriter("GlobalState.xml"))
{
xs.Serialize(wr, this);
}
}
public void ImportConfiguration()
{
XmlSerializer xs = new XmlSerializer(typeof(GlobalState));
using (StreamReader rd = new StreamReader("GlobalState.xml"))
{
GlobalState p = xs.Deserialize(rd) as GlobalState;
this.HardwareConfig1 = p.HardwareConfig1;
this.HardwareConfig2 = p.HardwareConfig2;
this.HardwareConfigGlobal = p.HardwareConfigGlobal;
this.VideoParameters = p.VideoParameters;
if (p.Motors?.Motors == null || p.Motors.Motors.Count == 0)
{
this.Motors.Reset();
}
else {
this.Motors = p.Motors;
}
}
}
}
public class VideoParameters : ApplicationState
{
public enum SelectedImage
{
InImage,
//MedianBlurIn,
InImageB,
InImageG,
InImageR,
InImageSum,
threshold,
Canny,
approxContour,
[Browsable(false)]
Count,
[Browsable(false)]
Last = Count - 1,
}
public int CaptureID { get; set; } = 0;
public Point[] Points { get; set; } = new Point[6];
public Point Center { get; set; }
public int PerpectiveSize { get; set; } = 100;
public int SquareDistance { get; set; } = 5;
public SelectedImage DebugImage { get; set; } = SelectedImage.Last;
public int CannyThreshold1 { get; set; } = 20;
public int CannyThreshold2 { get; set; } = 20;
public int MedianBlurSize { get; set; } = 11;
public double ContourEpsilon { get; set; } = 50;
public double Threshold { get; set; } = 120;
}
public class HardwareConfigGlobal : ApplicationState
{
[Description("Nombre natif de pas du moteur")]
public int StepsPerMotorRotation { get; set; } = 200;
[Description("Pilotage en quart (4)/huitième (8)/seizième (16)/... de pas")]
public int StepperDriverSubSteps { get; set; } = 16;
[Description("Rapport de réduction du réducteur du moteur")]
public int MotorReduction { get; set; } = 4;
[Description("Nombre de pas par tour de bielle")]
public int StepsPerRotation => StepsPerMotorRotation * StepperDriverSubSteps * MotorReduction;
[Description("Nombre de doigt de la croix/bielle")]
public int MotorTeeth
{
get
{
return 4;
}
}
[Description("Milliseconds avant un timeout de l'instruction WaitTargetReached")]
public int WaitTargetReachedInstructionTimeoutMilliseconds { get; set; } = 8000;
[Description("Nombre de dents dans une couronne")]
public int BallTeeth
{
get
{
return 8;
}
}
[Description("Rapport de réduction total")]
public int StepsPerCubeQuarter
{
get
{
return StepsPerRotation * BallTeeth / MotorTeeth / 4;
}
}
public int EngagedAcceleration { get; set; }
public int DisengagedAcceleration { get; set; }
public int EngagedSpeed { get; set; }
public int DisengagedSpeed { get; set; }
}
public class Motor : ApplicationState
{
[ReadOnly(true)]
public Axe Axe { get; set; }
[ReadOnly(true)]
public Couronne Courronne { get; set; }
public RampsSteppers Stepper { get; set; }
[Browsable(false), XmlIgnore()]
public bool Enabled { get; private set; }
[Browsable(false), XmlIgnore()]
public int Position { get; private set; }
public void SetState(bool enabled, int rawPosition)
{
this.Enabled = enabled;
this.Position = Inverted ? -rawPosition : rawPosition;
}
[Description("Le sens positif de rotation du moteur est inversé par rapport au sens conventionnel du cube")]
public bool Inverted { get; set; }
public double DegreesFromMiddleToContact { get; set; }
[Description("Nombre de pas pour engreiner dans le sens positif depuis la position 0")]
public int StepsToPositivePosition {
get {
using (var state = GlobalState.GetState()) {
return (int)(DegreesFromMiddleToContact * state.HardwareConfigGlobal.StepsPerMotorRotation * state.HardwareConfigGlobal.StepperDriverSubSteps * state.HardwareConfigGlobal.MotorReduction / 360.0);
}
}
}
// index dans le tableau interne au code arduino
public byte Index
{
get
{
return (byte)Stepper;
}
}
// mask ne représentant que ce moteur
public int Mask
{
get
{
return 1<< Index;
}
}
public HardwareConfigBoard Board { get; set; }
public override string ToString()
{
return Axe + " " + Courronne;
}
public MotorColision PositiveCollision { get; set; } = new MotorColision();
public MotorColision NegativeCollision { get; set; } = new MotorColision();
}
[TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
public class MotorColision : ApplicationState
{
public override string ToString()
{
return items.OfType<bool>().Count((x) => x).ToString();
}
private bool[,] items = new bool[3,3];
public bool HasCollision( Axe a,Couronne c)
{
int cId;
switch (c) {
case Couronne.MidMin:
cId = 0;
break;
case Couronne.MidMax:
cId = 1;
break;
case Couronne.Max:
cId = 2;
break;
default:
return false;
}
return items[(int)a, cId];
}
private void SetCollision(Axe a, Couronne c, bool value)
{
int cId;
switch (c)
{
case Couronne.MidMin:
cId = 0;
break;
case Couronne.MidMax:
cId = 1;
break;
case Couronne.Max:
cId = 2;
break;
default:
return;
}
items[(int)a, cId] = value;
}
public bool X_MidMin { get => HasCollision(Axe.X, Couronne.MidMin); set => SetCollision(Axe.X, Couronne.MidMin, value); }
public bool X_MidMax { get => HasCollision(Axe.X, Couronne.MidMax); set => SetCollision(Axe.X, Couronne.MidMax, value); }
public bool X_Max { get => HasCollision(Axe.X, Couronne.Max); set => SetCollision(Axe.X, Couronne.Max, value); }
public bool Y_MidMin { get => HasCollision(Axe.Y, Couronne.MidMin); set => SetCollision(Axe.Y, Couronne.MidMin, value); }
public bool Y_MidMax { get => HasCollision(Axe.Y, Couronne.MidMax); set => SetCollision(Axe.Y , Couronne.MidMax, value); }
public bool Y_Max { get => HasCollision(Axe.Y, Couronne.Max); set => SetCollision(Axe.Y, Couronne.Max, value); }
public bool Z_MidMin { get => HasCollision(Axe.Z, Couronne.MidMin); set => SetCollision(Axe.Z, Couronne.MidMin, value); }
public bool Z_MidMax { get => HasCollision(Axe.Z, Couronne.MidMax); set => SetCollision(Axe.Z, Couronne.MidMax, value); }
public bool Z_Max { get => HasCollision(Axe.Z, Couronne.Max); set => SetCollision(Axe.Z, Couronne.Max, value); }
}
public enum HardwareConfigBoard
{
Board1,
Board2
}
public class HardwareConfig : ApplicationState
{
public HardwareConfig() { }
public HardwareConfig(HardwareConfigBoard index) { this.Index = index; }
public HardwareConfigBoard Index { get; set; }
public string COMPort { get; set; }
public int BaudRate { get; set; }
public bool Simulated { get; set; }
[Description("Temps en simulation pour 1/4 tour en ms")]
public int SimulatedTime { get; set; }
public bool Connected
{
get
{
return Runner.GetBoard(Index).Connected;
}
}
public bool ConnectAtStartup { get; set; }
public HardwareConfig CloneHardwareConfig()
{
return (HardwareConfig)Clone();
}
}
//public class ResolutionSession : ApplicationState
//{
// private IReadOnlyList<MotorMove> _motorMoves;
// public IReadOnlyList<MotorMove> MotorMoves
// {
// get
// {
// return _motorMoves;
// }
// set
// {
// _motorMoves = value;
// LastExecutedStep = -1;
// }
// }
// public bool IsRunning
// {
// get
// {
// return LastExecutedStep < 0;
// }
// }
// public int LastExecutedStep { get; set; }
// public override object Clone()
// {
// var res = new ResolutionSession();
// var lst = new List<MotorMove>();
// foreach(var mv in this.MotorMoves)
// {
// var newMv = new MotorMove(mv.Axe);
// newMv.MaxMovesCount = mv.MaxMovesCount;
// newMv.MidMaxMovesCount = mv.MidMaxMovesCount;
// newMv.MidMinMovesCount = mv.MidMinMovesCount;
// lst.Add(newMv);
// }
// res.MotorMoves = lst;
// res.LastExecutedStep = LastExecutedStep;
// return res;
// }
// public override bool Equals(object obj)
// {
// if (obj == null) return false;
// if (!(obj is ResolutionSession)) return false;
// var res = (ResolutionSession)obj;
// if (res.MotorMoves?.Count != MotorMoves?.Count) return false;
// for(int i = 0; i < MotorMoves.Count; i++)
// {
// if (MotorMoves[i] != res.MotorMoves[i]) return false;
// }
// return res.LastExecutedStep == LastExecutedStep;
// }
//}
public class Scanner : ApplicationState
{
public static Faces FirstScannedFace = Faces.L;
// face en cours de scan
public Faces CurrentScannedFace { get; set; } = FirstScannedFace;
// la face CurrentScannedFace est en attente d'apparition devant la caméra
public bool WaitingForFace { get; set; } = true;
public bool Starting
{
get
{
return CurrentScannedFace == FirstScannedFace && WaitingForFace;
}
}
public void Reset()
{
CurrentScannedFace = FirstScannedFace;
WaitingForFace = true;
}
/*
private const string WAIT_TEXT = "En attente du côté ";
public string GetStepText()
{
switch (Step)
{
case ScanStep.WaitFace:
return WAIT_TEXT + "Face";
break;
default:
return "aa";
}
}*/
}
public class MotorList : ApplicationState
{
public List<Motor> Motors { get; set; } = new List<Motor>();
public MotorList()
{
}
public void Reset()
{
Motors.Clear();
int i = 0;
for (Axe a = Axe.X; a < Axe.NUM; a++)
{
foreach (var c in Enum.GetValues(typeof(Couronne)).OfType<Couronne>().Except(new Couronne[] { Couronne.Min, Couronne.UNKNOWN }))
{
var m = new Motor();
m.Axe = a;
m.Courronne = c;
Motors.Add(m);
i++;
}
}
}
}
}