11#include " PickUpController.h"
2- #include < limits> // For numeric limits
3- #include < cmath> // For hypot
42
5- PickUpController::PickUpController () {
3+ PickUpController::PickUpController ()
4+ {
65 lockTarget = false ;
76 timeOut = false ;
87 nTargetsSeen = 0 ;
@@ -19,42 +18,54 @@ PickUpController::PickUpController() {
1918 result.PIDMode = SLOW_PID;
2019}
2120
22- PickUpController::~PickUpController () {
23- }
21+ PickUpController::~PickUpController () { /* Destructor*/ }
2422
2523void PickUpController::SetTagData (vector<Tag> tags)
2624{
2725
28- if (tags.size () > 0 ) {
26+ if (tags.size () > 0 )
27+ {
2928
3029 nTargetsSeen = tags.size ();
3130
31+ // we saw a target, set target_timer
32+ target_timer = current_time;
33+
3234 double closest = std::numeric_limits<double >::max ();
3335 int target = 0 ;
34- for (int i = 0 ; i < tags.size (); i++) { // this loop selects the closest visible block to makes goals for it
3536
36- if (tags[i].getID () == 0 ) {
37+ // this loop selects the closest visible block to makes goals for it
38+ for (int i = 0 ; i < tags.size (); i++)
39+ {
40+
41+ if (tags[i].getID () == 0 )
42+ {
3743
3844 targetFound = true ;
3945
40- double test = hypot (hypot (tags[i].getPositionX (), tags[i].getPositionY ()), tags[i].getPositionZ ()); // absolute distance to block from camera lens
46+ // absolute distance to block from camera lens
47+ double test = hypot (hypot (tags[i].getPositionX (), tags[i].getPositionY ()), tags[i].getPositionZ ());
48+
4149 if (closest > test)
4250 {
4351 target = i;
4452 closest = test;
4553 }
4654 }
47- else {
48- nTargetsSeen--;
55+ else
56+ {
4957
5058 if (tags[i].getID () == 256 )
5159 {
60+
5261 Reset ();
62+
5363 if (has_control)
5464 {
5565 cout << " pickup reset return interupt free" << endl;
5666 release_control = true ;
5767 }
68+
5869 return ;
5970 }
6071 }
@@ -65,9 +76,6 @@ void PickUpController::SetTagData(vector<Tag> tags)
6576 // /TODO: Explain the trig going on here- blockDistance is c, 0.195 is b; find a
6677 blockDistanceFromCamera = hypot (hypot (tags[target].getPositionX (), tags[target].getPositionY ()), tags[target].getPositionZ ());
6778
68- // WRONG WRONG WRONG WRONG
69- // blockDistance = hypot(tags[target].getPositionZ(), tags[target].getPositionY()); //distance from bottom center of chassis ignoring height.
70-
7179 if ( (blockDistanceFromCamera*blockDistanceFromCamera - 0.195 *0.195 ) > 0 )
7280 {
7381 blockDistance = sqrt (blockDistanceFromCamera*blockDistanceFromCamera - 0.195 *0.195 );
@@ -92,7 +100,8 @@ void PickUpController::SetTagData(vector<Tag> tags)
92100bool PickUpController::SetSonarData (float rangeCenter)
93101{
94102
95- if (rangeCenter < 0.12 && targetFound) {
103+ if (rangeCenter < 0.12 && targetFound)
104+ {
96105 result.type = behavior;
97106 result.b = nextProcess;
98107 result.reset = true ;
@@ -106,17 +115,21 @@ bool PickUpController::SetSonarData(float rangeCenter)
106115
107116void PickUpController::ProcessData ()
108117{
118+
109119 if (!targetFound)
110120 {
121+ // cout << "PICKUP No Target Seen!" << endl;
122+
111123 // Do nothing
112124 return ;
113125 }
114126
115- // if target is close enough
116127 // diffrence between current time and millisecond time
117128 long int Tdiff = current_time - millTimer;
118129 float Td = Tdiff/1e3 ;
119130
131+ // cout << "PICKUP Target Seen!" << endl;
132+
120133 // cout << "distance : " << blockDistanceFromCamera << " time is : " << Td << endl;
121134
122135 if (blockDistanceFromCamera < 0.14 && Td < 3.9 )
@@ -147,30 +160,34 @@ bool PickUpController::ShouldInterrupt(){
147160 return true ;
148161 }
149162
150- if ((targetFound && !interupted) || targetHeld) {
163+ if ((targetFound && !interupted) || targetHeld)
164+ {
151165 interupted = true ;
152166 has_control = false ;
153167 return true ;
154168 }
155- else if (!targetFound && interupted) {
169+ else if (!targetFound && interupted)
170+ {
156171 interupted = false ;
157172 has_control = false ;
158173 return true ;
159174 }
160- else {
175+ else
176+ {
161177 return false ;
162178 }
163179}
164180
165- Result PickUpController::DoWork () {
181+ Result PickUpController::DoWork ()
182+ {
166183
167184 has_control = true ;
168185
169- if (!targetHeld) {
186+ if (!targetHeld)
187+ {
170188 // threshold distance to be from the target block before attempting pickup
171189 float targetDistance = 0.15 ; // meters
172190
173-
174191 // -----------------------------------------------------------
175192 // millisecond time = current time if not in a counting state
176193 // when timeOut is true, we are counting towards a time out
@@ -208,13 +225,27 @@ Result PickUpController::DoWork() {
208225 // If we don't see any blocks or cubes turn towards the location of the last cube we saw.
209226 // I.E., try to re-aquire the last cube we saw.
210227
211- float grasp_time_begin = 1.7 ;
212- float raise_time_begin = 2.5 ;
228+ float grasp_time_begin = 1.1 ;
229+ float raise_time_begin = 2.0 ;
213230 float lower_gripper_time_begin = 4.0 ;
214231 float target_reaquire_begin= 4.2 ;
215232 float target_pickup_task_time_limit = 4.8 ;
216233
217234 // cout << "blockDistance DOWORK: " << blockDistance << endl;
235+
236+ // Calculate time difference between last seen tag
237+ float target_timeout = (current_time - target_timer)/1e3 ;
238+
239+ // delay between the camera refresh and rover runtime is 6/10's of a second
240+ float target_timeout_limit = 0.61 ;
241+
242+ // Timer to deal with delay in refresh from camera and the runtime of rover code
243+ if ( target_timeout >= target_timeout_limit )
244+ {
245+ // Has to be set back to 0
246+ nTargetsSeen = 0 ;
247+ }
248+
218249
219250 if (nTargetsSeen == 0 && !lockTarget)
220251 {
@@ -223,7 +254,7 @@ Result PickUpController::DoWork() {
223254 {
224255 result.pd .cmdVel = 0.0 ;
225256 result.pd .cmdAngularError = 0.0 ;
226- result.wristAngle = 0.8 ;
257+ result.wristAngle = 1.25 ;
227258 // result.fingerAngle does not need to be set here
228259
229260 // We are getting ready to start the pre-programmed pickup routine now! Maybe? <(^_^)/"
@@ -252,7 +283,7 @@ Result PickUpController::DoWork() {
252283 result.pd .cmdVel = vel;
253284 result.pd .cmdAngularError = -blockYawError;
254285 timeOut = false ;
255- nTargetsSeen = 0 ;
286+
256287 return result;
257288 }
258289 else if (!lockTarget) // if a target hasn't been locked lock it and enter a counting state while slowly driving forward.
@@ -279,11 +310,14 @@ Result PickUpController::DoWork() {
279310
280311
281312 // the magic numbers compared to Td must be in order from greater(top) to smaller(bottom) numbers
282- if (Td > target_reaquire_begin && timeOut) {
313+ if (Td > target_reaquire_begin && timeOut)
314+ {
283315 lockTarget = false ;
284316 ignoreCenterSonar = true ;
285317 }
286- else if (Td > lower_gripper_time_begin && timeOut) // if enough time has passed enter a recovery state to re-attempt a pickup
318+
319+ // if enough time has passed enter a recovery state to re-attempt a pickup
320+ else if (Td > lower_gripper_time_begin && timeOut)
287321 {
288322 result.pd .cmdVel = -0.15 ;
289323 result.pd .cmdAngularError = 0.0 ;
@@ -292,8 +326,8 @@ Result PickUpController::DoWork() {
292326 result.wristAngle = 0 ;
293327 }
294328
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
329+ // if no targets are found after too long a period go back to search pattern
330+ if (Td > target_pickup_task_time_limit && timeOut)
297331 {
298332 Reset ();
299333 interupted = true ;
@@ -306,7 +340,8 @@ Result PickUpController::DoWork() {
306340 return result;
307341}
308342
309- bool PickUpController::HasWork () {
343+ bool PickUpController::HasWork ()
344+ {
310345 return targetFound;
311346}
312347
0 commit comments