# Move TCP to Midpoint Between Two Contact Points

**URL:** <https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601>\
**Category:** General Application Help: How do I?\
**Created:** [April 14, 2022, 3:40pm UTC](https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601 "2022-04-14T15:40:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon507564](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@anon507564](https://forum.universal-robots.com/u/anon507564)\
**Post date:** [April 14, 2022, 3:40pm UTC](https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601/1 "2022-04-14T15:40:55Z")

</div>

I’m using a series of Direction / Until Contact nodes to find edges so I can more easily set waypoints.

I need to be able to move to a position that is centered between two points that I found using Direction / Until Contact.

So basically I want to:

```auto
Direction X+
  Until Contact
    Store Position1
Direction X-
  Until Contact
    Store Position2
Position3 = Position2 - Position1
Move Position3
Halt

```

How would I store Position1 and Position2 so I can derive Position3?

I’m going to use CASE selections for the user to pick which axis he wants to use (X, Y or Z) so once I get one axis figured out I will need to be able to replicate it in the other 2 axis.

UR10e running in Polyscope 5.10.xx

Thanks!

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [April 14, 2022, 10:04pm UTC](https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601/2 "2022-04-14T22:04:27Z")

</div>

Script:

```auto
def midpoint(first, second):
  mid = get_actual_tcp_pose()
  mid[0] = (first[0] + second[0]) / 2
  mid[1] = (first[1] + second[1]) / 2
  mid[2] = (first[2] + second[2]) / 2
  
  return mid
end

```

Add a script file into your before start sequence and type in/paste that into it. Then use an Assignment node to store the points after contact, and use another Assignment node to call the script and pass in the 2 stored points. You can use this newly assigned Midpoint just like any other waypoint, and just use a Variable waypoint and choose that one.

![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/7/762f8dcaef90fdab2a34a064f22116c0a963bfff.png)

I got a little ahead of myself actually and didn’t read your post all the way through. This will find the midpoint between any 2 poses. If you want to do only one axis at a time or something, feel free to modify the script.

---

<div class="post-metadata">

**Author:** ![anon507564](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@anon507564](https://forum.universal-robots.com/u/anon507564)\
**Post date:** [April 18, 2022, 1:42pm UTC](https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601/3 "2022-04-18T13:42:39Z")

</div>

Awesome, thanks AGAIN, Eric.

Finishing up runs on both robots so will by checking this out today.

:edit:

Added to one robot so far, works perfect. 👍

---

<div class="post-metadata">

**Author:** ![jarrah.l](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jarrah.l](https://forum.universal-robots.com/u/jarrah.l)\
**Post date:** [May 10, 2022, 6:03am UTC](https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601/4 "2022-05-10T06:03:43Z")

</div>

Hey @eric.feldmann,

Does this work on the CB series or is the ‘Until contact’ node only an e series feature.

Cheers,

Jarrah

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [May 10, 2022, 11:59am UTC](https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601/5 "2022-05-10T11:59:43Z")

</div>

I don’t know anything about the CB series unfortunately. We only have e-Series here. This can get the midpoint between any 2 points, it doesn’t have to be via Tool Contacts, if that makes a difference to you.

That said, here’s the script that gets generated by the e-series. Not sure if you have the ability to write your own script in CB or not:

```auto
def calculate_point_to_move_towards(feature, direction, position_distance):
    local posDir=[direction[0], direction[1], direction[2]]
    if (norm(posDir) < 1e-6):
      return get_target_waypoint()
    end
    local direction_vector_normalized=normalize(posDir)
    local displacement_pose=p[direction_vector_normalized[0] * position_distance,direction_vector_normalized[1] * position_distance,direction_vector_normalized[2] * position_distance,0,0,0]
    local wanted_displacement_in_base_frame=pose_sub(pose_trans(feature, displacement_pose), feature)
    return pose_add(get_target_waypoint(), wanted_displacement_in_base_frame)
  end
  while (True):
    $ 1 "Robot Program"
    $ 2 "MoveL"
    $ 3 "Direction: Base X+"
    global move_thread_flag_3=0
    thread move_thread_3():
      enter_critical
      move_thread_flag_3 = 1
      local towardsPos=calculate_point_to_move_towards(p[0.0,0.0,0.0,0.0,0.0,0.0], [1.0,0.0,0.0], 1000.0)
      movel(towardsPos, a=1.2, v=0.050799972568014815)
      move_thread_flag_3 = 2
      exit_critical
    end
    move_thread_flag_3 = 0
    move_thread_han_3 = run move_thread_3()
    while (True):
      local targetTcpDirection=get_target_tcp_speed()
      local stepsToRetract=tool_contact(direction=targetTcpDirection)
      if (stepsToRetract > 0):
        kill move_thread_han_3
        stopl(3.0)
        local backTrackMovement=get_actual_joint_positions_history(stepsToRetract)
        local contactPose=get_forward_kin(backTrackMovement)
        local posDir=[targetTcpDirection[0],targetTcpDirection[1],targetTcpDirection[2]]
        local retractTo=contactPose
        if (norm(posDir) > 1e-6):
          local normalizedPosDir=normalize(posDir)
          local additionalRetraction=p[normalizedPosDir[0] * 0.0, normalizedPosDir[1] * 0.0, normalizedPosDir[2] * 0.0, 0, 0, 0]
          retractTo = pose_sub(contactPose, additionalRetraction)
        end
        movel(retractTo, a=3.0, v=0.1)
        $ 4 "Until (tool_contact_detection)"
        break
      end
      sync()
    end
  end

```

---

<div class="post-metadata">

**Author:** ![jarrah.l](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jarrah.l](https://forum.universal-robots.com/u/jarrah.l)\
**Post date:** [May 10, 2022, 10:05pm UTC](https://forum.universal-robots.com/t/move-tcp-to-midpoint-between-two-contact-points/20601/6 "2022-05-10T22:05:15Z")

</div>

No worries thanks Eric.

I might play around with it in the URSim when i need it and see what i come up with.

Cheers,

Jarrah
