Skip to content

Commit 2cfe425

Browse files
committed
Merge branch 'master' into aron
2 parents 2939c82 + 53c7f73 commit 2cfe425

34 files changed

+775
-130
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.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ void Buggy::go(bool silent) {
44
if (going) {
55
return;
66
}
7-
motor.go();
7+
motor->go();
88
lastGoTime = millis();
99
if (!silent) {
1010
comms->writeXbee("GOING");
@@ -16,7 +16,7 @@ void Buggy::stop(bool silent) {
1616
if (!going) {
1717
return;
1818
}
19-
motor.stop();
19+
motor->stop();
2020
travelledTime += millis() - lastGoTime;
2121
if (!silent) {
2222
comms->writeXbee("STOPPED");
@@ -102,16 +102,16 @@ void Buggy::updateParking() {
102102
}
103103

104104
if (parkingState == BEFORE_INTERSECTION) {
105-
if (travelDirection == CLOCKWISE && motor.getState() != LEFT_OVERRIDE) {
106-
motor.leftOverride();
107-
} else if (travelDirection == ANTI_CLOCKWISE && motor.getState() != RIGHT_OVERRIDE) {
108-
motor.rightOverride();
105+
if (travelDirection == CLOCKWISE && motor->getState() != LEFT_OVERRIDE) {
106+
motor->leftOverride();
107+
} else if (travelDirection == ANTI_CLOCKWISE && motor->getState() != RIGHT_OVERRIDE) {
108+
motor->rightOverride();
109109
}
110110
}
111111

112112
unsigned long sinceGantry = timeTravelledSinceGantry();
113113
if (parkingState == BEFORE_INTERSECTION && sinceGantry > parking_overrideOffAt) {
114-
motor.go();
114+
motor->go();
115115
parkingState = AFTER_INTERSECTION;
116116
} else if (parkingState == AFTER_INTERSECTION && sinceGantry > parking_stopAt) {
117117
stop(true);

Buggy/Buggy.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Buggy {
2222
const unsigned int parking_stopAt = 7000;
2323

2424
const CommTrans *comms;
25-
MotorControls motor;
25+
MotorControls *motor;
2626

2727
bool going = false;
2828
Direction travelDirection;
@@ -44,10 +44,10 @@ class Buggy {
4444
static const short LED_PIN = 13;
4545

4646
Buggy() = delete;
47-
Buggy(short ID, CommTrans *c) : comms(c) {
47+
Buggy(short ID, MotorControls *m, CommTrans *c) : motor(m), comms(c) {
4848
pinMode(LED_PIN, OUTPUT);
4949
pinMode(IR_PIN, INPUT);
50-
motor.stop();
50+
motor->stop();
5151
if (ID == 1) {
5252
travelDirection = CLOCKWISE;
5353
} else {

Buggy/Buggy.ino

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
#include "CommTrans.h"
22
#include "Buggy.h"
33
#include "UltraSonic.h"
4+
#include "Lights.h"
45

5-
CommTrans* comm;
6-
Buggy* buggy;
6+
CommTrans *comm;
7+
MotorControls *motors;
8+
Buggy *buggy;
79
UltraSonic *sonic;
10+
Lights *christmasTree;
811

912
void setup() {
10-
MotorControls().stop();
13+
motors = new MotorControls();
14+
christmasTree = new Lights();
15+
christmasTree->setMotor(motors);
16+
1117
short buggyID = 1;
1218
comm = new CommTrans(buggyID);
1319
comm->init();
14-
buggy = new Buggy(buggyID, comm);
15-
comm->setDefaultHandler( [] { comm->writeXbee("INVALID"); });
16-
comm->addHandler("PING", [] { comm->writeXbee("PONG"); });
17-
comm->addHandler("PONG", [] { comm->writeXbee("PING"); });
18-
comm->addHandler("SYN", [] { });
19-
comm->addHandler("GO", [] { buggy->go(); });
20-
comm->addHandler("STOP", [] { buggy->stop(); });
21-
comm->addHandler("PARK", [] { buggy->park(); });
20+
buggy = new Buggy(buggyID, motors, comm);
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(); });
2228

2329
attachInterrupt(digitalPinToInterrupt(Buggy::IR_PIN), IR_ISR, RISING);
2430

2531
sonic = new UltraSonic(buggy, comm);
32+
christmasTree->setUltrasonic(sonic);
2633
}
2734

2835
void loop() {
2936
buggy->update();
3037
sonic->ultraLoop();
38+
christmasTree->update();
3139
}
3240

3341
void serialEvent() {

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 &);

Buggy/Lights.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "Lights.h"
2+
3+
const int16_t Lights::loopDuration = 500;
4+
const int16_t Lights::indicatorPeriod = 800;
5+
const int8_t Lights::leftIndicatorPin = 12;
6+
const int8_t Lights::rightIndicatorPin = 13;
7+
const int8_t Lights::pins[] = {5, 6, 7, 8, 9, 10, 11};
8+
const int8_t Lights::nPins = 7;
9+
10+
Lights::Lights() {
11+
for (const auto &pin : pins) {
12+
pinMode(pin, OUTPUT);
13+
off(pin);
14+
}
15+
pinMode(leftIndicatorPin, OUTPUT);
16+
pinMode(rightIndicatorPin, OUTPUT);
17+
on(leftIndicatorPin);
18+
on(rightIndicatorPin);
19+
}
20+
21+
void Lights::setMotor(const MotorControls *motor) {
22+
motor_ = motor;
23+
}
24+
25+
void Lights::setUltrasonic(const UltraSonic *ultra) {
26+
ultra_ = ultra;
27+
}
28+
29+
void Lights::update() {
30+
if (motor_ && motor_->getState() == MotorState::STOPPED) {
31+
for (int8_t i = 0; i < nPins; i++) {
32+
on(pins[i]);
33+
}
34+
const bool obstacle = (ultra_ && ultra_->isBlockedByObstacle());
35+
setLightState(leftIndicatorPin, obstacle);
36+
setLightState(rightIndicatorPin, obstacle);
37+
} else {
38+
const int16_t progress = ((millis() % loopDuration) / (loopDuration / nPins)) % nPins;
39+
bool pinOn;
40+
for (int8_t i = 0; i < nPins; i++) {
41+
pinOn = (progress == i || ((progress + nPins / 2) % nPins) == i);
42+
setLightState(pins[i], pinOn);
43+
}
44+
off(leftIndicatorPin);
45+
off(rightIndicatorPin);
46+
if(motor_ && motor_->getState() == MotorState::RIGHT_OVERRIDE) {
47+
const bool indicatorOn = (millis() % indicatorPeriod) < indicatorPeriod / 2;
48+
setLightState(leftIndicatorPin, indicatorOn);
49+
off(rightIndicatorPin);
50+
} else if(motor_ && motor_->getState() == MotorState::LEFT_OVERRIDE) {
51+
const bool indicatorOn = (millis() % indicatorPeriod) < indicatorPeriod / 2;
52+
off(leftIndicatorPin);
53+
setLightState(rightIndicatorPin, indicatorOn);
54+
}
55+
}
56+
}

Buggy/Lights.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <Arduino.h>
2+
3+
#include "MotorControls.h"
4+
#include "UltraSonic.h"
5+
6+
class Lights {
7+
public:
8+
Lights();
9+
void setMotor(const MotorControls *);
10+
void setUltrasonic(const UltraSonic *);
11+
void update();
12+
13+
private:
14+
const MotorControls* motor_;
15+
const UltraSonic* ultra_;
16+
17+
static const int16_t loopDuration;
18+
static const int16_t indicatorPeriod;
19+
static const int8_t leftIndicatorPin;
20+
static const int8_t rightIndicatorPin;
21+
static const int8_t nPins;
22+
static const int8_t pins[];
23+
24+
// May have to use non-intuitive polarities if using an NPN emitter follower
25+
void setLightState(uint8_t pin, bool on) const { digitalWrite(pin, on ? LOW : HIGH); }
26+
void on(uint8_t pin) const { setLightState(pin, true); }
27+
void off(uint8_t pin) const { setLightState(pin, false); }
28+
};

Buggy/UltraSonic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ class UltraSonic {
3232
}
3333

3434
void ultraLoop();
35+
36+
bool isBlockedByObstacle() { return obstacle; }
3537
};

0 commit comments

Comments
 (0)