Skip to content

Commit

Permalink
Merge pull request #49 from ChristopherRemde/Azure_Kinect_Temp_Sync
Browse files Browse the repository at this point in the history
Added Temporal Synchronisation
  • Loading branch information
MarekKowalski authored Mar 6, 2021
2 parents 13e634f + 6fed026 commit 7d56c52
Show file tree
Hide file tree
Showing 17 changed files with 1,248 additions and 251 deletions.
2 changes: 0 additions & 2 deletions LiveScanPlayer/FrameFileReaderBin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public void ReadFrame(List<float> vertices, List<byte> colors)
lineParts = ReadLine().Split(' ');
int frameTimestamp = Int32.Parse(lineParts[1]);

Console.WriteLine((frameTimestamp / 1000).ToString());

short[] tempVertices = new short[3 * nPoints];
byte[] tempColors = new byte[4 * nPoints];

Expand Down
211 changes: 211 additions & 0 deletions LiveScanServer/KinectServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Net.Sockets;
using System.Net;
using System.ComponentModel;
using System.Windows.Forms;

namespace KinectServer
{
Expand All @@ -31,8 +32,15 @@ public class KinectServer
Socket oServerSocket;

bool bServerRunning = false;
bool bWaitForSubToStart = false;

//This lock prevents the user from enabeling/disabling the Temp Sync State while the cameras are in transition to another state.
//When starting the server, all devices are already initialized, as the LiveScanClient can only connect with an initialized device
bool allDevicesInitialized = true;

KinectSettings oSettings;
SettingsForm fSettingsForm;
MainWindowForm fMainWindowForm;
object oClientSocketLock = new object();
object oFrameRequestLock = new object();

Expand Down Expand Up @@ -132,6 +140,21 @@ public KinectServer(KinectSettings settings)
this.oSettings = settings;
}

public void SetSettingsForm(SettingsForm settings)
{
fSettingsForm = settings;
}

public void SetMainWindowForm(MainWindowForm main)
{
fMainWindowForm = main;
}

public SettingsForm GetSettingsForm()
{
return fSettingsForm;
}

private void SocketListChanged()
{
if (eSocketListChanged != null)
Expand Down Expand Up @@ -225,13 +248,178 @@ public void SendCalibrationData()
{
lock (oClientSocketLock)
{

for (int i = 0; i < lClientSockets.Count; i++)
{
lClientSockets[i].SendCalibrationData();
}
}
}

/// <summary>
/// Request the device Sync state, so that we know which Device is Master and which are Subordinates before starting them
/// </summary>
public void RequestDeviceSyncState()
{
lock (oClientSocketLock)
{
for (int i = 0; i < lClientSockets.Count; i++)
{
lClientSockets[i].RequestTempSyncState();
}
}
}

/// <summary>
/// When a client has send its Device Sync State, we try to Set the Client Sync State
/// </summary>
public void SendTemporalSyncData()
{
lock (oClientSocketLock)
{
int masterCount = 0;
int subordinateCount = 0;

//First we check if we have recieved the Device Sync State of all Devices
//If not, we return and the next client who confirms their state starts this function again
for (int i = 0; i < lClientSockets.Count; i++)
{
if(lClientSockets[i].currentDeviceTempSyncState == KinectSocket.eTempSyncConfig.MASTER)
{
masterCount++;
}

if (lClientSockets[i].currentDeviceTempSyncState == KinectSocket.eTempSyncConfig.SUBORDINATE)
{
subordinateCount++;
}

if (lClientSockets[i].currentDeviceTempSyncState == KinectSocket.eTempSyncConfig.UNKNOWN)
{
return;
}
}

//On a second step we check if we have exactly one master and at least one subordinate

if(masterCount != 1 || subordinateCount < 1)
{
//If not, we show a error message and disable the temporal sync

fMainWindowForm?.SetStatusBarOnTimer("Temporal Sync cables not connected properly", 5000);
fSettingsForm?.ActivateTempSyncEnableButton();
return;
}


allDevicesInitialized = false;

byte syncOffSetCounter = 0;

for (int i = 0; i < lClientSockets.Count; i++)
{
if(lClientSockets[i].currentDeviceTempSyncState == KinectSocket.eTempSyncConfig.SUBORDINATE)
{
syncOffSetCounter++;
}

lClientSockets[i].SendTemporalSyncStatus(true, syncOffSetCounter);

}

bWaitForSubToStart = true;
}
}


/// <summary>
/// Sets all clients as standalone
/// </summary>
public void DisableTemporalSync()
{
allDevicesInitialized = false;

lock (oClientSocketLock)
{
for (int i = 0; i < lClientSockets.Count; i++)
{
lClientSockets[i].SendTemporalSyncStatus(false, 0);
}
}
}


public void ConfirmTemporalSyncDisabled()
{
if (bWaitForSubToStart)
return;

lock (oClientSocketLock)
{
for (int i = 0; i < lClientSockets.Count; i++)
{
if (lClientSockets[i].currentClientTempSyncState != KinectSocket.eTempSyncConfig.STANDALONE)
{
return;
}
}
}

allDevicesInitialized = true;
}

/// <summary>
/// Called when a sub client has started. This checks if all sub clients have already started. If yes, we initialize the master
/// </summary>
public void CheckForMasterStart()
{
if (!bWaitForSubToStart)
{
return;
}

bool allSubsStarted = true;

lock (oClientSocketLock)
{
for (int i = 0; i < lClientSockets.Count; i++)
{
if (!lClientSockets[i].bSubStarted && lClientSockets[i].currentClientTempSyncState == KinectSocket.eTempSyncConfig.SUBORDINATE)
{
allSubsStarted = false;
break;
}
}

bWaitForSubToStart = false;

if (allSubsStarted)
{
for (int i = 0; i < lClientSockets.Count; i++)
{
if(lClientSockets[i].currentDeviceTempSyncState == KinectSocket.eTempSyncConfig.MASTER)
{
lClientSockets[i].SendMasterInitialize();
return;
}
}
}
}
}

/// <summary>
/// Tells the server that it is now ok to start recieving user changes again
/// </summary>
public void MasterSuccessfullyRestarted()
{
allDevicesInitialized = true;
}

public bool GetAllDevicesInitialized()
{
return allDevicesInitialized;
}

public bool GetStoredFrame(List<List<byte>> lFramesRGB, List<List<Single>> lFramesVerts)
{
bool bNoMoreStoredFrames;
Expand Down Expand Up @@ -303,6 +491,7 @@ public void GetLatestFrame(List<List<byte>> lFramesRGB, List<List<Single>> lFram

//Wait till frames received
bool allGathered = false;

while (!allGathered)
{
allGathered = true;
Expand All @@ -318,6 +507,7 @@ public void GetLatestFrame(List<List<byte>> lFramesRGB, List<List<Single>> lFram
}
}
}

}

//Store received frames
Expand All @@ -330,6 +520,7 @@ public void GetLatestFrame(List<List<byte>> lFramesRGB, List<List<Single>> lFram
lFramesBody.Add(new List<Body>(lClientSockets[i].lBodies));
}
}

}
}

