Skip to content

Commit 28a0d77

Browse files
committed
Week 9 Work
Lots of wok done, introduced buggy power off failsafe, issue with locking
1 parent ab4bd6b commit 28a0d77

23 files changed

+360
-47
lines changed

.gitattributes

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

Station/Station/Buggy.cs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class Buggy
1616
private int laps = 0;
1717
private Station station;
1818
private int requiredLaps = 0;
19+
private bool muted = false;
20+
private bool motion = false;
21+
private Thread onlineThread;
1922

2023
public Buggy(int ID, Direction direction, Station station, Communications comms)
2124
{
@@ -24,18 +27,44 @@ public Buggy(int ID, Direction direction, Station station, Communications comms)
2427
this.comms = comms;
2528
this.station = station;
2629

27-
sendDirection();
30+
onlineThread = new Thread(new ThreadStart(() =>
31+
{
32+
while (true)
33+
{
34+
Thread.Sleep(1000);
35+
bool firstTry = syn();
36+
if (!firstTry)
37+
{
38+
buggyAction("Back online");
39+
if (motion)
40+
{
41+
go();
42+
}
43+
}
44+
}
45+
}));
46+
onlineThread.Start();
2847
}
2948
public void setRequiredLaps(int laps)
3049
{
3150
requiredLaps = laps;
3251
}
52+
public void mute()
53+
{
54+
muted = true;
55+
}
56+
public void unmute()
57+
{
58+
muted = false;
59+
}
3360
public void go()
3461
{
62+
motion = true;
3563
comms.send(ID, "GO");
3664
}
3765
public void stop()
3866
{
67+
motion = false;
3968
comms.send(ID, "STOP");
4069
}
4170
public void sendPing()
@@ -46,6 +75,10 @@ public void sendPong()
4675
{
4776
comms.send(ID, "PONG");
4877
}
78+
public bool syn()
79+
{
80+
return comms.send(ID, "SYN", () => { buggyAction("Offline"); });
81+
}
4982
public void onGantry(int gantry_num)
5083
{
5184
stop();
@@ -105,37 +138,31 @@ public void buggyParked()
105138
Program.print("Buggy " + ID + " parked! " + (laps - 1) + " lap(s) completed!");
106139
}
107140
}
108-
private void sendDirection()
109-
{
110-
if (direction == Direction.AntiClockwise)
111-
{
112-
comms.send(ID, "ACLOCK");
113-
}
114-
else
115-
{
116-
comms.send(ID, "CLOCK");
117-
}
118-
}
119-
120141
public void pingRecieved()
121142
{
122-
Program.print("PING recieved");
143+
buggyAction("PING recieved");
123144
}
124145
public void pongRecieved()
125146
{
126-
Program.print("PONG recieved");
147+
buggyAction("PONG recieved");
127148
}
128149
public void going()
129150
{
130-
151+
buggyAction("GOING");
131152
}
132153
public void stopped()
133154
{
134-
155+
buggyAction("STOPPED");
135156
}
136-
private void buggyAction()
157+
private void buggyAction(String command = null)
137158
{
138-
Console.Write("> Buggy " + ID + ": ");
159+
if (!muted)
160+
{
161+
if (command == null)
162+
Console.Write("> Buggy " + ID + ": " + command);
163+
else
164+
Console.WriteLine("> Buggy " + ID + ": " + command);
165+
}
139166
}
140167
private void trackState(string call, int num)
141168
{
@@ -174,16 +201,5 @@ private void onLap()
174201
buggyAction();
175202
Console.WriteLine("is on lap " + laps);
176203
}
177-
private int earlyAction()
178-
{
179-
int a;
180-
{
181-
if (direction == Direction.AntiClockwise)
182-
a = 2;
183-
else
184-
a = 3;
185-
}
186-
return a;
187-
}
188204
}
189205
}

