Skip to content

Instantly share code, notes, and snippets.

@abhayraw1
Last active May 31, 2020 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhayraw1/6eb565ff582b228573c61b64d8f50964 to your computer and use it in GitHub Desktop.
Save abhayraw1/6eb565ff582b228573c61b64d8f50964 to your computer and use it in GitHub Desktop.
TeleOperation (using mouse) Plugin for RosSharp. Add to the RosConnector Object. Inputs are the topic name, camera, a prefab for pointer and the base link of the robot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShowMousePosition : MonoBehaviour
{
public GameObject mousePointer;
public bool clipToInteger;
public Camera cam;
private bool active = true;
// Start is called before the first frame update
void Start()
{
mousePointer = Instantiate(mousePointer, new Vector3(), Quaternion.identity);
}
// Update is called once per frame
public void Update()
{
if(active) {
mousePointer.transform.position = getPointerPosition();
}
}
public Vector3 getPointerPosition()
{
if (clipToInteger)
return snapPosition(getWorldPoint());
else
return getWorldPoint();
}
public Vector3 getWorldPoint()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
return hit.point;
}
return Vector3.zero;
}
public Vector3 snapPosition(Vector3 original)
{
Vector3 snapped;
snapped.x = Mathf.Floor(original.x + 0.5f);
snapped.y = Mathf.Floor(original.y + 0.5f);
snapped.z = Mathf.Floor(original.z + 0.5f);
return snapped;
}
public void Deactivate()
{
active = false;
HidePointer();
}
public void Activate()
{
active = true;
ShowPointer();
}
private void ShowPointer()
{
mousePointer.active = true;
}
private void HidePointer()
{
mousePointer.active = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RosSharp.RosBridgeClient
{
public class TeleOperator : UnityPublisher<MessageTypes.Geometry.Twist>
{
public Camera cam;
public GameObject mousePointer;
public Transform baseLink;
private ShowMousePosition mousePosition;
private MessageTypes.Geometry.Twist message;
private bool active;
public void Deactivate()
{
active = false;
mousePosition.Deactivate();
}
public void Activate()
{
active = true;
mousePosition.Activate();
}
// Start is called before the first frame update
protected override void Start()
{
base.Start();
InitializeMessage();
mousePosition = cam.gameObject.AddComponent<ShowMousePosition>();
mousePosition.cam = cam;
mousePosition.mousePointer = mousePointer;
Deactivate();
}
public void Update()
{
if (Input.GetMouseButtonDown(0)) {
Activate();
}
if (Input.GetMouseButtonUp(0)) {
Deactivate();
}
}
// Update is called once per frame
private void FixedUpdate()
{
if (active) {
UpdateMessage();
} else {
message.linear = GetGeometryVector3(new Vector3());
message.angular = GetGeometryVector3(new Vector3());
Publish(message);
}
}
private void UpdateMessage()
{
Vector3 currPose = baseLink.position.Unity2Ros();
Vector3 localGoalPose = baseLink.InverseTransformPoint(
mousePosition.getPointerPosition()
).Unity2Ros();
localGoalPose[2] = 0;
float distance = localGoalPose.sqrMagnitude;
float linearVel = Mathf.Clamp(0.15f * distance, -0.1f, 0.3f);
float angularVel = Mathf.Clamp(
Mathf.Atan2(localGoalPose[1], localGoalPose[0])*Mathf.Rad2Deg,
-45, 45
);
message.linear = GetGeometryVector3(new Vector3(linearVel, 0, 0));
message.angular = GetGeometryVector3(new Vector3(0, 0, angularVel));
Publish(message);
}
private void InitializeMessage()
{
message = new MessageTypes.Geometry.Twist();
message.linear = new MessageTypes.Geometry.Vector3();
message.angular = new MessageTypes.Geometry.Vector3();
}
private static MessageTypes.Geometry.Vector3 GetGeometryVector3(Vector3 vector3)
{
MessageTypes.Geometry.Vector3 geometryVector3 = new MessageTypes.Geometry.Vector3();
geometryVector3.x = vector3.x;
geometryVector3.y = vector3.y;
geometryVector3.z = vector3.z;
return geometryVector3;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment