Newer
Older
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
#include "strategieV2.h"
#include "leds.h"
#include "positionPlusAngle.h"
#include "commandLookAt.h"
#include "asservissement.h"
#include "memory.h"
#include "actionGoTo.h"
#include "ramenerVerres.h"
#include "recalibrerOdometrie.h"
#include "evitement.h"
#include "commandAllerA.h"
#ifndef NULL
#define NULL 0
#endif
StrategieV2* StrategieV2::strategie = NULL;
int StrategieV2::updateCount = 0;
Command* StrategieV2::currentCommand = NULL;
MediumLevelAction* StrategieV2::currentAction = NULL;
int StrategieV2::actionsCount = 0;
MediumLevelAction* StrategieV2::actionsToDo[32];
bool StrategieV2::hasToGoBase = false;
bool StrategieV2::hasJustAvoided = false;
bool StrategieV2::mustDeleteAction = false;
int StrategieV2::glassGathered = 0;
int StrategieV2::timeSinceLastRecalibration = 0;
bool StrategieV2::somethingDetected = false;
StrategieV2::StrategieV2()
{
/*actionsToDo[0] = new RamasserVerreV2(Position(900,550));
actionsToDo[1] = new RamasserVerreV2(Position(1050,800));
actionsToDo[2] = new RamasserVerreV2(Position(900,1050));
actionsToDo[3] = new RamasserVerreV2(Position(1200,1050));
actionsToDo[4] = new RamasserVerreV2(Position(1200,550));
actionsToDo[5] = new RamasserVerreV2(Position(1350,800));
actionsToDo[6] = new RamasserVerreV2(Position(1650,800));
actionsToDo[7] = new RamasserVerreV2(Position(1800,1050));
actionsToDo[8] = new RamasserVerreV2(Position(1800,550));
actionsToDo[9] = new RamasserVerreV2(Position(1950,800));
actionsToDo[10] = new RamasserVerreV2(Position(2100,550));
actionsToDo[11] = new RamasserVerreV2(Position(2100,1050));*/
//actionsToDo[0] = new Evitement();
actionsToDo[0] = new ActionGoTo(Position(1000,230), false);
actionsToDo[1] = new ActionGoTo(Position(2300,230), false);
actionsToDo[2] = new ActionGoTo(Position(2600,400), false);
actionsToDo[3] = new ActionGoTo(Position(2600,550), false);
actionsToDo[4] = new ActionGoTo(Position(300, 550), false);
actionsToDo[5] = new ActionGoTo(Position(2600,550), true);
actionsToDo[6] = new ActionGoTo(Position(2600,800), false);
actionsToDo[7] = new ActionGoTo(Position(300, 800), false);
actionsToDo[8] = new ActionGoTo(Position(2600,800), true);
actionsToDo[9] = new ActionGoTo(Position(2600,1070), false);
actionsToDo[10] = new ActionGoTo(Position(300,1070), false);
//currentAction = new RecalibrerOdometrie(new LimitSwitchSensor(LimitSwitchSensor::BACK_LEFT, GPIO_Pin_14, GPIOD), new LimitSwitchSensor(LimitSwitchSensor::BACK_RIGHT, GPIO_Pin_1, GPIOD));
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
currentAction = actionsToDo[actionsCount];
//currentCommand = new CommandAllerA(currentAction->getGoalPosition());
//Asservissement::setCommand(currentCommand);
//currentAction = new RecalibrerOdometrie();
//hasJustAvoided = true;
currentAction->update();
StrategieV2::strategie = this;
}
StrategieV2::~StrategieV2()
{
//dtor
}
void StrategieV2::update()
{
if (StrategieV2::strategie == NULL)
return;
if (actionsCount == 11)
return;
// check sensors:
if (somethingDetected)
{
hasJustAvoided = true;
if (true) // if (canStillDoAction)
{
if (currentAction)
{
currentAction->collisionAvoided();
currentAction->update();
}
}
hasJustAvoided = false;
somethingDetected = false;
}
//std::cout << "updating action" << std::endl;
if (currentAction->update() == -1)
{
//std::cout << "Changing action" << std::endl;
Position currentPos = Odometrie::odometrie->getPos().getPosition();
// delete the current command
//if (currentCommand != NULL)
//delete currentCommand;
if (mustDeleteAction && currentAction != NULL)
delete currentAction;
// must go to base :
if (hasToGoBase)
{
currentAction = new RamenerVerres(Position(300,currentPos.getY()));
hasToGoBase = false;
mustDeleteAction = true;
}
/*else if (timeSinceLastRecalibration % 3 == 2)
{
currentAction = new RecalibrerOdometrie();
mustDeleteAction = true;
timeSinceLastRecalibration++;
}*/
//else
//{
if (mustDeleteAction) // temporary action
mustDeleteAction = false;
actionsCount++;
if (actionsCount == 11)
{
Asservissement::asservissement->setCommandSpeeds(NULL);
return;
}
else
{
currentAction = actionsToDo[actionsCount];
mustDeleteAction = false;
}
//timeSinceLastRecalibration++;
//}
//if (currentAction != NULL)
//delete action;
currentAction->update();
}
if (currentCommand)
currentCommand->update();
Asservissement::asservissement->setCommandSpeeds(currentCommand);
updateCount ++;
}
void StrategieV2::setCurrentGoal(Position goal, bool goBack)
{
if (currentCommand != NULL)
delete currentCommand;
currentCommand = new CommandAllerA(goal, goBack);
Asservissement::asservissement->setCommandSpeeds(currentCommand);
}
void StrategieV2::setCurrentGoal(Position goal, Position center, float vitesse, bool goBack)
{
if (currentCommand != NULL)
delete currentCommand;
currentCommand = new CommandAllerEnArcA(goal, center, vitesse*5, goBack);
Asservissement::asservissement->setCommandSpeeds(currentCommand);
}
void StrategieV2::lookAt(Position pos)
{
if (currentCommand != NULL)
delete currentCommand;
currentCommand = new CommandTournerVers(pos); // create the command
Asservissement::asservissement->setCommandSpeeds(currentCommand); // apply it
}
void StrategieV2::addTemporaryAction(MediumLevelAction* action)
{
currentAction = action;
mustDeleteAction = true; // this is a temporary action that needs to be deleted
}
void StrategieV2::setJustAvoided(bool avoided)
{
hasJustAvoided = avoided;
}
bool StrategieV2::getJustAvoided()
{
return hasJustAvoided;
}
bool StrategieV2::willCollide()
{
somethingDetected = true;
Asservissement::asservissement->setCommandSpeeds(NULL); // stoppe le robot
}
void StrategieV2::gatherGlass()
{
glassGathered++;
if (glassGathered > 0)
{
glassGathered = 0;
hasToGoBase = true;
}
}