// Update is called once per frame void Update() { if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))//检测是否连接 { if (!_init) { if (!LogitechGSDK.LogiIsPlaying(0, LogitechGSDK.LOGI_FORCE_SPRING)) { if (LogitechGSDK.LogiPlaySpringForce(0, 0, 40, 100)) { _init = true; Debug.Log("init spring force success"); } } else { LogitechGSDK.LogiStopSpringForce(0); Debug.Log("init spring force failed"); } } else { Debug.Log("have init"); } _rec = LogitechGSDK.LogiGetStateUnity(0); } }
private void MapAxesValues() { rec = LogitechGSDK.LogiGetStateUnity(0); acceleratorValue = 32767 - rec.lY; sendThisAcceleration = (float)acceleratorValue / (float)65535; brakeValue = 32767 - rec.lRz; sendThisBrake = (float)brakeValue / (float)65535; steeringValue = rec.lX; }
void Start() { RenderSettings.fogDensity = 0.001f; VisualizeWheel(FL, FR, RL, RR, FLMesh, FRMesh, RLMesh, RRMesh); rb = GetComponent <Rigidbody>(); audioSource = GetComponent <AudioSource>(); _carFollower = GameObject.Find("CarFollower").GetComponent <Camera>(); _springJoint = new JointSpring(); _springJoint.spring = 8000; _springJoint.damper = 5000; _springJoint.targetPosition = 0.85f; // steering first initialize if (!LogitechGSDK.LogiSteeringInitialize(false)) { Debug.Log("LogiInitialize return false"); } velocityText.text = "0 km/h"; gearText.text = "N"; for (int i = 0; i < GearBox.Length; i++) { GearBox[i] = (Shift)ScriptableObject.CreateInstance(typeof(Shift)); if (i > 1) { GearBox[i].minSpeed = (i - 2) * 80; GearBox[i].maxSpeed = (i - 2) * 80 + 80; GearBox[i].shift = (char)(i - 1); GearBox[i].id = i; continue; } if (i == 0) { GearBox[0].minSpeed = 0; GearBox[0].maxSpeed = 50; GearBox[0].shift = 'R'; GearBox[0].id = 0; } else { GearBox[1].minSpeed = 0; GearBox[1].maxSpeed = 0; GearBox[1].shift = 'N'; GearBox[1].id = 1; } } Gear = (Shift)ScriptableObject.CreateInstance(typeof(Shift)); Gear = GearBox[1]; tempGear = 1; steerState = new LogitechGSDK.DIJOYSTATE2ENGINES(); StartUp(); Invoke(nameof(Idle), 0.65f); }
void g29_control() { int rotationAngle = 0; int savedAngle = 0; const int thresholdAngle = 7; System.Random crandom = new System.Random(); rec = LogitechGSDK.LogiGetStateUnity(0); moveToDegree(WHEEL_RANGE / 2); // init, centering Thread.Sleep(1000); // sleep 1 sec while (true) { #if false if (loopCount++ > 10000) { if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0)) { LogitechGSDK.LogiStopConstantForce(0); } break; } #endif rotationAngle = Convert.ToInt16(modelAngle); //Debug.Log ("savedAngle = "+savedAngle); //Debug.Log ("rotationAngle = "+rotationAngle); if (Math.Abs(savedAngle - rotationAngle) < thresholdAngle) { //Debug.Log ("continue"); Thread.Sleep(25 + crandom.Next(5, 25)); // random sleep,TBC continue; } savedAngle = rotationAngle; //rotationAngle = rotationAngle * 6; rotationAngle = rotationAngle * 2; if (manualMode == false) { moveToDegree((WHEEL_RANGE / 2) + rotationAngle); } Thread.Sleep(25 + crandom.Next(5, 25)); // random sleep,TBC } }
public void breaking(LogitechGSDK.DIJOYSTATE2ENGINES brakeControl) { if (brakeControl.lRz <= 32756) { FL.brakeTorque = breakTorque * Mathf.Abs(brakeControl.lRz - 32767) / 32767; FR.brakeTorque = breakTorque * Mathf.Abs(brakeControl.lRz - 32767) / 32767; RL.brakeTorque = breakTorque * Mathf.Abs(brakeControl.lRz - 32767) / 32767; RR.brakeTorque = breakTorque * Mathf.Abs(brakeControl.lRz - 32767) / 32767; } else { FL.brakeTorque = 0; FR.brakeTorque = 0; RL.brakeTorque = 0; RR.brakeTorque = 0; } }
void moveToDegree(float deg) { deg = (deg / WHEEL_RANGE) * 100; moveToPos = Convert.ToInt16(deg); if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0)) { int min, max; while (true) { rec = LogitechGSDK.LogiGetStateUnity(0); currentPos = normalize(rec.lX, -32767, +32767, 0, 100); if (deg < currentPos) { LogitechGSDK.LogiPlayConstantForce(0, +40); } else { LogitechGSDK.LogiPlayConstantForce(0, -40); } rec = LogitechGSDK.LogiGetStateUnity(0); currentPos = normalize(rec.lX, -32767, +32767, 0, 100); min = moveToPos - 1; max = moveToPos + 1; if (currentPos >= min && currentPos <= max) { //Debug.Log ("wheel at pos:" + currentPos); //Debug.Log ("move complete, turn off force"); LogitechGSDK.LogiPlayConstantForce(0, 0); //LogitechGSDK.LogiStopConstantForce (0); break; } } } }
void Update() { /* update current in-game time and round to nearest millisecond */ gameTime = Time.timeSinceLevelLoad; gameTime = (float)(Math.Round((double)gameTime, 3)); /* check if game is over */ if ((int)gameTime > gameLength) { gameIsRunning = false; saveScoreFile(errorData.ToArray()); SceneManager.LoadScene(5); } /* check for Esc key press */ if (Input.GetKey("escape")) { /* go to Main Menu if pressed */ SceneManager.LoadScene(0); } /* if game is not over & still running */ if (gameIsRunning) { //horizontalLine.SetPositions(new Vector3[2] { new Vector3(-11, 0, 1), new Vector3(11, 0, 1) }); /* update action quantization level & notification */ actionQuantizationLevel = (int)Math.Ceiling(actionQuantizationCurve.Evaluate(gameTime)); actionQuantizationNotification.text = "Action Quant: " + actionQuantizationLevel; /* update vision quantization level & notification */ visionQuantizationLevel = (int)Math.Ceiling(visionQuantizationCurve.Evaluate(gameTime)); visionQuantizationNotification.text = "Vision Quant: " + visionQuantizationLevel; /* update path line */ lineUpdate(ref pathLine, visionDelayCurve.Evaluate(gameTime), pathHistory); /* update path position variable */ pathPositionXPos = trailPositionCurve.Evaluate(gameTime); /*update time notification*/ timeNotification.text = (int)gameTime + "s"; /* update bump size variable */ bumpSize = bumpSizeCurve.Evaluate(gameTime); /* update vision delay notification */ visionDelay = visionDelayCurve.Evaluate(gameTime); visionDelayNotification.text = "Vision Delay: " + visionDelay; /* update action delay notification */ actionDelay = actionDelayCurve.Evaluate(gameTime); actionDelayNotification.text = "Action Delay: " + actionDelay; /* make sure steering wheel is initialized and connected: */ if (!logiIni) { logiIni = LogitechGSDK.LogiSteeringInitialize(false); } if (!LogitechGSDK.LogiIsConnected(deviceIdx)) { Debug.Log("PLEASE PLUG IN A STEERING WHEEL OR A FORCE FEEDBACK CONTROLLER"); } /********************************** * Wheel is Connected and Working * **********************************/ else if (LogitechGSDK.LogiUpdate()) { /* record state of the wheel and converts to an angle in degrees */ rec = LogitechGSDK.LogiGetStateCSharp(deviceIdx); normalizedAngle = rec.lX * 900 / 65536; wheelAngles.Add(new float[] { gameTime, normalizedAngle }); for (int i = 0; i < wheelAngles.Count; i++) { if (gameTime - wheelAngles[i][0] > actionDelay) { quantizedAngle = QuantizeAngle(wheelAngles[i][1], actionQuantizationLevel); wheelAngles.RemoveRange(0, i); } } /* add to the player position with a scaled factor of the angle of the wheel * NOTE: player position is related to previous position of the player. */ playerLineXPos = playerLineXPos + sensitivity * quantizedAngle; LogitechGSDK.LogiPlayConstantForce(deviceIdx, (int)bumpSize); /* keep the player position from going off the screen */ if (playerLineXPos < XminScreen) { playerLineXPos = XminScreen; } else if (playerLineXPos > XmaxScreen) { playerLineXPos = XmaxScreen; } /* set position of the player line */ playerLine.SetPositions(new Vector3[2] { new Vector3(playerLineXPos, -playerLineHeight, 0), new Vector3(playerLineXPos, playerLineHeight, 0) }); /* calculate the error and add to list */ reportPositions(); } else { Debug.Log("THIS WINDOW NEEDS TO BE IN FOREGROUND IN ORDER FOR THE SDK TO WORK PROPERLY... OR TEST ENDED"); } } }
private float visionDelay; /* current amount of vision delay in seconds */ void Start() { /* load variables from menu class */ inputFilePath = BeginButtonControl.inputFilePath; RecordFileName = BeginButtonControl.saveFileName; sensitivity = 0.01f * BeginButtonControl.sensitivity; LoadTestParameters(ref trailPositionCurve, ref actionQuantizationCurve, ref visionDelayCurve, ref bumpSizeCurve, ref actionDelayCurve, ref visionQuantizationCurve); /************************************** * Initialize Logitech Steering Wheel * **************************************/ /* allow full range of motion for the steering wheel & set steering wheel gains */ logiIni = LogitechGSDK.LogiSteeringInitialize(false); LogitechGSDK.LogiControllerPropertiesData properties = new LogitechGSDK.LogiControllerPropertiesData(); wheelPropSetup(ref properties); LogitechGSDK.LogiSetPreferredControllerProperties(properties); /* set properties of wheel */ rec = LogitechGSDK.LogiGetStateCSharp(deviceIdx); /*********************** * Set Up GUI Elements * **********************/ /* create the horizontal axis line */ horizontalLine.SetWidth(horizontalLineWidth, horizontalLineWidth); horizontalLine.SetVertexCount(2); horizontalLine.SetPositions(new Vector3[2] { new Vector3(-11, 0, 0), new Vector3(11, 0, 0) }); horizontalLine.material = new Material(Shader.Find("Sprites/Default")); horizontalLine.SetColors(Color.magenta, Color.magenta); /* create path line */ pathLine.SetWidth(pathLineWidth, pathLineWidth); pathLine.SetVertexCount(2); pathLine.SetPositions(new Vector3[2] { new Vector3(0, -pathLineHeight, 0), new Vector3(0, pathLineHeight, 0) }); pathLine.material = new Material(Shader.Find("Sprites/Default")); pathLine.SetColors(Color.grey, Color.grey); pathHistory = 1f; /* amount of history of path shown in seconds */ /* create vertical line that represents player position */ playerLine.SetWidth(playerLineWidth, playerLineWidth); playerLine.SetVertexCount(2); playerLineXPos = 0; playerLine.SetPositions(new Vector3[2] { new Vector3(playerLineXPos, -playerLineHeight, 0), new Vector3(playerLineXPos, playerLineHeight, 0) }); playerLine.material = new Material(Shader.Find("Sprites/Default")); playerLine.SetColors(Color.green, Color.green); playerLine.sortingOrder = 1; /****************************************** * Set up Parameters for Game Experiments * ******************************************/ saveFilePath = Path.GetDirectoryName(Application.dataPath); gameDate = DateTime.UtcNow.ToString("yyyyMMddhhmmss"); gameIsRunning = true; }
void FixedUpdate() { if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0)) { steerState = LogitechGSDK.LogiGetStateUnity(0); Shifting(ref tempGear); currentVelocity = rb.velocity.magnitude * 3600 / 1000; if (currentVelocity > Gear.maxSpeed) { motorTorqueV = 0; if (!Gear.shift.Equals('N')) { if (!audioSource.clip.Equals(maxRpm)) { MaxRPM(); } } else { tempPitch = (float)steerState.lY / (-32767) + 1.5f; if (tempPitch >= 2.45) { if (!audioSource.clip.Equals(maxRpm)) { MaxRPM(); } } else { if (!audioSource.clip.Equals(idleSound)) { Idle(); } } audioSource.pitch = tempPitch; } } else { if (!audioSource.clip.Equals(idleSound)) { Idle(); audioSource.pitch = tempPitch; } } if (currentVelocity <= Gear.maxSpeed && currentVelocity >= Gear.minSpeed) { float velocityAverage = Gear.maxSpeed - Gear.minSpeed / 2; if ((currentVelocity < velocityAverage + velocityAverage / 10) && (currentVelocity >= velocityAverage - velocityAverage / 10)) { motorTorqueV = Mathf.Abs(steerState.lY - 32767) * motorTorque / 32767; } else { float velocityDifference = Mathf.Abs(currentVelocity - velocityAverage); if (velocityDifference <= 10) { motorTorqueV = Mathf.Abs(steerState.lY - 32767) * motorTorque / 32767; motorTorqueV = motorTorqueV / 1.1f; } else { motorTorqueV = Mathf.Abs(steerState.lY - 32767) * motorTorque / 32767 / (velocityDifference / 10); } } if (!GearBox[tempGear].shift.Equals('N')) { if (!audioSource.clip.Equals(idleSound)) { audioSource.clip = idleSound; } if ((currentVelocity + 20.0f) / ((!Gear.shift.Equals('R') ? ((float)Gear.id - 1) : (1.0f)) * 40) <= 0.5f) { tempPitch = 0.5f; audioSource.pitch = tempPitch; } else { tempPitch = (currentVelocity + 20.0f) / ((!Gear.shift.Equals('R') ? ((float)Gear.id - 1) : (1.0f)) * 40); audioSource.pitch = tempPitch; } } else { tempPitch = (float)steerState.lY / (-32767) + 1.5f; if (tempPitch >= 2.45) { if (!audioSource.clip.Equals(maxRpm)) { MaxRPM(); } } else { if (!audioSource.clip.Equals(idleSound)) { Idle(); } } audioSource.pitch = tempPitch; } } if (currentVelocity < Gear.minSpeed) { float velocityDifference = Gear.minSpeed - currentVelocity; if (velocityDifference <= 10) { motorTorqueV = Mathf.Abs(steerState.lY - 32767) * motorTorque / 32767 / 10; motorTorqueV = motorTorqueV / 1.1f; } else { motorTorqueV = (Mathf.Abs(steerState.lY - 32767) * motorTorque / 32767 / 10) / (velocityDifference / 10); } if (!GearBox[tempGear].shift.Equals('N')) { if ((currentVelocity + 20.0f) / ((!Gear.shift.Equals('R') ? ((float)Gear.id - 1) : (1.0f)) * 40) <= 0.5f) { tempPitch = 0.5f; audioSource.pitch = tempPitch; } else { tempPitch = (currentVelocity + 20.0f) / ((!Gear.shift.Equals('R') ? ((float)Gear.id - 1) : (1.0f)) * 40); audioSource.pitch = tempPitch; } } else { tempPitch = (float)steerState.lY / (-32767) + 1.5f; if (tempPitch >= 2.45) { if (!audioSource.clip.Equals(maxRpm)) { MaxRPM(); } } else { if (!audioSource.clip.Equals(idleSound)) { Idle(); } audioSource.pitch = tempPitch; } audioSource.pitch = tempPitch; } } float steering = (float)steerState.lX / 32767 * steerAngle; if (driveMod == DriveMod.FrontDrive) { if (Gear.shift != 'R') { FL.motorTorque = FR.motorTorque = motorTorqueV; } else { FL.motorTorque = FR.motorTorque = -motorTorqueV; } } else if (driveMod == DriveMod.RearDrive) { if (Gear.shift != 'R') { RR.motorTorque = RL.motorTorque = motorTorqueV; } else { RR.motorTorque = RL.motorTorque = -motorTorqueV; } } else if (driveMod == DriveMod.FullDrive) { if (Gear.shift == 'R') { FL.motorTorque = FR.motorTorque = motorTorqueV; RR.motorTorque = RL.motorTorque = motorTorqueV; } else { FL.motorTorque = FR.motorTorque = -motorTorqueV; RR.motorTorque = RL.motorTorque = -motorTorqueV; } } FL.steerAngle = FR.steerAngle = steering; breaking(steerState); } if (currentVelocity < 200) { _springJoint.damper = 5000 + 75 * currentVelocity; _springJoint.targetPosition = 0.85f + 0.00075f * currentVelocity; FR.suspensionSpring = _springJoint; FL.suspensionSpring = _springJoint; RR.suspensionSpring = _springJoint; RL.suspensionSpring = _springJoint; } }
private int gameLength; /* gameLength in seconds */ void Start() { /* get file interaction & sensitivity values from menu class */ RecordFileName = BeginButtonControl.saveFileName; inputFilePath = BeginButtonControl.inputFilePath; sensitivity = 0.1f * ValSensitivity.sensitivity; LoadTestParameters(ref distances, ref widths); /************************************** * Initialize Logitech Steering Wheel * **************************************/ /* allow full range of motion for the steering wheel & set steering wheel gains */ wheelProp = ""; logiIni = LogitechGSDK.LogiSteeringInitialize(false); LogitechGSDK.LogiControllerPropertiesData properties = new LogitechGSDK.LogiControllerPropertiesData(); wheelPropSetup(ref properties); LogitechGSDK.LogiSetPreferredControllerProperties(properties); rec = LogitechGSDK.LogiGetStateCSharp(deviceIdx); /*responsible for recording state of the wheel*/ /* create the horizontal axis line */ horizontalLine.SetWidth(0.1f, 0.1f); horizontalLine.SetVertexCount(2); horizontalLine.SetPositions(new Vector3[2] { new Vector3(-11, 0, 0), new Vector3(11, 0, 0) }); horizontalLine.material = new Material(Shader.Find("Sprites/Default")); horizontalLine.SetColors(Color.magenta, Color.magenta); /* create vertical line that represents player position */ playerLine.SetWidth(0.05f, 0.05f); playerLine.SetVertexCount(2); playerLineXPos = 0; playerLine.SetPositions(new Vector3[2] { new Vector3(playerLineXPos, -playerLineHeight, 0), new Vector3(playerLineXPos, playerLineHeight, 0) }); playerLine.material = new Material(Shader.Find("Sprites/Default")); playerLine.SetColors(Color.green, Color.green); playerLine.sortingOrder = 2; /*create fat line that represent goal zone*/ goalLineWidth = widths.Evaluate(0.1f); goalLine.SetWidth(goalLineWidth, goalLineWidth); goalLine.SetVertexCount(2); goalLineHeight = 10; goalLineXPos = distances.Evaluate(0.1f); goalLine.SetPositions(new Vector3[2] { new Vector3(goalLineXPos, -goalLineHeight, 0), new Vector3(goalLineXPos, goalLineHeight, 0) }); goalLine.material = new Material(Shader.Find("Sprites/Default")); goalLine.SetColors(Color.gray, Color.gray); goalLine.sortingOrder = 1; errorData = new List <string>(); DateTime nowDate = DateTime.UtcNow; gameDate = nowDate.ToString("yyyyMMddhhmmss"); /****************************************** * Set up Parameters for Game Experiments * ******************************************/ saveFilePath = Path.GetDirectoryName(Application.dataPath); gameIsRunning = true; }
void Update() { /* update current in-game time and round to nearest millisecond */ gameTime = Time.timeSinceLevelLoad; gameTime = (float)(Math.Round((double)gameTime, 3)); /* check if game is over * if so, write output file & open end game scene */ if ((int)gameTime > gameLength) { gameIsRunning = false; saveScoreFile(errorData.ToArray()); SceneManager.LoadScene(5); } /* if game is not over & still running */ if (gameIsRunning) { /* update time notification */ timeNotification.text = (int)gameTime + "s"; /* update width of goal zone with info already loaded from text file */ goalLineWidth = widths.Evaluate(gameTime); goalLine.SetWidth(goalLineWidth, goalLineWidth); goalLineXPos = distances.Evaluate(gameTime); goalLine.SetPositions(new Vector3[2] { new Vector3(goalLineXPos, -goalLineHeight, 0), new Vector3(goalLineXPos, goalLineHeight, 0) }); /* make sure steering wheel is initialized and connected: */ if (!logiIni) { logiIni = LogitechGSDK.LogiSteeringInitialize(false); } if (!LogitechGSDK.LogiIsConnected(deviceIdx)) { Debug.Log("PLEASE PLUG IN A STEERING WHEEL OR A FORCE FEEDBACK CONTROLLER"); } /********************************** * Wheel is Connected and Working * **********************************/ else if (LogitechGSDK.LogiUpdate()) { /* record state of the wheel and converts to an angle in degrees */ rec = LogitechGSDK.LogiGetStateCSharp(deviceIdx); NormalizedAngle = rec.lX * 900 / 65536; /* set the player position to a scaled factor of the angle of the wheel * NOTE: player position is not related to previous position of the player. * this means angle of the wheel = position on the screen */ playerLineXPos = sensitivity * NormalizedAngle; /* keep the player position from going off the screen */ if (playerLineXPos < XminScreen) { playerLineXPos = (float)XminScreen; } else if (playerLineXPos > XmaxScreen) { playerLineXPos = (float)XmaxScreen; } /* set position of the player line */ playerLine.SetPositions(new Vector3[2] { new Vector3(playerLineXPos, -playerLineHeight, 0), new Vector3(playerLineXPos, playerLineHeight, 0) }); /* calculate the error and add to list for output */ reportPositions(); } else { Debug.Log("THIS WINDOW NEEDS TO BE IN FOREGROUND IN ORDER FOR THE SDK TO WORK PROPERLY... OR TEST ENDED"); } } }