Expand Down Expand Up @@ -360,6 +551,11 @@ private void ListeningWorker()
lClientSockets.Add(new KinectSocket(newClient));
lClientSockets[lClientSockets.Count - 1].SendSettings(oSettings);
lClientSockets[lClientSockets.Count - 1].eChanged += new SocketChangedHandler(SocketListChanged);
lClientSockets[lClientSockets.Count - 1].eSubInitialized += new SubOrdinateInitialized(CheckForMasterStart);
lClientSockets[lClientSockets.Count - 1].eMasterRestart += new MasterRestarted(MasterSuccessfullyRestarted);
lClientSockets[lClientSockets.Count - 1].eSyncJackstate += new RecievedSyncJackState(SendTemporalSyncData);
lClientSockets[lClientSockets.Count - 1].eStandAloneInitialized += new StandAloneInitialized(ConfirmTemporalSyncDisabled);

if (eSocketListChanged != null)
{
eSocketListChanged(lClientSockets);
Expand Down Expand Up @@ -436,6 +632,21 @@ private void ReceivingWorker()
lClientSockets[i].bLatestFrameReceived = true;
}

else if (buffer[0] == 4)
{
lClientSockets[i].ReceiveTemporalSyncStatus();
}

else if(buffer[0] == 5)
{
lClientSockets[i].RecieveMasterHasRestarted();
}

else if(buffer[0] == 6)
{
lClientSockets[i].RecieveDeviceSyncState();
}

buffer = lClientSockets[i].Receive(1);
}
}
Expand Down
14 changes: 14 additions & 0 deletions LiveScanServer/KinectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public class KinectSettings
public bool bMergeScansForSave = true;
public bool bSaveAsBinaryPLY = true;

public bool bSyncEnabled = false;

public bool bAutoExposureEnabled = true;

public int nExposureStep = -8;

public KinectSettings()
{
aMinBounds[0] = -5f;
Expand Down Expand Up @@ -100,6 +106,14 @@ public List<byte> ToByteList()
bTemp = BitConverter.GetBytes(iCompressionLevel);
lData.AddRange(bTemp);

if (bAutoExposureEnabled)
lData.Add(1);
else
lData.Add(0);

bTemp = BitConverter.GetBytes(nExposureStep);
lData.AddRange(bTemp);

return lData;
}
}
Expand Down
Loading

0 comments on commit 7d56c52

Please sign in to comment.