-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstallDetectionHeader.h
More file actions
218 lines (205 loc) · 6.29 KB
/
stallDetectionHeader.h
File metadata and controls
218 lines (205 loc) · 6.29 KB
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
#pragma once
#include <ros/ros.h>
//#include <chrono>
#include <boost/chrono.hpp>
#include <iostream>
#include <deque>
#include <queue>
#include <math.h>
#include <geometry_msgs/Twist.h>
#include <std_msgs/Bool.h>
#include <geometry_msgs/Pose2D.h>
#include <sensor_msgs/PointCloud.h>
#include "boost/date_time/posix_time/posix_time.hpp"
#include <string.h>
class StallDetection
{
private:
ros::NodeHandle n;
ros::Publisher stallVelocityPub;
ros::Publisher navigationPIDStatusPub;
ros::Subscriber robotCurrentVelocitySub;
ros::Subscriber expectedVelocitySub;
ros::Subscriber actualVelocitySub;
ros::Subscriber eStopStatusSub;
int queueSize;
deque<geometry_msgs::Pose2D> robotVelocityHistory;
bool disableNavigationPID;
bool eStopStatus;
geometry_msgs::Twist actualVelocity;
geometry_msgs::Twist expectedVelocity;
boost::posix_time::ptime stuckTime;
double maxSpeed;
bool stallDetected;
double overrideSpeed;
double reverseDurationInMilli;
//Initialize tolerance thresholds and time saving for Slip detection
double noMovementTolerance;// 10 cm
double noRotationTolerance; //5 degrees
//DateTime lastStuckTime; //save last stuck time
//TimeSpan reverseDuration = new TimeSpan(0, 0, 0, 0, 800); // 0.5 Seconds
double reverseSpeed;
//Private Functions
double findMinElement(string returnType)
{
if (returnType == "x")
{
std::deque<geometry_msgs::Pose2D>::iterator it = std::min_element(robotVelocityHistory.begin(), robotVelocityHistory.end(),
[](geometry_msgs::Pose2D &a, geometry_msgs::Pose2D &b)
{
return a.x < b.x;
});
return (*it).x;
}
else if (returnType == "y")
{
std::deque<geometry_msgs::Pose2D>::iterator it = std::min_element(robotVelocityHistory.begin(), robotVelocityHistory.end(),
[](geometry_msgs::Pose2D &a, geometry_msgs::Pose2D &b)
{
return a.y < b.y;
});
return (*it).y;
}
else if (returnType == "theta")
{
std::deque<geometry_msgs::Pose2D>::iterator it = std::max_element(robotVelocityHistory.begin(), robotVelocityHistory.end(),
[](geometry_msgs::Pose2D &a, geometry_msgs::Pose2D &b)
{
return a.theta < b.theta;
});
return (*it).theta;
}
else
return 0.0;
}
double findMaxElement(string returnType)
{
if (returnType == "x")
{
std::deque<geometry_msgs::Pose2D>::iterator it = std::max_element(robotVelocityHistory.begin(), robotVelocityHistory.end(),
[](geometry_msgs::Pose2D &a, geometry_msgs::Pose2D &b)
{
return a.x < b.x;
});
return (*it).x;
}
else if (returnType == "y")
{
std::deque<geometry_msgs::Pose2D>::iterator it = std::max_element(robotVelocityHistory.begin(), robotVelocityHistory.end(),
[](geometry_msgs::Pose2D &a, geometry_msgs::Pose2D &b)
{
return a.y < b.y;
});
return (*it).y;
}
else if (returnType == "theta")
{
std::deque<geometry_msgs::Pose2D>::iterator it = std::max_element(robotVelocityHistory.begin(), robotVelocityHistory.end(),
[](geometry_msgs::Pose2D &a, geometry_msgs::Pose2D &b)
{
return a.theta < b.theta;
});
return (*it).theta;
}
else
return 0.0;
}
void robotPositionCallback(const geometry_msgs::Pose2D::ConstPtr& robotLocation)
{
addVelocityToHistory(*robotLocation);
}
void eStopStatusCallback(const std_msgs::Bool::ConstPtr& gpio)
{
//eStopStatusCallback = gpio->eStop;
}
void expectedRobotVelocityCallback(const geometry_msgs::Twist::ConstPtr& velocity)
{
expectedVelocity = *velocity;
}
void actualRobotVelocityCallback(const geometry_msgs::Twist::ConstPtr& velocity)
{
actualVelocity = *velocity;
}
public:
StallDetection()
{
robotCurrentVelocitySub = n.subscribe("/localization/robot_location", 100, &StallDetection::robotPositionCallback, this);
actualVelocitySub = n.subscribe("/localization/velocity", 100, &StallDetection::expectedRobotVelocityCallback, this);
expectedVelocitySub = n.subscribe("/obstacle_reaction/velocity", 100, &StallDetection::expectedRobotVelocityCallback, this);
eStopStatusSub = n.subscribe("gpio/inputs", 1, &StallDetection::eStopStatusCallback, this);
n.param("robot_velocity_queue_size", queueSize, 100);
n.param("slip_detection_noMovementTolerance", noMovementTolerance, 0.1);// 10 cm
n.param("slip_detection_noRotationTolerance", noRotationTolerance, 5.0); //5 degrees
n.param("slip_detection_reverseSpeed", reverseSpeed, -0.5);
n.param("stall_detection_maxSpeed", maxSpeed, 0.7);
n.param("stall_detection_reverse_duration", reverseDurationInMilli, 0.7);
stallDetected = false;
robotVelocityHistory.resize(queueSize);
}
bool getStallStatus()
{
return stallDetected;
}
double getReverseDurationInMilli()
{
return reverseDurationInMilli;
}
double getExpectedVelocity()
{
return expectedVelocity.linear.x;
}
double getMaxSpeed()
{
return maxSpeed;
}
double getReverseSpeed()
{
return reverseSpeed;
}
boost::posix_time::ptime getLastStuckTime()
{
return stuckTime;
}
void addVelocityToHistory(geometry_msgs::Pose2D robotCurrentVelocity)
{
if (robotVelocityHistory.size() > queueSize)
{
robotVelocityHistory.pop_front();
}
robotVelocityHistory.push_back(robotCurrentVelocity);
if (robotVelocityHistory.size() >= queueSize)
{
activateStallDetection();
}
}
void activateStallDetection()
{
double minX = findMinElement("x");
double maxX = findMaxElement("x");
double minY = findMinElement("y");
double maxY = findMaxElement("y");
double minHeading = findMinElement("theta");
double maxHeading = findMaxElement("theta");
if ((pow(maxX - minX, 2) + pow(maxY - minY, 2) < noMovementTolerance)
&& maxHeading - minHeading < noRotationTolerance && eStopStatus &&
(expectedVelocity.linear.x != actualVelocity.linear.x) || (expectedVelocity.angular.z != actualVelocity.angular.z))
{
stallDetected = true;
ptime t(second_clock::local_time());//save the time which slipping was detected
stuckTime = t;
robotVelocityHistory.clear();// clear the QUeue so that stall detection cannot occur again until the queue is full
}
else
stallDetected = false;
}
//to be called in drive_mode_switch
void doBackup(geometry_msgs::Twist &stallVelocity)
{
//This is the part saying to backup
//Send Reverse Speed
double reverseSpeed = getMaxSpeed() * getReverseSpeed();
stallVelocity.linear.x = reverseSpeed;
stallVelocity.angular.z = 0.0;
//TODO: try accelerating forward at different rates via PID,
}
};