Skip to content

Commit 53c7f73

Browse files
committed
Merge branch 'rob'
2 parents 4f29a70 + fbf94b8 commit 53c7f73

28 files changed

+633
-116
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

Buggy/Buggy.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ void setup() {
1818
comm = new CommTrans(buggyID);
1919
comm->init();
2020
buggy = new Buggy(buggyID, motors, comm);
21-
comm->setDefaultHandler( [] { comm->writeXbee("INVALID"); });
22-
comm->addHandler("PING", [] { comm->writeXbee("PONG"); });
23-
comm->addHandler("PONG", [] { comm->writeXbee("PING"); });
24-
comm->addHandler("SYN", [] { });
25-
comm->addHandler("GO", [] { buggy->go(); });
26-
comm->addHandler("STOP", [] { buggy->stop(); });
27-
comm->addHandler("PARK", [] { buggy->park(); });
21+
comm->setDefaultHandler( [] (const String & command) { comm->writeXbee("INVALID: " + command); });
22+
comm->addHandler("PING", [] { comm->writeXbee("PONG"); });
23+
comm->addHandler("PONG", [] { comm->writeXbee("PING"); });
24+
comm->addHandler("SYN", [] { });
25+
comm->addHandler("GO", [] { buggy->go(); });
26+
comm->addHandler("STOP", [] { buggy->stop(); });
27+
comm->addHandler("PARK", [] { buggy->park(); });
2828

2929
attachInterrupt(digitalPinToInterrupt(Buggy::IR_PIN), IR_ISR, RISING);
3030

Buggy/CommTrans.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ void CommTrans::processCommand(char c) {
3737
if (command != "ACK") {
3838
writeXbee("ACK");
3939
}
40-
// writeXbee(String("Recieved command: ") + command);
4140

4241
VoidFunction f = handlers.get(command);
4342
if (f != NULL) {
4443
f();
4544
} else if (defaultHandler != NULL) {
46-
defaultHandler();
45+
defaultHandler(command);
4746
}
4847
}
4948

@@ -60,6 +59,6 @@ void CommTrans::addHandler(String command, VoidFunction handler) {
6059
handlers.add(command, handler);
6160
}
6261

63-
void CommTrans::setDefaultHandler(VoidFunction handler) {
62+
void CommTrans::setDefaultHandler(StringVoidFunction handler) {
6463
defaultHandler = handler;
6564
}

Buggy/CommTrans.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class CommTrans {
1313
void writeXbee(String command) const;
1414
void processCommand(char c);
1515
void addHandler(String command, VoidFunction handler);
16-
void setDefaultHandler(VoidFunction handler);
16+
void setDefaultHandler(StringVoidFunction handler);
1717

1818
private:
1919
String message;
2020
HashMap handlers;
21-
VoidFunction defaultHandler = NULL;
21+
StringVoidFunction defaultHandler = NULL;
2222
const short my_ID;
2323
};

Buggy/Functions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#pragma once
22

3+
#include <Arduino.h>
4+
35
typedef void (*VoidFunction)();
6+
typedef void (*StringVoidFunction)(const String &);

Station/Station/Buggy.cs

Lines changed: 104 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,70 @@ class Buggy
1616
private int laps = 0;
1717
private Station station;
1818
private int requiredLaps = 0;
19+
private volatile bool motion = false;
20+
private Thread onlineThread = null;
21+
private volatile bool onlineThreadShouldRun = false;
1922

2023
public Buggy(int ID, Direction direction, Station station, Communications comms)
2124
{
2225
this.ID = ID;
2326
this.direction = direction;
2427
this.comms = comms;
2528
this.station = station;
26-
27-
sendDirection();
29+
}
30+
public void startOnlineCheck()
31+
{
32+
if (onlineThread != null)
33+
return;
34+
onlineThread = new Thread(new ThreadStart(() =>
35+
{
36+
while (onlineThreadShouldRun)
37+
{
38+
try
39+
{
40+
Thread.Sleep(1000);
41+
bool firstTry = syn();
42+
if (!firstTry)
43+
{
44+
buggyAction("is back online!");
45+
if (motion)
46+
{
47+
go();
48+
}
49+
}
50+
} catch (ThreadInterruptedException e) {}
51+
}
52+
}));
53+
onlineThreadShouldRun = true;
54+
onlineThread.Start();
55+
}
56+
public void stopOnlineCheck()
57+
{
58+
if (onlineThread == null)
59+
return;
60+
onlineThreadShouldRun = false;
61+
onlineThread.Interrupt();
62+
onlineThread.Join();
2863
}
2964
public void setRequiredLaps(int laps)
3065
{
3166
requiredLaps = laps;
3267
}
68+
public ConsoleColor getColour()
69+
{
70+
if (ID == 1)
71+
return ConsoleColor.DarkBlue;
72+
else
73+
return ConsoleColor.DarkRed;
74+
}
3375
public void go()
3476
{
77+
motion = true;
3578
comms.send(ID, "GO");
3679
}
3780
public void stop()
3881
{
82+
motion = false;
3983
comms.send(ID, "STOP");
4084
}
4185
public void sendPing()
@@ -46,20 +90,28 @@ public void sendPong()
4690
{
4791
comms.send(ID, "PONG");
4892
}
93+
public bool syn(bool silent = false)
94+
{
95+
return comms.send(ID, "SYN", () =>
96+
{
97+
if (!silent)
98+
buggyAction("is Offline! \nWill keep pinging buggy " + ID);
99+
});
100+
}
49101
public void onGantry(int gantry_num)
50102
{
51103
stop();
52104
Thread.Sleep(1000);
53105

54106
if (((direction == Direction.Clockwise) && (gantry_num == 2)) || ((direction == Direction.AntiClockwise) && (gantry_num == 1)))
55107
laps++;
56-
else if (((direction == Direction.Clockwise) && (last_gantry == 1) && (gantry_num != 2))
108+
else if (((direction == Direction.Clockwise) && (last_gantry == 1) && (gantry_num != 2))
57109
|| ((direction == Direction.AntiClockwise) && (last_gantry == 2) && (gantry_num != 1))) {
58110
laps++;
59111
}
60112
go();
61113
last_gantry = gantry_num;
62-
trackState("Gantry", gantry_num);
114+
trackState("Gantry");
63115
if (station.getNumberOfBuggies() < 2)
64116
{
65117
if (laps >= requiredLaps && gantry_num == 2)
@@ -88,99 +140,97 @@ public void onGantry(int gantry_num)
88140
}
89141
public void goPark()
90142
{
91-
comms.send(ID, "PARK");
143+
comms.send(ID, "PARK");
144+
}
145+
public void obstacle(string condition)
146+
{
147+
trackState(condition);
92148
}
93149
public void buggyParked()
94150
{
151+
motion = false;
95152
if (direction == Direction.AntiClockwise)
96153
{
97154
station.buggySwitch(ID);
98-
Program.print("Buggy " + ID + " is in the park lane");
155+
buggyAction(" is in the park lane");
99156
}
100157
else
101158
{
102159
if (station.getNumberOfBuggies() == 1)
103-
Program.print("Buggy " + ID + " parked! " + (laps) + " lap(s) completed!");
160+
buggyAction("parked! " + (laps) + " lap(s) completed!");
104161
else
105-
Program.print("Buggy " + ID + " parked! " + (laps - 1) + " lap(s) completed!");
162+
buggyAction("parked! " + (laps - 1) + " lap(s) completed!");
106163
}
107164
}
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-
120165
public void pingRecieved()
121166
{
122-
Program.print("PING recieved");
167+
buggyAction("PING recieved");
123168
}
124169
public void pongRecieved()
125170
{
126-
Program.print("PONG recieved");
171+
buggyAction("PONG recieved");
127172
}
128173
public void going()
129174
{
130-
175+
buggyAction("GOING");
131176
}
132177
public void stopped()
133178
{
134-
179+
buggyAction("STOPPED");
135180
}
136-
private void buggyAction()
181+
private void buggyAction(String command = "")
137182
{
138-
Console.Write("> Buggy " + ID + ": ");
183+
Program.print("Buggy " + ID + " " + command, getColour());
139184
}
140-
private void trackState(string call, int num)
185+
private void trackState(string call)
141186
{
142-
buggyAction();
143-
if (direction == Direction.AntiClockwise)
187+
int section;
188+
if (direction == Direction.Clockwise)
189+
section = last_gantry;
190+
else
144191
{
145-
if (num == 1)
146-
num = num + 2;
192+
if (last_gantry == 1)
193+
section = 3;
147194
else
148-
num--;
195+
section = last_gantry - 1;
149196
}
150197
if (call == "Gantry") {
151198
onLap();
152-
Console.Write((" stopped at gantry " + last_gantry + " Entering track section: "));
153-
if ((laps >= requiredLaps && last_gantry == 2)
154-
|| (direction == Direction.AntiClockwise && last_gantry == 1)) {
155-
Console.WriteLine(("Park Lane"));
156-
return;
199+
if (last_gantry == -10)
200+
buggyAction("gantry interpreted as invalid");
201+
else
202+
{
203+
buggyAction("stopped at gantry " + last_gantry);
204+
205+
string sectionString;
206+
if ((direction == Direction.Clockwise && laps >= requiredLaps && last_gantry == 2)
207+
|| (direction == Direction.AntiClockwise && last_gantry == 1))
208+
sectionString = "Park Lane";
209+
else
210+
sectionString = section.ToString();
211+
buggyAction("entering track section: " + sectionString);
157212
}
158-
Console.WriteLine((num.ToString()));
159213
}
160214
else if (call == "Stop")
161215
{
162-
Console.WriteLine((" has stopped in section " + num));
216+
buggyAction("has stopped in section " + section);
163217
}
164218
else if (call == "Go")
165219
{
166-
Console.WriteLine((" is on the move in section " + num));
220+
buggyAction("is on the move in section " + section);
167221
}
168-
}
169-
private void onLap()
170-
{
171-
buggyAction();
172-
Console.WriteLine("is on lap " + laps);
173-
}
174-
private int earlyAction()
175-
{
176-
int a;
222+
else if (call == "OBSTACLE")
177223
{
178-
if (direction == Direction.AntiClockwise)
179-
a = 2;
180-
else
181-
a = 3;
224+
buggyAction("has detected an obstacle in section " + section);
182225
}
183-
return a;
226+
else if (call == "PATHCLEAR")
227+
{
228+
buggyAction("is now able to progress in section " + section);
229+
}
230+
}
231+
private void onLap()
232+
{
233+
buggyAction("is on lap " + laps);
184234
}
185235
}
186236
}

0 commit comments

Comments
 (0)