-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_ROBOT201.cpp
228 lines (210 loc) · 6.44 KB
/
class_ROBOT201.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//------------------------------------------------------------------
// File name: class_ROBOT201.cpp
// Assign ID: TERMproject
// Due Date: 12/07/2021
//
// Purpose: Derived robot class implementation.
//
// Author: brownM105 Mario Brown
//------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;
#include "class_ROBOT101.h"
#include "class_ROBOT201.h"
//-----------------------------------------------------------------
// Constructor: use specified grid (MINx, MINy) to (MAXx, MAXy)..
//-----------------------------------------------------------------
ROBOT201::ROBOT201( ):ROBOT101()
{
inService = false;
Current.xpos = Current.ypos = 0;
gridLO.xpos = gridLO.ypos = 0;
gridHI.xpos = gridHI.ypos = 20;
Establish_Grid(0,0,20,20);
Power = 500;
totDist = 0;
}
//-----------------------------------------------------------------
// Set Grid
//-----------------------------------------------------------------
void ROBOT201::Set_Grid(LOCATION MIN, LOCATION MAX)
{
//Check values
if(MIN.xpos >= 0 && MIN.xpos < MAX.xpos && MIN.ypos >= 0
&& MAX.xpos > 0 && MAX.ypos > 0 && MIN.ypos < MAX.ypos)
{
gridLO = MIN;
gridHI = MAX;
Establish_Grid(MIN.xpos,MIN.ypos,MAX.xpos,MAX.ypos);
}
else return;
}
//-----------------------------------------------------------------
// Set Robot ID.
//-----------------------------------------------------------------
void ROBOT201::Set_RobotID(string ID)
{
RobotID = ID;
}
//-----------------------------------------------------------------
// Get Robot ID.
//-----------------------------------------------------------------
string ROBOT201::Get_RobotID()
{
return RobotID;
}
//-----------------------------------------------------------------
// Set Robot Power
// Power level: 0-1000
//-----------------------------------------------------------------
void ROBOT201::Set_Power(float power)
{
//check for valid power
if (power < 0 || power > 1000)
return;
else Power = power;
}
//-----------------------------------------------------------------
// Get current power level.
//-----------------------------------------------------------------
float ROBOT201::Get_Power()
{
return Power;
}
//-----------------------------------------------------------------
// Get distance moved while in service.
//-----------------------------------------------------------------
float ROBOT201::Get_Distance()
{
return totDist;
}
//-----------------------------------------------------------------
// Return whether robot is in service.
//
//-----------------------------------------------------------------
bool ROBOT201::InService()
{
return inService;
}
//-----------------------------------------------------------------
// Place make robot in-service.
//-----------------------------------------------------------------
void ROBOT201::Place_InService()
{
if(!inService)
StartUp(0,0);
inService = true;
}
//-----------------------------------------------------------------
// Return current location.
//-----------------------------------------------------------------
LOCATION ROBOT201::Get_CurLocation()
{
//Update the Current struct with ROBOT101 values
Current.xpos = Get_Xcoord();
Current.ypos = Get_Ycoord();
return Current;
}
//-----------------------------------------------------------------
// Move from current location to destination if robot.
//-----------------------------------------------------------------
bool ROBOT201::MoveTo(LOCATION dest)
{
//Check for invalid values and return false
if(!inService || dest.xpos > gridHI.xpos || dest.xpos < gridLO.xpos ||
dest.ypos > gridHI.ypos || dest.ypos < gridLO.ypos) return false;
//Call this function to get the most up-to-date values from ROBOT101
Get_CurLocation();
//calculate distance between current position and future destination
float x_dist = dest.xpos - Current.xpos;
float y_dist = dest.ypos - Current.ypos;
// Check for valid values and return true
if(Current.ypos+y_dist >= gridLO.ypos && Current.ypos+y_dist <= gridHI.ypos
&& Current.xpos+x_dist >= gridLO.xpos && Current.xpos+y_dist <= gridHI.xpos)
{
// East = positive x distance
if(x_dist > 0)
{
for(int i = 0; i < x_dist; i++)
{
if(Power > 0)
{
Move('E');
Current.xpos =+ 1;
totDist = totDist+1;
Power = Power-1;
}
if(Power == 0)
{
inService = false;
}
}
}
// West = negative x distance
else if(x_dist < 0)
{
for(int i = 0; i < abs(x_dist); i++)
{
if(Power > 0)
{
Move('W');
Current.xpos =- 1;
totDist = totDist+1;
Power = Power-1;
}
if(Power == 0)
{
inService = false;
}
}
}
// North = positive y distance
if(y_dist > 0)
{
for(int i = 0; i < y_dist; i++)
{
if(Power > 0)
{
Move('N');
Current.ypos =+ 1;
totDist = totDist+1;
Power = Power-1;
}
if(Power == 0)
{
inService = false;
}
}
}
// South = negative y distance
else if(y_dist < 0)
{
for(int i = 0; i < abs(y_dist); i++)
{
if(Power > 0)
{
Move('S');
Current.xpos =- 1;
totDist = totDist+1;
Power = Power-1;
}
if(Power == 0)
{
inService = false;
}
}
}
return true;
}
else return false;
}
//-----------------------------------------------------------------
// Take robot out of service.
//-----------------------------------------------------------------
void ROBOT201::Take_OutOfService()
{
inService = false;
}