22#include < limits> // For numeric limits
33#include < cmath> // For hypot
44
5- PickUpController::PickUpController () {
5+ PickUpController::PickUpController ()
6+ {
67 lockTarget = false ;
78 timeOut = false ;
89 nTargetsSeen = 0 ;
@@ -19,42 +20,54 @@ PickUpController::PickUpController() {
1920 result.PIDMode = SLOW_PID;
2021}
2122
22- PickUpController::~PickUpController () {
23- }
23+ PickUpController::~PickUpController () { /* Destructor*/ }
2424
2525void PickUpController::SetTagData (vector<Tag> tags)
2626{
2727
28- if (tags.size () > 0 ) {
28+ if (tags.size () > 0 )
29+ {
2930
3031 nTargetsSeen = tags.size ();
3132
33+ // we saw a target, set target_timer
34+ target_timer = current_time;
35+
3236 double closest = std::numeric_limits<double >::max ();
3337 int target = 0 ;
34- for (int i = 0 ; i < tags.size (); i++) { // this loop selects the closest visible block to makes goals for it
3538
36- if (tags[i].getID () == 0 ) {
39+ // this loop selects the closest visible block to makes goals for it
40+ for (int i = 0 ; i < tags.size (); i++)
41+ {
42+
43+ if (tags[i].getID () == 0 )
44+ {
3745
3846 targetFound = true ;
3947
40- double test = hypot (hypot (tags[i].getPositionX (), tags[i].getPositionY ()), tags[i].getPositionZ ()); // absolute distance to block from camera lens
48+ // absolute distance to block from camera lens
49+ double test = hypot (hypot (tags[i].getPositionX (), tags[i].getPositionY ()), tags[i].getPositionZ ());
50+
4151 if (closest > test)
4252 {
4353 target = i;
4454 closest = test;
4555 }
4656 }
47- else {
48- nTargetsSeen--;
57+ else
58+ {
4959
5060 if (tags[i].getID () == 256 )
5161 {
62+
5263 Reset ();
64+
5365 if (has_control)
5466 {
5567 cout << " pickup reset return interupt free" << endl;
5668 release_control = true ;
5769 }
70+
5871 return ;
5972 }
6073 }
@@ -65,9 +78,6 @@ void PickUpController::SetTagData(vector<Tag> tags)
6578 // /TODO: Explain the trig going on here- blockDistance is c, 0.195 is b; find a
6679 blockDistanceFromCamera = hypot (hypot (tags[target].getPositionX (), tags[target].getPositionY ()), tags[target].getPositionZ ());
6780
68- // WRONG WRONG WRONG WRONG
69- // blockDistance = hypot(tags[target].getPositionZ(), tags[target].getPositionY()); //distance from bottom center of chassis ignoring height.
70-
7181 if ( (blockDistanceFromCamera*blockDistanceFromCamera - 0.195 *0.195 ) > 0 )
7282 {
7383 blockDistance = sqrt (blockDistanceFromCamera*blockDistanceFromCamera - 0.195 *0.195 );
@@ -92,7 +102,8 @@ void PickUpController::SetTagData(vector<Tag> tags)
92102bool PickUpController::SetSonarData (float rangeCenter)
93103{
94104
95- if (rangeCenter < 0.12 && targetFound) {
105+ if (rangeCenter < 0.12 && targetFound)
106+ {
96107 result.type = behavior;
97108 result.b = nextProcess;
98109 result.reset = true ;
@@ -106,17 +117,21 @@ bool PickUpController::SetSonarData(float rangeCenter)
106117
107118void PickUpController::ProcessData ()
108119{
120+
109121 if (!targetFound)
110122 {
123+ // cout << "PICKUP No Target Seen!" << endl;
124+
111125 // Do nothing
112126 return ;
113127 }
114128
115- // if target is close enough
116129 // diffrence between current time and millisecond time
117130 long int Tdiff = current_time - millTimer;
118131 float Td = Tdiff/1e3 ;
119132
133+ // cout << "PICKUP Target Seen!" << endl;
134+
120135 // cout << "distance : " << blockDistanceFromCamera << " time is : " << Td << endl;
121136
122137 if (blockDistanceFromCamera < 0.14 && Td < 3.9 )
@@ -147,30 +162,34 @@ bool PickUpController::ShouldInterrupt(){
147162 return true ;
148163 }
149164
150- if ((targetFound && !interupted) || targetHeld) {
165+ if ((targetFound && !interupted) || targetHeld)
166+ {
151167 interupted = true ;
152168 has_control = false ;
153169 return true ;
154170 }
155- else if (!targetFound && interupted) {
171+ else if (!targetFound && interupted)
172+ {
156173 interupted = false ;
157174 has_control = false ;
158175 return true ;
159176 }
160- else {
177+ else
178+ {
161179 return false ;
162180 }
163181}
164182
165- Result PickUpController::DoWork () {
183+ Result PickUpController::DoWork ()
184+ {
166185
167186 has_control = true ;
168187
169- if (!targetHeld) {
188+ if (!targetHeld)
189+ {
170190 // threshold distance to be from the target block before attempting pickup
171191 float targetDistance = 0.15 ; // meters
172192
173-
174193 // -----------------------------------------------------------
175194 // millisecond time = current time if not in a counting state
176195 // when timeOut is true, we are counting towards a time out
@@ -208,13 +227,27 @@ Result PickUpController::DoWork() {
208227 // If we don't see any blocks or cubes turn towards the location of the last cube we saw.
209228 // I.E., try to re-aquire the last cube we saw.
210229
211- float grasp_time_begin = 1.7 ;
212- float raise_time_begin = 2.5 ;
230+ float grasp_time_begin = 1.1 ;
231+ float raise_time_begin = 2.0 ;
213232 float lower_gripper_time_begin = 4.0 ;
214233 float target_reaquire_begin= 4.2 ;
215234 float target_pickup_task_time_limit = 4.8 ;
216235
217236 // cout << "blockDistance DOWORK: " << blockDistance << endl;
237+
238+ // Calculate time difference between last seen tag
239+ float target_timeout = (current_time - target_timer)/1e3 ;
240+
241+ // delay between the camera refresh and rover runtime is 6/10's of a second
242+ float target_timeout_limit = 0.61 ;
243+
244+ // Timer to deal with delay in refresh from camera and the runtime of rover code
245+ if ( target_timeout >= target_timeout_limit )
246+ {
247+ // Has to be set back to 0
248+ nTargetsSeen = 0 ;
249+ }
250+
218251
219252 if (nTargetsSeen == 0 && !lockTarget)
220253 {
@@ -223,7 +256,7 @@ Result PickUpController::DoWork() {
223256 {
224257 result.pd .cmdVel = 0.0 ;
225258 result.pd .cmdAngularError = 0.0 ;
226- result.wristAngle = 0.8 ;
259+ result.wristAngle = 1.25 ;
227260 // result.fingerAngle does not need to be set here
228261
229262 // We are getting ready to start the pre-programmed pickup routine now! Maybe? <(^_^)/"
@@ -252,7 +285,7 @@ Result PickUpController::DoWork() {
252285 result.pd .cmdVel = vel;
253286 result.pd .cmdAngularError = -blockYawError;
254287 timeOut = false ;
255- nTargetsSeen = 0 ;
288+
256289 return result;
257290 }
258291 else if (!lockTarget) // if a target hasn't been locked lock it and enter a counting state while slowly driving forward.
@@ -279,11 +312,14 @@ Result PickUpController::DoWork() {
279312
280313
281314 // the magic numbers compared to Td must be in order from greater(top) to smaller(bottom) numbers
282- if (Td > target_reaquire_begin && timeOut) {
315+ if (Td > target_reaquire_begin && timeOut)
316+ {
283317 lockTarget = false ;
284318 ignoreCenterSonar = true ;
285319 }
286- else if (Td > lower_gripper_time_begin && timeOut) // if enough time has passed enter a recovery state to re-attempt a pickup
320+
321+ // if enough time has passed enter a recovery state to re-attempt a pickup
322+ else if (Td > lower_gripper_time_begin && timeOut)
287323 {
288324 result.pd .cmdVel = -0.15 ;
289325 result.pd .cmdAngularError = 0.0 ;
@@ -292,8 +328,8 @@ Result PickUpController::DoWork() {
292328 result.wristAngle = 0 ;
293329 }
294330
295-
296- if (Td > target_pickup_task_time_limit && timeOut) // if no targets are found after too long a period go back to search pattern
331+ // if no targets are found after too long a period go back to search pattern
332+ if (Td > target_pickup_task_time_limit && timeOut)
297333 {
298334 Reset ();
299335 interupted = true ;
@@ -306,7 +342,8 @@ Result PickUpController::DoWork() {
306342 return result;
307343}
308344
309- bool PickUpController::HasWork () {
345+ bool PickUpController::HasWork ()
346+ {
310347 return targetFound;
311348}
312349
0 commit comments