htc 抓取 释放、瞬移完整代码

htc 抓取 释放、瞬移完整代码
晋中职业技术学院 智祥明 QQ 1064270685
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class LaserPointer : MonoBehaviour {

public SteamVR_Behaviour_Pose pose_Laser;

public SteamVR_Action_Boolean teleport_Laser = SteamVR_Input.GetBooleanAction("Teleport");
public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction("InteractUI");

public GameObject laserPrefab;
private GameObject laser;
private Transform laserTransform;
private Vector3 hitPoint;

public Transform cameraRigTransform;
public GameObject teleportReticlePrefab;
private GameObject reticle;
private Transform teleportReticleTransform;
public Transform headTransform;
public Vector3 teleportReticleOffset;
public LayerMask teleportMask;
private bool shouldTeleport;

FixedJoint joint;

private GameObject collidingObject;
private GameObject objectInHand;

private void SetColldingObject(Collider col)
{
    if(collidingObject || !col.GetComponent <Rigidbody >())
    {
        
        return;
    }
    
    collidingObject = col.gameObject;
    
}

public  void OnTriggerEnter(Collider other)
{
    SetColldingObject(other);
}

public  void OnTriggerStay(Collider other)
{
    SetColldingObject(other);
   
    

}
public  void OnTriggerExit(Collider other)
{
    if (!collidingObject)
        return;
    collidingObject = null;
}

private void GrabObject()
{
    if (collidingObject)
    {
        objectInHand = collidingObject;
        collidingObject = null;
        print(objectInHand);
        joint = AddFixedJoint();
        joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
    }

}
private FixedJoint AddFixedJoint()
{
    joint = gameObject.AddComponent<FixedJoint>();
    
    joint.breakForce = 20000;
    joint.breakTorque = 20000;
    return joint;
}


private void ShowLaser(RaycastHit hit)
{
    laser.GetComponent<MeshRenderer>().enabled = true;
    laserTransform.position = Vector3.Lerp(pose_Laser.transform.position, hitPoint, 0.5f);
    laserTransform.LookAt(hitPoint);
    laserTransform.localScale = new Vector3(laserTransform.localScale.x, laserTransform.localScale.y, hit.distance);
}

// Use this for initialization
void Start () {
    laser = Instantiate(laserPrefab);
    laserTransform = laser.transform;
    reticle = Instantiate(teleportReticlePrefab);
    teleportReticleTransform = reticle.transform;
}

// Update is called once per frame
void Update () {

    if (teleport_Laser.GetStateDown(pose_Laser.inputSource))
    {

        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 100, teleportMask))
        {
            hitPoint = hit.point;
            ShowLaser(hit);
            reticle.SetActive(true);
            teleportReticleTransform.position = hitPoint + teleportReticleOffset;
            shouldTeleport = true;
        }
       
    }
    else
    {
        laser.GetComponent<MeshRenderer>().enabled = false;
        reticle.SetActive(false);
        }
    if (teleport_Laser.GetStateUp(pose_Laser.inputSource) && shouldTeleport)
    {
        Teleport();
    }
    if (teleport.GetStateDown(pose_Laser.inputSource))
        {


            GrabObject();

        }
    if (teleport.GetStateUp(pose_Laser.inputSource))
    {
        if (objectInHand)
        {
            ReleaseObject();
        }
    }


}

private void Teleport()
{
    shouldTeleport = false;
    reticle.SetActive(false);
    Vector3 difference = cameraRigTransform.position - headTransform.position;
    difference.y = 0;
    cameraRigTransform.position = hitPoint + difference;
}
private void ReleaseObject()
{
    // 1
    if (gameObject .GetComponent<FixedJoint>())
    {
        // 2
        gameObject .GetComponent<FixedJoint>().connectedBody = null;//将FixedJiont(固定关节)组件的链接刚体属性置为空


        Object.DestroyImmediate(joint);//销毁FixedJiont(固定关节)组件
        // 3
        objectInHand.GetComponent<Rigidbody>().velocity = pose_Laser .GetVelocity();
        objectInHand.GetComponent<Rigidbody>().angularVelocity = pose_Laser .GetAngularVelocity();
    }
    // 4
    objectInHand = null;
}

}

机械臂抓取的视觉Python代码通常涉及计算机视觉、机器学习和机器人控制技术。这里提供一个简化版的示例,展示了如何用OpenCV处理图像并结合简单的目标检测(比如颜色或边缘检测)来指导机械臂抓取。请注意,实际应用可能需要更复杂的深度学习模型(如YOLO、TensorFlow Object Detection等)。 ```python # 导入所需的库 import cv2 import numpy as np from pypot import Robot, Move # 初始化机械臂 robot = Robot('your_robot_model') # 定义目标颜色或阈值 target_color = (0, 255, 0) # 假设绿色作为目标 def detect_object(frame): # 将帧转换为HSV空间,便于颜色查找 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 设置颜色范围(在这里假设是绿叶的颜色) lower_green = np.array([36, 43, 46]) upper_green = np.array([79, 255, 255]) # 进行颜色过滤 mask = cv2.inRange(hsv, lower_green, upper_green) # 查找轮廓 contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contours: # 取第一个轮廓(如果有多个,这里是简化版本,只抓取第一个) contour = contours x, y, w, h = cv2.boundingRect(contour) return (x, y) # 返回目标位置坐标 def main(): cap = cv2.VideoCapture(0) # 打开摄像头 while True: _, frame = cap.read() target_pos = detect_object(frame) if target_pos is not None: # 使用找到的位置移动机械臂 robot.arm_move(target_pos, target_pos) # 这里假设机械臂有arm_move方法 cv2.imshow("Frame", frame) key = cv2.waitKey(1) if key == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": main() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学则路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值