-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.h
116 lines (103 loc) · 2.87 KB
/
Robot.h
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
#pragma once
#include "BluetoothManager.h"
#include "Constants.h"
#include "ServoManager.h"
#include "DectectionManager.h"
#include "MotorManager.h"
// Robot class Manages all the Managers and Controls the Logic of the Vehicle.
class Robot {
private:
BluetoothManager mBluetoothManager;
ServoManager mServoManager;
DectectionManager mDetectionManager;
MotorManager mMotorManager;
char mCurrentState = STATE_STOP;
public:
// Initialise all Managers. Must be called in `Setup` function
void Init() {
mBluetoothManager.Init();
mServoManager.Init();
mDetectionManager.Init();
mMotorManager.Init(255);
}
// Poll and Handle Inputs. Must be called inside `Loop` function.
void Update() {
pollInputs();
handleInputs();
}
private:
void pollInputs() {
char character = mBluetoothManager.Next();
if (character != STATE_NULL) mCurrentState = character;
}
void handleInputs() {
switch (mCurrentState) {
case STATE_FRONT:
handleForward();
break;
case STATE_BACK:
handleBackward();
break;
case STATE_LEFT:
handleLeft();
break;
case STATE_RIGHT:
handleRight();
break;
default: // i.e. NULL or STOP
handleStop();
break;
}
}
/**
Check if it is save to move in a specific direction.
This rotates the Servo and gets the distance. Depending on the SAFE distances in Constants.h,
It returns true or false.
*/
bool isSafeToMove(char direction) {
int angle = 0;
if (direction == STATE_FRONT) angle = RELATIVE_FRONT_ANGLE;
else if (direction == STATE_LEFT) angle = RELATIVE_LEFT_ANGLE;
else if (direction == STATE_RIGHT) angle = RELATIVE_RIGHT_ANGLE;
mServoManager.RotateTowards(angle);
if(angle != 90) delay(500);
int distance = mDetectionManager.GetDistance();
if (direction == STATE_FRONT && distance > SAFE_DISTANCE_FRONT) return true;
else if (direction == STATE_LEFT && distance > SAFE_DISTANCE_LEFT) return true;
else if (direction == STATE_RIGHT && distance > SAFE_DISTANCE_RIGHT) return true;
else if (direction == STATE_BACK) return true;
else return false;
}
void handleForward() {
if (isSafeToMove(STATE_FRONT)) {
mMotorManager.MoveForward();
} else {
mCurrentState = STATE_STOP;
}
}
void handleBackward() {
mMotorManager.MoveBackward();
}
void handleLeft() {
if (isSafeToMove(STATE_LEFT)) {
mMotorManager.MoveLeft();
delay(500);
mCurrentState = STATE_FRONT;
} else {
mCurrentState = STATE_STOP;
}
}
void handleRight() {
if (isSafeToMove(STATE_RIGHT)) {
mMotorManager.MoveRight();
delay(500);
mCurrentState = STATE_FRONT;
} else {
mCurrentState = STATE_STOP;
}
}
void handleStop() {
mMotorManager.Stop();
mServoManager.RotateTowards(RELATIVE_FRONT_ANGLE);
}
};