-
Notifications
You must be signed in to change notification settings - Fork 2
/
robot.cpp
138 lines (106 loc) · 3.31 KB
/
robot.cpp
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
// Exports a robot structure with all sensor information and methods.
#include "robot.hpp"
#include <assert.h>
Robot::Robot(const uint8_t *LED_PINS, uint8_t LED_PIN_COUNT) {
moduleCount = 0;
state = InitializeState;
name = "Robot";
this->LED_PINS = LED_PINS;
this->LED_PIN_COUNT = LED_PIN_COUNT;
this->position = RedButton;
this->targetPosition = MoleWhite;
// Wall to back wheel when bumper is pressed: 20.6cm (0.206m)
arenaDistances[MoleGreen] = 0.365;
arenaDistances[MoleBlue] = 0.370;
arenaDistances[MoleWhite] = 0.365;
arenaDistances[RedButton] = 0.325;
arenaDistances[MoleRed] = 0.36;
arenaDistances[MolePurple] = 0.355;
arenaDistances[MoleYellow] = 0.372;
}
Robot::~Robot() {
free(modules);
}
bool Robot::initialize() {
logger->log("[robot] %d modules identified...", moduleCount);
// Initialize LED pins
for(int i = 0; i < LED_PIN_COUNT; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
for(int i = 0; i < moduleCount; i++) {
if(modules[i] == NULL) continue;
if(!modules[i]->initialize()) {
logger->log("[robot] Initialization failed on module %s", modules[i]->name);
return false;
}
logger->log("[robot] Successfully initialized module %s", modules[i]->name);
}
return true;
}
bool Robot::systemsCheck() {
logger->log("[robot] Running systems check on identified modules...");
for(int i = 0; i < moduleCount; i++) {
if(modules[i] == NULL) continue;
if(!modules[i]->systemsCheck()) {
logger->log("[robot] System module check failed on module %s", modules[i]->name);
return false;
}
logger->log("[robot] Successfully checked module %s", modules[i]->name);
}
logger->log("[robot] Systems check completed successfully!");
return true;
}
void Robot::addModule(RobotModule *module, int index) {
logger->log("[robot] Adding module: %s", module->name);
if(index >= ROBOT_MAX_MODULES) {
logger->log("[robot] Module index out of bounds: %d", index);
return;
}
module->attachLogger(logger);
modules[index] = module;
moduleCount++;
}
void Robot::setState(int newState) {
logger->log("[robot] Changing state from %d to %d", state, newState);
setLEDs(newState);
state = newState;
}
int Robot::getState() {
return state;
}
int Robot::getPosition() { return position; }
void Robot::setPosition(int newPos) {
this->position = newPos;
}
void Robot::nextPosition() {
position++;
}
void Robot::previousPosition() {
position--;
}
int Robot::getTargetPosition() { return targetPosition; }
void Robot::setTargetPosition(int newPos) {
this->targetPosition = newPos;
}
void Robot::setLEDs(int state) {
// Set the LEDs
for(int i = 0; i < LED_PIN_COUNT; i++) {
digitalWrite(LED_PINS[i], ((state >> i) & 0000000001));
}
}
float Robot::distanceFromCenter(int index) {
return arenaDistances[index];
}
void Robot::followLine(float averageSpeed, float turnSpeed) {
// Read the sensors
Lines *lines = (Lines *) modules[LinesModule];
Motor *motor = (Motor *) modules[MotorModule];
float value = (float) lines->read();
float difference = ((value - 3500) / 7000) * turnSpeed;
// float difference = 0;
float leftValue = averageSpeed + difference;
float rightValue = averageSpeed - difference;
motor->setSpeed(leftValue, rightValue);
motor->setTargetSpeed(leftValue, rightValue);
motor->move();
}