Station/Station/Communications.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ class Communications
1212
{
1313
private Dictionary<string, Action<int>> buggyhash = new Dictionary<string, Action<int>>();
1414
private Action<int, string> defaultHandler = null;
15+
private object[] receiveLocks = { new object(), new object() };
1516
private SerialPort port = new SerialPort();
16-
private bool recieved = false;
17+
private bool[] received = { false, false };
18+
private object sendingLock = new object();
1719
public Communications()
1820
{
19-
port.PortName = "COM4";
21+
port.PortName = "COM11";
2022
port.BaudRate = 9600;
2123
port.Open();
2224

@@ -27,21 +29,48 @@ public Communications()
2729

2830
port.DiscardInBuffer();
2931
port.DataReceived += recievedData;
32+
33+
addCommand("ACK", (int ID) => { });
3034
}
31-
public void send(int buggy_id, string command)
35+
public bool send(int buggy_id, string command, Action offlineHandler = null)
3236
{
3337
int reps = 0;
3438
int sender_id = 0;
35-
recieved = false;
36-
while (!recieved && reps < 10)
39+
lock (receiveLocks[buggy_id])
40+
{
41+
received[buggy_id] = false;
42+
while (!received[buggy_id])
43+
{
44+
reps++;
45+
46+
if (reps == 2 && !received[buggy_id])
47+
{
48+
if (offlineHandler != null)
49+
{
50+
offlineHandler();
51+
} else
52+
{
53+
Console.WriteLine("Command: " + command + "\nnot being recieved by buggy: " + buggy_id +
54+
"\nWill keep sending command");
55+
}
56+
57+
}
58+
lock (sendingLock)
59+
{
60+
port.Write(sender_id + " " + buggy_id + " " + command + "\n");
61+
}
62+
Monitor.Wait(receiveLocks[buggy_id], 100);
63+
}
64+
}
65+
if (reps > 1)
3766
{
38-
port.Write(sender_id + " " + buggy_id + " " + command + "\n");
39-
//Thread.Sleep(100);
40-
Task.Delay(100);
41-
reps++;
67+
if (offlineHandler == null)
68+
{
69+
Console.WriteLine(" Command: " + command + " received after " + reps + " attempts");
70+
}
71+
return false;
4272
}
43-
if (reps == 10)
44-
Console.WriteLine("Command: " + command + "\nNot being recieved by buggy: " + buggy_id);
73+
return true;
4574
}
4675
public void recievedData(object sender, SerialDataReceivedEventArgs e)
4776
{
@@ -56,14 +85,15 @@ public void recievedData(object sender, SerialDataReceivedEventArgs e)
5685
return;
5786
if (sender_id != 1 && sender_id != 2)
5887
return;
59-
recieved = true;
88+
89+
lock (receiveLocks[sender_id])
90+
{
91+
received[sender_id] = true;
92+
}
6093
if (!buggyhash.ContainsKey(command))
6194
defaultHandler?.Invoke(sender_id, command);
6295
else
63-
{
64-
Console.WriteLine("> Buggy" + sender_id + ": " + command);
6596
buggyhash[command](sender_id);
66-
}
6797
}
6898
public void addCommand(string command, Action<int> handler)
6999
{

Station/Station/Station.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,24 @@ public void setUp()
8282
Console.WriteLine("How many buggies are you using? ");
8383
Int32.TryParse(Console.ReadLine(), out buggies);
8484
}
85-
Console.WriteLine("How many laps would you like to do? ");
86-
Int32.TryParse(Console.ReadLine(), out laps);
85+
while (laps <= 0)
86+
{
87+
Console.WriteLine("How many laps would you like to do? ");
88+
Int32.TryParse(Console.ReadLine(), out laps);
89+
}
8790
buggy1 = new Buggy(1, Direction.Clockwise, this, comms);
91+
buggy1.mute();
92+
buggy1.sendPing();
93+
Console.WriteLine("Buggy: 1 OK");
94+
buggy1.unmute();
8895
if (buggies == 2)
96+
{
8997
buggy2 = new Buggy(2, Direction.AntiClockwise, this, comms);
98+
buggy2.mute();
99+
buggy2.sendPong();
100+
Console.WriteLine("Buggy: 2 OK");
101+
buggy2.unmute();
102+
}
90103
else
91104
buggy2 = null;
92105
setNumberOfBuggies(buggies);
37 KB
Binary file not shown.

c# test/TimeTest/TimeTest.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimeTest", "TimeTest\TimeTest.csproj", "{446A1C8F-38D0-4D66-9748-F3596A58D17E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{446A1C8F-38D0-4D66-9748-F3596A58D17E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{446A1C8F-38D0-4D66-9748-F3596A58D17E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{446A1C8F-38D0-4D66-9748-F3596A58D17E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{446A1C8F-38D0-4D66-9748-F3596A58D17E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

c# test/TimeTest/TimeTest/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

c# test/TimeTest/TimeTest/Program.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.IO.Ports;
8+
using System.Threading;
9+
10+
namespace TimeTest
11+
{
12+
class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
SerialPort port = new SerialPort();
17+
port.PortName = "COM11";
18+
port.BaudRate = 9600;
19+
port.Open();
20+
21+
port.Write("+++");
22+
Thread.Sleep(1100);
23+
port.WriteLine("ATID 3308, CH C, CN");
24+
Thread.Sleep(10000);
25+
26+
port.DiscardInBuffer();
27+
28+
while (true)
29+
{
30+
Stopwatch watchstop = new Stopwatch();
31+
watchstop.Start();
32+
port.Write("a");
33+
port.ReadChar();
34+
watchstop.Stop();
35+
Console.WriteLine(watchstop.ElapsedMilliseconds);
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)