/**
     * @brief Update the app's gui and take actions based on user interaction with gui.
     * Additional buttons can be added to easily send EagleAPI commands<br>
     * if (GUI.Button(new Rect(200, 200, 75, 20), "Reset Position"))    EagleAPI.actuators[target_actuator].ResetPosition();
     */
    private void OnGUI()
    {
        // ///Search for a serial port that has an eagle controller, stop and white light when correct port found
        switch (EagleAPI.correctPort)
        {
        case false:
            style.normal.textColor = Color.black;
            if ((Time.time - waiting_for_response) > 0.05)
            {
                EagleAPI.Handshake();
                waiting_for_response = Time.time;
            }
            break;

        case true:
            style.normal.textColor = Color.white;
            break;
        }

        ///Comm port that is being used by the Serial class
        GUI.Label(new Rect(Screen.width - 250, Screen.height - 100, 250, 40), "Port:     " + Serial.GetPortName(), style);

        ///Send a system ready command
        if (GUI.Button(new Rect(50, 250, 150, 20), "System Ready"))
        {
            EagleAPI.SystemReady();
        }

        ///Actuator information, target acutator, position, actuator force, temperature etc.
        GUI.Label(new Rect(50, 50, 250, 40), "Target Actuator:               " + target_actuator.ToString());
        target_actuator = Mathf.RoundToInt(GUI.HorizontalSlider(new Rect(50, 30, 150, 20), target_actuator, 0, 5));
        if (target_actuator != last_target_actuator)
        {
            pcenableflag = true;
        }
        GUI.Label(new Rect(50, 75, 250, 20), "Actuator Position (mm):    " + (EagleAPI.actuators[target_actuator].position / 1000f).ToString("0.00")); //position is received in micrometers
        GUI.Label(new Rect(50, 100, 250, 20), "Actuator Force:                " + EagleAPI.actuators[target_actuator].force.ToString("0"));
        GUI.Label(new Rect(50, 125, 250, 20), "Errors:                            " + EagleAPI.actuators[target_actuator].errors.ToString());
        GUI.Label(new Rect(50, 150, 250, 20), "Temperature (C):              " + EagleAPI.actuators[target_actuator].temperature.ToString());
        GUI.Label(new Rect(50, 175, 250, 20), "Voltage (V):                     " + EagleAPI.actuators[target_actuator].voltage.ToString("0.00"));
        GUI.Label(new Rect(50, 200, 250, 20), "Power (W):                      " + EagleAPI.actuators[target_actuator].power.ToString("0.00"));

        ///Force Control section, spring constant and center, sine magnitude and frequency.
        if (forceControl = GUI.Toggle(new Rect(500, 50, 250, 20), forceControl, "ForceControl", "Button"))
        {
            if (positionControl)
            {
                pcenableflag = true;
            }
            positionControl = false;
        }
        GUI.Label(new Rect(300, 75, 250, 20), "Spring Constant(force/mm): " + springK.ToString("0.00"));
        springK = (GUI.HorizontalSlider(new Rect(500, 80, 250, 20), springK, 0, 5));
        GUI.Label(new Rect(300, 100, 250, 20), "Spring Center(mm):             " + springCenter.ToString("0.00"));
        springCenter = (GUI.HorizontalSlider(new Rect(500, 105, 250, 20), springCenter, 0, 200));
        GUI.Label(new Rect(300, 125, 250, 20), "Sine Magnitude:                  " + sine_mag.ToString("0.00"));
        sine_mag = (GUI.HorizontalSlider(new Rect(500, 130, 250, 20), sine_mag, 0, 50));
        GUI.Label(new Rect(300, 150, 250, 20), "Sine Frequency (Hz):           " + sine_freq.ToString("0"));
        sine_freq = (GUI.HorizontalSlider(new Rect(500, 155, 250, 20), sine_freq, 0, 50));
        GUI.Label(new Rect(300, 175, 250, 20), "Constant Force:                  " + const_force.ToString("0"));
        const_force = (GUI.HorizontalSlider(new Rect(500, 180, 250, 20), const_force, -30, 30));

        ///Position control sections
        if (positionControl = GUI.Toggle(new Rect(500, 250, 250, 20), positionControl, "Position Control", "Button"))
        {
            if (forceControl)
            {
                pcenableflag = true;
            }
            forceControl = false;
        }
        GUI.Label(new Rect(300, 275, 250, 20), "Target Position(mm):          " + target_position.ToString("0"));
        target_position = (int)(GUI.HorizontalSlider(new Rect(500, 280, 250, 20), target_position, 0, 150));

        GUI.Label(new Rect(50, Screen.height - 175, 150, 20), "Received: " + EagleAPI.lastResponse);
        GUI.Label(new Rect(50, Screen.height - 150, 150, 20), "Sent: " + EagleAPI.lastSent);
        ///General serial command sending box
        GUI.Label(new Rect(50, Screen.height - 100, 150, 20), "Send Serial Command");
        stringCommand = GUI.TextField(new Rect(50, Screen.height - 75, 150, 20), stringCommand);
        if (GUI.Button(new Rect(200, Screen.height - 75, 75, 20), "Send"))
        {
            Serial.Write(stringCommand + "\r");
        }
    }
    /** @brief Updates at a fixed timestep, calculates physics for force control effects or position control
     */
    void FixedUpdate()
    {
        if (initial_handshake)
        {
            EagleAPI.Handshake();       ///check if there is an Eagle controller on the connected port
            waiting_for_response = Time.time;
            initial_handshake    = false;
        }
        //  if (Serial.correctPort){
        last_step = Time.time;
        ///send an extended force command every second whne in force control mode to update temperature adn voltage etc.
        if ((Time.time - last_second) > 1)
        {
            last_second         = Time.time;
            extended_servo_flag = true;
        }
        ///calculate spring force
        spring_force = -(EagleAPI.actuators[target_actuator].position - springCenter * 1000f) * springK / 1000f;

        ///calculate sine force
        sine_force = sine_mag * Mathf.Sin(2 * Mathf.PI * sine_freq * Time.time);

        ///layer force effects
        send_force = spring_force + sine_force + const_force;

        ///Cap the maximum force
        if (Mathf.Abs(send_force) > maxForce)
        {
            send_force = Mathf.Sign(send_force) * maxForce;
        }

        ///If the actuator position is invalid do not send a force
        if (EagleAPI.actuators[target_actuator].position > 500000)
        {
            send_force = 0;
        }
        ///If the position Control button is toggled on enable postion contoller if needed and send a position control command to the target position.
        if (positionControl)
        {
            if (pcenableflag)
            {
                EagleAPI.actuators[target_actuator].EnablePositionControl(1);
                pcenableflag = false;
            }
            EagleAPI.actuators[target_actuator].PositionControl(target_position);
        }
        ///If the force Control button is toggled on disabled the position controller if needed and send a force command to the actuator using the layered effects.
        else if (forceControl)
        {
            if (pcenableflag)
            {
                EagleAPI.actuators[target_actuator].EnablePositionControl(0);
                pcenableflag = false;
            }
            if (extended_servo_flag)
            {
                EagleAPI.actuators[target_actuator].ExtendedForce((int)send_force); extended_servo_flag = false;
            }
            else
            {
                EagleAPI.actuators[target_actuator].Force((int)send_force);
            }
        }
        ///If neither force conttrol of position control are toggled on send a 0 force so the acutator position is still streamed
        else
        {
            EagleAPI.actuators[target_actuator].Force(0);
        }
        //   }
    }