Skip to content

Commit f4bc976

Browse files
authored
Merge pull request #41 from BCLab-UNM/feature-dont-get-stuck-in-center
Feature: don't get stuck in the centre
2 parents ee82d24 + 2c8b59c commit f4bc976

14 files changed

Lines changed: 390 additions & 104 deletions
-40.4 KB
Loading

src/behaviours/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include_directories(
2323

2424
add_executable(
2525
behaviours
26+
src/Tag.cpp
2627
src/ObstacleController.cpp
2728
src/PickUpController.cpp
2829
src/DropOffController.cpp

src/behaviours/src/DropOffController.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void DropOffController::Reset() {
288288

289289
}
290290

291-
void DropOffController::SetTargetData(vector<TagPoint> tags) {
291+
void DropOffController::SetTargetData(vector<Tag> tags) {
292292
countRight = 0;
293293
countLeft = 0;
294294

@@ -298,10 +298,10 @@ void DropOffController::SetTargetData(vector<TagPoint> tags) {
298298

299299
// this loop is to get the number of center tags
300300
for (int i = 0; i < tags.size(); i++) {
301-
if (tags[i].id == 256) {
301+
if (tags[i].getID() == 256) {
302302

303303
// checks if tag is on the right or left side of the image
304-
if (tags[i].x + cameraOffsetCorrection > 0) {
304+
if (tags[i].getPositionX() + cameraOffsetCorrection > 0) {
305305
countRight++;
306306

307307
} else {

src/behaviours/src/DropOffController.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define HEADERFILE_H
44

55
#include "Controller.h"
6-
#include "TagPoint.h"
6+
#include "Tag.h"
77
#include <math.h>
88

99
class DropOffController : virtual Controller
@@ -22,12 +22,12 @@ class DropOffController : virtual Controller
2222
void SetCurrentLocation(Point current);
2323
void SetTargetPickedUp();
2424
void SetBlockBlockingUltrasound(bool blockBlock);
25-
void SetTargetData(vector<TagPoint> tags);
25+
void SetTargetData(vector<Tag> tags);
2626
bool HasTarget() {return targetHeld;}
2727

2828
float GetSpinner() {return spinner;}
2929

30-
void UpdateData(vector<TagPoint> tags);
30+
void UpdateData(vector<Tag> tags);
3131

3232
void SetCurrentTimeInMilliSecs( long int time );
3333

src/behaviours/src/LogicController.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,24 +238,24 @@ void LogicController::controllerInterconnect() {
238238

239239
//obstacle needs to know if the center ultrasound should be ignored
240240
if(pickUpController.GetIgnoreCenter()) {
241-
obstacleController.SetIgnoreCenter();
241+
obstacleController.setIgnoreCenterSonar();
242242
}
243243

244244
//pickup controller annouces it has pickedup a target
245245
if(pickUpController.GetTargetHeld()) {
246246
dropOffController.SetTargetPickedUp();
247-
obstacleController.SetTargetHeld();
247+
obstacleController.setTargetHeld();
248248
searchController.SetSuccesfullPickup();
249249
}
250250
}
251251

252252
//ask if drop off has released the target from the claws yet
253253
if (!dropOffController.HasTarget()) {
254-
obstacleController.SetTargetHeldClear();
254+
obstacleController.setTargetHeldClear();
255255
}
256256

257257
//obstacle controller is running driveController needs to clear its waypoints
258-
if(obstacleController.GetShouldClearWaypoints()) {
258+
if(obstacleController.getShouldClearWaypoints()) {
259259
driveController.Reset();
260260
}
261261
}
@@ -264,7 +264,7 @@ void LogicController::controllerInterconnect() {
264264
void LogicController::SetPositionData(Point currentLocation) {
265265
searchController.SetCurrentLocation(currentLocation);
266266
dropOffController.SetCurrentLocation(currentLocation);
267-
obstacleController.SetCurrentLocation(currentLocation);
267+
obstacleController.setCurrentLocation(currentLocation);
268268
driveController.SetCurrentLocation(currentLocation);
269269
}
270270

@@ -282,15 +282,15 @@ void LogicController::SetMapVelocityData(float linearVelocity, float angularVelo
282282

283283
}
284284

285-
void LogicController::SetAprilTags(vector<TagPoint> tags) {
285+
void LogicController::SetAprilTags(vector<Tag> tags) {
286286
pickUpController.SetTagData(tags);
287-
obstacleController.SetTagData(tags);
287+
obstacleController.setTagData(tags);
288288
dropOffController.SetTargetData(tags);
289289
}
290290

291291
void LogicController::SetSonarData(float left, float center, float right) {
292292
pickUpController.SetSonarData(center);
293-
obstacleController.SetSonarData(left,center,right);
293+
obstacleController.setSonarData(left,center,right);
294294
}
295295

296296
// Called once by RosAdapter in guarded init
@@ -319,5 +319,5 @@ void LogicController::SetCurrentTimeInMilliSecs( long int time )
319319
current_time = time;
320320
dropOffController.SetCurrentTimeInMilliSecs( time );
321321
pickUpController.SetCurrentTimeInMilliSecs( time );
322-
obstacleController.SetCurrentTimeInMilliSecs( time );
322+
obstacleController.setCurrentTimeInMilliSecs( time );
323323
}

src/behaviours/src/LogicController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class LogicController : virtual Controller
3737
bool ShouldInterrupt() override;
3838
bool HasWork() override;
3939

40-
void SetAprilTags(vector<TagPoint> tags);
40+
void SetAprilTags(vector<Tag> tags);
4141
void SetSonarData(float left, float center, float right);
4242
void SetPositionData(Point currentLocation);
4343
void SetMapPositionData(Point currentLocationMap);

src/behaviours/src/ObstacleController.cpp

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,32 @@ void ObstacleController::Reset() {
1515
delay = current_time;
1616
}
1717

18-
Result ObstacleController::DoWork() {
19-
20-
clearWaypoints = true;
21-
set_waypoint = true;
22-
result.PIDMode = CONST_PID;
18+
// Avoid crashing into objects detected by the ultraound
19+
void ObstacleController::avoidObstacle() {
20+
21+
//obstacle on right side
22+
if (right < 0.8 || center < 0.8 || left < 0.8) {
23+
result.type = precisionDriving;
2324

24-
if(centerSeen){
25+
result.pd.cmdAngular = -K_angular;
2526

27+
result.pd.setPointVel = 0.0;
28+
result.pd.cmdVel = 0.0;
29+
result.pd.setPointYaw = 0;
30+
}
31+
}
2632

33+
// A collection zone was seen in front of the rover and we are not carrying a target
34+
// so avoid running over the collection zone and possibly pushing cubes out.
35+
void ObstacleController::avoidCollectionZone() {
36+
2737
result.type = precisionDriving;
2838

2939
result.pd.cmdVel = 0.0;
3040

31-
if(countLeft < countRight) {
41+
// Decide which side of the rover sees the most april tags and turn away
42+
// from that side
43+
if(count_left_collection_zone_tags < count_right_collection_zone_tags) {
3244
result.pd.cmdAngular = K_angular;
3345
} else {
3446
result.pd.cmdAngular = -K_angular;
@@ -37,21 +49,21 @@ Result ObstacleController::DoWork() {
3749
result.pd.setPointVel = 0.0;
3850
result.pd.cmdVel = 0.0;
3951
result.pd.setPointYaw = 0;
40-
41-
}
42-
else {
52+
}
4353

4454

45-
//obstacle on right side
46-
if (right < 0.8 || center < 0.8 || left < 0.8) {
47-
result.type = precisionDriving;
55+
Result ObstacleController::DoWork() {
4856

49-
result.pd.cmdAngular = -K_angular;
57+
clearWaypoints = true;
58+
set_waypoint = true;
59+
result.PIDMode = CONST_PID;
5060

51-
result.pd.setPointVel = 0.0;
52-
result.pd.cmdVel = 0.0;
53-
result.pd.setPointYaw = 0;
54-
}
61+
// The obstacle is an april tag marking the collection zone
62+
if(collection_zone_seen){
63+
avoidCollectionZone();
64+
}
65+
else {
66+
avoidObstacle();
5567
}
5668

5769
if (can_set_waypoint) {
@@ -73,15 +85,15 @@ Result ObstacleController::DoWork() {
7385
}
7486

7587

76-
void ObstacleController::SetSonarData(float sonarleft, float sonarcenter, float sonarright) {
88+
void ObstacleController::setSonarData(float sonarleft, float sonarcenter, float sonarright) {
7789
left = sonarleft;
7890
right = sonarright;
7991
center = sonarcenter;
8092

8193
ProcessData();
8294
}
8395

84-
void ObstacleController::SetCurrentLocation(Point currentLocation) {
96+
void ObstacleController::setCurrentLocation(Point currentLocation) {
8597
this->currentLocation = currentLocation;
8698
}
8799

@@ -91,7 +103,7 @@ void ObstacleController::ProcessData() {
91103
long int Tdifference = current_time - timeSinceTags;
92104
float Td = Tdifference/1e3;
93105
if (Td >= 0.5) {
94-
centerSeen = false;
106+
collection_zone_seen = false;
95107
phys= false;
96108
if (!obstacleAvoided)
97109
{
@@ -100,9 +112,9 @@ void ObstacleController::ProcessData() {
100112
}
101113

102114
//Process sonar info
103-
if(ignoreCenter){
104-
if(center > reactivateCenterThreshold){
105-
ignoreCenter = false;
115+
if(ignore_center_sonar){
116+
if(center > reactivate_center_sonar_threshold){
117+
ignore_center_sonar = false;
106118
}
107119
else{
108120
center = 3;
@@ -124,7 +136,7 @@ void ObstacleController::ProcessData() {
124136
}
125137

126138

127-
if (centerSeen || phys)
139+
if (collection_zone_seen || phys)
128140
{
129141
obstacleDetected = true;
130142
obstacleAvoided = false;
@@ -136,29 +148,49 @@ void ObstacleController::ProcessData() {
136148
}
137149
}
138150

139-
void ObstacleController::SetTagData(vector<TagPoint> tags){
140-
float cameraOffsetCorrection = 0.020; //meters;
141-
centerSeen = false;
142-
countLeft = 0;
143-
countRight = 0;
151+
// Report April tags seen by the rovers camera so it can avoid
152+
// the collection zone
153+
// Added relative pose information so we know whether the
154+
// top of the AprilTag is pointing towards the rover or away.
155+
// If the top of the tags are away from the rover then treat them as obstacles.
156+
void ObstacleController::setTagData(vector<Tag> tags){
157+
collection_zone_seen = false;
158+
count_left_collection_zone_tags = 0;
159+
count_right_collection_zone_tags = 0;
144160

145161
// this loop is to get the number of center tags
146162
if (!targetHeld) {
147163
for (int i = 0; i < tags.size(); i++) {
148-
if (tags[i].id == 256) {
149-
150-
// checks if tag is on the right or left side of the image
151-
if (tags[i].x + cameraOffsetCorrection > 0) {
152-
countRight++;
164+
if (tags[i].getID() == 256) {
153165

154-
} else {
155-
countLeft++;
156-
}
157-
centerSeen = true;
166+
collection_zone_seen = checkForCollectionZoneTags( tags );
158167
timeSinceTags = current_time;
159168
}
160169
}
161170
}
171+
}
172+
173+
bool ObstacleController::checkForCollectionZoneTags( vector<Tag> tags ) {
174+
175+
for ( auto & tag : tags ) {
176+
177+
// Check the orientation of the tag. If we are outside the collection zone the yaw will be positive so treat the collection zone as an obstacle. If the yaw is negative the robot is inside the collection zone and the boundary should not be treated as an obstacle. This allows the robot to leave the collection zone after dropping off a target.
178+
if ( tag.calcYaw() > 0 )
179+
{
180+
// checks if tag is on the right or left side of the image
181+
if (tag.getPositionX() + camera_offset_correction > 0) {
182+
count_right_collection_zone_tags++;
183+
184+
} else {
185+
count_left_collection_zone_tags++;
186+
}
187+
}
188+
189+
}
190+
191+
192+
// Did any tags indicate that the robot is inside the collection zone?
193+
return count_left_collection_zone_tags + count_right_collection_zone_tags > 0;
162194

163195
}
164196

@@ -191,16 +223,17 @@ bool ObstacleController::HasWork() {
191223
return !obstacleAvoided;
192224
}
193225

194-
void ObstacleController::SetIgnoreCenter(){
195-
ignoreCenter = true; //ignore center ultrasound
226+
//ignore center ultrasound
227+
void ObstacleController::setIgnoreCenterSonar(){
228+
ignore_center_sonar = true;
196229
}
197230

198-
void ObstacleController::SetCurrentTimeInMilliSecs( long int time )
231+
void ObstacleController::setCurrentTimeInMilliSecs( long int time )
199232
{
200233
current_time = time;
201234
}
202235

203-
void ObstacleController::SetTargetHeld() {
236+
void ObstacleController::setTargetHeld() {
204237
targetHeld = true;
205238

206239

0 commit comments

Comments
 (0)