Combination of contact and contactless detection to avoid tool contact with workpiece