Skip to content

Commit

Permalink
Updates for 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
adeguet1 committed Jan 2, 2024
1 parent d2d0243 commit 1380fc3
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 25 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Change log
==========


1.2.0 (2024-01-02)
==================

* API changes:
* In JSON configuration file, `data` has been replaced by `read-commands`
* Deprecated features:
* `.rosintall` has been removed, migrated to `vcs`
* New features:
* Added support for void and write events
* Added void and write commands
* Added snippet of code for C#/Unity
* Bug fixes:
* Fixed CMake to use new cisst CMake macros, compiles with catkin and colcon

1.1.0 (2021-04-08)
==================

Expand Down
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#
# (C) Copyright 2019-2022 Johns Hopkins University (JHU), All Rights
# Reserved.
# (C) Copyright 2019-2024 Johns Hopkins University (JHU), All Rights Reserved.
#
# --- begin cisst license - do not edit ---
#
Expand All @@ -11,7 +10,7 @@
# --- end cisst license ---

cmake_minimum_required(VERSION 3.10)
project (sawSocketStreamerAll VERSION 1.1.0)
project (sawSocketStreamerAll VERSION 1.2.0)

find_package (cisst REQUIRED)
include (${CISST_USE_FILE})
Expand All @@ -23,3 +22,5 @@ add_subdirectory (components)

