private void SetupWrist() { if (this.glove != null && this.glove.gloveData.dataLoaded) { string ID = this.gloveData.deviceID; if (this.gloveData.firmwareVersion <= 2.19f) { if (ID.Contains("120206")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quat.FromEuler(Mathf.PI / 2.0f, 0, Mathf.PI)); //correction for glove 1 SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } else if (ID.Contains("120101")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quat.FromEuler(Mathf.PI, 0, 0)); //correction for glove 1 SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } else if (ID.Contains("120203")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quat.FromEuler(0, 0, Mathf.PI / 2.0f)); //correction? SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } else if (ID.Contains("120307") || ID.Contains("120304") || ID.Contains("120310") || ID.Contains("120309") || ID.Contains("120311") || ID.Contains("120312")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quat.FromEuler(0, 0, Mathf.PI)); //correction for glove 7 & 4? SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } } } }
/// <summary> Set the position of the CameraRig so that the HMD is at the new desired location. </summary> /// <param name="newHMDPos"></param> public void Teleport(Vector3 newHMDPos) { if (this.cameraRig != null) { if (this.camera_eye != null) { Vector3 dPos = this.camera_eye.transform.position - this.cameraRig.transform.position; float newY = ignoreY ? this.cameraRig.transform.position.y : newHMDPos.y; Vector3 newpos = new Vector3 ( newHMDPos.x - dPos.x, newY, newHMDPos.z - dPos.z ); this.cameraRig.transform.position = newpos; } else { this.cameraRig.transform.position = newHMDPos; } this.coolDownTimer = 0; } else { SenseGlove_Debugger.LogError(this.name + ".SenseGlove_Teleport requires access to a CameraRig."); } }
/// <summary> Remove an object from this dropzone and restore its original settings. </summary> /// <param name="objectIndex"></param> public void RemoveObject(int objectIndex) { //SenseGlove_Debugger.Log("The script wishes to remove " + objectIndex); if (objectIndex >= 0 && objectIndex < this.objectsInside.Count) { SenseGlove_Debugger.Log("removing " + this.objectsInside[objectIndex].name + " from the DropZone!"); SenseGlove_Grabable obj = this.objectsInside[objectIndex]; SenseGlove_Debugger.Log("RBProps.lengh = " + RBprops.Count); if (obj.GetComponent <Rigidbody>() != null && RBprops[objectIndex] != null) { //if it is currently picked up, we assign the previous properties to its grabscript, which will then apply them once it lets go. SenseGlove_Debugger.Log("It has a physicsBody"); if (obj.IsGrabbed()) { obj.SetOriginalParent(this.originalParent[objectIndex]); obj.SetRBProps(this.RBprops[objectIndex][0], this.RBprops[objectIndex][1]); } else { obj.transform.parent = this.originalParent[objectIndex]; obj.physicsBody.useGravity = this.RBprops[objectIndex][0]; obj.physicsBody.isKinematic = this.RBprops[objectIndex][1]; } } this.objectsInside.RemoveAt(objectIndex); SenseGlove_Debugger.Log("Removed it from ObjectsInside!"); this.RBprops.RemoveAt(objectIndex); this.originalParent.RemoveAt(objectIndex); obj.SetInteractable(true); //now the function can also be used to force removal of the object. this.OnObjectRemoved(obj); } }
// Runs every frame private void Update() { if (Input.GetKeyDown(this.resetCalibrationKey)) { SenseGlove_Debugger.Log("Resetting Hand Parameters"); this.senseGlove.ResetKinematics(); } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(resetKey)) { SenseGlove_Debugger.Log("Reset!"); this.Reset(); } }
//-------------------------------------------------------------------------------------------------------------------------- // Internal Calibration Algorithms #region InternalCalibration /// <summary> Reset the Calibration of the glove if, for instance, something went wrong, or if we are shutting down. </summary> public void CancelCalibration() { if (linkedGlove != null) { linkedGlove.StopCalibration(); SenseGlove_Debugger.Log("Canceled Calibration"); } }
//------------------------------------------------------------------------------------------------------------------------------------ // (Semi)Automatic Calibration Steps /// <summary> Start a semi-automatic calibration of the thumb, using thumb abduction. </summary> public void CalibrateThumb() { if (this.GloveReady()) { SenseGlove_Debugger.Log("Calibrating Thumb"); CalibrationAlgorithm algorithm = new Circle2D(new bool[] { true, false, false, false, false }); this.glove.StartSemiAutoCalibration(algorithm, true, 10, 1f, 5); } }
/// <summary> Fires when the Sense Glove has calculated new finger Lengths. </summary> /// <param name="source"></param> /// <param name="args"></param> private void SenseGlove_OnCalibrationFinished(object source, CalibrationArgs args) { if (this.canUpdate) { //the new gloveLengths have already been applied to the SenseGlove-Object, but should be updated in the database. HandProfile updatedProfile = new HandProfile(args.newFingerLengths); UserProfiles.AddEntry(this.userName, updatedProfile); //update / add entry SenseGlove_Debugger.Log("Updated " + this.userName); } }
/// <summary> /// Calibrate the Wrist, based on the orientation of the foreArm. /// </summary> /// <returns></returns> public bool CalibrateWrist() { if (glove != null && glove.IsConnected() && foreArm != null) { glove.CalibrateWrist(null, SenseGlove_Util.ToQuaternion(this.foreArm.transform.rotation)); SenseGlove_Debugger.Log("Calibrated Wrist"); return(true); } return(false); }
/// <summary> /// Set the current lowerArm quaternion as the 'zero' /// </summary> /// <param name="lowerArm"></param> /// <returns></returns> public bool CalibrateWrist(Quaternion lowerArm) { if (glove != null && glove.IsConnected()) { glove.CalibrateWrist(null, SenseGlove_Util.ToQuaternion(lowerArm)); SenseGlove_Debugger.Log("Calibrated Wrist"); return(true); } return(false); }
/// <summary> Reset the Calibration of the glove if, for instance, something went wrong. </summary> public void CancelCalibration() { if (glove != null) { glove.StopCalibration(); this.calSteps = 0; this.calibrating = false; SenseGlove_Debugger.Log("Canceled Calibration"); } }
private void TrackedGlove_OnCalibrationFinished(object source, CalibrationArgs args) { SenseGlove_Debugger.Log("Resizing Model."); //Debug.Log( "Old: " + SenseGlove_Util.ToString(args.oldJointPositions[1]) + ", New: " + SenseGlove_Util.ToString(args.newJointPositions[1])); Vector3 dJoints = args.newJointPositions[1] - args.oldJointPositions[1]; this.RescaleHand(args.newFingerLengths); this.handGroup.transform.localPosition = this.handGroup.transform.localPosition - dJoints; this.gloveGroup.transform.localPosition = this.gloveGroup.transform.localPosition - dJoints; }
// OnApplicationQuit is called when the game shuts down. void OnApplicationQuit() { this.CancelCalibration(); if (glove != null && glove.IsConnected()) { glove.SimpleBrakeCmd(0, 0, 0, 0, 0); glove.Disconnect(); SenseGlove_Debugger.Log("Disconnected the SenseGlove on " + glove.communicator.Address()); } SenseGloveCs.DeviceScanner.CleanUp(); }
// Update is called once per frame void Update() { if (this.senseGlove != null) { if (Input.GetKeyDown(this.calibrateWristKey)) { this.senseGlove.CalibrateWrist(); } /////// if (Input.GetKeyDown(this.calibrateFingersKey)) { this.senseGlove.StartCalibration(); } /////// if (Input.GetKeyDown(this.manualCalibrationKey)) { this.senseGlove.NextCalibrationStep(this.calibrateFingersKey); } //////// if (Input.GetKeyDown(this.cancelCalibrationKey)) { this.senseGlove.CancelCalibration(); } ///////// if (Input.GetKeyDown(this.calibrateThumb)) { this.senseGlove.CalibrateThumb(); } ///////// if (Input.GetKeyDown(this.resetFingerLengthsKey)) { SenseGlove_Debugger.Log("Reset Fingers"); this.senseGlove.ResetFingers(); } } if (grabScript != null) { if (Input.GetKeyDown(this.releaseObjectKey)) { this.grabScript.ManualRelease(this.releaseTimeOut); } } }
void Start() { if (!this.CanInteract()) { SenseGlove_Debugger.Log("Warning: " + this.name + " is unable to pick up anything."); } if (this.senseGlove == null) { this.senseGlove = this.GetComponent <SenseGlove_Object>(); } }
/// <summary> Check if two SenseGlove_Touch scripts are touching the same object </summary> /// <param name="other"></param> /// <returns></returns> public bool IsTouching(SenseGlove_Touch other) { if (other == null) { SenseGlove_Debugger.LogError("Other is Null"); //If this occurs then our grabScript(s) aren't up to scratch. } else if (this.touchedObject != null && other.touchedObject != null) { return(GameObject.ReferenceEquals(this.touchedObject, other.touchedObject)); } return(false); }
/// <summary> Dispose of resources. </summary> private void Dispose() { this.CancelCalibration(); if (glove != null && glove.IsConnected()) { glove.StopBrakes(); glove.StopBuzzMotors(); glove.Disconnect(); SenseGlove_Debugger.Log("Disconnected the SenseGlove on " + glove.communicator.Address()); } this.Disconnect(); }
/// <summary> Add an object to this DropZone, and apply the desired settings. </summary> /// <param name="obj"></param> public void AddObject(SenseGlove_Grabable obj) { SenseGlove_Debugger.Log("Adding " + obj.name + " to the DropZone!"); // remember original parent if (obj.IsGrabbed()) { this.originalParent.Add(obj.GetOriginalParent()); } else { this.originalParent.Add(obj.transform.parent); } bool[] props = null; if (this.snapToMe && (this.takeFromHand || !obj.IsGrabbed())) { obj.EndInteraction(); Transform zoneParent = this.snapTarget; if (zoneParent == null) { zoneParent = this.gameObject.transform; } obj.gameObject.transform.parent = zoneParent; obj.gameObject.transform.localPosition = Vector3.zero; obj.gameObject.transform.localRotation = Quaternion.identity; Rigidbody RB = obj.physicsBody; if (RB != null) { if (obj.IsGrabbed()) { props = obj.GetRBProps(); } else { props = new bool[2] { RB.useGravity, RB.isKinematic }; } RB.useGravity = false; RB.isKinematic = true; } } this.RBprops.Add(props); this.objectsInside.Add(obj); obj.SetInteractable(!this.disableInteration); //enable / disable interactions. this.OnObjectDetected(obj); }
/// <summary> /// Disconnect and retry the connecting to the SenseGlove, /// such as when a different glove is connected or when the (manual) connection is lost. /// </summary> public void RetryConnection() { this.Disconnect(); if (this.connectionMethod != ConnectionMethod.HardCoded) { if (!SenseGloveCs.DeviceScanner.IsScanning()) { SenseGloveCs.DeviceScanner.pingTime = 200; SenseGloveCs.DeviceScanner.scanDelay = 500; SenseGloveCs.DeviceScanner.StartScanning(true); } } else //we're dealing with a custom connection! { if (canReport) { SenseGlove_Debugger.Log("Attempting to connect to " + this.address); } Communicator PCB = null; if (this.address.Contains("COM")) //Serial connections { if (this.address.Length > 4 && this.address.Length < 6) { this.address = "\\\\.\\" + this.address; } PCB = new SerialCommunicator(this.address); } if (PCB != null) { PCB.Connect(); if (PCB.IsConnected()) { this.glove = new SenseGlove(PCB); this.glove.OnFingerCalibrationFinished += Glove_OnFingerCalibrationFinished; } else if (canReport) { SenseGlove_Debugger.Log("ERROR: Could not connect to " + this.address); canReport = false; } } else if (canReport) { SenseGlove_Debugger.Log("ERROR: " + this.address + " is not a valid address."); canReport = false; } } this.elapsedTime = 0; this.standBy = false; }
/// <summary> Check if two SenseGlove_Touch scripts are touching the same object </summary> /// <param name="other"></param> /// <returns></returns> public bool IsTouching(SenseGlove_Touch other) { if (other == null) { SenseGlove_Debugger.Log("Other is Null"); } else if (this.touchedObject != null && other.touchedObject != null) { return(GameObject.ReferenceEquals(this.touchedObject, other.touchedObject)); } return(false); }
/// <summary> Retrieve the Material properties from the chosen Library. </summary> /// <param name="libName"></param> /// <param name="matName"></param> /// <returns></returns> public static MaterialProps GetMaterial(string matName) { matName = matName.ToLowerInvariant(); //always convert to lower case to prevent typos on Dev/User side; MaterialProps res; if (materials.TryGetValue(matName, out res)) { return(res); } else { SenseGlove_Debugger.Log("Error loading " + matName + ": No such material loaded."); } return(MaterialProps.Default()); }
private void TrackedGlove_OnGloveLoaded(object source, System.EventArgs args) { SenseGlove_Debugger.Log("Setting up WireFrame..."); SetupGlove(trackedGlove.GloveData()); SetupHand(trackedGlove.GloveData()); SetGlove(false); //hide the glove by default. SetHand(true); //show the hand by default. SetupGrabColliders(); this.setupComplete = true; //remove the preview models, if any are available if (this.preview != null) { Destroy(this.preview); } }
/// <summary> Create a Physics Joint between a grabable and a snapZone. </summary> /// <param name="grabable"></param> /// <param name="snapZoneBody"></param> /// <param name="breakForce"></param> public void CreateJoint(SenseGlove_Grabable grabable, Rigidbody snapZoneBody, float breakForce) { if (this.myJoint == null) { if (grabable.physicsBody != null) { this.myJoint = grabable.physicsBody.gameObject.AddComponent <FixedJoint>(); this.myJoint.connectedBody = snapZoneBody; this.myJoint.enableCollision = false; this.myJoint.breakForce = breakForce; } } else { SenseGlove_Debugger.LogError("Multiple Physics connections to my Properties. Wrong index!"); } }
//-------------------------------------------------------------------------------------- // Profile / Class Methods /// <summary> If this user exists within the database, apply their hand profile to this SenseGlove_Object. </summary> /// <param name="userName"></param> public void SetProfile(string name) { if (userName != null && userName.Length > 0) { this.canUpdate = false; this.userName = name; UserProfiles.SetLastUser(this.userName, this.senseGlove.GloveData().deviceID); HandProfile newProfile = UserProfiles.GetProfile(name); this.SetProfile(newProfile); SenseGlove_Debugger.Log("Set Profile for " + this.userName); this.canUpdate = true; //prevent additional calls to fingerCalibrationFinished while we update the profile. } else { SenseGlove_Debugger.LogWarning("Invalid Username"); } }
/// <summary> Tell the database that this user is the last used for this deviceID </summary> /// <param name="deviceID"></param> public static void SetLastUser(string userName, string deviceID) { if (userName.Length > 0) { if (GetLastUser(deviceID).Length > 0) //check if already loaded. { lastProfiles[deviceID] = userName; } else //new entry { lastProfiles.Add(deviceID, userName); } } else { SenseGlove_Debugger.LogWarning("Warning: Invalid Username"); } }
/// <summary> Apply the profile when the Sense Glove has loaded </summary> /// <param name="source"></param> /// <param name="args"></param> private void SenseGlove_OnGloveLoaded(object source, System.EventArgs args) { //check if a profile is already established for this glove. If not assign the name of this glove. string ID = this.senseGlove.GloveData().deviceID; string lastUsedName = UserProfiles.GetLastUser(ID); if (lastUsedName.Length > 0) { //a lastUsedName exits this.userName = lastUsedName; } else { //not been assigned yet, tell the database this is the new lastUser UserProfiles.SetLastUser(this.userName, ID); } this.SetProfile(UserProfiles.GetProfile(this.userName)); SenseGlove_Debugger.Log("Applied profile for " + userName); this.canUpdate = true; }
/// <summary> Load a materials library from a TextAsset </summary> /// <param name="databaseFile"></param> public static void LoadLibrary(string dir, string file) { if (!IsLoaded(file)) { string[] fLines; if (Util.FileIO.ReadTxtFile(dir + file, out fLines)) { //Splitup file int line = 0; List <string> dataBlock = new List <string>(); string lastName = ""; while (line <= fLines.Length) { if (line >= fLines.Length || (fLines[line].Length > 0 && fLines[line][0] == '#')) //its a new Material { if (dataBlock.Count > 0 && lastName.Length > 0) //parse & add previous Material if it has a good name { AddMaterial(lastName, MaterialProps.Parse(dataBlock)); } dataBlock.Clear(); if (line < fLines.Length) { try //extract name of new material { lastName = fLines[line].Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries)[1].ToLowerInvariant(); //condition name } catch (System.Exception Ex) { SenseGlove_Debugger.LogWarning(Ex.Message); lastName = ""; } } } if (line < fLines.Length) { dataBlock.Add(fLines[line]); } line++; } } libraryNames.Add(file); //prevents us from continuously trying to open files } }
//------------------------------------------------------------------------------------------------------------------------------------ // Backwards Compatibility Methods #region BackwardsComp /// <summary> Manually assign IMU Correction for old firmware versions. </summary> private void SetupWrist() { if (this.glove != null && this.glove.gloveData.dataLoaded) { string ID = this.gloveData.deviceID; if (ID.Contains("220102")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quaternions.FromEuler(Mathf.PI, 0, Mathf.PI / 2.0f)); //correction for glove 1 SenseGlove_Debugger.Log("Red Glove Compensation"); } string[] gloveVersion = this.gloveData.firmwareVersion.Split('.'); if (gloveVersion[0][0] == 'v') { gloveVersion[0] = gloveVersion[0].Substring(1); } //if there is a v in front of it, remove this. int mainVersion = int.Parse(gloveVersion[0]); int subVersion = int.Parse(gloveVersion[1]); if (mainVersion <= 2 && subVersion <= 19) { if (ID.Contains("120206")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quaternions.FromEuler(Mathf.PI / 2.0f, 0, Mathf.PI)); //correction for glove 1 SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } else if (ID.Contains("120101")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quaternions.FromEuler(Mathf.PI, 0, 0)); //correction for glove 1 SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } else if (ID.Contains("120203")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quaternions.FromEuler(0, 0, Mathf.PI / 2.0f)); //correction? SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } else if (ID.Contains("120307") || ID.Contains("120304") || ID.Contains("120310") || ID.Contains("120309") || ID.Contains("120311") || ID.Contains("120312")) { this.glove.gloveData.wrist.SetHardwareOrientation(Quaternions.FromEuler(0, 0, Mathf.PI)); //correction for glove 7 & 4? SenseGlove_Debugger.Log("Firmware Version v2.19 or earlier. Adding Hardware Compensation"); } } } }
/// <summary> Begin the interaction between this object and a GrabScript. </summary> /// <param name="grabScript"></param> /// <param name="fromExternal"></param> public void BeginInteraction(SenseGlove_GrabScript grabScript, bool fromExternal = false) { if (grabScript != null) { if (this.isInteractable || fromExternal) //interactions only possible through these parameters. { bool begun = this.InteractionBegin(grabScript, fromExternal); if (begun) { this.originalDist = (grabScript.grabReference.transform.position - this.transform.position).magnitude; this.OnInteractBegin(grabScript, fromExternal); } } } else { SenseGlove_Debugger.LogError("ERROR: You are attempting to start an interaction with " + this.name + " with grabscript set to NULL"); } }
/// <summary> Check if all RigidBody settings allow us to pick up objects. </summary> protected void ValidateRB() { this.physicsBody = this.GetComponent <Rigidbody>(); if (this.physicsBody != null) { if (this.physicsBody.useGravity) { SenseGlove_Debugger.LogWarning(this.name + ".DropZone has a rigidbody that uses gravity, and might move from its desired location."); } } else //we don't have a RigidBody, so one of the ObjectsToGet should have it! { if (this.objectsToGet.Count > 0) { bool noRBs = true; for (int i = 0; i < this.objectsToGet.Count; i++) { if (this.objectsToGet[i].physicsBody == null) { SenseGlove_Debugger.LogWarning(this.objectsToGet[i].name + " will not be detected by " + this.name + ".DropZone, as neither have a RigidBody attached."); } else { noRBs = false; } } if (noRBs) { Debug.LogWarning("Since none of the ObjectsToGet in " + this.name + " have a RigidBody attached, one was autmatically attached to the GameObject."); this.physicsBody = this.gameObject.AddComponent <Rigidbody>(); this.physicsBody.useGravity = false; this.physicsBody.isKinematic = true; } } else { SenseGlove_Debugger.LogWarning(this.name + ".DropZone has no RigidBody of its own, and will therefore only " + "detect Grabables with a RigidBody attached."); } } }