# Malfunction of Distance function

**URL:** <https://tech-community.robotics.abb.com/t/malfunction-of-distance-function/8430>\
**Category:** RobotStudio\
**Created:** [August 28, 2018, 9:47am UTC](https://tech-community.robotics.abb.com/t/malfunction-of-distance-function/8430 "2018-08-28T09:47:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrB](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/petrb/32/248_2.png) [@PetrB](https://tech-community.robotics.abb.com/u/PetrB)\
**Post date:** [August 28, 2018, 9:47am UTC](https://tech-community.robotics.abb.com/t/malfunction-of-distance-function/8430/1 "2018-08-28T09:47:57Z")

</div>

Hello,

I’m using function _Distance_ to detect current position position of TCP for decision which path to take.

 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/1X/78e7d412d3ce157bc53586d7a8eaa43b5ff3842b.png)  
 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/1X/9e0dd3acb0de890731c9741736be8d48758b59a5.png)

There is Task running in background (type Static), which switching DO bits on/off based on current position. There are around 50 positions.

When PLC issues command _Go to home,_ based on DO states or distance from certain position, controller use correct path to get to home position. Those conditions are in main motion task.

 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/2X/0/0abe2eaffd96b2c95896237b91930c7e814f8fa4.png)

This logic was working fine for few weeks, until today. Basically robot took a wrong path to home and would hit the stand, if I wouldn’t stop it. I went through logic numerous times, but can’t find any bug in my code and taken in account, that logic was working flawlessly for weeks, I don’t think it’s bug in my coding.

There are two options what happen. First is that controller set wrong DO bit or result of _Distance_ function returned incorrect number. When issue happened, TCP was about 1000mm away from indicated position.

Any ideas what could be wrong?

Thank you

---

<div class="post-metadata">

**Author:** ![lemster68](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/lemster68/32/1945_2.png) [@lemster68](https://tech-community.robotics.abb.com/u/lemster68)\
**Post date:** [August 28, 2018, 1:59pm UTC](https://tech-community.robotics.abb.com/t/malfunction-of-distance-function/8430/2 "2018-08-28T13:59:31Z")

</div>

I would be a bit wary of the background task setting bits like so, especially if the robot is moving quickly. Could be a lag in the bits changing, and where in your code do you turn off bits that were previously on but no longer valid? I am assuming that you have more ELSEIF for other positions. I wrote this code some time ago which could be used for a recovery to home function.

MODULE Utility  
VAR string stRobt:=stEmpty;  
VAR string stClosestPoint;  
CONST num nMaxRecoverDist:=2000;  
PERS tooldata tCheck;  
PERS string stToolName;  
TASK PERS wobjdata wobjCheck:=[FALSE,TRUE,“”,[[0,0,0],[1,0,0,0]],[[0,0,0],[1,0,0,0]]];  
PERS string stWobjName:=“wobj0”;

PROC FindNearestPoint()  
VAR robtarget pCurrentPosition;  
VAR num n12Distance:=10000;  
VAR robtarget checkrobt;  
VAR datapos block;

GetSysData tCheck\ObjectName:=stToolName;  
GetSysData wobjCheck\ObjectName:=stWobjName;  
pCurrentPosition:=CRobT(\Tool:=tCheck\WObj:=wobjCheck);  
SetDataSearch “robtarget”\InMod:=“R1\_Data”;  
WHILE GetNextSym(stRobt,block\Recursive) DO  
GetDataVal stRobt\TaskName:=“T\_ROB1”, checkrobt;  
! Compare current robot position with stored robtarget  
! If it is less than maximum recovery distance store the name  
! and distance from current position.  
IF (Distance(checkrobt.trans,pCurrentPosition.trans) \< nMaxRecoverDist) THEN  
! Check to see if point is within Maximum recovery  
! but maybe farther than last compared point.  
! Should update n12Distance if closer point is found.  
IF Distance(checkrobt.trans,pCurrentPosition.trans) \< n12Distance THEN  
n12Distance:=Distance(checkrobt.trans,pCurrentPosition.trans);  
stClosestPoint:=stRobt;  
ELSE  
! Do nothing, point is farther from last found point  
! within max recover distance nMaxRecoverDist.  
ENDIF  
ENDIF  
ENDWHILE  
IF n12Distance \>= nMaxRecoverDist THEN  
TPWrite “Nearest taught point is Not Found!!! Caution!!!”;  
TPWrite “Not close enough to a known taught point!!!”;  
TPWrite "Active tool used to check was " + stToolName;  
TPWrite "Active work object used to check was " + stWobjName;  
ELSE  
TPWrite "Nearest taught point is: " + stClosestPoint;  
TPWrite "Distance from current position is: "\Num:=n12Distance;  
TPWrite "Active tool used to check was " + stToolName;  
TPWrite "Active work object used to check was " + stWobjName;  
ENDIF  
ENDPROC

ENDMODULE

---

<div class="post-metadata">

**Author:** ![PetrB](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/petrb/32/248_2.png) [@PetrB](https://tech-community.robotics.abb.com/u/PetrB)\
**Post date:** [August 28, 2018, 7:33pm UTC](https://tech-community.robotics.abb.com/t/malfunction-of-distance-function/8430/3 "2018-08-28T19:33:22Z")

</div>

Lag would make sense, if robot took closer to TCP. But as I said, Robot took path, which was 1000mm from TCP, so that bit should be off for seconds.

Bits are being turned off right in same IF function. Yes, there are about 50 more IF’s.  
 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/1X/9e0dd3acb0de890731c9741736be8d48758b59a5.png)

Thanks for example. I’m operating robot inside steel box and there very limited space for maneuvering, so basically if robot is between two defined positions, he cannot recover, cause from every point there is complex path how to get to home position.

---

<div class="post-metadata">

**Author:** ![PetrB](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/petrb/32/248_2.png) [@PetrB](https://tech-community.robotics.abb.com/u/PetrB)\
**Post date:** [August 29, 2018, 3:24am UTC](https://tech-community.robotics.abb.com/t/malfunction-of-distance-function/8430/4 "2018-08-29T03:24:27Z")

</div>

I probably figure out the problem.

I have multi move station with 2 robots. According to manual, for CRobT function there is argument [\TaskRef], to identify the robot. I made two separate function with [\TaskRef] refering to right robot (T\_ROB1Id and T\_ROB2Id).

Function with all the parameters, if any one need it in future:

_PERS num nDist\_R1;_  
_PERS robtarget pCurrentPosition\_R1;_

_FUNC num nDistance\_R1(_  
_robtarget pEnd\_R1,_  
_PERS wobjdata WObj\_R1)_

_pCurrentPosition\_R1:=CRobT(\TaskRef:=T\_Rob1Id\Tool:=ToxTool\_R1\WObj:=WObj\_R1);_  
_nDist\_R1:=Distance(pCurrentPosition\_R1.trans,pEnd\_R1.trans);_

_Return nDist\_R1;_

---

<div class="post-metadata">

**Author:** ![lemster68](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/lemster68/32/1945_2.png) [@lemster68](https://tech-community.robotics.abb.com/u/lemster68)\
**Post date:** [August 29, 2018, 12:03pm UTC](https://tech-community.robotics.abb.com/t/malfunction-of-distance-function/8430/5 "2018-08-29T12:03:16Z")

</div>

I see that you are turning the bits off, wasn’t paying attention closely enough. ☹