include (CPack)
cpack_add_component (sawSocketStreamer)
cpack_add_component (sawSocketStreamer-dev
DEPENDS sawSocketStreamer)
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Each component created need a configuration file that specifies which read comma
}
]
,
"void-commands": ["Freeze"]
"void-commands": ["hold", "free"]
,
"write-commands": [
{
Expand Down Expand Up @@ -155,3 +155,36 @@ commands, send an empty string (`""`). You can find an example in
Python in the sawIntuitiveResearchKit repository under
`share/socket-streamer`:
https://github.com/jhu-dvrk/sawIntuitiveResearchKit/blob/devel/share/socket-streamer/example.py

## Using the socket streamer with Unity

This is based on code that can be found in the `share` directory.
This code was originally written by An Chi Chen and Muhammad Hadi when
they were students at Johns Hopkins. Their goal was to visualize and
control a dVRK PSM from Unity.

The main parts are setting up the socket in the ``start()``:
```csharp
// udp using socket
data = new byte[1024];
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 48051); // ensure that this port is the same as the one youre sending data from
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(ip);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
remote = (EndPoint)(sender);
```

And then you can read from the socket using:
```csharp
// read in message from dVRK
data = new byte[1024];
socket.ReceiveFrom(data, ref remote);
dVRK_msg = Encoding.UTF8.GetString(data); // dVRK_msg will then contain the data
```

Finally an example of how you would send data:
```csharp
// send json strings to dVRK
send_msg = Encoding.UTF8.GetBytes(pose_message); // pose_message is string to send
socket.SendTo(send_msg, remote);
```
13 changes: 4 additions & 9 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# (C) Copyright 2019-2023 Johns Hopkins University (JHU), All Rights Reserved.
# (C) Copyright 2019-2024 Johns Hopkins University (JHU), All Rights Reserved.
#
# --- begin cisst license - do not edit ---
#
Expand All @@ -10,11 +10,7 @@
# --- end cisst license ---

cmake_minimum_required (VERSION 3.10)
project (sawSocketStreamer VERSION 1.1.0)

set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
project (sawSocketStreamer VERSION 1.2.0)

# create a list of required cisst libraries
set (REQUIRED_CISST_LIBRARIES cisstCommon
Expand All @@ -23,7 +19,7 @@ set (REQUIRED_CISST_LIBRARIES cisstCommon
cisstMultiTask
cisstParameterTypes)

find_package (cisst 1.2.0 REQUIRED ${REQUIRED_CISST_LIBRARIES})
find_package (cisst 1.2.1 REQUIRED ${REQUIRED_CISST_LIBRARIES})

if (cisst_FOUND_AS_REQUIRED)

Expand Down Expand Up @@ -61,8 +57,7 @@ if (cisst_FOUND_AS_REQUIRED)
"${sawSocketStreamer_SOURCE_DIR}/include/sawSocketStreamer"
"${sawSocketStreamer_BINARY_DIR}/include/sawSocketStreamer"
DESTINATION include
COMPONENT sawSocketStreamer-dev
PATTERN .svn EXCLUDE)
COMPONENT sawSocketStreamer-dev)

install (TARGETS sawSocketStreamer COMPONENT sawSocketStreamer
RUNTIME DESTINATION bin
Expand Down
12 changes: 0 additions & 12 deletions saw_socket_streamer.rosinstall

This file was deleted.

255 changes: 255 additions & 0 deletions share/UDPComm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
// script to communicate with dVRK using UDP


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;



public class UDPComm : MonoBehaviour
{

// for udp socket connection
public static byte[] data;
public static Socket socket;
public static EndPoint remote;
public static byte[] send_msg;
public static string pose_message;
public static string jaw_message;
public static string dVRK_msg;
public static bool jaw_match;
public static int read_msg_count = 0;

public static float jaw_angle; // read in jaw angle from dVRK
public static Quaternion EE_quat;
public static Vector3 EE_pos; // EE pos from dVRK
bool ReaddVRKmsg = false;
int readcounter = 0;
float timer;
int filename = 0;
float delta_timer_holo;
float timer_holo;

public GameObject hololens;
public TextMesh read; // text for recording

bool pause = false;
string file_name_dVRK;
string file_name_holo;

public GameObject straight_wire;
public GameObject s_wire;
public GameObject angled_wire;

// Start is called before the first frame update
void Start()
{
// udp using socket
data = new byte[1024];
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 48051);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(ip);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
remote = (EndPoint)(sender);
//Debug.Log("start again");
}

// Update is called once per frame
void Update()
{
if (Main.MTM == true)
{
Debug.Log("Main.MTM");
if (straight_wire.activeSelf)
{
file_name_dVRK = filename + "_MTM_Omni_straight";
file_name_holo = filename + "_MTM_Omni_straight";
}

if(s_wire.activeSelf)
{
file_name_dVRK = filename + "_MTM_Omni_curved";
file_name_holo = filename + "_MTM_Omni_curved";
}

if (angled_wire.activeSelf)
{
file_name_dVRK = filename + "_MTM_Omni_angled";
file_name_holo = filename + "_MTM_Omni_angled";
}

}
else
{
if (straight_wire.activeSelf)
{
file_name_dVRK = filename + "_Hand_straight";
file_name_holo = filename + "_Hand_straight";
}

else if (s_wire.activeSelf)
{
file_name_dVRK = filename + "_Hand_curved";
file_name_holo = filename + "_Hand_curved";
}

else if (angled_wire.activeSelf)
{
file_name_dVRK = filename + "_Hand_angled";
file_name_holo = filename + "_Hand_angled";
}
else
{

file_name_dVRK = filename + "_Hand_drawn";
file_name_holo = filename + "_Hand_drawn";

}




}

string incoming_pose = Path.Combine(Application.persistentDataPath, "dVRKpose_" + file_name_dVRK + ".txt");
//string incoming_jaw = Path.Combine(Application.persistentDataPath, "dVRKjaw_" + filename + ".txt");
string hololens_transform_pinch = Path.Combine(Application.persistentDataPath, "hololens_" + file_name_holo + ".txt");

read_msg_count = 0;

while (read_msg_count < 2)
{
// read in message from dVRK
data = new byte[1024];
socket.ReceiveFrom(data, ref remote);
dVRK_msg = Encoding.UTF8.GetString(data);
// if jaw message
if (parser.StringMatch(dVRK_msg, "\"jaw/setpoint_js\":"))
{
// extract jaw angle
jaw_angle = parser.GetJawAngle(dVRK_msg);

// reocord data
//if (ReaddVRKmsg)
//{
// using (StreamWriter writer = new StreamWriter(incoming_jaw, true))
// {

// timer += Time.deltaTime;
// string jaw = "\n " + timer + " " + jaw_angle.ToString("R");
// writer.WriteLine(jaw);

// }
//}
}

// if pose message
else if (parser.StringMatch(dVRK_msg, "\"setpoint_cp\":"))
{
// extract rot and pos
EE_quat = QuaternionFromMatrix(parser.GetMatrix4X4(dVRK_msg));
Matrix4x4 temp = parser.GetMatrix4X4(dVRK_msg);
//Debug.Log("dVRK rot: " + temp.rotation);
EE_pos = parser.GetPos(dVRK_msg);

// record data
if (ReaddVRKmsg)
{
Debug.Log(pause);
if (!pause)
{
read.text = "Recording";
using (StreamWriter writer = new StreamWriter(incoming_pose, true))
{
timer += Time.deltaTime;
string pose = "\n " + timer + " " + EE_pos[0].ToString("R") + " " + EE_pos[1].ToString("R") + " " + EE_pos[2].ToString("R") + " " + EE_quat[0].ToString("R") + " " + EE_quat[1].ToString("R") + " " + EE_quat[2].ToString("R") + " " + EE_quat[3].ToString("R");
writer.WriteLine(pose);

}

using (StreamWriter writer = new StreamWriter(hololens_transform_pinch, true))
{
delta_timer_holo += Time.deltaTime;
timer_holo = Time.unscaledDeltaTime;
string holo_transform = "\n" + timer_holo + " " + timer + " " + hololens.transform.position[0].ToString("R") + " " + hololens.transform.position[1].ToString("R") + " " + hololens.transform.position[2].ToString("R") + " " + hololens.transform.rotation[0].ToString("R") + " " + hololens.transform.rotation[1].ToString("R") + " " + hololens.transform.rotation[2].ToString("R") + " " + hololens.transform.rotation[3].ToString("R");
writer.WriteLine(holo_transform);
}
}
else
{
Debug.Log("paused");
read.text = "Recording paused";
}
}
else
{
read.text = "Recording complete";
}
}

// if neither, probably restarted
else
{
HandTrack.new_EE_pos = Vector3.zero;
}

read_msg_count += 1;
}
}


// get quaternion from homogeneous matrix
public static Quaternion QuaternionFromMatrix(Matrix4x4 m)
{
return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1));
}

public void ReaddVRK()
{
readcounter++;
if (readcounter > 1)
{
ReaddVRKmsg = false;
Debug.Log("stop Reading dVRK");
readcounter = 0;

//read.text = "Recording complete";

read.GetComponent<Renderer>().enabled = true;

}
else
{

ReaddVRKmsg = true;
Debug.Log("Reading dVRK");
readcounter++;
filename++;
//read.text = "Recording";

read.GetComponent<Renderer>().enabled = true;
}
}

// sends pose and jaw messages to dVRK over UDP connection
public void UDPsend(string pose_message, string jaw_message)
{
// send json strings to dVRK //
send_msg = Encoding.UTF8.GetBytes(pose_message);
socket.SendTo(send_msg, remote);
send_msg = Encoding.UTF8.GetBytes(jaw_message);
socket.SendTo(send_msg, remote);
}

public void PauseRecord()
{
pause = !pause;
}

}
Loading

0 comments on commit 1380fc3

Please sign in to comment.